﻿/// <reference path="/Scripts/jquery-1.6.4-vsdoc.js" />

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-5163499-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);
    })();

String.prototype.format = function () {
    var s = this,
        i = arguments.length;

    while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return s;
}; 

function initHomePage() {
    maxWidth = 730; minWidth = 50;
    lastBlock = $("#a0");


    $(document).ready(function () {
        speed = 800; //900
        $('#a1').delay((speed * 5)).fadeIn(speed, function () {
            $('#a2').fadeIn(speed, function () {
                $('#a3').fadeIn(speed, function () {
                    $('#a4').fadeIn(speed, function () {
                        $('#a5').fadeIn(speed, function () {
                            $('#JQAccordion li').css("display", "block");

                            setTimeout(function () {
                                $('#JQAccordion .JQAccordionLIText').css("display", "block");
                                $(lastBlock).stop().animate({ width: minWidth + "px" }, { duration: speed });
                                $('#a1').stop().animate({ width: maxWidth + "px" }, { duration: speed });
                                lastBlock = $("#a1");
                                $('[data-accordion-statusicon]').addClass("JQAccordionExpand");
                                $('#a1 > [data-accordion-statusicon]').addClass("JQAccordionContract");
                            }, (speed * 3));
                        });
                    });
                });
            });

        });

        $('#frontPageBottomSlideshow').append("<img src='/Content/Images/frontPage/f2.jpg' />");
        $('#frontPageBottomSlideshow').append("<img src='/Content/Images/frontPage/f3.jpg' />");

        $("#JQAccordion li ").click(
        function () {
            if ($(lastBlock).attr("id") != "a5") { $(lastBlock).stop().animate({ width: minWidth + "px" }, { duration: speed }); };
            $(this).stop().animate({ width: maxWidth + "px" }, { duration: speed });
            lastBlock = this;
            $('[data-accordion-statusicon]').removeClass("JQAccordionContract");
            $(this).children('[data-accordion-statusicon]').addClass("JQAccordionContract");
        } //function
    ); //click
    });            //document.ready
}
function saveFile() {
    var sku = document.getElementById("productCodeSelection").innerHTML.replace('.', '_');
    var fileName = "/Content/Products/Photo/" + sku.substr(0, 3) + "X/" + document.getElementById("ddlFileType").value + "/" + sku + "." + document.getElementById("ddlFileType").value;
    window.location.href = fileName;
}

function startSlideshow() {
    var $active = $('[data-slideshow] > img.active');
    if ($active.length == 0) $active = $('[data-slideshow] > img:last');
    var $next = $active.next().length ? $active.next() : $('[data-slideshow] > img:first');
    $active.addClass('last-active');
    //  $("[data-slideshow] .slideshowText").html($next.attr("alt"));
    $next.css({ opacity: 0.0 }).addClass('active').animate({ opacity: 1.0 }, 1000, function () {
        $active.removeClass('active last-active');
    });
}

function updateCompareLink() {
    var c = getProductsCookie();
    document.getElementById("compareCount").innerHTML = '';

    if (c.length > 0) {
        $('#navigationCompare').hide();
        for (var i = 0; i < 4; i++) {
            if (i < c.length) {
                $('#compareCount').show().prepend('<img src="/Content/Images/contract.png" alt="Compare" />');
            } else {
                $('#compareCount').show().prepend('<img src="/Content/Images/expand.png" alt="Compare" />');
            }
        }
        $('#navigationCompare').fadeIn('slow');
    } else {
        $('#navigationCompare').hide();
    }
}

function initProductPage() {
    $(document).ready(function () {
        $("#productMatrix ul").children('li:first-child').css("background", "#dadada"); //#c9c9c9

        var i = '';
        var j = 0;
        $("#productMatrix ul").children('li:first-child').each(function () {
            j++;
            i = i + $(this).attr("data-suffix");
            $("#matrix" + j).html($(this).attr("data-suffix"));
        });
        $('#productCodeSelection').html(i + staticSuffix);
        $('#productCodeExample').html('example - ' + i + staticSuffix);

        $('#productMatrix li').click(function () {
            $(this).parent().children().css("background", "white");
            $(this).css("background", "#dadada");
            var matrix = 'matrix' + $(this).parent().attr("data-matrix");
            var val = $(this).attr("data-suffix");
            $('#' + matrix).html(val);
            $('#productCodeSelection').html("");
            $("span[id^='matrix']").each(function () {
                $('#productCodeSelection').html($('#productCodeSelection').html() + $(this).html());
            });
            $('#productCodeSelection').html($('#productCodeSelection').html() + staticSuffix);
        }); //click

        var startImagesrc = $('#productImagesGallery img').first().attr("data-fullsize-src");
        var startImageText = $('#productImagesGallery img').first().attr("data-gallery-text");
        $("#gallery-text").text(startImageText);
        $('#productImagesMain a').attr("href", startImagesrc);
        $('#productImagesMain img').attr("src", startImagesrc);
        $('#productImagesGallery img:first').css("border", "1px solid #c9c9c9");
        $("#tabs").tabs();
        $('[data-tooltip]').tipsy();
        $('[data-fullsize-src]').click(function () {
            var s = $(this).attr("data-fullsize-src");
            var t = $(this).attr("data-gallery-text");
            $('[data-fullsize-src]').css("border", "1px solid #e9e9e9");
            $(this).css("border", "1px solid #c9c9c9");
            $('#productImagesMain img').fadeOut(150, function () {
                $('#productImagesMain img').attr("src", s);
                $('#productImagesMain a').attr("href", s);
                $('#productImagesMain img').fadeIn(500);
                $("#gallery-text").text(t);
            });
        });

    });         //Document Ready
}

$(document).ready(function () {
    //Place general events here

    $(".menu ul li").hover(
                function () { $(this).children("div").first().delay(500).fadeIn(10); },
                function () { $(this).children("div").first().stop().css("display", "none"); }
    );

    //Search box watermarking
    searchText = "Search";
    $('#q').focus(function () { if ($(this).val() == searchText) { $(this).val(""); } });
    $('#q').blur(function () { if ($(this).val() == "") { $(this).val(searchText); } });

    //Init Compare
    var c = getProductsCookie();
    for (var i = 0; i < c.length; i++) {
        $('[data-compare="' + c[i] + '"]').html("Remove From Compare");
        $('#navigationCompare').show();
    }
    updateCompareLink();
    $('[data-compare]').click(function () {
        var action = doCompare($(this).attr("data-compare"));
        if (action == -1) { $(this).fadeOut('normal', function () { $(this).html("Add To Compare"); $(this).fadeIn(); }); }
        if (action == 1) { $(this).fadeOut('normal', function () { $(this).html("Remove From Compare"); $(this).fadeIn(); }); }
        if (action != 0) { updateCompareLink(); }
    });

    $('[data-zoom]').fancybox({ 'transitionIn': 'none', 'transitionOut': 'none' });
    $('[data-zoom]').hover(
            function () { $(this).parent().children(".zoom").fadeIn(); }
            ,
            function () { $(this).parent().children(".zoom").fadeOut(); });

    $('.productGroupThumbnailText').hover(
        function () { $(this).parent().children('.productGroupThumbnailDescription').fadeIn(500) },
        function () { $(this).parent().children('.productGroupThumbnailDescription').fadeOut(100); }
        );

}); 
