// Global variable needed to keep track of status changes to overviewAjaxRequest.
var overviewAjaxRequest;
var overviewAjaxTimer = 0;
var overviewAjaxTimeout = 25000;
var moreDisplayed = new Array();
var moreTabsTimers = new Array();
var moreTabsTimeout = 500;
var overviewFaderTimer = 0;
var overviewFaderStep = 20;
var overviewFadingTime = 450;
var overviewFadingStartOpacity = 80;
var overviewFaderEndTime = 0;
var needsFading = 0;
var firstGuideUsage = 1;

var doubleEventGuard = 0;

/**
 * Cancels double event guard.
 */
function cancelEventGuard() {
  doubleEventGuard = 0;
}
 
/**
 * Alias for function ajaxRequestOverview
 */
function aRO(categoryId, readSelections) {
				// Do statistics (tell that guide has been used or overview updated.)
				if (firstGuideUsage && gEI("gSelString").value != null && gEI("gSelString").value.length > 0) {
								gEI('stater').src = 'guideTracker.php?categoryId=' + categoryId;
								firstGuideUsage = 0;
				}
				else {
								gEI('stater').src = 'overviewTracker.php?categoryId=' + categoryId;
								firstGuideUsage = 0;
				}
				
				// Do actual overview update.
				return ajaxRequestOverview(categoryId, readSelections);
}

/**
 * Server call to retreive product data when user clicks on product in list.
 */ 
function ajaxRequestOverview(categoryId, readSelections) {
				clearTimeout(overviewAjaxTimer); // Stop any old updating!
				needsFading = 1;
  
  // Tell user that values are updating!
//  gEI("fetchingOverview").style.display = "inline";
  
				// Remove stored productId for other uses (mailing link), also fix so that products are listed from page 1.
				gEI("selProductId").value = "";

				// Create query with selection and sorting info if requested.
				var overviewUrl = "overview.php?categoryId=" + categoryId;
				if (readSelections == 1) {
								var sortingUrl = gEI("sortString").value;
								if (sortingUrl.length > 0) {
												overviewUrl += "&sortString=" + sortingUrl;
								}
								var selectionUrl = gEI("selString").value;
								if (selectionUrl.length > 0) {
												overviewUrl += "&selString=" + selectionUrl;
								}
								var gSelectionUrl = gEI("gSelString").value;
								if (gSelectionUrl.length > 0) {
												overviewUrl += "&gSelString=" + gSelectionUrl;
								}
								var startNo = gEI("startNo").value;
								if (startNo > 1) {
												overviewUrl += "&startNo=" + startNo;
								}
//	var inStock = gEI("inStock").checked == true ? "1" : "0";
//	overviewUrl += "&inStock=" + inStock;
				}
				overviewUrl = encodeURI(overviewUrl);
				if (window.XMLHttpRequest) {
								overviewAjaxRequest = new XMLHttpRequest();
								overviewAjaxRequest.onreadystatechange = showOverview;
								overviewAjaxRequest.open("GET", overviewUrl, true);
								overviewAjaxRequest.send(null);
				}
				else if (window.ActiveXObject) {
								overviewAjaxRequest = new ActiveXObject("Microsoft.XMLHttp");
								if (overviewAjaxRequest) {
												overviewAjaxRequest.onreadystatechange = showOverview;
												overviewAjaxRequest.open("GET", overviewUrl);
												overviewAjaxRequest.send();
								}
				}
//document.title = overviewUrl;  
				overviewAjaxTimer = setTimeout("overviewAjaxTimedOut()", overviewAjaxTimeout);
				overviewFader(1);
}

/**
 * Updates product info box with data retreived from server.
 */
function showOverview() {
				if (overviewAjaxRequest.readyState == 4) {
								if (overviewAjaxRequest.status != null &&  overviewAjaxRequest.status == 200) {
												clearTimeout(overviewAjaxTimer);
												needsFading = 0;
												clearTimeout(overviewFaderTimer);
												var newProductInfo = overviewAjaxRequest.responseText;
												var overviewcontent = gEI("overviewcontent"); 
												overviewcontent.innerHTML = newProductInfo;
												productDetailsHeights = new Array();
												retreivedProductDetails = new Array();
												productDetailsCachedData = new Array();
//      eval(overviewcontent.innerHTML);
//	  var scripts = gEI("overviewcontent").getElementsByTagName("script");
//	  for (var scriptNo = 0; scriptNo < scripts.length; scriptNo++) {
//	    eval(scripts[scriptNo].text);
//	  }
								}
								else {
												clearTimeout(overviewAjaxTimer);
												overviewAjaxTimedOut();
								} 
				}
}

