﻿// focus on first element with class focus
$(document).ready(function () {
    $(".focus:first").focus();
});


// enable the main menu drop downs
$(document).ready(function () {
    $("#nav li").mouseover(function () {
        $(this).addClass("over");
    });
    $("#nav li").mouseout(function () {
        $(this).removeClass("over");
    });
});

// enable the side bar to collapse and expand
$(document).ready(function () {
    $("#accordion h3").click(function () {
        $(this).next().toggle("medium");
        return false;
    });
});

// enable date picker for all elements with the date class
$(document).ready(function () {
    $('.date').datepicker({ dateFormat: "mm/dd/yy", showButtonPanel: true });
});


// apply jquery ui to buttons
$(document).ready(function () {
    $("button, input:submit", "div.main").button();
    $("a.button").button();
    $(".view-cart-button").button({ icons: { primary: 'ui-icon-cart', secondary: null }, text: true })
});

// make all the list items in each list the same height
$(document).ready(function () {
    $(".result-listing ul").each(function (i) {
        var maxHeight = 0;
        $("a", this).each(function (i) {
            if ($(this).height() > maxHeight) {
                maxHeight = $(this).height();
            }
        });
        $("a", this).each(function (i) {
            var changeInHeight = maxHeight - $(this).height();
            $(this).height(maxHeight);
            $("span.add-quantity", this).css({ "margin-top": changeInHeight + 4 });
        });
    });
    $(".result-listing ul span.add-quantity input").click(function (event) {
        event.preventDefault();
    });
});


// google analytics
$(document).ready(function () {
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-9649376-6']);
    _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);
    })();
});

// slideshow
$(document).ready(function () {
    function getData(element, key, defaultValue) {
        var data = $(element).data(key);
        return undefined === data ? defaultValue : data;
    }
    function setSlideshowTimer(slideshow, options, delay) {
        options.timer = setTimeout(function () {
            var next = null;
            if (options.random) {
                var hiddenImages = slideshow.children(options.tagName + ":hidden");
                next = hiddenImages.eq(Math.floor(Math.random() * hiddenImages.length));
            } else {
                var next = slideshow.children(options.tagName + ":visible").next(options.tagName);
                if (next.length == 0) {
                    next = slideshow.children(options.tagName + ":first");
                }
            }
            showSlideshowImage(next, slideshow, options);
            setSlideshowTimer(slideshow, options);
        }, (undefined === delay ? options.delay : delay));
    }
    function showSlideshowImage(image, slideshow, options) {
        slideshow.children(options.tagName + ":visible").hide();
        image.show(options.showDuration);
        slideshow.children(".ss-controls").children("a.ssc-selected").removeClass("ssc-selected");
        slideshow.children(".ss-controls").children("a.ssc-numbered").eq(image.index()).addClass("ssc-selected");
    }
    $(".slideshow").each(function (index) {
        var slideshow = $(this);
        var options = {};
        options.delay = getData(slideshow, "ss-delay", 8000);
        options.random = getData(slideshow, "ss-random", false);
        options.tagName = getData(slideshow, "ss-tag-name", "a");
        options.showDuration = getData(slideshow, "ss-duration", 1000);
        options.playPauseControl = getData(slideshow, "ss-play-pause-control", true);
        options.numberedControls = getData(slideshow, "ss-numbered-controls", true);
        options.prevNextControls = getData(slideshow, "ss-prev-next-controls", false);
        options.bottom = getData(slideshow, "ss-bottom", 15);
        options.right = getData(slideshow, "ss-right", 15);
        options.top = getData(slideshow, "ss-top", false);
        options.left = getData(slideshow, "ss-left", false);
        options.autoPlay = getData(slideshow, "ss-auto-play", true);

        if (slideshow.children(options.tagName).length < 2) {
            return;
        }

        if (options.playPauseControl || options.numberedControls || options.prevNextControls) {
            // create the controls
            var controls = $('<div class="ss-controls"></div>');
            slideshow.append(controls);
            if (false !== options.bottom) controls.css("bottom", options.bottom);
            if (false !== options.right) controls.css("right", options.right);
            if (false !== options.top) controls.css("top", options.top);
            if (false !== options.left) controls.css("left", options.left);
            if (options.playPauseControl) {
                controls.append('<a href="#" class="ssc-play-pause ssc-pause"></a>');
            }
            if (options.prevNextControls) {
                controls.append('<a href="#" class="ssc-prev">&lt;</a>');
            }
            if (options.numberedControls) {
                for (var i = 0; i < slideshow.children(options.tagName).length; i++) {
                    controls.append('<a href="#" class="ssc-numbered' + (i == 0 ? ' ssc-selected' : '') + '">' + (i + 1) + '</a>');
                }
            }
            if (options.prevNextControls) {
                controls.append('<a href="#" class="ssc-next">&gt;</a>');
            }

            // wire up the click handlers
            controls.children("a.ssc-play-pause").click(function () {
                clearTimeout(options.timer);
                if ($(this).hasClass("ssc-play")) {
                    setSlideshowTimer(slideshow, options, 0);
                }
                $(this).toggleClass("ssc-play ssc-pause");
                return false;
            });
            controls.children("a.ssc-prev").click(function () {
                var prev = slideshow.children(options.tagName + ":visible").prev(options.tagName);
                if (prev.length == 0) {
                    prev = slideshow.children(options.tagName + ":last");
                }
                showSlideshowImage(prev, slideshow, options);
                return false;
            });
            controls.children("a.ssc-numbered").click(function () {
                showSlideshowImage(slideshow.children(options.tagName).eq($(this).text() - 1), slideshow, options);
                return false;
            });
            controls.children("a.ssc-next").click(function () {
                var next = slideshow.children(options.tagName + ":visible").next(options.tagName);
                if (next.length == 0) {
                    next = slideshow.children(options.tagName + ":first");
                }
                showSlideshowImage(next, slideshow, options);
                return false;
            });
        }
        if (options.autoPlay) {
            setSlideshowTimer(slideshow, options);
        }
    });
});

