//global variables
var gDivOperatorNotes = "divOperatorNotes";
var gDivProductEssentials = "divProductEssentials";
var gProductWarrantyTabDiv = "ProductWarrantyTabDiv";
var gProductComponentsTabDiv = "ProductComponentsTabDiv";
var gProductSpecialsTabDiv = "ProductSpecialsTabDiv";
var gProductSpecificationsTabDiv = "ProductSpecificationsTabDiv";
var gProductHighlightsTabDiv = "ProductHighlightsTabDiv";
var gProductAccessoriesTabDiv = "ProductAccessoriesTabDiv";
var gProductDiv = "MiniProductDiv";
var gProductDisplayDiv = "miniproductdisplay";
var gPurchaseButtonDiv = "MiniCartPurchaseButtonDiv";
var gCloseButtonDiv = "MiniCartCloseButtonDiv";
var gCartDiv = "MiniCartDiv";
var gProductAdded = 0;
var gTimeoutId = null;
var gFullCartDiv = "FullCartDiv";
var gHiddenDiv = "divPopup";
//var gHiddenPriceDiv = "divPricePopup";
var gUrlPrefix = "/";
var gDivAdminMessages = "AdminMessagesDiv";
var gCategoryHiddenContent = "categoryHiddenContent";
var dragObj = new Object();
dragObj.zIndex = 0;

var pricetreeTabs;
var productDetailTabs;
var nTabIndex;

var productsToCompare = new Array();

function onCompareRelatedProducts(mainPID)
{
    var url = gUrlPrefix+'ProductCompare.aspx?prodList='+mainPID;
    
    if(productsToCompare.length == 0)
        return;
    
    for(var i=0; i<productsToCompare.length; i++){
       
       url += '%2c'+productsToCompare[i];
    }    
    
    window.location = url;
    
}

function compareClick( box )
{
   if(box != null)
   {
       if(box.checked)
       {
            productsToCompare.push(box.value);
            
            if(productsToCompare.length > 2)
            {
                var c = $$('input.cbCompare');
                
                for(var i=0; i<c.length; i++){
                   var cb = c[i];
                   
                   if(!cb.checked)
                   {
		                cb.disabled = true;
		            }
	            }
	        }
       }
       else
       {
            for (var index = 0; index < productsToCompare.length; ++index) {
                  var item = productsToCompare[index];
                  
                  if(item == box.value)
                  {
                        productsToCompare.splice(index, 1);
                  }
                  
                }
                
            if(productsToCompare.length < 3)
            {
                var c = $$('input.cbCompare');
                
                for(var i=0; i<c.length; i++){
                   var cb = c[i];
                   
                   if(!cb.checked)
                   {
		                cb.disabled = false;
		            }
	            }
	        }

       }
   }
}

