var lastOpenedDiv = -1;
var lastSelectedCategory = -1;
var selectedSubCategory = -1;

var currentProductImage;
var currentBigImage;
var currentProductDescription;

function navigateTo(url)
{
	window.location = url;
}

function searchProducts(e) 
{
   var enterKeyCode = 13;
   if (e.keyCode == 13) 
   {
		navigateToSearchResults();
   }
}

function navigateToSearchResults()
{
	var searchText = document.getElementById("txtSearch");
	if (searchText != null)
	{
		if (searchText.value.length > 0)
		{
			document.forms[0].action= "ProductSearchResults.aspx?Filter=" + searchText.value;
			document.forms[0].submit();
		}
	}
}

// --------------------------------------------------------------------------------
// Display an arrow when the user points to a specific sub category (vertical menu)
// --------------------------------------------------------------------------------
function setCategoryArrow(itemID)
{
	if ((selectedSubCategory != itemID) && (selectedSubCategory != -1))
	{
		var currentSpan = document.getElementById("SubCategorySpan" + itemID);
		
		if ((lastSelectedCategory != -1) && (lastSelectedCategory != itemID))
		{
			var prevCategory = document.getElementById("SubCategorySpan" + lastSelectedCategory);
			prevCategory.src = "Images/whiteArrow.jpg";
		}
									
		lastSelectedCategory = itemID;
				
		if (currentSpan != null)
		{
			currentSpan.src = "Images/selectedArrow.jpg";
		}
	}
}

// -----------------------------------------------------------------------------------
// Clear the arrow when the focus is lost from a specific sub category (vertical menu)
// -----------------------------------------------------------------------------------
function clearCategoryArrow(itemID)
{
	if ((selectedSubCategory != itemID) && (selectedSubCategory != -1))
	{
		var currentA = document.getElementById("SubCategoryNameID" + itemID);
		if (currentA != null)
		{
			currentA.style.color = "#825A37";
		}
		
		var currentSpan = document.getElementById("SubCategorySpan" + itemID);
		
		if (currentSpan != null)
		{
			currentSpan.src = "Images/whiteArrow.jpg";
		}
	}
}

// -----------------------------------------------
// Apply the passed color to the element 
// -----------------------------------------------
function setcolor(elementID, sColor)
{
	var elementOn = document.getElementById(elementID);
	if (elementOn != null)
	{
		elementOn.style.color = sColor;
	}
}

// -------------------------------------------------------
// Apply the passed color to the background of the element 
// -------------------------------------------------------
function setBackgroundColor(elementID, sColor)
{
	var elementOn = document.getElementById(elementID);
	if (elementOn != null)
	{
		elementOn.style.background = sColor;
	}
}

// --------------------------------------------------------------
// Apply the passed color to the element and set the font to bold
// --------------------------------------------------------------
function setSelectedItem(elementID, sColor)
{
	var elementOn = document.getElementById(elementID);
	if (elementOn != null)
	{
		elementOn.style.color = sColor;
		elementOn.style.font = "normal normal bold 8pt 1pt Arial";
	}
	
	if(window.screen.height < 900)
	{
		document.getElementById("divHeader").style.display = "none";
	}
}

// --------------------------------------------------
// Set the border of the passed image to be "focused"
// --------------------------------------------------
function setImageFocus(imageID)
{
	var selectedImage = document.getElementById("image" + imageID);
	if (selectedImage != null)
	{
		selectedImage.style.border = "#6e0000 1px solid";
	}
}

// --------------------------------------
// Clear the focus of the specified image
// --------------------------------------
function clearImageFocus(imageID)
{
	var selectedImage = document.getElementById("image" + imageID);
	if (selectedImage != null)
	{
		selectedImage.style.border = "#E9E2C0 1px solid";
	}
}

