var shop_dropdown;    
window.addEvent('domready', function() { 
	shop_dropdown = $('shop_dropdown');
	$('shop').addEvent('mouseover', function(){
	    fadeIn(shop_dropdown);
	});     
	$('wrapper').addEvent('mouseover', function(){
	    fadeOut(shop_dropdown);
	});                           
	$$('.view_cart').each(function(el){
	    el.addEvent('click', function(){
			showShoppingCart();
		});
	});
	$('overlay').addEvent('click', function(){
		hideShoppingCart();
	});
});

function fadeIn (element) {
	if(element.style.opacity != 1)
	{                         
	    element.style.display = "block";
		var myEffect = new Fx.Morph(element, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		myEffect.start({'opacity': [0, 1]});              
	}
}
function fadeOut (element) {
	if(element.style.opacity != 0)
	{                         
	    element.style.display = "none";
		var myEffect = new Fx.Morph(element, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		myEffect.start({'opacity': [1, 0]});              
	}
}                              

function getScrollY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

function showShoppingCart (skip_update) {
	if(! skip_update)
	{
		updateCart();   
	}
	
	var shopping_cart = $('shopping_cart_wrapper');
	var overlay = $('overlay');                   

	var height = getScrollY();
	shopping_cart.style.marginTop = ((-shopping_cart.style.height / 2) + height - 180) + "px";         
	overlay.style.marginTop = ((-overlay.style.height / 2) + height) + "px";
	document.body.style.overflow = "hidden";
		
	shopping_cart.style.display = "block";
	overlay.style.display = "block";
	
	var shopping = new Fx.Morph(shopping_cart, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
	shopping.start({'opacity': [0, 1]});
	
	var overla = new Fx.Morph(overlay, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
	overla.start({'opacity': [0, .4]});
}	
function hideShoppingCart () {                    
	var shopping_cart = $('shopping_cart_wrapper');
	var overlay = $('overlay');
	
	shopping_cart.style.opacity = 0;
	overlay.style.opacity = 0;
	
	shopping_cart.style.display = "none";
	overlay.style.display = "none";	
	document.body.style.overflow = "auto";
}

function addToCart (type, prod_opt_id, size, panel, amount) {
	if(! amount)amount = 1;
	var request = "";
	if(type == "Ring Sling")
	{
		request = "action=addToCart&prod_opt_id="+prod_opt_id+"&size="+size+"&amount="+amount+"&panel="+panel;
	}
	else
	{
		request = "action=addToCart&prod_opt_id="+prod_opt_id+"&size="+size+"&amount="+amount;
	}
	// alert(request);
	var req = new Request({  
	    method: 'post',  
	    url: "ajax_receiver.php",
	    onComplete: function(response) { 
			$('shopping_cart_wrapper').innerHTML = response; 
			updateTotals ();                                                                                              
			showShoppingCart(1);
		}  
    }).send(request);
}                             

function updateCart () {
	var req = new Request({  
	    method: 'post',  
	    url: "ajax_receiver.php",
	    onComplete: function(response) { 
			$('shopping_cart_wrapper').innerHTML = response;
			updateTotals ();
		}  
    }).send("action=displayCart");
}

function updateTotalsCart () {
	$$('.product').each(function(el){
		if(el.remove.checked){
			amount = 0;
		}else{
			amount = el.quantity.value;
		}
		var req = new Request({  
		    method: 'post',  
		    url: "ajax_receiver.php",
		    onComplete: function(response) { 
				$('shopping_cart_wrapper').innerHTML = response;
				updateTotals ();
			}  
	    }).send("action=updateCart&prod_opt_id="+el.id+"&size="+el.size.value+"&amount="+amount);
	});	
}