(function($){$().ajaxSend(function(a,xhr,s){xhr.setRequestHeader("Accept","text/javascript, text/html, application/xml, text/xml, */*")})})(jQuery);(function($){$.fn.reset=function(){return this.each(function(){if(typeof this.reset=="function"||(typeof this.reset=="object"&&!this.reset.nodeType)){this.reset()}})};$.fn.enable=function(){return this.each(function(){this.disabled=false})};$.fn.disable=function(){return this.each(function(){this.disabled=true})}})(jQuery);(function($){$.extend({fieldEvent:function(el,obs){var field=el[0]||el,e="change";if(field.type=="radio"||field.type=="checkbox"){e="click"}else{if(obs&&field.type=="text"||field.type=="textarea"){e="keyup"}}return e}});$.fn.extend({delayedObserver:function(delay,callback){var el=$(this);if(typeof window.delayedObserverStack=="undefined"){window.delayedObserverStack=[]}if(typeof window.delayedObserverCallback=="undefined"){window.delayedObserverCallback=function(stackPos){observed=window.delayedObserverStack[stackPos];if(observed.timer){clearTimeout(observed.timer)}observed.timer=setTimeout(function(){observed.timer=null;observed.callback(observed.obj,observed.obj.formVal())},observed.delay*1000);observed.oldVal=observed.obj.formVal()}}window.delayedObserverStack.push({obj:el,timer:null,delay:delay,oldVal:el.formVal(),callback:callback});var stackPos=window.delayedObserverStack.length-1;if(el[0].tagName=="FORM"){$(":input",el).each(function(){var field=$(this);field.bind($.fieldEvent(field,delay),function(){observed=window.delayedObserverStack[stackPos];if(observed.obj.formVal()==observed.obj.oldVal){return}else{window.delayedObserverCallback(stackPos)}})})}else{el.bind($.fieldEvent(el,delay),function(){observed=window.delayedObserverStack[stackPos];if(observed.obj.formVal()==observed.obj.oldVal){return}else{window.delayedObserverCallback(stackPos)}})}},formVal:function(){var el=this[0];if(el.tagName=="FORM"){return this.serialize()}if(el.type=="checkbox"||self.type=="radio"){return this.filter("input:checked").val()||""}else{return this.val()}}})})(jQuery);(function($){$.fn.extend({visualEffect:function(o){e=o.replace(/\_(.)/g,function(m,l){return l.toUpperCase()});return eval("$(this)."+e+"()")},appear:function(speed,callback){return this.fadeIn(speed,callback)},blindDown:function(speed,callback){return this.show("blind",{direction:"vertical"},speed,callback)},blindUp:function(speed,callback){return this.hide("blind",{direction:"vertical"},speed,callback)},blindRight:function(speed,callback){return this.show("blind",{direction:"horizontal"},speed,callback)},blindLeft:function(speed,callback){this.hide("blind",{direction:"horizontal"},speed,callback);return this},dropOut:function(speed,callback){return this.hide("drop",{direction:"down"},speed,callback)},dropIn:function(speed,callback){return this.show("drop",{direction:"up"},speed,callback)},fade:function(speed,callback){return this.fadeOut(speed,callback)},fadeToggle:function(speed,callback){return this.animate({opacity:"toggle"},speed,callback)},fold:function(speed,callback){return this.hide("fold",{},speed,callback)},foldOut:function(speed,callback){return this.show("fold",{},speed,callback)},grow:function(speed,callback){return this.show("scale",{},speed,callback)},highlight:function(speed,callback){return this.show("highlight",{},speed,callback)},puff:function(speed,callback){return this.hide("puff",{},speed,callback)},pulsate:function(speed,callback){return this.show("pulsate",{},speed,callback)},shake:function(speed,callback){return this.show("shake",{},speed,callback)},shrink:function(speed,callback){return this.hide("scale",{},speed,callback)},squish:function(speed,callback){return this.hide("scale",{origin:["top","left"]},speed,callback)},slideUp:function(speed,callback){return this.hide("slide",{direction:"up"},speed,callback)},slideDown:function(speed,callback){return this.show("slide",{direction:"up"},speed,callback)},switchOff:function(speed,callback){return this.hide("clip",{},speed,callback)},switchOn:function(speed,callback){return this.show("clip",{},speed,callback)}})})(jQuery);

function control_in(control, text){ if (text == control.value) control.value = ''; }
function control_out(control, text){ if ('' == control.value) control.value = text;}
function control_clear(control, text){ control.value = control.value.replace(text,'');}

function set_cookie(name,value) {
	document.cookie = name + "=" + value + "; path=/";
}

function get_cookie(name) {
	name = name + "=";
    
	var cookies = document.cookie.split(';');
        
	for(var i = 0; i < cookies.length; i++) {        
        var c = cookies[i].replace(/^\s+|\s+$/g, '');
        if (0 == c.indexOf(name)){ 
            return c.substring(name.length, c.length);
        }    
	}
    
	return null;
}


function comment_hide_form(id){   
    $('#' + id).css('display', 'none');
    return false;
}

