jQuery(function () {
    jQuery('.autohint').focus(function () { if (jQuery(this).val() == jQuery(this).attr('title')) jQuery(this).attr('value', ''); });
    jQuery('.autohint').blur(function () { if (jQuery(this).val() == '') jQuery(this).attr('value', jQuery(this).attr('title')); });
    $('#s1').cycle({ fx: 'scrollHorz', next: '.next', prev: '.prev', speed: 1000, auto: 2000 });
    jQuery('section aside.left UL.asidelist LI A').click(function () { jQuery(this).parent().toggleClass('active'); })
    $('.illusion INPUT').attr('autocomplete', 'off');
    jQuery('.illusion INPUT').click(function () { jQuery(this).parent().toggleClass('checked'); })
    jQuery(window).scroll(function () {
        popupwrapper('.popup .night');
        jQuery('.artboard').animate({ top: jQuery(window).height() / 2 - jQuery('.artboard').height() / 2 + jQuery(window).scrollTop() + "px" }, { queue: false, duration: 250 });
    });
    jQuery('P.email A', jQuery('.incontent')).click(function () {        
        jQuery('.popup', jQuery('#page-container')).css('display', 'block');
        setter('.artboard');

        return false;
    });
    jQuery('.popup .night', jQuery('#page-container')).click(function () {
        jQuery(this).parent().css('display', 'none');
    })
    popupwrapper('.popup .night');
    jQuery('.artboard BUTTON').click(function () { initForm(); })
    jQuery('.options P.print A').click(function () {
        window.print(); return false;
    });

    
     
        
         
       
         
    jQuery(document).ready(function () {
        var li = $("a[href='" + document.location.pathname.replace('/', '') + "']").parent();
        $(li).addClass("active");
        $(li).parents("li").addClass("active");
        $(li).children("li").addClass("active");            

        $(".wrapper-looking-for").quickSearch("pages.aspx");


        jQuery.validator.addMethod("ExactlySet1", function (value, element) {
        return ExactlySet(element, 1);
    }, "tick 1 box only");

    jQuery.validator.addMethod("ExactlySet2", function (value, element) {
        return ExactlySet(element, 2);
    }, "tick 2 boxes only");
        jQuery.validator.addMethod("ExactlySet3", function (value, element) {
            return ExactlySet(element, 3);
        }, "tick 2 boxes only");

        jQuery("form").validate();
    });
    

});
function setter( str){
	jQuery(str).css({'top':jQuery(window).height()/2-jQuery(str).height()/2 +jQuery(window).scrollTop()+"px", 'left':jQuery(window).width()/2-jQuery(str).width()/2})
}
function popupwrapper(dom_element){
	jQuery(dom_element).css({'height':+jQuery(document).height() +'px','width': jQuery(document).width() +'px'})
}
function initForm(){
	jQuery('#mailpage').unbind('submit');
	jQuery('#mailpage').submit(function () {	    
	    jQuery("#title").val(jQuery("title").text());
	    var formData = $(this).serialize();
	    var form = $(this);
	    $.ajax({ type: 'POST', url: form.attr('action'), data: formData,
	        success: function (responseData) {
	            if (responseData == 'success') {
	                alert('Your email was successfully sent');
	                jQuery('.popup .night').trigger('click');
	            } else if (responseData == "problem") alert('some connection problems occured'); else { alert(responseData); }
	        }
	    });
	    return false;
	});
};	
function checkEmail(email, existsFunction, notExistsFunction)
{
        var params = {'email': email};
        $.ajax({
            type: "POST",
            url: "ajax.aspx/CheckEmail",
            data: JSON.stringify(params),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg, textStatus) {
                if (msg.d == true) {
                    if (existsFunction != null)
                        existsFunction();
                }
                else {
                    if (notExistsFunction != null)
                        notExistsFunction();
                }
            }
        });
    }
(function ($) {
    $.fn.quickSearch = function (location) {                
        var parent = this;        
        this.find("input[type='text']").bind('keypress', function (e) {
                var code = (e.keyCode ? e.keyCode : e.which);
                if (code == 13) { //Enter keycode
                    e.preventDefault();                    
                    search(parent,location);
                }
            });  
               
         this.find("button").click(function(){
            search(parent,location);
         });
    };
})(jQuery);


function search(element,location) {            
            
        var inputs = $(element).find("input[type='text']");
        var query = $.query.copy();
        for (var i = 0; i < inputs.length; i++) {
            query.REMOVE($(inputs[i]).attr("name"));
            if ($(inputs[i]).val() != $(inputs[i]).attr("title"))
                query.SET($(inputs[i]).attr("name"), $(inputs[i]).val());
        }

        var newLocation = (location != null && location != "") ? location + query.toString() : query.toString();
        window.location = newLocation;
    }



 function ExactlySet(element, expectedElements) {
              return $(element).parents("div:first").find("input[type='checkbox']:checked").length == expectedElements;
          }

         
