﻿cookieCart.adjustUI = function () {
    $('.cart').html(
        '<img src="//s3.amazonaws.com/aventura/images/nav_images/shopping_bag.gif" />' + 
        ' (' + 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,
    isMacSafari = (navigator.userAgent.indexOf('Mac') > -1 && navigator.userAgent.indexOf('Safari') > -1),
    topAdjustment = (isMacSafari) ? 45 : 10;
    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 + topAdjustment, '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 () { // menu script
    function makeOn($img) {
        var src = $img.attr('src');
        $img.attr('src', src.replace('_off.', '_on.'));
    }
    $('.menu a').each(function () {
        var href = $(this).attr('href');
        if (window.location.pathname.indexOf(href) === 0) {
            $(this).data('keepon', true);
            makeOn($(this).find('img'));
        }
    }).hover(function () {
        makeOn($(this).find('img'));
    }, function () {
        if ($(this).data('keepon')) {
            return;
        }
        var $img = $(this).find('img'), src = $img.attr('src');
        $img.attr('src', src.replace('_on.', '_off.'));
    });
});
