/**
 * © 2005 Propel Development, LLC - All Rights Reserved.
 * 
 * www.propeldev.com
 * 
 * Contact us for licensing info.
 * 
 */

N = (document.all) ? 0 : 1;
var ob = false;
var setNotRan=true;

var droppingArea;
var currentYposition;
var productSKU;
var checkboxID;
var mouseHasMoved = false;

var cart_open_height = 86;
var cart_open_height_IE_Win = -5;
var cart_open_margin_IE_Win = '0 0 85px 0';

var cart_closed_height = 25;
var cart_closed_height_IE_Win = -66;
var cart_closed_margin_IE_Win = '0 0 24px 0';	//on windows ie, this is so the scrollbar will position properly

var is_IE_Mac = false;
var is_IE_Win = false;

if(navigator.appName.indexOf("Microsoft") != -1 && navigator.platform.indexOf("Mac") != -1)
	is_IE_Mac = true;

if(navigator.appName.indexOf("Microsoft") != -1 && !is_IE_Mac)
	is_IE_Win = true;


function MD(sku, currentCheckboxID, iconType)
{
	
	mouseHasMoved = false;
	productSKU = sku;
	checkboxID = currentCheckboxID;
	if(iconType == '') // just to make sure there is an icon
		document.s.src = 'images_icons/icon_font.gif';
	else
		document.s.src = 'images_icons/icon_'+iconType+'.gif';
	
	if (N)
	{
		ob = document.getElementById('s').style;
		return false;
	}
	else
	{
		ob = document.s.parentElement.style;		
		X=event.offsetX;
		Y=event.offsetY;
		setNotRan=true;
  }
  
  return false;
  
}
function MM(e)
{
	ob.visibility='visible'; //the layer is now visible
	
	if (ob)
	{
		mouseHasMoved = true;
		mouseXpositionOffset = 50; //these values will center the mouse on the image
		mouseYpositionOffset = 100;
	
		if (N)
		{
			ob.left = (e.pageX) - mouseXpositionOffset;
			ob.top = (e.pageY) - mouseYpositionOffset;
			droppingArea = document.getElementById("cart").offsetTop + document.body.scrollTop;
			currentYposition = e.pageY;
		} else {
			ob.pixelLeft = event.clientX - mouseXpositionOffset;
			ob.pixelTop = event.clientY - mouseYpositionOffset + document.body.scrollTop;
			droppingArea = document.getElementById("cart").offsetTop;	
			currentYposition = event.clientY;
		}
			  
		if(currentYposition > droppingArea)
		{
			document.getElementById('cartDisplayArea').style.backgroundColor='#CCCCCC';
			cart_open_preference();  //make sure the cart is open
		} else {
			document.getElementById('cartDisplayArea').style.backgroundColor='#FFFFFF';
			cart_restore_preference();
		}
		
		return false;
	  
	}
}


function MU()
{
	if(mouseHasMoved)
	{
		if(N)
		{
			document.getElementById('cartDisplayArea').style.backgroundColor='#FFFFFF';
			ob.left=10;
			ob.top=10;
			ob.visibility='hidden';
			ob = null;
			ob = false;
		}
		else
		{
			document.getElementById('cartDisplayArea').style.backgroundColor='#FFFFFF';
			ob.pixelLeft=10;
			ob.pixelTop=10;
			//setTimeout("remove_visibility()", 500);
			ob.visibility='hidden';
			ob = null;
			ob = false;
		}
		
		cart_restore_preference();
		
		if(currentYposition > droppingArea)
		{			
			d=document;
		
			//verify checkbox exist
			if(d.getElementById('productCheckbox'+checkboxID)!=null)
			{
				x=d.getElementById('productCheckbox'+checkboxID);
				x.checked = true;
			}			
			add_to_cart(productSKU);
		}
	}
	else
	{
		ob = null;
		ob = false;
	}

	mouseHasMoved = false;
	
	document.s.src = 'images_icons/spacer.gif';

//	return false;

}

if (N)
{
	document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
}
document.onmousemove = MM;
document.onmouseup = MU;


