(function($){
    $.fn.reveal = function(){
        return this.css('visibility','visible');
    }
    $.fn.cloak = function(){
        return this.css('visibility','hidden');
    }
})(jQuery);


function validate(){

    if(contact_is_submitting) return false;
    
    contact_is_submitting=true;
    
    $("#message-fieldset").empty().hide();
     clearTimeout(message_timeout);
    
    $("input,textarea").removeClass("error");

    var $el = $("#name");

    if($el.val().length<2){
        $el.addClass("error").focus();
        contact_is_submitting=false; 
        $("#message-fieldset").html("Please enter name -  min 2 char.").fadeIn();
        return false;
    }

    $el = $("#email");

    if(!$el.val().match(/^[0-9a-zA-Z\-\_\.]+\@[0-9a-zA-Z\-\_\.]+\.[a-zA-Z]{2,5}$/)){
        $el.addClass("error").focus();
        contact_is_submitting=false; 
        $("#message-fieldset").html("Please enter valid email").fadeIn();
        return false;
    }

    $el = $("#message");

    if($el.val().length<10){
        $el.addClass("error").focus();
        contact_is_submitting=false; 
        $("#message-fieldset").html("Please enter name -  min 10 char.").fadeIn();
        return false;
    }
    
    $("#message-fieldset").html("Sending message").fadeIn();
    var form_data =  $("div.contact-form form").serialize();
    $.get("contact.php",form_data,function(r){
        $("#message-fieldset").html(r);
        clearTimeout(message_timeout);
        message_timeout=setTimeout(function(){
           $("#message-fieldset").fadeOut(); 
        },5000);
        contact_is_submitting=false;   
    })

    return false;
}

function load_page(page){

    if(page=='') {
        window.location.hash="home";
        return false;
        
    }

    var $page = $("#"+page+".page");

    if($page.size()>0){
        page_is_loading = true;
        var $curr_page = $("div.page.open");

        if($curr_page.size()>0){

            var curr_height = $curr_page.outerHeight(true);
            var page_height = $page.outerHeight(true);
            var new_height ;
            if(curr_height < page_height)
                new_height = $("#content").height() +page_height - curr_height;
            else
                new_height = curr_height;
            $("#content").height(new_height);
            
            if($curr_page.attr("id")=="home"){
                $("#cosmo-jarvis").animate({"background-position" : "481px 0px"},250,function(){
                    $("#neil-davidge").animate({"background-position" : "481px 0px"},250,function(){
                                  
                        $curr_page.css("position","absolute").animate({"left":"100%"},500,function(){
                            $curr_page.attr("style","").removeAttr("style").removeClass("open");
                            $page.css("left","-100%").reveal().animate({"left":0},500,function(){
                                $page.css("position","relative").addClass("open");

                                if(page=="home"){

                                    $("#cosmo-jarvis").css({"background-position" : "-481px 0px"}).animate({"background-position" : "0px 0px"},300,function(){
                                        $("#neil-davidge").css({"background-position" : "-481px 0px"}).animate({"background-position" : "0px 0px"},300);
										
											$("#the-answer").css({"background-position" : "-481px 0px"}).animate({"background-position" : "0px 0px"},300);  
                                    })
                                }
                                $("#content").css("height","auto");
                                page_is_loading = false;
                            });

                        });
            
                    }); 
                })
            }
            else {
            
                $curr_page.css("position","absolute").animate({"left":"100%"},500,function(){
                    $curr_page.attr("style","").removeAttr("style").removeClass("open");
                    $page.css("left","-100%").reveal().animate({"left":0},500,function(){
                        $page.css("position","relative").addClass("open");

                        if(page=="home"){

                            $("#cosmo-jarvis").css({"background-position" : "-481px 0px"}).animate({"background-position" : "0px 0px"},300,function(){
                                $("#neil-davidge").css({"background-position" : "-481px 0px"}).animate({"background-position" : "0px 0px"},300); 
									$("#the-answer").css({"background-position" : "-481px 0px"}).animate({"background-position" : "0px 0px"},300);
                            })
                        }
                        $("#content").css("height","auto");
                        page_is_loading = false;
                    });

                });
            
            }
            
        }
        else {
            $page.css("position","relative").reveal().addClass("open");
                    
            page_is_loading = false;

            if(page=="home"){

                $("#cosmo-jarvis").animate({"background-position" : "0px 0px"},300,function(){
                    $("#neil-davidge").animate({"background-position" : "0px 0px"},300); 
						$("#the-answer").animate({"background-position" : "0px 0px"},300); 
                });
            }
        }
    }

}

var page_is_loading = false , contact_is_submitting = false , message_timeout;

$(document).ready(function(){
    $.history.init(load_page);
    $("a[href^=#]").click(function(e){
        e.preventDefault();
        if(!page_is_loading)
        window.location.href=$(this).attr("href");
    });
})
    
