$(document).ready(function() {

    $('body').append('<div id="dialogContainer"></div>');
    $('#dialogContainer').hide();

    basket.ini();

    // loading gfx
    $('body').ajaxStart(function() {
        $('#basketLoadingImage').show();
    }).ajaxStop(function() {
        $('#basketLoadingImage').hide();
    });

    $('#newHighlightsNext').show();
    $('#newHighlightsPrev').show();

    // offer of the month
    if ($('#currentOfferArticles').length > 0) {
        $('#currentOfferArticles').cycle({
            fx : 'scrollHorz',
            speed : 600,
            pause : true,
            timeout : 6000,
            next : '#newHighlightsNext',
            prev : '#newHighlightsPrev'
        });
    }

    shippingCalculator.ini();
});

var shippingCalculator = {
    ini : function() {
        if ($('.shippingCalculator')) {
            var container = $('.shippingCalculator');

            $('#btnCalculateShipping').live('click', function() {

                $('#basketShippingCosts').html('');

                var countryContainer = $('#basketShipToCountry');
                var zipContainer = $('#basketShipToZip');

                $.getJSON('/xhr/basket/calculate_shipping.php', {
                    zip : zipContainer.val(),
                    country : countryContainer.val()
                }, function(data) {
                    if (data.status == 'success') {
                        $('#basketShippingCosts').html(data.shippingCosts);
                    } else {
                        $('#basketShippingCosts').html(data.shippingLabel);
                    }
                });
            });
        }
    }
};

var basket = {

    isTransparent : false,

    isUp : false,

    ini : function() {

        if ($('#wrapper').length == 0) {
            return;
        }

        $('#wrapper').append('<div id="footerShoppingBasket"></div>');
        basket.registerForms();

        basket.update();

        $('#footerShoppingBasket').css({
            opacity : "0.8",
            marginBottom : "-70px"
        });

    },

    update : function() {

        $('#footerShoppingBasket').load('/xhr/basket/bottom.php', {
            loc : conf.loc,
            localeCode : conf.localeCode
        }, function(responseText, textStatus, XMLHttpRequest) {
            if (textStatus == 'success') {
                basket.registerBottomButtons();
            }
        });

    },

    registerForms : function(handler) {

        // article addding to basket
        $('.BasketForm', handler).live('submit', function() {

            // close all dialogs
            $('#dialogContainer').dialog('destroy');

            $(this).ajaxSubmit({
                success : basket.add,
                url : '/xhr/basket/add.php'
            });

            return false;
        });

    },

    handleFooterBasket : function() {

        if (basket.isUp) {
            basket.down();
        } else {
            basket.up();
        }
    },

    up : function() {
        if (!basket.isUp) {
            $('#footerShoppingBasket').animate({
                opacity : "1.0",
                bottom : "0px",
                marginBottom : "0px"
            }, 500);

            // set btn gfx
            $('.basketHandlerArrow').removeClass('up');
            $('.basketHandlerArrow').addClass('down');

            basket.isUp = true;
            basket.isTransparent = false;

            if ($('#dialogContainer').length > 0) {
                $('#dialogContainer').dialog('destroy');
            }
        }
    },

    down : function() {
        if (basket.isUp) {
            $('#footerShoppingBasket').animate({
                opacity : "0.8",
                marginBottom : "-70px"
            }, 500);

            // set btn gfx
            $('.basketHandlerArrow').removeClass('down');
            $('.basketHandlerArrow').addClass('up');

            basket.isUp = false;
            basket.isTransparent = true;
        }
    },

    add : function(responseText, statusText) {

        var basketSystemResponse = responseText;

        $('#dialogContainer').html(basketSystemResponse);

        $('#continueShopping').click(function() {
            $('#dialogContainer').dialog('destroy');
            return false;
        });

        $('#dialogContainer').dialog({
            close : function(event, ui) {
                $(this).dialog('destroy');
            },
            autoOpen : true,
            bgiframe : true,
            width : 550,
            modal : true,
            draggable : false,
            resizable : false
        });

        $('.ui-widget-overlay').live('click', function() {
            $('#dialogContainer').dialog("close");
        });

        if (statusText == 'success') {

            $('#footerShoppingBasket').load('/xhr/basket/bottom.php', {
                loc : conf.loc,
                localeCode : conf.localeCode
            }, function(responseText, textStatus, XMLHttpRequest) {
                if (textStatus == 'success') {
                    toolTip.ini();
                    basket.up();
                    basket.registerBottomButtons();
                }
            });

            if ($('#customerHeaderBasket').length > 0) {
                $('#customerHeaderBasket').load(
                        '/xhr/basket/customer-header.php', {
                            loc : conf.loc,
                            localeCode : conf.localeCode
                        });
            }

            // if there is the old BasketSummary update it
            if ($('#BasketSummary').length > 0) {
                $('#BasketSummary').load('/xhr/basket.php', {
                    loc : conf.loc,
                    localeCode : conf.localeCode,
                    bact : 'bskupdate'
                });
                $('#BasketSummary').removeAttr('style');
            }

        }

    },

    registerBottomButtons : function() {

        $('.basketHandlerArrow').click(function() {
            basket.handleFooterBasket();
        });

        if (!basket.isUp) {
            $('.basketHandlerArrow').removeClass('down');
            $('.basketHandlerArrow').addClass('up');
        } else {
            $('.basketHandlerArrow').removeClass('up');
            $('.basketHandlerArrow').addClass('down');
        }

        toolTip.ini();

    }

};