function add_to_cart(currentSKU)
{
	document.cartDrop.src = 'addtocart/?sku='+escape(currentSKU)+'&rid='+Math.floor((Math.random()*1000001))+'baseblank=0';
	if(document.getElementById("addtionalProductsMap").innerHTML == '') //to fix a safari issue that if you added 2 items quickly, it would not write properly
		document.getElementById("addtionalProductsMap").innerHTML = '<map name="cartDropMap"><area shape="rect" coords="448,6,675,52" href="searchprod.php?showrelated=1" class="cursorLink"><area shape="rect" coords="680,30,757,52" href="viewcart.php" class="cursorLink"></map>';
}

function remove_from_cart(currentSKU)
{
	document.cartDrop.src = 'removefromcart/?sku='+escape(currentSKU)+'&rid='+Math.floor((Math.random()*1000001))+'baseblank=0';
	document.getElementById("addtionalProductsMap").innerHTML = '';
}

function checkbox_event(currentSKU, currentCheckboxID, updateCartTotals)
{
	d=document;
	x=d.getElementById('productCheckbox'+currentCheckboxID);
	if(x.checked)
	{
		add_to_cart(currentSKU);
	}
	else
	{
		remove_from_cart(currentSKU);
	}
	if(updateCartTotals == 5) //this is only passe in viewcart in the checkbox
	{
		document.cart_totals.src = 'viewcarttotals/?rid='+Math.floor((Math.random()*1000001));
	}
}

function toggle_cart()
{

	if(getCookie('cartPref')==0 || getCookie('cartPref')==null)
	{
		if(is_IE_Win)
		{
			document.getElementById("cart").style.bottom = cart_closed_height_IE_Win;
			document.body.style.margin = cart_closed_margin_IE_Win;
		}
		else
		{
			document.getElementById("cart").style.height = cart_closed_height;
		}
		document.cartStatus.src = 'images/cart_open.gif';
		setCookie('cartPref', 1);
	}
	else
	{
		if(is_IE_Win)
		{
			document.getElementById("cart").style.bottom = cart_open_height_IE_Win;
			document.body.style.margin = cart_open_margin_IE_Win;
		}
		else
		{
			document.getElementById("cart").style.height = cart_open_height;
		}
		document.cartStatus.src = 'images/cart_close.gif';
		setCookie('cartPref', 0);
	}

	//return true;
}

function cart_open_preference()
{
	if(is_IE_Win)
	{
		document.getElementById("cart").style.bottom = cart_open_height_IE_Win;
		document.body.style.margin = cart_open_margin_IE_Win;
	}
	else
	{
		document.getElementById("cart").style.height = cart_open_height;
	}
}

function cart_restore_preference()
{
	
	var current_cart_pref = '';
	
	if(getCookie('cartPref') == null)
	{
		current_cart_pref = 1; //the default is currently set to open;
		setCookie('cartPref', current_cart_pref);
		document.cartStatus.src = 'images/cart_open.gif';
	}
	else
	{
		current_cart_pref = getCookie('cartPref');
	}
	
	if(current_cart_pref==1)
	{
		if(is_IE_Win)
		{
			document.getElementById("cart").style.bottom = cart_closed_height_IE_Win;
			document.body.style.margin = cart_closed_margin_IE_Win;
		}
		else
		{
			document.getElementById("cart").style.height = cart_closed_height;
		}
		
	}
	else
	{
		if(is_IE_Win)
		{
			document.getElementById("cart").style.bottom = cart_open_height_IE_Win;
			document.body.style.margin = cart_open_margin_IE_Win;
		}
		else
		{
			document.getElementById("cart").style.height = cart_open_height;
		}
	}
}

function custom_cd_selection() 
{
	var custom_cd = '';
	
	if(document.getElementById('custom_cd').checked)
		custom_cd = 1;
	document.cart_totals.src = 'additionalitems/?cop='+custom_cd+'&rid='+Math.floor((Math.random()*1000001));
	
	setTimeout("dragndrop_refresh()", 500);
}

function additional_items_selection()
{
	var custom_cd = '';
	var additional_item = '';
		
	if(document.getElementById('custom_cd').checked)
		custom_cd = 1;
	
	if(document.getElementById('additional_item')!=null)
	{
		if(document.getElementById('additional_item').checked)
			additional_item = 1;
	}
		
	document.cart_totals.src = 'additionalitems/?cop='+custom_cd+'&add1='+additional_item+'&rid='+Math.floor((Math.random()*1000001));
	
	setTimeout("dragndrop_refresh()", 500);
}

function dragndrop_refresh()
{
	document.cartDrop.src = 'dragndropcart/?'+'&rid='+Math.floor((Math.random()*1000001));
}
