// JavaScript Document
/*------------------------------------------------------------
description: Show or hide the express button when a vistor
             rolls over a product image
			 
parameter: id - id of the button container to display the
                express button
		   show - true (show button)
		          false (hide the button)
------------------------------------------------------------*/
function showExpressBtn (id,show)
{
	var btnContainer = document.getElementById('quickViewBtn_'+id);
	
	//if the button container doesn't exist
	if(!btnContainer)
		return;

	//show the quick view button
	if(show)
	{
		btnContainer.innerHTML = '<img id="btn_'+id+'" src="/express_shop/images/quick_info.gif" alt="quick info" title="quick info" border="0" style="cursor:pointer;" onmouseover="this.src=\'/express_shop/images/quick_info_over.gif\'" onmousedown="this.src=\'/express_shop/images/quick_info_down.gif\'" onmouseup="this.src=\'/express_shop/images/quick_info_over.gif\'" onclick="showBox(\''+id+'\',\''+btnContainer.title+'\'); setValue(\''+id+'\',\''+btnContainer.title+'\')" />';
	}
	
	//if the show is false
	if(!show)
	{
		btnContainer.innerHTML = '';
	}
	
	return;
}





/*------------------------------------------------------------
description: the mouse event listener that gets attached to
             the whole document. It will determine when to
			 show or hide a quick view button
			 
parameter: mEvent - the event object that contains the 
                    element that threw it.
------------------------------------------------------------*/
var savedSKU = '';
function mouseEventHandler(mEvent)
{
	var id,currentSKU;
	
	//internet explorer
	if(mEvent.srcElement)
	{
		id = mEvent.srcElement.id;
	}
	//Netscape and Firefox
	else if(mEvent.target)
	{
		id = mEvent.target.id;
	}
	
	var prod_image = /^[a-zA-Z0-9]+$/.test(id);
	var quickView_image = /^btn_[a-zA-Z0-9]+$/.test(id);
	var quickView_container = /^quickViewBtn_[a-zA-Z0-9]+$/.test(id);
	
	//retrieve the current sku
	if(prod_image)
		currentSKU=id;
	else if(quickView_image)
	{
		var temp = id.split('_');
		currentSKU = temp[1];
	}
	else if(quickView_container)
	{
		var temp = id.split('_');
		currentSKU = temp[1];
	}
	
	//if the saved sku doesn't match the current sku, hide the button
	if(savedSKU != currentSKU)
		showExpressBtn(savedSKU,false)
	
	//show the quick view if it's a product image or quickView image
	if(prod_image || quickView_image || quickView_container)
	{
		//if rolled over prod_image, show the express button
		if(prod_image)
			showExpressBtn(id,true);

		//save the current sku
		savedSKU = currentSKU;
	}
	
	return;
}





/*------------------------------------------------------------
description: switch the image based on the color and sku
			 
parameter: sku - sku of the item
           color - color to show
------------------------------------------------------------*/
function switchImage (sku,color)
{
	color = color.toLowerCase();
	
	var view = document.getElementById('view');
	var colDrop = document.getElementById('colDrop');
	var size = document.getElementById('size').value;
	
	//if the view exists
	if(view && color != '')
	{
		//change view image
		view.style.backgroundImage = 'url(prod_images/'+sku+'/'+sku+'_'+color+'_medium.jpg)';
		
		//change drop down
		var i,curColor;
		for(i=0; i<colDrop.length; i++)
		{
			//select the drop down
			curColor = colDrop.options[i].value.toLowerCase();
			
			if(curColor == color)
				colDrop.selectedIndex = i;
		}
		
		//set the current sku and color
		setValue(sku,color);
		
		//if the zoom window is open
		if (zoomWin && !zoomWin.closed && zoomWin.location) 
			zoom_image();
	}
	
	getSize(sku,color,size);
	getPrice(sku,color);
	
	return;
}





/*------------------------------------------------------------
description: open the zoom image page
			 
parameter: none
------------------------------------------------------------*/
function getPrice (sku,color)
{
	var container = document.getElementById('priceContainer');

	if(color == '')
		return;
	
	//CREATE THE AJAX OBJECT
	var xmlHttp;
	try
	{  
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}
	catch (e)
	{  
		// Internet Explorer  
		try
		{    
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}
		catch (e)
		{    
			try
			{      
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}
			catch (e)
			{      
				alert("Your browser does not support AJAX!");      
				return false;      
			}    
		}  
	}
	
	//SET UP THE PARAMS
	var params = 'color='+color+'&sku='+sku+'&action=3';
	
	//process ajax returned request
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			container.innerHTML = xmlHttp.responseText;
		}
	}
	
	//posted values to the product_category_process.php
	xmlHttp.open("POST","/express_shop/express_process.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
		
	return;
}





/*------------------------------------------------------------
description: Get the size for the current color
			 
parameter: none
------------------------------------------------------------*/
function getSize (sku,color,size)
{
	var container = document.getElementById('sizeContainer');
	
	//CREATE THE AJAX OBJECT
	var xmlHttp;
	try
	{  
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}
	catch (e)
	{  
		// Internet Explorer  
		try
		{    
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}
		catch (e)
		{    
			try
			{      
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}
			catch (e)
			{      
				alert("Your browser does not support AJAX!");      
				return false;      
			}    
		}  
	}
	
	//SET UP THE PARAMS
	var params = 'color='+color+'&sku='+sku+'&size='+size+'&action=4';
	
	//process ajax returned request
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			container.innerHTML = xmlHttp.responseText;
		}
	}
	
	//posted values to the product_category_process.php
	xmlHttp.open("POST","/express_shop/express_process.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	
	return;
}





/*------------------------------------------------------------
description: open the zoom image page
			 
parameter: none
------------------------------------------------------------*/
var current_sku,current_color,zoomWin;
function zoom_image()
{
	var zoomurl='/zoom_window.php?sku=' + current_sku + '&view=zoom_1&color=' + current_color;
	zoomWin = window.open(zoomurl,'zoom_window','width=550,height=590,resizable,scrollbars=no,menubar=0');
	zoomWin.focus();	
	
	return;
}





/*------------------------------------------------------------
description: sets the current sku and color value
			 
parameter: none
------------------------------------------------------------*/
function setValue (sku,color)
{
	current_sku = sku;
	current_color = color;
	
	return;
}