function comment_check_form(cfid){
    result = true;
       
    if ('' == $('#' + cfid + ' #comment_data').val()){
        $('#' + cfid + ' #comment_data').addClass('error')
        result = false;
    }
    else{
        $('#' + cfid + ' #comment_data').removeClass('error')
    }
    
    if ($('#' + cfid + '#captcha').size() > 0){               
        if ('' == $('#' + cfid + ' #captcha').val()){
            $('#' + cfid + ' .field').addClass('error')
            result = false;
        }
        else{
            $('#' + cfid + ' .field').removeClass('error')
        }
    }    
    
    return result;
}

function check_q(anon_def){

    result = true;
    
    if ('' == $('#q_data').val()) { 
        $('#q_data').addClass('error'); 
        result = false; 
    }
    else{ 
        $('#q_data').removeClass('error');
    }
        
    if ($('#captcha').size() > 0){           
        if ('' == $('#captcha').val()){
            $('#simple_captcha .field').addClass('error'); 
            result = false;
        }
        else{ 
            $('#simple_captcha .field').removeClass('error') 
        }
    }    
    
    if ($('#q_author').size() > 0) {  
        if ('' == $('#q_author').val().replace(anon_def, '')){
            $('#q_author').addClass('error'); 
            result = false; 
        }
        else{ 
            $('#q_author').removeClass('error') 
        }
    }    
    
    return result;
}

var prev_page;
var next_page;

function get_sel_text(){
    if (window.getSelection){
        return window.getSelection();
    }
    else if (document.getSelection){
        return document.getSelection();
    }
    else if (document.selection){
        return document.selection.createRange().text;
    }
    
    return '';
}


init_keypress = function(){

    prev_page = $('.q-pages .prev:first');
    next_page = $('.q-pages .next:first');
        
    
    jQuery(document).keypress(function(e) {
    
        if (e.ctrlKey == true && (e.keyCode == 13 || e.keyCode == 10)) {        
            var atext = encodeURI(get_sel_text());
        
            jQuery.get("/public/mistype", {data:[atext], url:[window.location.href]}, function(msg) { alert(msg); });
        }
        
                
        if (e.ctrlKey == true && e.keyCode == 37 && prev_page.size() > 0) {
            window.location = prev_page.attr("href");
        }   
    
        if (e.ctrlKey == true && e.keyCode == 39 && next_page.size() > 0) {            
            window.location = next_page.attr("href");
        } 
    });    
}

//jQuery(document).ready(init_keypress);

function check_contacts(){

    result = true;
       
    if ('' == $('#data').val()) { 
        $('#data').addClass('error'); 
        result = false; 
    }
    else{ 
        $('#data').removeClass('error') 
    }
           
    if (0 == String($('#captcha').attr('value')).length){
        $('#simple_captcha .field').addClass('error')
        result = false;
    }
    else{
        $('#simple_captcha .field').removeClass('error')
    }

    return result;
}

function check_search_form(){
    if ('' == $('#search').attr('value') || 'найти суеверие' == $('#search').attr('value')){
        $('#search').focus();
        return false;
    }       
    
    return true
}

function openid_open(){
    var page_sizes = get_page_size();
       
    $('#overlay').fadeIn();
        
    $('#openid').css({left:	Math.round((page_sizes[0] - $('#openid').width())/2)}).show();
}

function openid_close(){
    $("#openid").fadeOut("slow");
    $("#overlay").fadeOut("slow");
}

function openid_check_form(){
    result = true;
           
    if ('' == $('#openid #account').val()){
        result = false;
    }
   
    return result;
}

/* @return Array Return an array with page width, height and window width, height */
function get_page_size() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {	
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth; 
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }	
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else { 
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){	
        pageWidth = xScroll;		
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
};

/* @return Array Return an array with x,y page scroll values. */
function get_page_scroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;	
    }
    arrayPageScroll = new Array(xScroll,yScroll);
    return arrayPageScroll;
};

function init_solutions(){
    $(".explanation-hide").hover(
        function(){
            $(this).find("p").fadeIn();
        },
        function(){
            $(this).find("p").fadeOut();
        }        
    );
}

function init_note(){   
    $('.note-link').click(
        function(){
            var fs = get_cookie('n');
            
            f = $(this).attr('id').replace('nl', '');
            
            if(fs){    
                fs = fs.split('#');
            
                for(var i = 0; i < fs.length; i++) { 
                    if (fs[i] == f){
                        f = '';
                    }
                }        
            }
            else{
                fs = new Array();
            }
            
            if (f){
                fs[fs.length] = f;
                
                set_cookie('n', fs.join('#'));
            }
            
            
            $(this).fadeOut();
            
            show_note();
            
            return false;
        }
    );
}

function remove_note(f){
    var fs = get_cookie('n');
        
    if(fs){    
        fs = fs.split('#');
                                     
        for(var i = 0; i < fs.length; i++) {             
            if (fs[i] == f){
                fs.splice(i,1); 
            }    
        }
    }
    else{
        fs = new Array();
    }
    
    set_cookie('n', fs.join('#'));
}

function show_note(){
}

$(document).ready(init_solutions);
$(document).ready(init_note);