function productPageInit(pid) {
	pricetreeTabs = new YAHOO.widget.TabView("pricetree");
	productDetailTabs = new YAHOO.widget.TabView("productDetailTabs");

	LoadAccessoryTab(pid);
	LoadProductDetailMerch(pid);
	LoadWarrantyTab(pid);
	LoadComponentsTab(pid);
	LoadEssentials(pid);
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function OpenEmailPriceTab()
{	
	if( nTabIndex != 3)
		return;
	
	var tabs = pricetreeTabs.get('tabs');
	if(tabs)
	{
		var theTab = tabs[tabs.length-1];
		if(theTab)
			pricetreeTabs.set('activeIndex', pricetreeTabs.getTabIndex(theTab));
	}
}
function OpenMyPriceTab()
{	
	if( nTabIndex != 2)
		return;
	
	var tabs = pricetreeTabs.get('tabs');
	if(tabs)
	{
		var theTab = tabs[tabs.length-1];
		if(theTab)
			pricetreeTabs.set('activeIndex', pricetreeTabs.getTabIndex(theTab));
	}
}

function RemoveDetailTabByName(tabNameToRemove)
{
	var tabs = productDetailTabs.get('tabs');
	if(tabs)
	{
		var theTab;
		for (var t=0;t<tabs.length;t++)
		{
				theTab = tabs[t];
				var label = theTab.get('label');
				if(label == tabNameToRemove)
				break;
			
		}
		
		if(theTab)
			productDetailTabs.removeTab(theTab);
	}
}

function SelectAccTab(categoryName)
{
	ChangeDetailTabs(2);
	
	if(categoryName)
		location.hash = categoryName;
}

function SelectAccessoryTab(hashLocation)
{
    ChangeDetailTabs(2);
	if(hashLocation != null)
	    location.hash = hashLocation;
}

function SelectReviewsTab()
{
	ChangeDetailTabs(1);
	location.hash = 'tabDetails';
}

function ChangeDetailTabs(nDetailIndex)
{
	productDetailTabs.set('activeIndex', nDetailIndex);
}

function ChangeTabs()
{
	if(nTabIndex)
	{
		pricetreeTabs.set('activeIndex', nTabIndex);
    }
}

//var checkList1 = "content1";
//var checkList2 = "content3";

function AddToCart(pid)
{
	//gProductAdded = pid;
	var params = 'pid='+pid+'&time='+new Date();
	//var params2 = 'pid='+pid+'&time='+new Date();
	//var queryString = window.location.search;
	/*
	if(queryString.length > 0)
	{
		params = queryString + '&' + params;
		params2 = queryString + '&' + params2;
	}
	*/

	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/MiniCart/MiniAddToCart.aspx',
		{
			method: 'get',
			parameters: params,
			asynchronous: false
		});
		//onComplete: LoadMiniProduct
		
		
		setTimeout(function()
		{
			window.location.href = gUrlPrefix+"Cart.aspx";
		}, 0);
	
	
	//Omniture("scAdd", pid);
	/*
	GetMiniCart();
	
	var myAjax = new Ajax.Request(
		'Controls/MiniCart/MiniProductLoader.aspx',
		{
			method: 'get',
			parameters: params2,
			onComplete: LoadMiniProduct
		});
	gTimeoutId = setTimeout("CloseProductDisplay()",6000);
	//console.debug("gTimeoutId:AddToCart",gTimeoutId);
	var productDiv = $(gProductDisplayDiv);
	productDiv.onmouseover = function () {clearTimeout(gTimeoutId);};
	productDiv.onmouseout = function () {gTimeoutId = setTimeout("CloseProductDisplay()",2000);};
	*/
}


/*function ExpandCheckList()
{
	var params = '';
	
	var myAjax = new Ajax.Request(
		'Controls/ProductCheckList/CheckListFull.aspx',	
		{
			method: 'get',
			parameters: params,
			asynchronous: false,
			onComplete: LoadFullCheckList
		});
		//onComplete: LoadFullCheckList
}

function LoadFullCheckList(originalRequest)
{
	var content1 = $(checkList1);
	var content2 = $(checkList2);
	
	Effect.Fade(content1);
	Effect.Appear(content2);
	
	if(content2 != null)
		content2.focus();
}*/

