﻿cookieCart.adjustUI = function () {
    var c = cookieCart.itemCount(),
            p = (c === 1) ? '' : 'S';
    $('#cart-items').text(c + ' ITEM' + p + ' IN YOUR CART');
    if (c === 0) {
        $('.checkout').css({ display: 'none' });
        $('#cart').css({ visibility: 'visible' });

    } else {
        $('.checkout').css({ display: 'inline' });
        $('.checkout').css({ visibility: 'visible' });
        $('#cart').css({ visibility: 'visible' });
    }
}
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 () {
    var preloadImages = [
                '//s3.amazonaws.com/dragon.com/images/popup_top.png',
                '//s3.amazonaws.com/dragon.com/images/popup_bottom.png',
                '//s3.amazonaws.com/dragon.com/images/popup_middle.png'
            ],
            $body = $('body');
    for (var i = 0; i < preloadImages.length; i++) {
        $body.append($('<img />').attr('src', preloadImages[i]).css({
            'position': 'absolute',
            'left': -1000,
            'top': -10000
        }));
    }
    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 = 776,
                mh = 563;

    $('.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;
}
function closeModalWindow() {
    modalOpen = false;
    $('.modal-window').fadeOut('fast');
    $('.modal-backdrop').fadeOut('fast');
}
var modalTimeout = null,
            modalTime = 250,
            modalOpen = false;
$(function () {
    function sizeModalBackdrop() {
        $('.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 = $('.modal-window').data('dimensions');
                var $window = $(window),
                            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);
            }
        }, modalTime);
    });
    $('.modal-window .close').click(function (ev) {
        ev.preventDefault();
        closeModalWindow();
    });
    $('.coming-soon').click(function (ev) {
        ev.preventDefault();
        if ($('.coming-soon-back').length === 0) {
            var $w = $(window);
            $('<div />')
                .appendTo($('body'))
                .addClass('coming-soon-back')
                .css({ opacity: 0, left: $w.scrollLeft, top: $w.scrollTop(), width: $w.width(), height: $w.height() })
                .append(
                    $('<div />').text($(this).attr('title'))
                        .css({fontSize: 40, top: ($w.scrollTop() + ($w.height() / 2) - 40) })
                )          
                .animate({ opacity : 0.7},'fast')
                .delay(4000)
                .fadeOut('fast',function(){
                    $('.coming-soon-back').remove();
                });
        }
    });
});
(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 () {
        $('#header a').hover(
                    function () {
                        var $this = $(this),
                        $dd = $('#dropdown_' + $this.attr('id')),
                        pos = $this.position();
                        $dd.css({ 'top': pos.top + 127, 'left': pos.left, zIndex : 1000000 });
                        changeDD($dd, 'slideDown');
                    },
                    function () {
                        var $dd = $('#dropdown_' + $(this).attr('id'));
                        changeDD($dd, 'slideUp');
                    }
                );
            $('.ddl-header-filter').hover(
                function () {
                    var $this = $(this),
                    $dd = $('#dropdown_' + $this.attr('id')),
                    pos = $this.position();
                    $dd.css({ 'top': pos.top + 15, 'left': pos.left, zIndex : 1000000 });
                    changeDD($dd, 'slideDown');
                },
                function () {
                    var $dd = $('#dropdown_' + $(this).attr('id'));
                    changeDD($dd, 'slideUp');
                }
            );
        $('.dropmenu').hover(
                    function () {
                        var $dd = $(this);
                        clearDDEvent($dd);
                    },
                    function () {
                        var $dd = $(this);
                        changeDD($dd, 'slideUp');
                    }
                );
        var originalNewsletterValue = $('#newsletteremail').val();
        $('#newsletteremail').click(function () {
            if (this.value === originalNewsletterValue) {
                this.value = '';
            }
        }).blur(function(){
            if ($.trim(this.value) === '') {
                this.value = originalNewsletterValue;
            }
        });
        $('.copyright-wording').click(function(){
            kitgui.showTab();
        });
    });
    $('<img />')
        .attr('src','//s3.amazonaws.com/dragon.com/images/menuarrow.png')
        .css({display:'none'})
        .appendTo('body');
    //var isAU = (window.location.href.indexOf('/en-au') > -1);
    /*window.setInterval(function(){
        if (isAU) {
            $('span:contains("$"),div:contains("$")').each(function(){
                var html = $(this).html();
                $(this).html(html.replace('$','AUD'));
            });
        }
    },500);*/
})();
