var nCurrentCount = 0;
var oEventElement, nProductID, oCountLabel, oProductTextbox, oProductCount, oCategoryID;
var sTempString;
oCountLabel = document.getElementById ("compare_count");
oProductTextbox = document.getElementById("txtCompareProductID");
oProductCount = document.getElementById("txtCompareProductCount")
oCategoryID = document.getElementById("txtCategoryID");
if(oProductCount != null) nCurrentCount = parseInt(oProductCount.value,10);
if (oCountLabel != null) oCountLabel.innerText = nCurrentCount;

function increaseCompare ()
{													
	if (oCountLabel != null && oProductCount!= null && oProductTextbox != null)
	{
		// Increament and show current count
		oCountLabel.innerText = ++nCurrentCount;
		oProductCount.value = nCurrentCount;			
		
		// Get the element that invoked this event, change its behaviour to match decreament			
		oEventElement = window.event.srcElement;				
		oEventElement.onclick = decreaseCompare;
		oEventElement.setAttribute ("src", "images/template/compare_remove.gif");
		
		// Get the attribute value that contains the product ID
		// and set it to the form element so it can be submitted			
		nProductID = oEventElement.getAttribute ("productID");
		sTempString = new String(oProductTextbox.value);
		oProductTextbox.value = sTempString.replace("[" + nProductID + "]", "");
		oProductTextbox.value = oProductTextbox.value + "[" + nProductID + "]";
	}			
}

function decreaseCompare()
{
	if (oCountLabel != null && oProductCount!= null && oProductTextbox != null)
	{
		// Decrease and show current count
		oCountLabel.innerText = --nCurrentCount;
		oProductCount.value = nCurrentCount;
		
		// Get the element that invoked this event, change its behaviour to match increament				
		oEventElement = window.event.srcElement;				
		oEventElement.onclick = increaseCompare;
		oEventElement.setAttribute ("src", "images/template/compare.gif");

		// Get the attribute value that contains the product ID
		// and remove it to the form element so it can be submitted			
		nProductID = oEventElement.getAttribute ("productID");
		sTempString = new String(oProductTextbox.value);
		oProductTextbox.value = sTempString.replace("[" + nProductID + "]", "");		
	}		
}		
function loadWindow(url,width, height)
{
	loadWindow(url,width,height,false)
}

function loadWindow(url,width,height,modal)
{
	var oManager = GetRadWindowManager();     
	var oWnd = oManager.GetWindowByName("radWDescription");    
	oWnd.SetUrl(url);
	oWnd.SetSize(width,height);
	oWnd.SetModal(modal);
	oWnd.Show(); 
}

function compareProduct()
{
	if (oProductTextbox != null && oCategoryID != null)
	{
		var sValue = new String(oProductTextbox.value);
		if (sValue.indexOf("][") > -1) 
		{
			loadWindow("productCompare.aspx?cid=" + oCategoryID.value +"&ids=" + oProductTextbox.value, 600, 600, true);
			/**window.open ("productCompare.aspx?cid=" + oCategoryID.value +"&ids=" + oProductTextbox.value, "product_compare", "resizable=1,toolbar=0,location=0,width=550,height=420,scrollbars=1");**/
		} 
		else
		{
			alert("More than ONE product needs to be selected for comparison.");
		}
	}
}

var markRefresh;
markRefresh = 20; // milliseconds

// set common object reference
if (!document.all) document.all = document;
if (!document.all.object_compare.style) document.all.object_compare.style = document.all.object_compare;

var oCompare = document.all.object_compare.style;
var navDOM = window.innerHeight; // Nav DOM flag
var nCurrentPos=0;
var nMoveMargin=10;

function wRefresh() {	
	var nScrollPos = (navDOM?pageYOffset:document.body.scrollTop);	
	
	if (nScrollPos > nCurrentPos || nCurrentPos > 151 ) {
		if ( nScrollPos > 151) {
			nMoveMargin = (nScrollPos - nCurrentPos) / 3;
			nCurrentPos = nCurrentPos + nMoveMargin;			
		} else if (nScrollPos < nCurrentPos) {
			nMoveMargin = (nCurrentPos - nScrollPos) / 3;
			nCurrentPos = nCurrentPos - nMoveMargin;			
		}	
		
		if (nCurrentPos < 151) nCurrentPos = 151;

		oCompare.top = nCurrentPos;			
	}
}

function animateMove(moveTo)
{
	if (nCurrentPos < moveTo)
	{	
		nCurrentPos = nCurrentPos + 1;
		oCompare.top = nCurrentPos;	
	}
	
	if (nCurrentPos > moveTo)
	{
		nCurrentPos = nCurrentPos - 1;
		oCompare.top = nCurrentPos;		
	}
}

function markMe() {	
	nCurrentPos = 151;
	markID = setInterval ("wRefresh()",markRefresh);
}

window.onload=markMe;