/* -*- js2 -*-
 *
 * $id$
 */

var BasketThing = {

    basketURL: null,
    hold: false,
    productsVisible: true,
    basketCount: null,

    getBasketCount: function() {
        if (this.basketCount != null) {
            return this.basketCount;
        }
        else {
            var sel = $(".basket_info .basket_count");
            if (sel.size() < 1) return 0;
            return parseInt(sel.text());
        };
    },

    realign: function() {
        //alert(jQuery.browser.msie + ' + ' + jQuery.browser.version);
        if (jQuery.browser.msie && jQuery.browser.version == "6.0") {
            var left = ($(window).width()-290)+($(window).scrollLeft());
            var top = ($(window).scrollTop()+105);
            $("#basket_thing_wrapper").css({
                'position': 'absolute',
                'left': left+'px',
                'top': top+'px'
            });
            $("#basket_thing").css({
                'position': 'absolute',
                left: ($(window).width()-32+$(window).scrollLeft())+'px',
                top: $(window).scrollTop()+105+'px'
            });

            $("#basket_thing").stop().animate({
                opacity: 1, // BasketThing.calcOpacity(true),
                left: ($(window).width()-32+$(window).scrollLeft())+'px',
                top: $(window).scrollTop()+105+'px'
            });
        }
    },

    init: function() {
        this.basketURL = $(".basket_info a").attr('href');
        $("#basket_thing").css({
            display: 'block',
            opacity: 1
        });

        this.setDefaultContent();
        $(window).scroll(this.realign).resize(this.realign);
        $("#basket_thing_wrapper").hover(
            function() {
                BasketThing.hold = true;
            },
            function() {
                BasketThing.hold = false;
                BasketThing.hideTheThing();
            });
        $("#basket_thing").hover(
            BasketThing.showTheThing,
            null);
        this.realign();
    },

    flashMessage: function(html) {
        var dest = $("#basket_thing_content");
        dest.html(html);
        dest = $("#basket_thing_wrapper");
        //dest.css('display', 'block')
        BasketThing.showTheThing();
        dest.animate({
            opacity: 0.8
        }, 2500).animate({
            opacity: 0.01
        }, 500, function() {
            BasketThing.setDefaultContent();
            BasketThing.showTheThing();
            setTimeout("BasketThing.hideTheThing()", 3000);
        });
    },

    setDefaultContent: function() {
        var dest = $("#basket_thing_wrapper");
        dest = dest.find("#basket_thing_content");
        if (!$.gt.messages[$.gt.lang]) {
            // translations not ready
            setTimeout("BasketThing.setDefaultContent()", 200);
            return;
        }
        dest.html("<h1>"+_("Basket preview")+": "+this.getBasketCount()+" "
                  + $.gt.ngettext("product", "products", this.getBasketCount())
                  +"</h1>"+
                  _("Total value")+": <span id='basket_thing_value'>["+_("loading")+"...]</span><br />"+
                  //"<a class='morelink' href='#'>"+_("show products")+" &raquo;</a><br />"+
                  "<div id='basket_thing_more' style='display: block'></div>"+
                  "<br />"+
                  "<a href='"+this.basketURL+"'>"+_("Go to checkout")+"</a>.");

        dest.find('a.morelink').click(function() {
            $(this).html(
                (BasketThing.toggleProducts()) ? "&laquo;"+_('hide products') : _('show products')+' &raquo;');
            $('#basket_thing_more').slideToggle(300);
            return false;
        });
        var data = {
            'class': 'Basket',
            'method': 'getTotal',
            'format': 't'
        };
        $.getJSON('/_json/', data, function(json) {
            /* var cost = parseFloat(json['value'].replace(',', '.').replace(/[^,.0-9]/g, ''));
            var currencies = json['value'].replace(/[\s.,]/gi, '|').split('|');
            $("#basket_thing_value").html(currencies[0]+' '+parseInt(cost/100)+'<sup>'+((cost%100 < 10)?'0':'')+(cost%100)+'</sup> '+currencies[1]); */
            $("#basket_thing_value").html(json['value']);
            $("#basket_thing_more").html(json['more']);
        });
    },

    toggleProducts: function() {
        return !(this.productsVisible = !this.productsVisible);
    },

    calcOpacity: function(considerWrapper) {
        if (considerWrapper && ($('#basket_thing_wrapper').css('opacity') > 0)) return 0;
        return 1; // Math.min(1, ($(window).scrollTop()/59));
    },

    showTheThing: function() {
        $("#basket_thing_wrapper").stop();
        $("#basket_thing").stop().animate({
            opacity: 0
        }, 500);
        $("#basket_thing_wrapper").css({
            display: 'block'
        }).animate({
            opacity: 1
        }, 500);
    },

    hideTheThing: function() {
        if (this.hold === true) return;
        var btc = $("#basket_thing_wrapper");
        btc.stop();
        $("#basket_thing").stop();
        btc.animate({
            opacity: 0.00
        }, 500, function() {
            btc.css('display', 'none')
        });
        $("#basket_thing").animate({
            opacity: BasketThing.calcOpacity()
        }, 1000);
    }
};