// ----------------------------------------------------
// Set the border of the passed image - attribute item 
// ----------------------------------------------------
function setProductAttribteImageFocus(frameID, image, bigImage, displayName, catalogNumber)
{
	var selectedFrame = document.getElementById("attributeFrame" + frameID);
	if (selectedFrame != null)
	{
		selectedFrame.style.border = "#6e0000 1px solid";
	}
	
	// Set the display and big image of the selected product
	// -----------------------------------------------------
	var objImage = document.getElementById("productImage");
	
	if (objImage != null)
	{
		// save the old values
		// -------------------
		currentProductImage = objImage.src;
		currentBigImage = objImage.getAttribute("BigImage");
		
		objImage.src = image;
		objImage.setAttribute("BigImage", bigImage);
	}
	
	// Set the catalog number of the current selection
	// -----------------------------------------------
	var objCatalog = document.getElementById("lblCatalogNumber");
	if (objCatalog != null)
	{
		var productCatalog = "  " + catalogNumber + displayName;
		
		currentProductDescription = objCatalog.innerText;
		if(productCatalog.length > 19)
		{
			productCatalog = productCatalog.substring(0, 19);
			productCatalog += " ...";
		}
				
		objCatalog.innerText = productCatalog;
		objCatalog.ToolTip = catalogNumber + displayName;
	}
}

// ------------------------------------------------
// Clear the border set to the attribute item image
// ------------------------------------------------
function clearProductAttribteImageFocus(frameID)
{
	if (lastSelectedItem != frameID)
	{
		var selectedFrame = document.getElementById("attributeFrame" + frameID);
		if (selectedFrame != null)
		{
			selectedFrame.style.border = "#DAC793 1px dashed";
		}
	}
	
	// Set the old values
	// ------------------
	var objImage = document.getElementById("productImage");
	if (objImage != null)
	{
		objImage.src = currentProductImage;
		objImage.setAttribute("BigImage", currentBigImage);
	}
	
	var objCatalog = document.getElementById("lblCatalogNumber");
	if (objCatalog != null)
	{
		objCatalog.innerText = currentProductDescription;
	}
}

// ------------------------------------------------------
// Set the color and style of the selected main category 
// ------------------------------------------------------
function setCategoryColor()
{
	var hiddenID = document.getElementById("txtCategoryID");
	var currentCategory = document.getElementById("CategoryID" + hiddenID.value);
	if (currentCategory != null)
	{
		currentCategory.style.background = "#F3F1DE";
		/*currentCategory.style.border = "#DAC793 1px solid";*/
		currentCategory.style.fontweight = "bolder";
	}
}

// -----------------------------------------------------
// Set the color and style of the selected sub category 
// -----------------------------------------------------
function setSubCategoryColor()
{
	var hiddenID = document.getElementById("txtSubCategoryID");
	/*save the selected category so the arrow won't be cleared on mouse out event*/
	selectedSubCategory = hiddenID.value;
	
	/*Set the arrow which points to the selected sub category*/
	var currentSpan = document.getElementById("SubCategorySpan" + hiddenID.value);
	if (currentSpan != null)
	{
		currentSpan.src = "Images/selectedArrow.jpg";
	}
	
	var currentA = document.getElementById("SubCategoryNameID" + hiddenID.value);
	if (currentA != null)
	{
		
		currentA.style.color = "#6E0000";
		currentA.style.fontweight = "bolder";
	}

}

function getTheLeft()
{
	var l=0;
	l=document.body.offsetWidth; //get width of window according to resolution
	/* l-=780; // take off width of outer table
	l/=2; // devide by 2 to get left position of outer table
	l-=5; // take off 5 for space between inner and outer tables
	l+=1; // add 1 to start after left position of inner table
	l-=120; // take off width of menu to get right alignment*/
	alert(l);
	return l;
}

// -----------------------------------------------------------------------
// Colapse / Expand the products of the selected / unselected sub category 
// -----------------------------------------------------------------------
function updateProductDisplay(divIndex, categoryID, subCategoryID)
{
	var divObject = document.getElementById("divProducts" + divIndex);
	if (divObject != null)
	{
		if (divObject.style.visibility == 'hidden')
		{	
			divObject.style.visibility = "visible";
			divObject.style.display = "block";
			
			if ((lastOpenedDiv != -1) && (lastOpenedDiv != divIndex))
			{
				var divToClose = document.getElementById("divProducts" + lastOpenedDiv);
				divToClose.style.visibility = "hidden";
				divToClose.style.display = "none";
			}
								
			lastOpenedDiv = divIndex;
		}
		else
		{
			divObject.style.visibility = "hidden";
			divObject.style.display = "none";
		}
	}

	// Navigate to the Products Page
	// -----------------------------
	window.location = "Subcategory.aspx?CategoryID=" + categoryID + "&SubCategoryID=" + subCategoryID; 
	
}

function AdjustToScreen()
{

	if(window.screen.height < 900)
	{
		document.getElementById("divHeader").style.display = "none";
	}
}