﻿cookieCart.adjustUI = function () {
    var c = cookieCart.itemCount();
    $('#cart-items').text('( ' + c + ' )');
    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();
    noNavOnLinkButtonClick();
});

function noNavOnLinkButtonClick() {
    $('.no-nav').attr('href', 'javascript:void(0)');
}

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 fixViewCartHeight() {
    $('#top-cart-contents').css('height', 'auto');    
}
function watcherPatternHandler(){
    fixViewCartHeight();
}
function openModalWindow(url, loadFunction) {//function open popup window
    //declaring variables
    var $window = $(window), //window
                ww = $window.width(), //window width
                wh = $window.height(), //window height
                mw = 664, // model width
                mh = 563, //model height
                $modelWindow = $('.modal-window'),
                $stanceModelWindow = $('.stance-modal-window'); 

    if (arguments.length > 2) {//if third arguement
        $modelWindow.removeClass('modal-window-big');
        
        if (arguments[2] === 'big') { //third argument is big
            mw = 664;//big width
            mh = 672;//big height
            $stanceModelWindow.css({
                width: mw,//set big width
                height: mh,//set bih height
                backgroundColor: 'black'
            }).addClass('modal-window-big')
            .find('.padding')
            .css({ width: 664, height: 612 });
            $('.modal-window').data('dimensions', { width: mw, height: mh });
        }
    }
    else {
        mw = 664;
        mh = 563;
        $('.modal-window').css({
            width: mw,
            height: mh,
            backgroundColor: 'black'
        }).find('.padding').css({ width: 664, height: 488 });
        $('.modal-window').data('dimensions', { width: mw, height: mh });
    }

    $('.modal-backdrop').css({ opacity: .5 }).fadeIn('fast');
    $('.modal-window')
                .find('.padding')
                .html('')
                .load(url, function () {
                    loadFunction();
                    $('.modal-window').fadeIn('fast', function () { modalOpen = true; });
                })
                .end()
                .css({
                    left: (ww / 2) - (mw / 2),
                    top: (wh / 2) - (mh / 2)
                });
    modalOpen = true;
}



/**
 *Opens the popup from a url
 */
function openProductModal(url, loadFunction) {//function open popup window
    //declaring variables
    var $window = $(window), //window
                ww = $window.width(), //window width
                wh = $window.height(), //window height,
                mw = 684, // model width
                mh = 519, //model height
                $modelWindow = $('.modal-window'),
                $modalBackDrop = $('.modal-backdrop');

        //set dimensions to modelWindow jquery data
        $modelWindow.data('dimensions', { width: mw, height: mh });
        
        $modalBackDrop.css({ opacity: .5, height: wh, width: ww, backgroundColor: 'black' }).fadeIn('fast');
        $modelWindow.css({
            width: mw,
            height: mh
        }).find('.padding')
                .html('')
                .load(url, function () {
                    loadFunction();
                    $modelWindow.fadeIn('fast', function () { modalOpen = true; });
                })
                .end()
                .css({
                    left: (ww / 2) - (mw / 2),
                    top: (wh / 2) - (mh / 2)
                });
    modalOpen = true;
}


/**
 *Close Modal Window function definition
 */
function closeModalWindow() {
    modalOpen = false;
    $('.modal-window').fadeOut('fast');
    $('.modal-backdrop').fadeOut('fast');
}

//close modal live event
$(function () {
    $('.close-modal')
    .live('click', function () {
        closeModalWindow();
        });
    });

var modalTimeout = null,
            modalTime = 250,
            modalOpen = false;