function AddItemToCart(productToAdd)
{

	var params = 'pid='+productToAdd+'&time='+new Date();
	
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/MiniCart/MiniAddToCart.aspx',
		{
			method: 'get',
			parameters: params,
			asynchronous: false
		});
		//onComplete: LoadMiniProduct

	//Omniture(omnievent, productToAdd);
	
	setTimeout(function()
		{
			window.location.href = gUrlPrefix+"Cart.aspx";
		}, 0);

}
function RemoveItemFromCart(productToRemove, omnievent)
{
	var params = 'pid='+productToRemove+'&time='+new Date();

		var myAjax = new Ajax.Request(
			gUrlPrefix+'Controls/MiniCart/MiniRemoveFromCart.aspx',
			{
				method: 'get',
				parameters: params,
				asynchronous: false
			});
		//Omniture(omnievent, productToRemove);
}
// omnievent  "scAdd", or "scOpen,scAdd", etc.
function AddToCart2(pid, omnievent)
{
	AddItemToCart(pid, omnievent);
	if(window.event)
	{
		window.event.returnValue = false;
	}
	setTimeout(function()
		{
			window.location.href = gUrlPrefix+"Cart.aspx";
		}, 0);
	/*
	gProductAdded = pid;
	var params2 = 'pid='+pid+'&time='+new Date();
	var queryString = window.location.search;
	
	if(queryString.length > 0)
	{
		//params = queryString + '&' + params;
		params2 = queryString + '&' + params2;
	}
	
	AddItemToCart(pid, omnievent);
	
	GetMiniCart();
	
	var myAjax = new Ajax.Request(
		'Controls/MiniCart/MiniProductLoader.aspx',
		{
			method: 'get',
			parameters: params2,
			onComplete: LoadMiniProduct
		});
	gTimeoutId = setTimeout("CloseProductDisplay()",6000);
	//console.debug("gTimeoutId:AddToCart",gTimeoutId);
	var productDiv = $(gProductDisplayDiv);
	productDiv.onmouseover = function () {clearTimeout(gTimeoutId);};
	productDiv.onmouseout = function () {gTimeoutId = setTimeout("CloseProductDisplay()",2000);};
	*/
}

function AddToCartInternal(pid)
{
	gProductAdded = pid;
	var params = 'pid='+pid+'&time='+new Date();
	var params2 = 'pid='+pid+'&time='+new Date();
	var queryString = window.location.search;
	
	if(queryString.length > 0)
	{
		params = queryString + '&' + params;
		params2 = queryString + '&' + params2;
	}
	

	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/MiniCart/MiniAddToCart.aspx',
		{
			method: 'get',
			parameters: params,
			asynchronous: false
		});
		//onComplete: LoadMiniProduct

	//Omniture("scAdd", pid);
	GetMiniCart();
	
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/MiniCart/MiniProductLoader.aspx',
		{
			method: 'get',
			parameters: params2,
			onComplete: LoadMiniProduct
		});
	gTimeoutId = setTimeout("CloseProductDisplay()",6000);
	//console.debug("gTimeoutId:AddToCart",gTimeoutId);
	var productDiv = $(gProductDisplayDiv);
	productDiv.onmouseover = function () {clearTimeout(gTimeoutId);};
	productDiv.onmouseout = function () {gTimeoutId = setTimeout("CloseProductDisplay()",2000);};
	
}


function LoadMiniProduct(originalRequest)
{ 
	try
	{
		//GetMiniCart();
		var productDiv = $(gProductDiv);
		
		if( productDiv ) 
		{
			try{ window.scrollTo(window.screenX,0); }catch(e){};
			
			productDiv.innerHTML = originalRequest.responseText;
			
			//hide purchase button
			//ToggleButtons(true);
			
			Effect.SlideDown(gProductDisplayDiv);
		}
		else
		{
			//console.error("no product div :-(");
		}
		
	}
	catch(e)
	{
		//console.error("LoadMiniCart", e);
	}
}

function ToggleButtons(hidden)
{
	var purchaseDiv = $(gPurchaseButtonDiv);
	//var closeDiv = $(gCloseButtonDiv);
	if(hidden)
	{
		purchaseDiv.style.display = "none";
		//closeDiv.style.display = "inline";
	}
	else
	{
		purchaseDiv.style.display = "inline";
		//closeDiv.style.display = "none";
	}
}

