var catSelectionList = ["avCatSelection", "compCatSelection", "gamesCatSelection", "shareCatSelection"];
var catSelectionHeights = Array();
var catButtonYOffset = 30;
var menuBlurTimeout = 100;
var hideCatSelTimer = 0;
var hideMenuTimeout = 700;
var menuSlideStartTime = 0;
var menuOutEndHeight = 0;
var menuOutCurrentHeight = 0;
var menuInStartHeight = 0;
var menuInCurrentHeight = 0;
var menuSlideOutTime = 50;
var menuSlideInTime = 50;
var menuSlidingStep = 5;
var menuTimer = 0;
var hideMenuTimer = 0;
var currentMenu = "";
var lastBlurred = "";

var menuOnDelay = 150;
var menuOffDelay = 100;
var lastMenuEvt = 0;

/**
 * Cancels menu switch timer.
 */
function cMS(version) {
  if (version == 'show') {
    clearTimeout(menuTimer);
  }
  else {
    clearTimeout(hideMenuTimer);
  }
}

/**
 * Turns on category list.
 */
function showCatSel(catCat) {
	var catSelection = gEI(catCat + 'CatSelection');
	if (hideCatSelTimer) { 
		//~ if (catSelection.style.display == 'block') {
			clearTimeout(hideCatSelTimer);
		//~ }
	}
	
	if (catSelection.style.display == 'block') {
		return;
	}

	for (var catCatNo = 0; catCatNo < catSelectionList.length; catCatNo++) {
		if (catSelectionList[catCatNo].indexOf(catCat) != 0) {
			gEI(catSelectionList[catCatNo]).style.display = 'none';
		}
	}
	catSelection.style.display = 'block';
	gEI("catSelBoard").style.display = 'block';
}

/**
 * Turns off category list.
 */
function hideCatSel(catCat) {
	var catSelection = gEI(catCat + 'CatSelection');

	hideCatSelTimer = setTimeout("gEI('" + catCat + "CatSelection" + "').style.display='none';gEI('catSelBoard').style.display='none';", hideMenuTimeout);
}


function tvCheckForm(form) {
  var searchString = top.document.getElementById("searchField").value;
  // Remove leading blanks
  while (searchString.length > 0 && searchString.indexOf(" ") == 0) {
    searchString = searchString.substr(1);
  }
  while (searchString.length > 0 && searchString.lastIndexOf(" ") == searchString.length - 1) {
    searchString = searchString.substring(0, searchString.length - 1);
  }
  if (searchString.length == 0) {
    alert("Skriv in något i sökrutan och försök igen!");
	return false;
  }
  searchString = searchString.replace(new RegExp("<=", "gi"), " <= ");
  searchString = searchString.replace(new RegExp("<", "gi"), " < ");
  searchString = searchString.replace(new RegExp("=", "gi"), " = ");
  searchString = searchString.replace(new RegExp(">=", "gi"), " >= ");
  searchString = searchString.replace(new RegExp(">", "gi"), " > ");
  searchString = searchString.replace(new RegExp("  ", "gi"), " ");
  searchString = searchString.replace(new RegExp(" ", "gi"), "+");
  searchString = searchString.replace(new RegExp("&", "gi"), "+");
  searchString = searchString.replace(new RegExp("$", "gi"), "+");
  top.location.href = "index.php?function=search&searchString=" + searchString;
  return false;
}

function mailPage() {
  var gotFirst = 0;
//  var urlToMail = lang("menu", "baseUrl") + "?";

  var loc = window.location.href;
  var tmpPos1 = loc.indexOf("/", 9);
  var urlBase = (tmpPos1 > 0) ? loc.substring(0, tmpPos1) : loc;
  var urlToMail = urlBase + "/" + "?";

  // Get function if set.
  var functionPos = loc.indexOf("function=");
  if (functionPos >= 0) {
    functionPos += "function=".length;
	endPos = loc.indexOf("&", functionPos);
	if (endPos < 0) {
	  endPos = loc.length;
	}
	var functionName = loc.substring(functionPos, endPos);
    urlToMail += (gotFirst ? ",amp," : "");
    urlToMail += "function=" + functionName;
	gotFirst = 1;
	if (functionName == "search") {
      urlToMail += (gotFirst ? ",amp," : "");
      urlToMail += "searchString=" + gEI("searchField").value;
	  gotFirst = 1;
	}
  }
  
  // Get categoryId if set.
  var catPos = loc.indexOf("categoryId=");
  if (catPos >= 0) {
    catPos += "categoryId=".length;
	endPos = loc.indexOf("&", catPos);
	if (endPos < 0) {
	  endPos = window.location.href.length;
	}
	var catId = loc.substring(catPos, endPos);
    urlToMail += (gotFirst ? ",amp," : "");
    urlToMail += "categoryId=" + catId;
	gotFirst = 1;
  }
  
  // Get product id OR sort string - not both!
  if (gEI("selProductId").value != null && gEI("selProductId").value.length > 0) {
    urlToMail += (gotFirst ? ",amp," : "");
    urlToMail += "productId=" + gEI("selProductId").value;
	gotFirst = 1;
  }
  else {
    if (gEI("sortString").value != null && gEI("sortString").value.length > 0) {
      urlToMail += (gotFirst ? ",amp," : "");
      urlToMail += "sortString=" + gEI("sortString").value;
	  gotFirst = 1;
	}
  }
//  if (gEI("categoryId").value != null && gEI("categoryId").value.length > 0) {
//    urlToMail += (gotFirst ? ",amp," : "");
//    urlToMail += "categoryId=" + gEI("categoryId").value;
//	gotFirst = 1;
//  }
  if (gEI("selString").value != null && gEI("selString").value.length > 0) {
    urlToMail += (gotFirst ? ",amp," : "");
    urlToMail += "selString=" + gEI("selString").value;
	gotFirst = 1;
  }
  if (gEI("gSelString").value != null && gEI("gSelString").value.length > 0) {
    urlToMail += (gotFirst ? ",amp," : "");
    urlToMail += "gSelString=" + gEI("gSelString").value;
	gotFirst = 1;
  }
  
//alert("?function=mailLink&url=" + urlToMail);
  go(lang("menu", "baseUrl") + "?function=mailLink&url=" + urlToMail);
}
