﻿cookieCart.adjustUI = function () {
    $('.cart').text('My Cart (' + cookieCart.itemCount() + ')');
}
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();
};
cookieCart.init();
$(function () {
    cookieCart.adjustUI();
});

(function() {
    var $mb = $('.modal-backdrop'),
    $mw = $('.modal-window'),
    $w = $(window),
    modalVisible = false;
    window.hubsoft = {
        adjustModalRelated : function() {
            if (modalVisible) {
                var ww = $w.width(),
                wh = $w.height(),
                mw = $mw.width(),
                mh = $mh.height();
                $mb.css({ width: ww, height: wh });
                $mb.css({left: (ww / 2) - (mw / 2), top: (wh / 2) - (mw / 2)});
            }
        },
        showLoading: function (msg) {
            modalVisible = true;
            adjustModalRelated();
            $mb.fadeIn('fast', function () {
                $mw.css({ opacity: 0 }).animate({ opacity: 1 }, 'fast');
            });
        },
        hideLoading: function () {
            modalVisible = false;
            $mw.stop(true,true);
            $mb.stop(true,true);
            $mw.animate({ opacity: 0 }, 'fast', null, function () {
                $mw.css({ left: -10000 });
                $mb.fadeOut('fast');
            });
        }
    };
    /*$(window).bind('resize scroll', function () {
        adjustModalRelated();
    });*/
})();
$(function () {
    function preventBlankSubmit(ev) {
        var $dfb = $(this).find('.default-text-behavior'),
        val = $dfb.val(),
        original = $dfb.data('original');
        if ($.trim(val) === '' || val === original) {
            ev.preventDefault();
        }
    }
    $('.default-text-behavior').each(function () {
        $(this).data('original', $(this).val()).closest('form').submit(preventBlankSubmit);
    }).focus(function () {
        if ($(this).data('original') === $(this).val()) {
            $(this).val('');
        }
    }).blur(function () {
        if ($.trim($(this).val()) === '') {
            $(this).val($(this).data('original'));
        }
    });
    $('#top .menu a').each(function () {
        $(this).css({
            width: $(this).width()
        });
    });
});
(function () {
    var slideTime = 300;
    function clearDDEvent($dd) {
        if (typeof ($dd.data('timeout')) !== 'undefined' && $dd.data('timeout') !== null) {
            window.clearTimeout($dd.data('timeout'));
            $dd.data('timeout', null);
        }
    }
    function changeDD($dd, slideMethod) {
        clearDDEvent($dd);
        $dd.data('timeout', window.setTimeout(function () {
            $dd[slideMethod]('fast');
        }, slideTime));
    }
    $(function () {
        $('#top .menu a').hover(
                    function () {
                        var $this = $(this),
                        cssClass = $this.attr('class').split(' ')[0],
                        $dd = $('#dropdown-' + cssClass),
                        pos = $this.offset();
                        $this.removeAttr('title');
                        $dd.css({ 'top': pos.top + 60, 'left': pos.left + 10 });
                        changeDD($dd, 'slideDown');
                    },
                    function () {
                        var cssClass = $(this).attr('class').split(' ')[0], 
                        $dd = $('#dropdown-' + cssClass);
                        changeDD($dd, 'slideUp');
                    }
                );
        $('.dropmenu').hover(
                    function () {
                        var $dd = $(this);
                        clearDDEvent($dd);
                    },
                    function () {
                        var $dd = $(this);
                        changeDD($dd, 'slideUp');
                    }
                );
        $('.copyright').click(function () {
            kitgui.showTab();
        });
    });
})();
$(window).bind('resize load', function () {
    var winHeight = $(window).height(),
    bodyHeight = $('body').height(),
    wrapHeight = (winHeight > bodyHeight) ? winHeight : bodyHeight;
    $('#site-wrap').css({ minHeight: wrapHeight });
});
function getHemmmingMessage(sku, hemming,showModifyLink) {
    if (hemming === null) {
        return { 'hasHemming': false, 'message': '' };
    }
    var hemmingMessage = '', hemCode, hasHemming = false, isHemmable = false;
    for (var i = 0; i < hemming.length; i++) {
        hasHemming = false;
        isHemmable = false;
        if (hemming[i].sku === sku) {
            hemCode = hemming[i].hemCode;
            hemmingMessage = '<br />';
            isHemmable = true;
            if (hemCode.substr(0, 1) === 'S') {
                hemmingMessage += 'Hem Straight ';
                hasHemming = true;
            } else if (hemCode.substr(0,1) == 'C') {
                hemmingMessage += 'Hem Cuffed ';
                hasHemming = true;
            }
            if (hasHemming) {
                hemmingMessage += hemCode.substr(1, 2)
                if (hemCode.length > 3) {
                    hemmingMessage += ' 1/2';
                }
                hemmingMessage += ' inches ($5.95 per hemming)';
            }
            break;
        }
    }
    if (showModifyLink) {
        if (hasHemming) {
            hemmingMessage += ' <a href="/Embellishments.aspx">adjust hemming</a>';
        } else if (isHemmable) {
            hemmingMessage += ' <a href="/Embellishments.aspx">add hemming</a>';
        }
    }
    return { 'hasHemming': hasHemming, 'message': hemmingMessage };
}
$(function () {
    $('.products .color').live('click', function () {
        var $this = $(this),
            $colors = $this.closest('.colors').find('.color'),
            data = $this.attr('title').split(';'),
            src = data[0],
            url = data[1],
            $price = $this.find('.price');
        $this.closest('.product')
            .find('.thumb img').attr('src', src).end()
            .find('a').each(function () {
                $(this).attr('href', url);
            }).end()
            .find(' > .price').html($price.html());
        $colors.removeClass('color-selected');
        $this.addClass('color-selected');
    });
});