/**
 * Shows a message when ajax call fails.
 */
function overviewAjaxTimedOut() {
  gEI("ajaxTimeout").style.display = 'inline';
//  overviewDiv = gEI("overviewProducts");
//  if (isIE) {
//    overviewDiv.zoom = 1;
//    if (overviewDiv.filters.alpha == null) {
//      overviewDiv.style.filter = "alpha(opacity=100)";
//	}
//	else {
//      overviewDiv.filters.alpha.Opacity = 100;
//	}
//  }
//  else {
//    overviewDiv.style.MozOpacity = 1;
//  }
}

/**
 * Calls category.php with selected products
 */
function jumpAndIncludeChecked(startUrl, endUrl) {
  var selectedItems = gEI("checkedIds").value;
  var url = startUrl;
  if (selectedItems.length > 0) {
    url += "&checkedIds=" + selectedItems;
  }
  url += endUrl;
  window.location.href = url;
}	

/**
 * Displays big image when hoovering over thumbnail.
 */
/*function showBigPicture(evt, url) {
  var fullImage = gEI("fullImage");
  fullImage.src = "g/inv_line_50.gif";
  fullImage.style.display = 'none';
  evt = (evt) ? evt : ((event) ? event : null);
  if (evt != null) {
//    alert(url);
    posX = evt.clientX - 75 + (window.scrollX != null ? window.scrollX : document.body.scrollLeft);
    posY = evt.clientY - 75 + (window.scrollY != null ? window.scrollY : document.body.scrollTop);
    fullImage.style.display = 'inline';
    fullImage.style.position = 'absolute';
    fullImage.style.left = posX;
    fullImage.style.top = posY;
    fullImage.src = url;
	if (fullImage.style.width > 500) {
	  fullImage.style.width = '500';
	}
  }
//  }
//  gEI('fullImage').style.left = this.style.left;
//  gEI('fullImage').style.top = this.style.top;
} */

/**
 * Fades overview information by steps specified by overviewFaderStep.
 */
function overviewFader(init) {
  if (!needsFading) {
    return;
  }
  
  if (init) {
    overviewFaderEndTime = new Date().getTime() + overviewFadingTime;
  }
  
  var overviewDiv = gEI("overviewProducts");
  var faderState = Math.floor(((overviewFaderEndTime - new Date().getTime()) / overviewFadingTime) * overviewFadingStartOpacity);
  faderState = faderState < 0 ? 0 : faderState;
  if (isIE) {
    overviewDiv.zoom = 1;
    if (overviewDiv.filters.alpha == null) {
      overviewDiv.style.filter = "alpha(opacity=" + faderState + ")";
	}
	else {
      overviewDiv.filters.alpha.Opacity = faderState;
	}
  }
  else {
    overviewDiv.style.opacity = faderState / 100;
  }
  if (faderState > 0) {
    overviewFaderTimer = setTimeout("overviewFader(0)", overviewFaderStep);
  }
  else {
    gEI("fetchingOverview").style.display = 'inline';
	if (isIE) {
//  	  overviewDiv.style.display = "none";
	}
  }
}

/**
 * Toggles visibility of grade and product specification sorting selection list.
 */
function toggleGradeSort(fromHeader) {
  toggleSortLists("grade", fromHeader);
}

function tSS(fromHeader) {
  toggleSortLists("specs", fromHeader);
}

/*  if (doubleEventGuard) {
    return;
  }
  gradeSortList = gEI("gradeSortList");
  if (gradeSortList.style.display != "block") {
    gradeSortVisibility = 1;
	if (!gradeSortList.style.left) {
	  posX = gEI("gradeHeader").offsetLeft;
	  posY = gEI("gradeHeader").offsetTop + gEI("gradeHeader").clientHeight + 2; 
	  gradeSortList.style.left = "" + posX + "px";
	  gradeSortList.style.top = "" + posY + "px";
	}
	sBEI("gradeSortList");
	gradeSortList.focus();
  }
  else {
	hEI("gradeSortList");
    gradeSortVisibility = 0;
  }
  doubleEventGuard = 1;
  setTimeout("cancelEventGuard()", 500);
}*/

