$(document).ready(function () {

    /**********************************************************************
    * ajaxForm
    **********************************************************************/
    function toggleForm(type) {
        switch (type) {
            case "recruitment":
                $("#ajaxForm > div:first").animate({ height: "520px" }, 500);
                $("#ajaxForm > div:first > div.form:not(.recruitment)").fadeOut();
                $("#ajaxForm > div:first > div.recruitment").height(461).fadeIn();
                break;

            case "contacts":
                $("#ajaxForm > div").animate({ height: "540px" }, 500);
                $("#ajaxForm > div:first > div.form:not(.contacts)").fadeOut();
                $("#ajaxForm > div:first > div.contacts").height(481).fadeIn();
                break;

            default:
                if ($("#ajaxForm > div:first").height() > 0) {
                    $("#ajaxForm > div").animate({ height: "0px" }, 500);
                }
                break;
        }
    }
    $("#menuRecruitment").click(function () {
        toggleForm("recruitment");
    });
    $("#menuContacts").click(function () {
        toggleForm("contacts");
    });
    $("#ajaxForm div.close").click(function () {
        toggleForm();
    });















    /**********************************************************************
    * Banner: home
    **********************************************************************/
    if ($("#banner.home").size() == 1) {

        // prevent sliding when mouse is on screen
        $("#banner.home").mouseenter(function () { shouldRotateBanner = false; });
        $("#banner.home").mouseleave(function () { shouldRotateBanner = true; });

        // banner 1: mobile apps
        $("#banner.home > div.mobileapps").bind("bannerEnters", function () {
            var phone1 = $(this).find("div.phone.first:first");
            var phone2 = $(this).find("div.phone.second:first");
            var phone3 = $(this).find("div.phone.third:first");
            var title = $(this).find("div.title:first");
            var text = $(this).find("div.text:first");

            $(phone1).animate({ bottom: "0px" }, 500);
            $(phone2).animate({ bottom: "0px" }, 500);
            $(phone3).animate({ bottom: "0px" }, 500);
            $(title).animate({ right: "0px" }, 500);
            $(text).fadeIn(500);
        });
        $("#banner.home > div.mobileapps").bind("bannerLeaves", function () {
            var phone1 = $(this).find("div.phone.first:first");
            var phone2 = $(this).find("div.phone.second:first");
            var phone3 = $(this).find("div.phone.third:first");
            var title = $(this).find("div.title:first");
            var text = $(this).find("div.text:first");

            $(phone1).css("bottom", "-" + $(phone1).height() + "px");
            $(phone2).css("bottom", "-" + $(phone2).height() + "px");
            $(phone3).css("bottom", "-" + $(phone3).height() + "px");
            $(title).css("right", "-" + $(title).width() + "px");
            $(text).hide();
        });

        // banner 2: localization
        $("#banner.home > div.localization").bind("bannerEnters", function () {
            var map = $(this).find("div.map:first");
            var menu = $(this).find("div.menu:first");
            $(map).animate({ top: "0px" }, 500);
            $(menu).animate({ bottom: "0px" }, 500);
        });
        $("#banner.home > div.localization").bind("bannerLeaves", function () {
            var map = $(this).find("div.map:first");
            var menu = $(this).find("div.menu:first");
            $(map).css("top", "-" + $(map).height() + "px");
            $(menu).css("bottom", "-" + $(menu).height() + "px");
        });

        // banner 3: mobile marketing
        $("#banner.home > div.mobilemarketing").bind("bannerEnters", function () {
            var phone = $(this).find("div.phone:first");
            var fade = $(this).find("div.fade:first");
            var shopping = $(this).find("div.shopping:first");
            var title = $(this).find("div.title:first");
            var text = $(this).find("div.text:first");

            $(phone).animate({ bottom: "0px" }, 500, function () {
                setTimeout(function () {
                    $(shopping).fadeIn(500, function () {
                        setTimeout(function () {
                            $(fade).fadeIn(500);
                        }, 500);
                    });
                }, 500);
            });

            $(title).animate({ left: "0px" }, 500);
            $(text).fadeIn(500);
        });
        $("#banner.home > div.mobilemarketing").bind("bannerLeaves", function () {
            var phone = $(this).find("div.phone:first");
            var fade = $(this).find("div.fade:first");
            var shopping = $(this).find("div.shopping:first");
            var title = $(this).find("div.title:first");
            var text = $(this).find("div.text:first");

            $(phone).css("bottom", "-" + ($(title).width() - 100) + "px");
            $(fade).hide();
            $(shopping).hide();
            $(title).css("left", "-" + $(title).width() + "px");
            $(text).hide();
        });

        // initalize banners
        var banners = new Array();
        $("#banner.home > div").each(function (i) {
            banners[i] = $(this);
            $(this).css("left", "-920px").trigger("bannerLeaves");
        });

        var currentBanner = 0;
        var nextBanner = 1;
        var shouldRotateBanner = true;
        $(banners[0]).css("left", "0px").trigger("bannerLeaves");
        $(banners[1]).css("left", "920px").trigger("bannerLeaves");

        setTimeout(function () {

            $(banners[0]).trigger("bannerEnters");

            var homeBannerTimer = setInterval(function () {
                if (shouldRotateBanner) {
                    $(banners[currentBanner]).animate({ left: "-=920" }, 1200, function () { $(this).trigger("bannerLeaves"); });
                    $(banners[nextBanner]).animate({ left: "-=920" }, 1200, function () { $(this).trigger("bannerEnters"); });

                    currentBanner = nextBanner;
                    nextBanner++;
                    if (nextBanner == banners.length) { nextBanner = 0; }

                    $(banners[nextBanner]).css("left", "920px");
                }
            }, 7000);
            //$(banners[2]).css("left", "0px");
        }, 1000);

    }















    /**********************************************************************
    * Highlights
    **********************************************************************/
    if ($("#highlights").size() == 1) {
        $("#highlights > div.news:first > div.overlay").hide();
        $("#highlights .highlight").mouseenter(function () {
            var side = "left";
            if ($(this).hasClass("middle")) side = "middle";
            if ($(this).hasClass("right")) side = "right";

            $("#highlights > .news:first > div.overlay." + side + ":first").fadeIn(200);


            //var menuItem = $(this).attr("menuItem");
            //if ($("#mapsTemplate > .map > .itemOverlay[menuItem=" + menuItem + "]:visible").size() == 0) {
            //$("#mapsTemplate > .map > .itemOverlay").fadeOut(100);
            //$("#mapsTemplate > .map > .itemOverlay[menuItem=" + menuItem + "]").fadeIn(100);
            //}
        });
        $("#highlights .highlight").mouseleave(function () {
            var side = "left";
            if ($(this).hasClass("middle")) side = "middle";
            if ($(this).hasClass("right")) side = "right";

            $("#highlights > .news:first > div.overlay." + side + ":first").fadeOut(200);
        });
        /*$("#highlights .highlight").click(function () {
        var side = "left";
        if ($(this).hasClass("middle")) side = "middle";
        if ($(this).hasClass("right")) side = "right";

        switch (side) {
        case "left":
        window.location = "http://www.google.pt/";
        break;
        case "middle":

        break;
        case "right":

        break;
        }

        window.location = "http://www.google.pt/";
        });*/



        // quotes
        var quotes;
        var lang = $("span:first").html();
        var nextQuote = "";
        $.ajax({
            type: "GET",
            url: "ws/getQuotes.aspx?lang=" + lang,
            dataType: "xml",
            cache: false,
            success: function (data) {
                quotes = [];
                $("quote", data).each(function (i) {
                    quotes[i] = $(this).text();
                });

                var quotesTimer = setInterval(function () {
                    showNextQuote();
                }, 10000);

                showNextQuote();
            },
            error: function (a, b, c) {
                alert("didn't get it...");
            }
        });
        function showRandomQuote() {
            nextQuote = quotes[Math.floor(Math.random() * quotes.length)];
            $("#highlights .quotes:first > span:first").animate({ right: "10px", opacity: 0 }, 500, function () {
                $(this).text(nextQuote);
            });

            setTimeout(function () {
                $("#highlights .quotes:first > span:first").animate({ right: "20px", opacity: 1 }, 500);
            }, 1000);
        }

        var nextQuoteIndex = 0;
        function showNextQuote() {
            nextQuoteIndex++;
            if (nextQuoteIndex == quotes.length) nextQuoteIndex = 0;
            nextQuote = quotes[nextQuoteIndex];
            $("#highlights .quotes:first > span:first").animate({ right: "10px", opacity: 0 }, 500, function () {
                $(this).text(nextQuote);
            });

            setTimeout(function () {
                $("#highlights .quotes:first > span:first").animate({ right: "20px", opacity: 1 }, 500);
            }, 1000);
        }
    }















    /**********************************************************************
    * appsTemplate
    **********************************************************************/
    if ($("#appsTemplate").size() == 1) {
        // events
        $("#appsTemplate .categoryContainer > span").click(function () {
            $(this).parent().find("div:first").slideToggle(150);
        });
        $("#appsTemplate .categoryContainer > div > img").mouseover(function () {
            var appTitle = $(this).attr("appTitle");
            $(this).parent().parent().find("span > span.secondaryTitle").text(appTitle);
        });
        $("#appsTemplate .categoryContainer > div > img").mouseout(function () {
            $(this).parent().parent().find("span > span.secondaryTitle").text("");
        });
        $("#appsTemplate .categoryContainer > div > img").click(function () {
            var appId = $(this).attr("appID");
            var lang = $("span:first").html();
            $.ajax({
                type: "GET",
                url: "ws/getAppById.aspx?appid=" + appId + "&lang=" + lang,
                dataType: "xml",
                cache: false,
                success: function (data) {
                    var app = $("app:first", data);
                    var platforms = $("platforms:first", data);
                    var screenshots = $("screenshots:first", data);
                    var videos = $("videos:first", data);
                    //alert($("title:first", app).text());

                    $("#appsTemplate > div.detailContainer > *").remove();

                    var html = "";

                    // title
                    html += "<div class=\"detailTitle\">" + $("title:first", app).text() + "</div>";

                    // details
                    html += "<div class=\"appDetails\">" +
                                "<img src=\"images/apps/icons/" + $("id:first", app).text() + ".png\" />" +
                                "<div>" +
                        	    "    <span>" +
                            	        $("description:first", app).text() +
                                "    </span>" +
                    //    <a href="#"><img src="images/store_apple.png" /></a>
                                "</div>" +
                            "</div>";

                    // video
                    if ($("video", videos).size() > 0) {
                        html += "<div class=\"detailTitle\">Video</div>" +
                               "<div class=\"appVideo\">";
                        $("video", videos).each(function (i) {
                            html += "<img src=\"images_tmp/vid_flattire.png\" />";
                        });
                        html += "</div>";
                    }

                    // screenshots
                    if ($("screenshot", screenshots).size() > 0) {
                        html += "<div class=\"detailTitle\">Screenshots</div>" +
                               "<div class=\"appScreenshots\">";
                        $("screenshot", screenshots).each(function (i) {
                            html += "<img src=\"images/apps/shots/" + $(this).text() + "_thumb.jpg\" shotID=\"\" />";
                        });
                        html += "</div>";
                    }

                    $("#appsTemplate > div.detailContainer").html(html);
                },
                error: function (a, b, c) {
                    alert("didn't get it...");
                }
            });
        });

        // hide all categories
        $("#appsTemplate .categoryContainer > div:not(:first)").hide();

        $("#appsTemplate .categoryContainer > div:first > img:first").click();
    }















    /**********************************************************************
    * infoPanelsTemplate
    **********************************************************************/
    if ($("#infopanels").size() > 0) {
        if (!$("#infopanels").hasClass("noclick")) {
            // hide banners
            $("#banner.infopanel > div").hide();
            // events
            $("#infopanels .panel").click(function () {
                var panelOpen = "194px";
                var panelClosed = "54px";

                if ($("#infopanels").hasClass("smallpanels")) {
                    panelOpen = "125px";
                }

                if (!$(this).hasClass("open")) {
                    $("#infopanels .panel.open").removeClass("open").animate({
                        "height": panelClosed
                    }, 500).find("span.text:first").fadeOut();

                    $(this).addClass("open").animate({
                        "height": panelOpen
                    }, 500).find("span.text:first").fadeIn();

                    if (!$("#infopanels").hasClass("nobanner")) {
                        var className = $(this).find(".icon:first").attr("class").substr(5);
                        $("#banner.infopanel div").fadeOut(200);
                        setTimeout(function () { $("#banner.infopanel div." + className).fadeIn(); }, 100);
                    }
                } else {
                    $("#infopanels .panel.open").removeClass("open").animate({
                        "height": panelClosed
                    }, 500).find("span.text:first").fadeOut();
                }
            });

            if ($("#infopanels").hasClass("mobileapps")) {
                setTimeout(function () { $("#infopanels div.panel:eq(1)").click(); }, 700);
            } else if ($("#infopanels").hasClass("mobilemarketing")) {
                setTimeout(function () { $("#infopanels div.panel:eq(6)").click(); }, 700);
            }
        }
    }















    /**********************************************************************
    * mapsTemplate
    **********************************************************************/
    if ($("#mapsTemplate").size() > 0) {
        // hide all map overlays
        $("#mapsTemplate > .map > .itemOverlay").hide();
        // events
        $("#mapsTemplate > .menu a").mouseenter(function () {
            var menuItem = $(this).attr("menuItem");
            if (menuItem == 4) {
                $("#mapsTemplate > .map > .trackingPlatform").stop(true, false).animate({ left: "0px" }, 500);
            } else {
                $("#mapsTemplate > .map > .itemOverlay[menuItem=" + menuItem + "]").stop(true, true).fadeIn(100);
            }
        });
        $("#mapsTemplate > .menu a").mouseleave(function () {
            var menuItem = $(this).attr("menuItem");
            if (menuItem == 4) {
                $("#mapsTemplate > .map > .trackingPlatform").animate({ left: "-600px" }, 500);
            } else {
                $("#mapsTemplate > .map > .itemOverlay[menuItem=" + menuItem + "]").stop(true, true).fadeOut(100);
            }
        });
    }















    /**********************************************************************
    * phoneRotator
    **********************************************************************/
    if ($("#phoneRotator").size() > 0) {
        var numberOfPhones = 3;
        var currentPhone = 0;

        $("#phoneRotator").append("<div></div>");
        for (var i = 1; i <= numberOfPhones; i++) {
            $("#phoneRotator > div:first").append("<div phone=\"" + numberOfPhones + "\" style=\"background-image:url(images/ar_" + i + ".png)\"></div>");
        }

        $("#phoneRotator > div > div").hide();

        setInterval(function () {
            prShowNextPhone();
        }, 8000);

        function prShowNextPhone() {
            currentPhone++;
            if (currentPhone > numberOfPhones) currentPhone = 1;

            $("#phoneRotator > div > div:visible").fadeOut(700);
            setTimeout(function () { $("#phoneRotator > div > div:eq(" + (currentPhone - 1) + ")").fadeIn(700); }, 700);
        }

        prShowNextPhone();
    }




});