function CloseProductDisplay()
{
	var productDiv = $(gProductDisplayDiv);
	productDiv.onmouseover = null;
	productDiv.onmouseout = null;
	clearTimeout(gTimeoutId);
	Effect.SlideUp(gProductDisplayDiv);
	//ToggleButtons(false);
}
function ToggleFullCartSheen(loading)
{
	var full = $('FullCartDiv');
	var fullSheen = $('FullCartDivSheenWrapper');
	if(full && fullSheen)
	{
		if(loading)
		{
			
			fullSheen.style.height = full.offsetHeight;
			//fullSheen.style.display = "block";
			fullSheen.setStyle({display: 'block'});
			Element.setOpacity(gFullCartDiv, 0.3);
			//new Effect.Opacity(gFullCartDiv, {from:1.0, to:0.3});
			
		}
		else
		{
			//fullSheen.style.display = "none";
			fullSheen.setStyle({display: 'none'});
			Element.setOpacity(gFullCartDiv, 1.0);
			//new Effect.Opacity(gFullCartDiv, {from:0.3, to:1.0});
		}
	}
}
// Function only called in by precart calls, so must use the multi events
function FullAddItemToCart(productToAdd)
{
	try
	{
		ToggleFullCartSheen(true);
		AddItemToCart(productToAdd, 'scAdd,event5');
		setTimeout("GetFullCart(gFullCartDiv)", 10);
		//GetFullCart(gFullCartDiv);
	}
	catch(e)
	{
	}
}

function FullRemoveItemFromCart(productToRemove)
{
	try
	{
		ToggleFullCartSheen(true);
		RemoveItemFromCart(productToRemove, 'scRemove')
		setTimeout("GetFullCart(gFullCartDiv)", 10);
		//GetFullCart(gFullCartDiv);
	}
	catch(e)
	{
	}
	
}

function RemoveLastFromCart()
{
	try
	{
		RemoveItemFromCart(gProductAdded);
		GetMiniCart();
		CloseProductDisplay();
	}
	catch(e)
	{
		//console.debug("RemoveLastFromCart",e);
	}
}

function GetMiniCart()
{
	var params = 'control=MiniCartDisplay&time='+new Date();
	var url = gUrlPrefix+'Controls/MiniCart/MiniCartLoader.aspx';
	var myAjax = new Ajax.Updater(
		gCartDiv,
		url,
		{
			method: 'get',
			parameters: params
		});	
}
function GetFullCart(fullCartDiv)
{
	var params = 'control=FullCartDisplay&time='+new Date();
	var url = gUrlPrefix+'Controls/MiniCart/MiniCartLoader.aspx';
	var myAjax = new Ajax.Updater(
		fullCartDiv,
		url,
		{
			method: 'get',
			parameters: params,
			asynchronous: false,
			onComplete: function(transport) { ToggleFullCartSheen(false);}
		});	
	//setTimeout("ToggleFullCartSheen(false)", 4000);
}

function LoadProductDetailMerch(pid)
{
	var params = 'pid='+pid+'&control=productdetailmerch&time='+new Date();
	
		
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/Products/TabLoader.aspx',
		{
			method: 'get',
			parameters: params,
			onComplete: LoadProductDetailMerchDiv
		});
}

function LoadAccessoryTab(pid)
{
	var currentproduct = pid;
	var params = 'pid='+pid+'&control=accessories&time='+new Date();
	/*var params2 = 'pid='+pid+'&time='+new Date();
	var queryString = window.location.search;
	
	if(queryString.length > 0)
	{
		params = queryString + '&' + params;
		params2 = queryString + '&' + params2;
	}
	* */
		
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/Products/TabLoader.aspx',
		{
			method: 'get',
			parameters: params,
			onComplete: LoadProductAccessoriesTab
		});
}

function LoadAccessoryTabWithCategory(pid,cat)
{
	var currentproduct = pid;
	var params = 'pid='+pid+'&control=accessories&cat='+cat+'&time='+new Date();
	/*var params2 = 'pid='+pid+'&time='+new Date();
	var queryString = window.location.search;
	
	if(queryString.length > 0)
	{
		params = queryString + '&' + params;
		params2 = queryString + '&' + params2;
	}
	* */
		
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/Products/TabLoader.aspx',
		{
			method: 'get',
			parameters: params,
			onComplete: LoadProductAccessoriesTab
		});
}

