/*---------------------------------------------------------------- 
  Copyright:
  (C) 2008 - 2010 IceTheme
  
  License:
  GNU/GPL http://www.gnu.org/copyleft/gpl.html
  
  Author:
  IceTheme - http://wwww.icetheme.com
---------------------------------------------------------------- */


// JavaScript Document
if( typeof(IceSliderCart) !=  'function' ){
	function IceSliderCart( isAutoHide ){	
			var slider = new Fx.Styles( $('cart-panel') , {duration:500,transition:Fx.Transitions.Sine.easeInOut,onComplete:function(){ slider=null; } });										
			var maxHeight =	parseInt($('ice_cart').getElement('.ice-content').getCoordinates().height);																
			if( $('cart-panel').className.indexOf('ice-hide') !=-1 ){					
				slider.stop().start( {'height':[0,maxHeight],'opacity':[0,1]} );
				// make_slideToggle(document.getElementById('cart-panel'));
				$('cart-panel').removeClass('ice-hide').addClass('ice-active');
				if( isAutoHide == true ) {
					(function(){ 
						 new Fx.Styles( $('cart-panel') , {duration:500,transition:Fx.Transitions.Sine.easeInOut,onComplete:function(){ slider=null; } }).stop().start( {'height':[maxHeight,0],opacity:[1,0]} );
						$('cart-panel').removeClass('ice-active').addClass('ice-hide');		   
					} ).delay(3000);
				}
			} else if( $('cart-panel').offsetHeight.toInt() <= maxHeight ){
				slider.stop().start( {'height':[maxHeight,0],opacity:[1,0]} );
				$('cart-panel').removeClass('ice-active').addClass('ice-hide');					
			}
			
	}	
}
$(window).addEvent('load', function(){
	var el = $("cart-panel");
	var cartvalue = $("ice_cart").getCoordinates();
	$("ice_cart").inject(document.body,"bottom");
	 $("ice_cart").setStyles({
		left:cartvalue.left,
		top: cartvalue.top+$('cart-button').offsetHeight.toInt()
	});
		 
		 
	$("cart_overlay").inject(document.body,"bottom");
	var el = $("cart-panel");
	el.setStyle('height',0); 
	el.addClass('ice-hide');
	el.setStyle("visibility","visible");
	var maxHeight =	parseInt(el.getElement('.ice-content').getCoordinates().height);
	if( $defined($('cart-button')) ){
		$('cart-button').addEvent('click', function(){
				IceSliderCart();
		} );						
	}
} );
/** 
 * Override the VirtueMart function;
 */								
$(window).addEvent('domready', function(){
	/** 
	 * Override the handleAddToCart function;
	 */								
	handleAddToCart = function(formId, parameters) { 			
		$('cart_overlay').setStyles({
			"height": '100%',
			"visibility": 'visible'
		});
		
		var myElement = $(document.body);
	
		var myFx = new SmoothScroll(myElement,{
			duration: 400,
			transition: Fx.Transitions.Quad.easeInOut,
			delay: 3000
		}).start(0, 0);	
		formCartAdd = $(formId);
		itemsToAdd = formCartAdd.getElement('input[name^=quantity]').value;
		//var vmCart = new Fx.Slide("cart-panel");
		var myElement = document.getElementById('cart-panel');
		var mySlwa = 2;
		new Ajax(formCartAdd.action, {
			method: 'post',
			postBody: $(formId).toQueryString(),
			evalScripts: true,
			onComplete: function(responseText) {
				if(mySlwa == 2){
					mySlwa = 1;
				}
				setTimeout( function(){
					if( $defined($('cart_overlay')) ) {					 
						$('cart_overlay').setStyles({"visibility": 'hidden'});
					}
				},1500);	
				updateMiniCarts();
				$('vm_cart').className = 'vm_cart-full';
			}
		}).request();
	}	
	
	/** 
	 * Override the handleAddToCart function;
	 */	
	updateMiniCarts = function() {
		var cartPanel = $('cart-panel');
		var mika = $('cart-button').getElement('strong'); 
		ex1 = parseInt(mika.innerHTML);
		ex2 = parseInt(itemsToAdd);
		ex3 = ex1+ex2;
	
		new Ajax(live_site + '/index2.php', {
			method: 'post',
			postBody: Object.toQueryString({
				"only_page": "1",
				"page": "shop.basket_short",
				"option": "com_virtuemart"
			}),
			onComplete: function(responseText) {
				carts = $('ice_cart').getElement('.ice-content');
				if( carts ) {
					try {
						carts.innerHTML = responseText;
						mika.innerHTML = ex3;
					} catch(e) {
						// ...
					}
					 $('cart-panel').addClass('ice-hide');
					IceSliderCart(true);
				}
			}
		}).request();
	}
});
