// -----------------------------------------
// Functions initialized after page load
// -----------------------------------------
$(window).load(function () {

    $("#birthdate").datepicker();
    var name = $("#name"),
			email = $("#email"),
			password = $("#password"),
			allFields = $([]).add(name).add(email).add(password),
			tips = $(".validateTips");

    function updateTips(t) {
        tips
				.text(t)
				.addClass("ui-state-highlight");
        setTimeout(function () {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("Length of " + n + " must be between " +
					min + " and " + max + ".");
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#createCard").dialog({
        autoOpen: false,
        height: 375,
        width: 375,
        modal: true,
        buttons: {
            "Order": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(name, "username", 3, 16);
                bValid = bValid && checkLength(email, "email", 6, 80);
                bValid = bValid && checkLength(password, "password", 5, 16);

                bValid = bValid && checkRegexp(name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter.");
                bValid = bValid && checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com");
                bValid = bValid && checkRegexp(password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9");

                if (bValid) {
                    $("#users tbody").append("<tr>" +
							"<td>" + name.val() + "</td>" +
							"<td>" + email.val() + "</td>" +
							"<td>" + password.val() + "</td>" +
						"</tr>");
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $(".orderCard").hover(
    function () { $(this).css("background-color", "#222").css("background-image", "url(Images/m_h.jpg)").find("img").attr("src", "Images/card_h.png"); },
    function () { $(this).css("background-color", "#333").css("background-image", "url(Images/m.jpg)").find("img").attr("src", "Images/card.png"); }
    );

    $(".orderCard").click(function () { $("#createCard").dialog("open"); });




    $.preloadImages("../Images/manzetti_logo.png");
    $(".slideshow").cycle().show();

    $("a").hover(
    function () {
        $(this).css("color", "fff");
        $(this).stop(true, true).fadeTo(400, 0.6);
    },
        function () {
            $(this).stop(true, true).fadeTo(400, 1);
        });

    if ($(".body").length != 0) {
        $(".body").vTicker({ showItems: 1, pause: 7000 });
    }
        
        $(".welcome").fadeTo(2000, 0, function () {                        
            $(".mainimage").find("span").css("display", "inline-block");            
        });    

    $(".shopbackground").fadeTo(400, 0);
    $(".caption").fadeTo(400, 0.5);
    $(".image").hover(
        function () {
            $(this).find(".shopbackground").fadeTo(400, 0.9);
            $(this).find(".shopcontents").fadeTo(400, 1);
            $(this).find(".caption").css("text-decoration", "none");
        },
        function () {
            $(this).find(".shopbackground").fadeTo(400, 0);
            $(this).find(".shopcontents").fadeTo(400, 0);
            $(this).find(".caption").css("text-decoration", "underline");
        }
    );

    $("#menu").find('li[class!="li"]').find('a').hover(
        function () { $(this).parent().stop(true, true).animate({ width: "170px" }, 200); },
        function () { $(this).parent().stop(true, true).animate({ width: "150px" }, 200); }
    );

    $("#menu").find('li[class!="li"]').find('span').hover(
        function () { $(this).parent().stop(true, true).animate({ width: "170px" }, 200); },
        function () { $(this).parent().stop(true, true).animate({ width: "150px" }, 200); }
    );

    $("#menu").find('li[class!="li"]').find('span').hover(
        function () { $(this).stop(true, true).fadeTo(400, 0.4).css("text-decoration", "underline"); ; },
        function () { $(this).stop(true, true).fadeTo(400, 1).css("text-decoration", "none"); ; }
    );
    $("#mainimage").find('span').hover(
        function () { $(this).stop(true, true).animate({ opacity: 1 }, 400); },
        function () { $(this).stop(true, true).animate({ opacity: 0.4 }, 400); }
    );

    $("div[class='shop']").find('a').hover(
        function () {
            $(this).find('span').css("text-decoration", "underline");
            $(this).find('span').stop(true, true).fadeTo(400, 0.6);
            $(this).find('img').css("border-color", "#888");
        },
        function () {
            $(this).find('span').stop(true, true).fadeTo(400, 1);
            $(this).find('span').css("text-decoration", "none");
            $(this).find('img').css("border-color", "#666");
        }
    );
    $("#manzettimap").click(function () { toggleBottomMap(); });
    $("#hidemap").click(function () { toggleBottomMap(); });
    $(".contacts").click(function () {
        if ($("#footer").find('iframe').is(":hidden")) {
            toggleBottomMap();
        }
        else { $.scrollTo('max', 700); }
    });
    $(".showvacancies").click(function () { toggleVacancies(); });

    $("#manzettimap").hover(
        function () { $(this).stop(true, true).fadeTo(400, 0.6).css("text-decoration", "underline"); },
        function () { $(this).stop(true, true).fadeTo(400, 1).css("text-decoration", "none"); }
    );

    $("#hidemap").hover(
        function () { $(this).stop(true, true).fadeTo(400, 0.6).css("text-decoration", "underline"); },
        function () { $(this).stop(true, true).fadeTo(400, 1).css("text-decoration", "none"); }
    );

    $(".showvacancies").hover(
        function () { $(this).css("text-decoration", "underline").stop(true, true).fadeTo(400, 0.6); },
        function () { $(this).css("text-decoration", "none").stop(true, true).fadeTo(400, 1); }
    );

});

function toggleBottomMap() {
    if ($("#footer").find('iframe').is(":hidden")) {        
        if ($("#workwithus").is(":visible")) {
            $("#workwithus").fadeTo('slow', 0, function () {
                $("#workwithus").hide();
                $(".workarrow").html("&dArr;");
                $("#footer").find('iframe').fadeTo('slow', 1, function () {                    
                    $.scrollTo('max', 700);
                });            
            });            
        }
        else { $("#footer").find('iframe').fadeTo('slow', 1, function () { $.scrollTo('max', 700); }); }
        $("#maparrow").html("&uArr;");
    }
    else {        
        $("#footer").find('iframe').fadeTo('slow', 0, function () { $("#footer").find('iframe').hide(); });
        $("#maparrow").html("&dArr;");
    }
}

function toggleVacancies() {
    if ($("#workwithus").is(":hidden")) {
        if ($("#footer").find('iframe').is(":visible")) {
            $("#footer").find('iframe').fadeTo('slow', 0, function () {
                $("#footer").find('iframe').hide();                
                $("#maparrow").html("&dArr;");
                $("#workwithus").fadeTo('slow', 1, function () { $.scrollTo('max', 700); });
            });            
        }
        else {$("#workwithus").fadeTo('slow', 1, function () { $.scrollTo('max', 700); }); }      
        $(".workarrow").html("&uArr;");
    }
    else {
        $("#workwithus").fadeTo('slow', 0, function () { $("#workwithus").hide(); });
        $(".workarrow").html("&dArr;");
    }
}

jQuery.preloadImages = function () {
    for (var i = 0; i < arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
};
