$(document).ready(function() {

    $(function(){$(".lavaLamp").lavaLamp({ fx: "linear", speed: 200 })});
    
    /**
     * link-like behavior on appropriate elements
     */    
    $(".arrow, .dot, .lavaLamp li span, #submit, #ujUzenet, #header h1").hover(function() { $(this).css("cursor", "pointer") });
    $("#submit, #ujUzenet").hover(
        function() {
            $(this).css("color", "white");
        },
        function() {
            $(this).css("color", "#777");
        }
    );
    
    var currentPage =  0;
    
    /*
     * with up and down cursors we can navigate through pages
     * but this function should be disabled when filling the contact form
     */
    var enableUpDown = true;

    $("#name, #email, #uzenet, #submit").focus(function() {
        enableUpDown = false;
    }).blur(function() {
        enableUpDown = true;
    });
    
    /**
     * clicking on header = cicle through pages
     * without using menu items or up/down cursors
     */
    $("#header h1").click(function() {
        currentPage++;
        if (currentPage == 4) {currentPage = 0;}
        if (currentPage == -1) {currentPage = 3;}
        $("#page" + currentPage).click();
    });

    var currentReferencia = 1;
    var sumRef = $("#horizontalScroller > div").size();
    
    /**
     * navigating through pages with menu
     */        
    $(".lavaLamp li span").click(function() {
        var currentPage =  $(this).attr("id").substr(4);
        moveVertical("page" + currentPage);
    });

    /**
     * catching the keydown on whole document
     */    
    $(document).keydown(function(e) {
        /**
         * disabling navigation through DOM elements with tab key in FF
         * this would mess up the hidden containers's positions
         */
        if (e.keyCode == 9) {
            if (enableUpDown) {
                return false;
            }
        }
        /**
         * left/right arrows will navigate through preferences
         * when theye are visible
         */        
        if(e.keyCode==37){ // left
            $("#prev .arrow:visible").trigger('click');
        }
        if(e.keyCode==39){ // right
            $("#next .arrow:visible").trigger('click');
        }
        if(e.keyCode==38){ // up
            if (enableUpDown) {
                currentPage--;
                if (currentPage == -1) {currentPage = 3;}
                $("#page" + currentPage).click();
                return false;
            }
        }
        if(e.keyCode==40){ // down
            if (enableUpDown) {
                currentPage++;
                if (currentPage == 4) {currentPage = 0;}
                $("#page" + currentPage).click();
                return false;
            }
        }
    });
    
    /**
     * navigating through preferences with GUI buttons
     */    
    $("#prev .arrow").click(function() {
        currentReferencia--;
        dotPaint();
        if (currentReferencia >= 1) {
            var marg = -890 * (currentReferencia-1) + "px";
            $("#horizontalScroller").animate({"margin-left": marg}, "slow");
            $("#next .arrow").fadeIn();
        }
        if (currentReferencia == 1) {
            $("#prev .arrow").fadeOut();
        }
    });
    
    $("#next .arrow").click(function() {
        currentReferencia++;
        dotPaint();
        if (currentReferencia <= sumRef) {
            var marg = -890 * (currentReferencia-1) + "px";
            $("#horizontalScroller").animate({"margin-left": marg}, "slow");
            $("#prev .arrow").fadeIn();
        }
        if (currentReferencia == sumRef) {
            $("#next .arrow").fadeOut();
        }
    });
    
    /**
     * navigating through preferences using dots
     * below the content box
     */    
    $(".dot").click(function() {
        var cf = $(this).attr("id");
        cf = cf.substring(3);
        currentReferencia = parseInt(cf);
        dotPaint();
        var marg = -890 * (currentReferencia-1) + "px";
        $("#horizontalScroller").animate({"margin-left": marg}, "slow");
        if (currentReferencia == 1) { $("#prev .arrow").fadeOut(); }
        else                 { $("#prev .arrow").fadeIn(); }
        if (currentReferencia == sumRef) { $("#next .arrow").fadeOut(); }
        else                      { $("#next .arrow").fadeIn(); }
        });

    /**
     * submitting the form
     */
    $("#submit").click(
        function() {
            $("#submit").hide();
            $("#tip").html("Kis türelmet...");
            
            var formData = {
                check:  $("#check").val(),
                name:   $("#name").val(),
                email:  $("#email").val(),
                uzenet: $("#uzenet").val()
            }
                        
            if (formData.check == "" && $("#nameError").is(':hidden') && $("#emailError").is(':hidden') && $("#uzenetError").is(':hidden'))
            {
                $.post(
                    "uzenetKuldes.php", 
                    formData,
                    function(response){
                        if (response == "email sent")
                        {
                            $("#uzenet").val("");
                            $("table").hide();
                            $("#siker p, #ujUzenet").show();
                            $('#tip').html("");
                            $("#submit").show();
                        }
                        else
                        {
                            $("#submit").show();
                            $("#tip").html("");
                            
                            rCheck =  parseInt(response.substr(0, 1));
                            rName =   parseInt(response.substr(1, 1));
                            rEmail =  parseInt(response.substr(2, 1));
                            rUzenet = parseInt(response.substr(3, 1));
                            
                            if (!rName) { $("#nameError").show(); }
                            else        { $("#nameError").hide(); }
                            
                            if (!rEmail) { $("#emailError").show(); }
                            else         { $("#emailError").hide(); }

                            if (!rUzenet) { $("#uzenetError").show(); }
                            else          { $("#uzenetError").hide(); }
                        }
                    }
                );
            }
            
            else
            {
                $("#submit").show();
                $("#tip").html("");
            }
        }
    );
    $("#ujUzenet").click(function() {
        $("#siker p, #ujUzenet").hide();
        $("table").show();
    });

    /**
     * highlight the actual dot in reference navigation
     */
    function dotPaint() {
        dotNum = "" + currentReferencia; if (currentReferencia<10) { dotNum = "0" + dotNum; }; dotNum = "dot" + dotNum;
        $(".dot").css({backgroundPosition: '0px 0px'});
        $("#" + dotNum).css({backgroundPosition: '0px -10px'});
    }

    /**
     * moving the vertical content to the actual page
     */    
    function moveVertical(page) {
        switch (page) {
            case "page0": // rólunk
                $("#verticalScroller").animate({"margin-top": "0px"}, "slow");
                $(".arrow").fadeOut();
                $("#dots").fadeOut();
                break;
            case "page1": // referenciák
                $("#verticalScroller").animate({"margin-top": "-450px"}, "slow");
                if (currentReferencia !== 1) { $("#prev .arrow").fadeIn(); }
                if (currentReferencia !== sumRef) { $("#next .arrow").fadeIn(); }
                $("#dots").fadeIn();
                break;
            case "page2": // tippek
                $("#verticalScroller").animate({"margin-top": "-900px"}, "slow");
                $(".arrow").fadeOut();
                $("#dots").fadeOut();
                break;
            case "page3": // kapcsolat
                $("#verticalScroller").animate({"margin-top": "-1350px"}, "slow", function() {
                    //$("#name").focus();
                
                });
                $(".arrow").fadeOut();
                $("#dots").fadeOut();
                break;
        }
        
    }
});
