﻿var showcasevisible = false;
$(document).ready(function () {


    $("[data-group]").not("[data-group=home]").hide();
    var hasClicked = false;
    $("#slideshow-wrapper").hide();
    $("[data-back]").fadeOut();
    $("#background1").load(function () { $(this).fadeIn(); });
    $("*[data-visibility]").css("opacity", "0");
    $("*[data-visibility]").css("overflow", "hidden");
    $(".second").show(); $("#third-container,#second-container").css("position", "fixed").css("top", "5000px").css("left", "5000px");
    setTimeout(function () {
        $("#third-container,#second-container").css("position", "static").css("zIndex", "1")
        if (!hasClicked) {
            $("#third-container,#second-container").fadeOut(0);
        }
    }, 12000);
    $("#background, #gallery-image").load(function () { $(this).fadeIn(); });

    $("li[data-template]").each(function () {
        var that = $(this);
        $("#hidden-load").load("/Content/Templates" + $(that).attr("data-template") + ".htm");
    });

    var current_first = "";
    var current_second = "";
    var current_slide = 1;
    var current_template = "";
    var current_background = "background_homepage.jpg";
    var back_index = 1;
    $("#main-menu li a").click(function (e) {
        hasClicked = true;
        $("#second-info").css("behavior", "");
        $("#third-container,#second-container").css("position", "static").css("zIndex", "1").fadeOut(0);
        if ($(this).attr("data-background") != "no-change") {
            $("#second-container li").show();
        } else {
            $("#second-container li").hide();
        }
        $("#main-menu li a").css("color", "white");
        $(this).css("color", "#4e0c0b");
        var template = $(this).attr("data-link");
        var image = $(this).attr("data-background");
        var that = $(this);
        var second = $("#second-info");
        $("#second-container").css("display", "block");
        $("#third-container").fadeOut(0);
        $("#third-column").hide();
        $("#second-info").animate({ width: '45%', height: '0%' }, 150, "swing", function () {
            $(current_first).css({ opacity: '0' });

            $(current_first).appendTo("#hidden-loading");
            $("#second-info").css("height", "0px");
            current_first = "#" + template;
            $("#" + template).css({ opacity: '1' }).appendTo(second);
            $("#second-info").animate({ width: '100%', marginTop: "8%" }, 150, function () {
                $("#second-info").animate({ height: $("#" + template).height() + 6 }, 120);
                setTimeout(function () { $("#second-info").css("behavior", " url(/Content/PIE.htc)"); }, 10);
                if (image == "no-change") {
                    $("#logo").animate({ top: "36px" }, 300, "swing");
                } else {
                    $("#logo").animate({ top: "100px" }, 300, "swing");
                }
                $("#second-info").animate({ height: $("#" + template).height(), marginTop: "0%" }, 200, "swing", function () {
                    var current_index = back_index;
                    back_index = $(that).attr("data-index");
                    if (back_index == undefined) {
                        back_index = 2;
                    }
                    if (image != "no-change" || image == null || image == "") {
                        $("#second-info [id$=TEMPLATE]").show();
                        $("#second-info [id=CREATIVE]").show();

                        $("[data-back]").not("#background" + current_index).fadeOut(200);
                        $("#background" + back_index).fadeIn(300, function () {
                            current_background = image;
                            if (current_index != back_index) {
                                $("#background" + current_index).fadeOut(200);

                            }
                            if ($.browser.msie && $.browser.version == "7.0") {
                                var width = $("#second-info").width();

                            }
                            RepositionTable();

                        });
                    } else {

                    }
                });
            })

        });
        e.stopPropagation();

    });
    $("[data-link=CREATIVE]").click(function () {
        $(".first").each(function () {
            fImage = new Image();
            fImage.src = "/Content/Images/" + $(this).attr("data-source");
        });
    });
    $(".second li").not(".gallery-link").click(function () {
        $("#third-info").css("behavior", "");
        $(".second li").css("color", "white");
        $(this).css("color", "#da0805");
        $("#third-container").show();
        $("#third-info").css({ width: "auto", minWidth: "200px", height: "auto" }).show();
        var template = $(this).attr("data-template");

        $("#slideshow-wrapper").hide().appendTo("body");

        var third = $("#third-container");
        var maxWidth = $(this).attr("data-maxW");
        $("#third-column").show();
        $(third).css("color", "transparent");
        $(third).animate({ width: ((3 * maxWidth) / 5) }, 180, "swing", function () {
            setTimeout(function () { $("#third-info").css("behavior", " url(/Content/PIE.htc)"); }, 10);
            $("#third-info").load("/Content/Templates" + template + ".htm", function () {

                $(third).animate({ width: maxWidth }, 220, function () {
                    $(third).css("color", "white");
                    RepositionTable();

                });
            });
        });

    });

    $(".slide-next").click(function () {
        current_slide += 1;
        var count = $("li[data-slideshow=" + current_template + "]").children("span").size();
        if (current_slide <= count) {

            $(".slide-previous").show();

            if (current_slide == count) {

                $(".slide-next").hide();

            }
            $("#current-slide").text(current_slide + " of " + count);
            $("#gallery-image").fadeOut(function () {
                $(this).attr("src", "/Content/Images/" + current_template + "_" + current_slide + ".png");
                $("#current").text(current_template.toUpperCase());

                $("#slide-title").text($("li[data-slideshow=" + current_template + "] span:nth-child(" + current_slide + ")").attr("data-description"));
            });
        } else {
            current_slide -= 1;
        }
    });
    $(".slide-previous").click(function () {
        current_slide -= 1;
        var count = $("li[data-slideshow=" + current_template + "]").children("span").size();
        if (current_slide > 0) {

            $(".slide-next").show();

            if (current_slide == 1) {

                $(".slide-previous").hide();

            }
            $("#current-slide").text(current_slide + " of " + count);
            $("#current").text(current_template.toUpperCase());

            $("#gallery-image").fadeOut(function () {
                $(this).attr("src", "/Content/Images/" + current_template + "_" + current_slide + ".png");
                $("#slide-title").text($("li[data-slideshow=" + current_template + "] span:nth-child(" + current_slide + ")").attr("data-description"));
            });
        } else {
            current_slide += 1;
        }
    });
    $(".gallery-link").click(function () {

        $(".second li").css("color", "white");
        $(this).css("color", "#da0805");
        current_slide = 1;
        var that = $(this);
        $("#third-info").css("behavior", " ");
        current_template = $(that).attr("data-gallery");
        $("#slideshow-wrapper").hide().appendTo("body");
        $("#third-container").css("width", "622px");
        $("#third-container").css("display", "block");
        $("#third-info").empty().css({ height: "500px" });
        $("#slideshow-wrapper").appendTo("#third-info").show();
        $("#third-info").css("display", "");
        $("#third-container").fadeOut(280, function () {
            $("#gallery-image").attr("src", "/Content/Images/" + current_template + "_1.png");
            $("#third-column").show();
            $("#third-container").fadeIn(0);

            var count = $("li[data-slideshow=" + current_template + "]").children("span").size();

            $(".slide-next").show();
            $(".slide-previous").hide();

            $("#current").text(current_template.toUpperCase());
            $("#slide-title").text($("li[data-slideshow=" + current_template + "] span:nth-child(1)").attr("data-description"));
            $("#current-slide").text(current_slide + " of " + count);
            RepositionTable();
            setTimeout(function () { $("#third-info").css("behavior", " url(/Content/PIE.htc)"); }, 10);
            $("li[data-slideshow=" + current_template + "] span[data-source]").each(function () {
                img = new Image();
                img.src = "/Content/Images/" + $(this).attr("data-source");
            });
        });


    });
    var throttle_complete = false;
    function RepositionTable(a) {
        throttle_complete = false;
        setTimeout(function () {
            throttle_complete = true;
        }, 250);
        var width = $("#MENU-TABLE").width();
        var height = $("#MENU-TABLE").height();
        if (width > 740 || width < 240 || a == true) {
            $("#MENU-TABLE").animate({ top: -128, left: 110 }, 120);
        }
    }
    RepositionTable(true);
    setInterval(function () {
        RepositionTable();
    }, 1500);
    RepositionTable();
    $(window).resize(function () {
        if (throttle_complete) {
            RepositionTable(true);
        }
    });
    $("#background1").fadeIn();
});