/**
 * Toggles visibility of filtering tool
 */
function toggleFilteringTool() {
  if (doubleEventGuard == "filter") {
    return;
  }
  if (gEI("filterSection").style.display != "table-cell" && gEI("filterSection").style.display != "block") {
    if (isIE)  {
	  gEI("filterSection").style.display = "block";
	}
	else {
	  gEI("filterSection").style.display = "table-cell";
	}
  }
  else {
	hEI("filterSection");
  }
  doubleEventGuard = "filter";
  setTimeout("cancelEventGuard()", 500);
}

/**
 * Toggles visibility of sorting selection lists.
 */
function toggleSortLists(listToToggle, fromHeader) {
				if (doubleEventGuard == listToToggle) {
								return;
				}
				sortList = gEI(listToToggle + "SortList");
				if (sortList.style.display != "block" && fromHeader == 1) {
								if (!sortList.style.left) {
												posX = gEI(listToToggle + "Header").offsetLeft;
												posY = gEI(listToToggle + "Header").offsetTop + gEI(listToToggle + "Header").clientHeight + 2; 
												sortList.style.left = "" + posX + "px";
												sortList.style.top = "" + posY + "px";
												if (isIE && parseFloat(navigator.appVersion) < 7.0) {
																if (listToToggle == "specs") {
																				sortList.style.width = "200px";
																}
																else {
																				sortList.style.width = "100px";
																}
												}
								}
								sBEI(listToToggle + "SortList");
								sortList.focus();
				}
				else {
								hEI(listToToggle + "SortList");
//	gEI(listToToggle + "Header").focus();
				}
				doubleEventGuard = listToToggle;
				setTimeout("cancelEventGuard()", 500);
}

/**
  * Shows "more" tabs
	*/
function showMore(productId) {
				clearTimeout(moreTabsTimers["" + productId]);
				gEI("more" + productId).style.display = isIE ? "inline" : "table-row";
}

/**
  * Hides "more" tabs
	*/
function hideMore(productId) {
				if (moreDisplayed[productId] == 1) {
								return;
				}
				moreTabsTimers["" + productId] = setTimeout("reallyHideMore(" + productId + ")", moreTabsTimeout);
}

function reallyHideMore(productId) {
				// Dont hide when on standalone page
				if (gEI("productBasicsTable") != null) {
								return;
				}
				
				moreDisplayed[productId] = 0;
				gEI("more" + productId).style.display = "none";
				var tdElement = gEI('more' + productId).childNodes[0];
				var divNo = 0;
//alert("tag" + tdElement.childNodes[childNo].tagName);
				for (childNo = 0; childNo < tdElement.childNodes.length; childNo++) {
//alert(tdElement.childNodes[childNo].tagName);				
								if (tdElement.childNodes[childNo].tagName && tdElement.childNodes[childNo].tagName.toLowerCase() == 'div') {
												if (divNo == 0) {
																tdElement.childNodes[childNo].className = 'more tests';
												}
												else if (divNo == 1) {
																tdElement.childNodes[childNo].className = 'more specs';
												}
												else if (divNo == 2) {
																tdElement.childNodes[childNo].className = 'more prices';
												}
												divNo++;
								}
				}
}

/**
  * Expands product information
	*/
function toggleProduct(productId, categoryId, divPointer, whatInfo) {
				clearTimeout(moreTabsTimers[productId]);
				moreDisplayed[productId] = 1;
				var tdElement = gEI('more' + productId).childNodes[0];
				var divNo = 0;
//alert("tag" + tdElement.childNodes[childNo].tagName);
				for (childNo = 0; childNo < tdElement.childNodes.length; childNo++) {
//alert(tdElement.childNodes[childNo].tagName);				
								if (tdElement.childNodes[childNo].tagName && tdElement.childNodes[childNo].tagName.toLowerCase() == 'div') {
												if (divNo == 0 && whatInfo != "tests") {
																tdElement.childNodes[childNo].className = 'more tests';
												}
												else if (divNo == 1 && whatInfo != "specs") {
																tdElement.childNodes[childNo].className = 'more specs';
												}
												else if (divNo == 2 && whatInfo != "prices") {
																tdElement.childNodes[childNo].className = 'more prices';
												}
												divNo++;
								}
				}
				divPointer.className = whatInfo + 'Exp expMore';
				aRPI(productId, categoryId, divPointer, whatInfo);
}