﻿
cookieCart.adjustUI = function () {
    var c = cookieCart.itemCount(),
                p = (c === 1) ? '' : 's';
    $('#cart-items').text(c + ' Item' + p + ' in cart');
    if (c === 0) {
        $('.top-checkout').hide();
    } else {
        $('.top-checkout').show();
    }
}
cookieCart.snapshot = function () {
    if (!cookieCart.history) {
        cookieCart.history = [];
    }
    cookieCart.history.push([]);
    var lastIndex = cookieCart.history.length - 1;
    for (var i = 0; i < cookieCart.items.length; i++) {
        cookieCart.history[lastIndex][i] = {};
        cookieCart.history[lastIndex][i].key = cookieCart.items[i].key;
        cookieCart.history[lastIndex][i].val = cookieCart.items[i].val;
    }
};
cookieCart.eraseHistory = function () {
    cookieCart.history = [];
};
cookieCart.undo = function () {
    if (cookieCart.history && cookieCart.history.length > 0) {
        cookieCart.items = [];
        var first = cookieCart.history[0];
        for (var i = 0; i < first.length; i++) {
            cookieCart.items[i] = {};
            cookieCart.items[i].key = first[i].key;
            cookieCart.items[i].val = first[i].val;
        }
    }
    cookieCart.history = [];
    cookieCart.repopulateCookie();
    cookieCart.adjustUI();
};
$(function () {
    cookieCart.init();
    cookieCart.adjustUI();
});
Array.prototype.remove = function (from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
};
function openModalWindow(url, loadFunction) {
    var $window = $(window),
                ww = $window.width(),
                wh = $window.height(),
                mw = 950,
                mh = 563;

    if (arguments.length > 2) {
        $('.stance-modal-window').removeClass('stance-modal-window-big');
        if (arguments[2] === 'big') {
            mw = 950;
            mh = 672;
            $('.stance-modal-window').css({
                width: mw,
                height: mh,
                backgroundImage: 'url(//s3.amazonaws.com/Stance/images/detailbox_lrg.png)'
            }).addClass('stance-modal-window-big').find('.padding').css({ width: 880, height: 612 });
            $('.stance-modal-window').data('dimensions', { width: mw, height: mh });
        }
    } else {
        mw = 950;
        mh = 563;
        $('.stance-modal-window').css({
            width: mw,
            height: mh,
            backgroundImage: 'url(//s3.amazonaws.com/Stance/images/detailbox.png)'
        }).find('.padding').css({ width: 880, height: 488 });
        $('.stance-modal-window').data('dimensions', { width: mw, height: mh });
    }

    $('.stance-modal-backdrop').css({ opacity: .5 }).fadeIn('fast');
    $('.stance-modal-window')
                .find('.padding')
                .html('')
                .load(url, function () {
                    loadFunction();
                    $('.stance-modal-window').fadeIn('fast', function () { modalOpen = true; });
                })
                .end()
                .css({
                    left: (ww / 2) - (mw / 2),
                    top: (wh / 2) - (mh / 2)
                });
    modalOpen = true;
}
function closeModalWindow() {
    modalOpen = false;
    $('.stance-modal-window').fadeOut('fast');
    $('.stance-modal-backdrop').fadeOut('fast');
}
var modalTimeout = null,
            modalTime = 250,
            modalOpen = false;
$(function () {
    function sizeModalBackdrop() {
        $('.stance-modal-backdrop').css({ width: $(window).width(), height: $(window).height() });
    }
    sizeModalBackdrop();
    $(window).bind('resize', function () {
        if (modalTimeout !== null) {
            window.clearTimeout(modalTimeout);
            modalTimeout = null;
        }
        modalTimeout = window.setTimeout(function () {
            if (modalOpen) {
                sizeModalBackdrop();
                var dimensions = $('.stance-modal-window').data('dimensions');
                var $window = $(window),
                            ww = $window.width(),
                            wh = $window.height(),
                            mw = dimensions.width,
                            mh = dimensions.height
                $('.stance-modal-window')
                            .stop(true, true)
                            .animate({
                                left: (ww / 2) - (mw / 2),
                                top: (wh / 2) - (mh / 2)
                            }, modalTime);
            }
        }, modalTime);
    });
    $('.stance-modal-window .close').click(function (ev) {
        ev.preventDefault();
        closeModalWindow();
    });

    var $region = $('#regionDDL'),
            path = window.location.pathname;
    for (var i = 1; i < $region[0].options.length; i++) {
        if (path.indexOf($region[0].options[i].value) > -1) {
            $region[0].selectedIndex = i;
            break;
        }
    }
    $region.change(function () {
        var a = path.substr(1).split('/');
        if ((/^[a-z]{2}-[A-Z]{2}$/).test(a[0])) {
            a.remove(0, 0);
        }
        page = a.join('/');
        window.location = $region.val() + page;
    });
    $('.top-links a img').hover(
                function () {
                    $(this).attr('src', $(this).attr('src').replace('_off.jpg', '_on.jpg'));
                },
                function () {
                    $(this).attr('src', $(this).attr('src').replace('_on.jpg', '_off.jpg'));
                });

    $('#sub-hero a img').hover(
                function () {
                    $(this).attr('src', $(this).attr('src').replace('_off.jpg', '_on.jpg'));
                },
                function () {
                    $(this).attr('src', $(this).attr('src').replace('_on.jpg', '_off.jpg'));
                });
});


var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17257468-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);
})();