$(function () {

    //Set size on modal backdrop
    function sizeModalBackdrop() {
        $('.modal-backdrop').css({ width: $(window).width(), height: $(window).height() });
    }

    //Do size modal backdrop
    sizeModalBackdrop();

    //Set position of modal popup on window resize
    $(window).bind('resize scroll', function () {
        if (modalTimeout !== null) {
            window.clearTimeout(modalTimeout);
            modalTimeout = null;
        }
        modalTimeout = window.setTimeout(function () {
            if (modalOpen) { //if modal is shown
                sizeModalBackdrop(); //resize backdrop
                var dimensions = $('.modal-window').data('dimensions');
                var $window = $(window), //take latest variables
                            ww = $window.width(),
                            wh = $window.height(),
                            mw = dimensions.width,
                            mh = dimensions.height
                $('.modal-window')
                            .stop(true, true)
                            .animate({
                                left: (ww / 2) - (mw / 2),
                                top: (wh / 2) - (mh / 2)
                            }, modalTime); //Animate the position of modal
            }
        }, modalTime);
    });

    //Modal close button clicked event
    $('modal-window .close').click(function (ev) {
        ev.preventDefault();
        closeModalWindow();
    });
});

//jQuery.fx.interval = 100;

//Animation
$(function () {
    var $bg = $('#bg'), colors = [0, 0, 0], colorIndex = 0, $window = $(window), $body = $('body');
    $window.bind('resize load scroll', function () {
        var ww = $window.width(), bw = $body.width(), w = (ww > bw) ? ww : bw,
        wh = $window.height(), bh = $body.height(), h = (wh > bh) ? wh : bh;
        $bg.css({
            position: 'absolute',
            width: w,
            height: h,
            opacity: 0.4
        });
    });
    
    function startColorAnimate(s, f) {
        if (window.isSiteLive == 0) {return; }
        $bg.css({ alpha: 0 });
        $({ count: s }).animate({ count: f }, {
            duration: 5000,
            step: function () {
                colors[colorIndex] = Math.round(this.count);
                var rgb = 'rgb(' + colors[0] + ',' + colors[1] + ',' + colors[2] + ')';
                $bg.css({
                    backgroundColor: rgb
                });
            },
            complete: function () {
                Math.random()
                var f = 0;
                colorIndex = Math.floor(Math.random() * 3 + 1) - 1;
                switch (colors[colorIndex]) {
                    case 0: f = 50; break;
                    case 50: f = 100; break;
                    case 100: f = 150; break;
                    case 150: f = 200; break;
                    case 200: f = 255; break;
                    case 255: f = 201; break;
                    case 201: f = 151; break;
                    case 151: f = 101; break;
                    case 101: f = 51; break;
                    case 51: f = 0; break;
                }
                setTimeout(
                    function(){
                        startColorAnimate(colors[colorIndex], f)
                    },
                    2000
                    );
            }
        });
    }
    colorIndex = Math.floor(Math.random() * 3 + 1) - 1;
    startColorAnimate(0, 50);
});
$(function () { // logo animation
    var $logo = $('#logo');
    function startLogoAnimate(s, f) {
        $({ count: s }).animate({ count: f }, {
            duration: 50000,
            easing: 'linear',
            step: function () {
                $logo.css({ 'background-position': this.count.toFixed(0) + 'px 0px' });
            },
            complete: function () {
                $logo.css({ 'background-position': '0px 0px' });
                startLogoAnimate(0, -458);
            }
        });
    }
    startLogoAnimate(0, -458);
});

$(function () {
    $('#LocatorSearchForm').submit(function () {
        if ($('#location').valid()) {
            window.location = '/stores/' + escape($('#location').val());
        }
        return false;
    });
    $('#location').waterMarker('location-input', 'location-input', 'Enter Postal Code');
    $('#LocatorSearchForm').validate();
    $('.newsletter-bottom .email')
    .data('default', $('.newsletter-bottom .email').val())
    .css({ color: '#666' })
    .blur(function () {
        if ($.trim($(this).val()) === '') {
            $(this).val($(this).data('default')).css({ color: '#666' });
        }
    }).focus(function () {
        if ($(this).val() === $(this).data('default')) {
            $(this).val('');
        }
        $(this).css({ color: '#000' });
    });
});
