﻿var cycleSpeed = 10000;
var menuSlideSpeed = 300;
var cycleIsRunning = true;

$(function () {

    if (isHomepage) {
        setupCycle();
    }

    bindColorSwitcher();
    bindScreenHeight();
    bindDivData();
    setupMenus();
    //    setupProductsMenus();
    bindModals();
    hijackForms();
    //    bindScrollPanes();
    googleAnalytics();
});

function googleAnalytics() {
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-25715353-1']);
    _gaq.push(['_trackPageview']);

    (function () {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
}

function setScrollbars($content) {
    $content.jScrollPane({
        showArrows: true
    });
}

function hijackForms() {
    $("form[contentdiv]").live("submit", function (e) {
        ajaxStartLoad();
        var $form = $(this);
        $.ajax($(this).attr("action"),
        {
            data: $form.serialize(),
            type: $form.attr("method"),
            success: function (data, textStatus, jqXHR) {
                var $content = $("#" + $form.attr("contentdiv"));
                fillContentData(data, $content, $form.attr("contentdiv") == "third_content" || $form.attr("contentdiv") == "fourth_content");
                showContent($content);
                ajaxEndLoad($form.attr("contentdiv"));
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
                ajaxEndLoad();
            },
            cache: false
        });
        e.preventDefault();
    });
}

var preStylePath = "/Themes/VSAmerica/Styles/styles_"
var whiteStylePath = "white.css";
var blackStylePath = "black.css";

function bindColorSwitcher() {
    $("#palette_white").bind("click", function () { switchStyles(whiteStylePath); return false; });
    $("#palette_black").bind("click", function () { switchStyles(blackStylePath); return false; });
}

function switchStyles(stylesheet) {
    $('link[href^="' + preStylePath + '"]').attr("href", preStylePath + stylesheet);
}

function bindScreenHeight() {
    sizeScreenHeight();
//    $(window).resize(sizeScreenHeight);
//    $(document).ajaxComplete(sizeScreenHeight);
    setInterval(sizeScreenHeight, 100);
}

function sizeScreenHeight() {
    var height = $(window).height();
    $("#content").height(height - 79);
    $("#content_generated #content_generated_text").height(height - 124);
    $("#fourth_content .fourth_content_ul, #fourth_content form").height(height < 734 ? height - 124 : 610);
    $(".product_details").height(height < 690 ? 521 - (689 - height) : 521);
    $("body").height(height < 800 ? 800 : "100%");

    setScrollbars($(".scrollable"));
    
}

function ajaxStartLoad() {
    $.blockUI({
        message: "<div class=\"loader_bg\"><div class=\"loader_fg\"></div></div>",
        css: {
            "background-color": "transparent",
            "border": "0",
            "top": "50%",
            "left": "50%",
            "margin-left": "-40px",
            "margin-top": "-40px"
        }
    });
}

function ajaxEndLoad(contentdiv) {
    if (contentdiv == "second_content")
        if ($("#third_content").hasClass("showing"))
            hideContent($("#third_content"));

    if (contentdiv == "second_content" || contentdiv == "third_content")
        if ($("#fourth_content").hasClass("showing"))
            hideContent($("#fourth_content"));

    $.unblockUI();
}

function modalUrl(url, callback) {
    ajaxStartLoad();
    $.ajax(url, {
        success: function (data, textStatus, jqXHR) {
            ajaxEndLoad();
            $.blockUI({
                message: data,
                onBlock: callback,
                css: {
                    "width": "650px",
                    "height": "450px",
                    "background-color": "#ffffff",
                    "border": "1px solid #ffffff",
                    "top": "50%",
                    "left": "50%",
                    "margin-left": "-325px",
                    "margin-top": "-225px",
                    "cursor": "default",
                    "overflow": "hidden"
                },
                overlayCSS: {
                    "cursor": "default"
                }
            });
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
            ajaxEndLoad();
        },
        cache: false
    });
}

function bindModals() {

    $(".showBlog").live("click", function () {
        modalUrl($(this).attr("href"),
        function () {
            setScrollbars($(".blockMsg #content_generated_text"));
        });
        return false;
    });

    $(".showModal").live("click", function () {
        var $this = $(this);
        modalUrl("/Webstation.Module.VSAmerica/Product/ProductModal/" + $this.data("id"),
            function () {
                $(".modal_" + $this.data("tab")).click();
            });
        return false;
    });

    $("#modal_tabs a").live("click", function () {
        $(".modal_panel").hide();
        $("#modal_tabs li a.selected").removeClass("selected");

        var id = $(this).attr("href");
        id = id.substring(id.indexOf("#"));
        $(id).show();
        $(this).addClass("selected");
        setScrollbars($("#modal_content"));
        return false;
    });

    $(".unblocker").live("click", function () {
        $.unblockUI();
        return false;
    });
}

function setupCycle() {
//    $("#background_image_cycle").cycle({
//        fx: "scrollLeft",
//        timeout: cycleSpeed,
//        next: "#cycle_next",
//        before: function (currSlideElement, nextSlideElement, options, forwardFlag) {
//            $("#content_text").attr("class",
//                $(nextSlideElement).attr("id").replace("background_image", "content_text")
//            );
//        }
//    });
    $("#background_image_cycle").crossSlide({
        fade: 4
    }, [
        { src: "/Themes/VSAmerica/Content/Images/image_1.jpg", from: "top left", to: "top right", time: 10 },
        { src: "/Themes/VSAmerica/Content/Images/image_2.jpg", from: "top right", to: "top left", time: 10 },
        { src: "/Themes/VSAmerica/Content/Images/image_3.jpg", from: "top left", to: "top right", time: 10 },
        { src: "/Themes/VSAmerica/Content/Images/image_4.jpg", from: "top right", to: "top left", time: 10 },
        { src: "/Themes/VSAmerica/Content/Images/image_5.jpg", from: "top left", to: "top right", time: 10 },
        { src: "/Themes/VSAmerica/Content/Images/image_6.jpg", from: "top right", to: "top left", time: 10 },
        { src: "/Themes/VSAmerica/Content/Images/image_7.jpg", from: "top left", to: "top right", time: 10 },
        { src: "/Themes/VSAmerica/Content/Images/image_8.jpg", from: "top right", to: "top left", time: 10 }
    ]);
}

function bindDivData() {
    $("div").each(function () {
        $.data(this, "position", $(this).position());
    });
    $("#second, #third, #fourth").hide();
}

function setupMenus() {
    $.ajax("/Webstation.Module.VSAmerica/Tools/Menu", {
        success: function (data, textStatus, jqXHR) {
            $("#replaceme").replaceWith(data);
        },
        cache: false
    });

    $(".menu_item").live("click", function () {
        var $clicker = $(this);
        var url = $clicker.attr("href");
        var $content = $($clicker.attr("contentdiv"));

        $($clicker.parents("div")[0]).find(".selected").removeClass("selected");
        $clicker.parent("li").addClass("selected");

        fillContent(url, $content, $content.attr("id") == "content_generated" || $content.attr("id") == "fourth_content");
        showContent($content, $clicker);

        return false;
    });

    $(".product_close a, .content_close a").live("click", function () {
        $("#left .selected").removeClass("selected");
        hideAll();
        return false;
    });

    $(".blog_close a").live("click", function () {
        $.unblockUI();
        return false;
    });
}

function fillContent(url, $content, addCloseBar) {
    ajaxStartLoad();
    $.ajax(url, {
        success: function (data, textStatus, jqXHR) {
            fillContentData(data, $content, addCloseBar);
            ajaxEndLoad($content.attr("id"));
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
            ajaxEndLoad();
        },
        cache: false
    });
}

function fillContentData(data, $content, addCloseBar) {
    $content.html(data);

    if (addCloseBar) {
        var scrollable = $content.attr("id") == "content_generated" ? " class=\"scrollable\"" : "";

        $content.append("<div id=\"content_padding\"></div>")
            .wrapInner("<div id=\"content_generated_text\"" + scrollable + "/>")
            .prepend("<div class=\"content_close\"><div class=\"butt_x\"><a href=\"#\"></a></div></div>");
    }
    
}

function showContent($content, $clicker) {
    if ($clicker != null && !$clicker.hasClass("nohider"))
        hideAll();

    if (cycleIsRunning)
        pauseCycle();

    $("#second, #third, #fourth").hide();

    switch ($content.attr("id")) {
        case "second_content":
            $("#second").show();
            break;
        case "third_content":
            $("#second").show();
            $("#third").show();
            break;
        case "fourth_content":
            $("#second").show();
            $("#third").show();
            $("#fourth").show();
            break;
    }

    $content.animate({
        left: "0px"
    }, {
        duration: menuSlideSpeed
    });

    $content.addClass("showing");
}

function hideContent($content) {
    $content.animate({
        left: $content.data("position").left
    }, {
        duration: menuSlideSpeed
    });
    $("#fourth_content").scrollTo(0);
    $content.removeClass("showing");
//    $content.html("");

    if (!cycleIsRunning && $(".showing").length == 0)
        startCycle();
}

function hideAll() {
    $(".showing").each(function (index, content) {
        hideContent($(content));
    });
    $("#fourth_content").scrollTo(0);

    if (!cycleIsRunning)
        startCycle();
}

function startCycle() {
    $("#background_image_cycle").crossSlideResume();
    //    $("#background_image_cycle").cycle("resume");
    cycleIsRunning = true;
}

function pauseCycle() {
    $("#background_image_cycle").crossSlidePause();
//    $("#background_image_cycle").cycle("pause");
    cycleIsRunning = false;
}
