// Mini shopping basket action

Event.observe(document,'dom:loaded',function(event){
	
	var TopShoppingBag=$('header-cart');

	if(!TopShoppingBag)	//if there is no shopping bag then do nothing
		return;


	TopShoppingBag.Show=function() {	//method to make the shopping bag appear with cool animation
		if(this.shown || this.showing)
			return;
		this.showing=true;
		var items=this.select("#header-cart li").length;
		//if(items)
			Effect.BlindDown("AjaxCartView",{
				//queue: {scope:"ShoppingBag", position:"end"},
				delay:0,
				duration:0.5,
				//duration:Math.min(0.25+0.05*items,1.0), //show bigger bags a bit slower, but never take more than 1s
				//afterUpdate: this.iefix?this.iefix.bind(this):null,
				afterFinish:this.afterShow.bind(this)
			});					//show the mini bag
	}

	TopShoppingBag.afterShow=function(){
		this.showing=false;
		this.shown=true;
	}

	TopShoppingBag.Hide=function(delayTime) {	//method to make the shopping bag disappear with cool animation
		if(!this.shown || this.hiding)
			return;
		this.hiding=true;
		var items=this.select("#header-cart li").length;
		//if(items)
			Effect.BlindUp("AjaxCartView",{
				//queue:{scope:"ShoppingBag", position:"end"},
				//delay:delayTime?delayTime:0,
				delay:0.25,
				duration:0.5,
				//duration:Math.min(0.25+0.05*items,1.0), //show bigger bags a bit slower, but never take more than 1s
				//afterUpdate: this.iefix?this.iefix.bind(this):null,
				afterFinish:this.afterHide.bind(this)
			});	//wait 3 seconds, then hide it
	}

	TopShoppingBag.afterHide=function(){
		this.hiding=false;
		this.shown=false;
	}

	

	TopShoppingBag.observe('activate',function(event){	//appear when the user tabs to the mini-bag
		this.Show();
	});
    
    TopShoppingBag.observe('mouseout',function(event){ //hide when the mouse pointer leaves the open shoping bag
        var o=this.cumulativeOffset(),d=this.getDimensions(),X=event.pointerX(),Y=event.pointerY();
        var inside=(X>=o.left && X<(o.left+d.width) && Y>=o.top && Y<=(o.top+d.height));
        if(!inside)    //only fire if the mouse moved out of the container
            this.Hide();
    });
    
	TopShoppingBag.observe('mouseover',function(event){	//appear when the user hovers over the mini-bag
		this.Show();	//show is interlocked so retriggering is not an issue
	});

	TopShoppingBag.observe('click',function(event){	//handler for the close button on the mini-bag
        window.location = "/checkout/cart";
        return false;
	});
    
    // close mini bag on click outside bag
    document.observe('click', function(event) {
        if(TopShoppingBag.visible())
            TopShoppingBag.Hide();
    });

});