function LoadProductDetailMerchDiv(originalRequest)
{
	try
	{
		var productDetailMerchDiv = $(gCategoryHiddenContent);
		
		if( productDetailMerchDiv ) 
		{
			if(originalRequest.responseText.length > 0)
			{
				productDetailMerchDiv.innerHTML = originalRequest.responseText;
			}
			
			
		}		
	}
	catch(e){}
}

function LoadProductAccessoriesTab(originalRequest)
{
	try
	{
		var productAccessoriesTabDiv = $(gProductAccessoriesTabDiv);
		
		if( productAccessoriesTabDiv ) 
		{
			if(originalRequest.responseText.length > 0)
			{
				productAccessoriesTabDiv.innerHTML = originalRequest.responseText;
				//var imgTab = $("tabAccessories");
					//imgTab.style.display = "inline";
				//var detailtab3 = $('detailtab3');
				//productDetailTabs.addTab( new YAHOO.widget.Tab({ label: 'Accessories', content: detailtab3.innerHTML }), 2 );
			}
			else
			{
				RemoveDetailTabByName('Accessories');
			}
			
		}		
	}
	catch(e){}
}

function LoadHighlightsTab(pid)
{
	var currentproduct = pid;
	var params = 'pid='+pid+'&control=highlights&time='+new Date();
		
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/Products/TabLoader.aspx',
		{
			method: 'get',
			parameters: params,
			onComplete: LoadProductHighlightsTab
		});
}

function LoadProductHighlightsTab(originalRequest)
{
	try
	{
		var productHighlightsTabDiv = $(gProductHighlightsTabDiv);
		
		if( productHighlightsTabDiv ) 
		{
			if(originalRequest.responseText.length > 0)
			{
				productHighlightsTabDiv.innerHTML = originalRequest.responseText;
				//var imgTab = $("tabHighlights");
					//imgTab.style.display = "inline";
			}
			
		}		
	}
	catch(e){}
}

function LoadSpecificationsTab(pid)
{
	var currentproduct = pid;
	var params = 'pid='+pid+'&control=specifications&time='+new Date();
		
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/Products/TabLoader.aspx',
		{
			method: 'get',
			parameters: params,
			onComplete: LoadProductSpecificationsTab
		});
}

function LoadProductSpecificationsTab(originalRequest)
{
	try
	{
		var productSpecificationsTabDiv = $(gProductSpecificationsTabDiv);
		
		if( productSpecificationsTabDiv ) 
		{
			if(originalRequest.responseText.length > 0)
			{
				productSpecificationsTabDiv.innerHTML = originalRequest.responseText;
				
			}
			
		}		
	}
	catch(e){}
}

function LoadSpecialsTab(pid)
{
	var currentproduct = pid;
	var params = 'pid='+pid+'&control=specials&time='+new Date();
		
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/Products/TabLoader.aspx',
		{
			method: 'get',
			parameters: params,
			onComplete: LoadProductSpecialsTab
		});
}

function LoadProductSpecialsTab(originalRequest)
{
	try
	{
		var productSpecialsTabDiv = $(gProductSpecialsTabDiv);
		
		if( productSpecialsTabDiv ) 
		{
			if(originalRequest.responseText.length > 0)
			{
				productSpecialsTabDiv.innerHTML = originalRequest.responseText;
				
			}
			
		}		
	}
	catch(e){}
}

function LoadWarrantyTab(pid)
{
	var currentproduct = pid;
	var params = 'pid='+pid+'&control=warranty&time='+new Date();
		
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/Products/TabLoader.aspx',
		{
			method: 'get',
			parameters: params,
			onComplete: LoadProductWarrantyTab
		});
}

function LoadComponentsTab(pid)
{
	var currentproduct = pid;
	var params = 'pid='+pid+'&control=components&time='+new Date();
		
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/Products/TabLoader.aspx',
		{
			method: 'get',
			parameters: params,
			onComplete: LoadProductComponentsTab
		});
}


function LoadProductWarrantyTab(originalRequest)
{
	try
	{
		var productWarrantyTabDiv = $(gProductWarrantyTabDiv);
		
		if( productWarrantyTabDiv ) 
		{
			if(originalRequest.responseText.length > 0)
			{
				productWarrantyTabDiv.innerHTML = originalRequest.responseText;
				//var detailtab5 = $('detailtab5');
				//productDetailTabs.addTab( new YAHOO.widget.Tab({ label: 'Warranty', content: detailtab5.innerHTML }), 4 );
			}
			else
			{
				RemoveDetailTabByName('Warranty');
			}
			
		}		
	}
	catch(e){}
}

function LoadProductComponentsTab(originalRequest)
{
	try
	{
		var productComponentsTabDiv = $(gProductComponentsTabDiv);
		
		if( productComponentsTabDiv ) 
		{
			if(originalRequest.responseText.length > 0)
			{
				productComponentsTabDiv.innerHTML = originalRequest.responseText;
				//var detailtab6 = $('detailtab6');
				//productDetailTabs.addTab( new YAHOO.widget.Tab({ label: 'Packages', content: detailtab6.innerHTML }), 5 );
			}
			else
			{
				RemoveDetailTabByName('Packages');
			}
			
		}		
	}
	catch(e){}
}


function LoadEssentials(pid)
{
	var currentproduct = pid;
	var params = 'pid='+pid+'&control=essentials&time='+new Date();
		
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/Products/TabLoader.aspx',
		{
			method: 'get',
			parameters: params,
			onComplete: LoadProductEssentials
		});
}

function LoadProductEssentials(originalRequest)
{
	try
	{
		var productEssentialsDiv = $(gDivProductEssentials);
		
		if( productEssentialsDiv ) 
		{
			if(originalRequest.responseText.length > 0)
			{
				productEssentialsDiv.innerHTML = originalRequest.responseText;
			}
			else
			{
				try
				{
					//var imgTab = $("tabWarranty");
					//imgTab.style.display = "none";
				}
				catch (ex){}
			}	
		}		
	}
	catch(e){}
}
/*
function LoadMessages()
{
	var params = '&control=adminMessages&time='+new Date();
	var url = gUrlPrefix+'Controls/Internal/InternalLoader.aspx';
		
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: params,
			onComplete: LoadAdminMessages
		});
}

function LoadAdminMessages(originalRequest)
{
	try
	{
		var adminMessagesDiv = $(gDivAdminMessages);
		
		if( adminMessagesDiv ) 
		{
			if(originalRequest.responseText.length > 2)
			{
				adminMessagesDiv.innerHTML = originalRequest.responseText;
				adminMessagesDiv.style.display = "block";
			}
		}		
	}
	catch(e){}
}
*/
function LoadOperatorView(pid)
{
	var currentproduct = pid;
	var params = 'pid='+pid+'&control=operator&time='+new Date();
		
	var myAjax = new Ajax.Request(
		gUrlPrefix+'Controls/Products/TabLoader.aspx',
		{
			method: 'get',
			parameters: params,
			onComplete: LoadProductOperatorView
		});
}

function LoadProductOperatorView(originalRequest)
{
	try
	{
		var operatorNotesDiv = $(gDivOperatorNotes);
		
		if( operatorNotesDiv ) 
		{
			if(originalRequest.responseText.length > 0)
			{
				operatorNotesDiv.innerHTML = originalRequest.responseText;
			}
			else
			{
				try
				{
					//var imgTab = $("tabWarranty");
					//imgTab.style.display = "none";
				}
				catch (ex){}
			}	
		}		
	}
	catch(e){}
}

sfHover = function() 
{
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
