var catSelectionList = ["avCatSelection", "compCatSelection", "gamesCatSelection"];
var catSelectionHeights = Array();
var catButtonYOffset = 30;
var menuBlurTimeout = 100;
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;

/**
 * Shows on category list.
 */
/*
function delayedMenuSwitch(action, catCat) {
  clearTimeout(menuTimer);
  var catSelection = gEI(catCat + 'CatSelection');
  if (action == "show" && catSelection.style.display != 'block') {
    menuTimeout = setTimeout("showCatSel('" + catCat + "')", menuOnDelay);
  }
  if (action == "hide" || catSelection.style.display == 'block') {
    menuTimeout = setTimeout("hideCatSel('" + catCat + "')", menuOffDelay);
  }
}
*/

/**
 * Cancels menu switch timer.
 */
function cMS(version) {
  if (version == 'show') {
    clearTimeout(menuTimer);
  }
  else {
    clearTimeout(hideMenuTimer);
  }
}

/**
 * Toggles state of category selection list.
 */
function toggleCatSel(catCat) {
  if (currentMenu == catCat) {
    if (isIE) {
      mozHideCatSel(catCat);
	}
	else {
	  mozHideCatSel(catCat);
	}
  }
  else {
    if (catCat != lastBlurred || (new Date().getTime()) > menuSlideStartTime + 500) {
	  lastBlurred = "";
	  if (isIE) {
        mozShowCatSel(catCat);
	  }
	  else {
	    mozShowCatSel(catCat);
	  }
	}
  }
}

/**
 * Hides menu category selection when it loses focus.
 */
function blurCatSel(catCat) {
  // Only hide if focus outside catSel.
//alert("tag " + document.activeElement.tagName);  
  if (!isIE || document.activeElement.tagName.toUpperCase() != "A") {
    if (isIE) {
	  setTimeout("mozHideCatSel('" + catCat + "')", menuBlurTimeout);
//      mozHideCatSel(catCat);
	}
	else {
	  setTimeout("mozHideCatSel('" + catCat + "')", menuBlurTimeout);
//	  mozHideCatSel(catCat);
	}
    lastBlurred = catCat;
  }
} 
 
/**
 * Turns on category list.
 */
function showCatSel(catCat) {
  var catSelection = gEI(catCat + 'CatSelection');
  if (menuOutEndHeight == 0) {
    // Initialize slide
    menuSlideStartTime = new Date().getTime();
    catSelection.style.overflow = "hidden";
    catSelection.parentNode.style.overflow = "hidden";
	catSelection.parentNode.style.height = "1px";
    catSelection.style.display = "block";
	if (catSelectionHeights[catCat] == null) {
	  catSelectionHeights[catCat] = catSelection.clientHeight;
	}
    menuOutEndHeight = catSelectionHeights[catCat];
  }

  // Calculate new height
  var elapsedTime = new Date().getTime() - menuSlideStartTime;
  menuOutCurrentHeight = Math.floor(menuOutEndHeight * (elapsedTime / menuSlideOutTime));
  menuOutCurrentHeight = menuOutCurrentHeight > menuOutEndHeight ? menuOutEndHeight : (menuOutCurrentHeight > 0 ? menuOutCurrentHeight : 1);

  // Resize and set new timer.
//document.title += "." + elapsedTime + " " + menuOutCurrentHeight;	
  catSelection.parentNode.style.height = "" + menuOutCurrentHeight + "px";
  if (menuOutCurrentHeight < menuOutEndHeight) {
    menuTimer = setTimeout("showCatSel('" + catCat + "')", menuSlidingStep);
  }
  else {
    // Set focus.
	cMS();
	lastBlurred = "";
	menuOutEndHeight = 0;
//	menuCurrentHeight = 0;
    catSelection.focus();
	currentMenu = catCat;
//document.title += "." + catSelection.offsetLeft;	
  }

}

/**
 * Turns on category list. Mozilla style...
 */
function mozShowCatSel(catCat) {
  var catSelection = gEI(catCat + 'CatSelection');
  if (menuOutEndHeight == 0) {
    // Initialize slide
    menuSlideStartTime = new Date().getTime();
	if (catSelectionHeights[catCat] == null) {
      clone = catSelection.cloneNode(true);
      clone.style.position = "absolute";
      clone.style.left = -50000;
      clone.style.display = "block";
      catSelection.parentNode.appendChild(clone);
	  catSelectionHeights[catCat] = clone.clientHeight;
      clone.parentNode.removeChild(clone);
	}
    menuOutEndHeight = catSelectionHeights[catCat];
    catSelection.style.overflow = "hidden";
	catSelection.style.height = "1px";
    catSelection.style.display = 'block';
  }

  // Calculate new height
  var elapsedTime = new Date().getTime() - menuSlideStartTime;
  menuOutCurrentHeight = Math.floor(menuOutEndHeight * (elapsedTime / menuSlideOutTime));
  menuOutCurrentHeight = menuOutCurrentHeight > menuOutEndHeight ? menuOutEndHeight : (menuOutCurrentHeight > 0 ? menuOutCurrentHeight : 1);

  // Resize and set new timer.
  catSelection.style.height = "" + menuOutCurrentHeight + "px";
  if (menuOutCurrentHeight < menuOutEndHeight) {
    menuTimer = setTimeout("mozShowCatSel('" + catCat + "')", menuSlidingStep);
  }
  else {
    // Set focus.
	cMS();
	lastBlurred = "";
	menuOutEndHeight = 0;
//	menuOutCurrentHeight = 0;
    catSelection.focus();
	currentMenu = catCat;
  }

}

/**
 * Turns off category list.
 */
function hideCatSel(catCat) {
  var catSelection = gEI(catCat + 'CatSelection');

  if (menuInStartHeight == 0) {
    // Initialize slide
    menuSlideStartTime = new Date().getTime();
    menuInStartHeight = catSelection.parentNode.style.height.replace(/px/, "");
  }

  // Calculate new height
  var elapsedTime = new Date().getTime() - menuSlideStartTime;
  menuInCurrentHeight = Math.floor(menuInStartHeight * (1.0 - (elapsedTime / menuSlideInTime)));
  menuInCurrentHeight = menuInCurrentHeight < 0 ? 0 : menuInCurrentHeight;

  // Resize and set new timer.
  catSelection.parentNode.style.height = "" + menuInCurrentHeight + "px";
  if (menuInCurrentHeight > 0) {
    hideMenuTimer = setTimeout("hideCatSel('" + catCat + "')", menuSlidingStep);
  }
  else {
    // Hide.
//    catSelection.style.display = 'none';
	cMS('hide');
	menuInCurrentHeight = 0;
	menuInStartHeight = 0;
	currentMenu = "";
  }
}

/**
 * Turns off category list.
 */
function mozHideCatSel(catCat) {
  var catSelection = gEI(catCat + 'CatSelection');

  if (menuInStartHeight == 0) {
    // Initialize slide
    menuSlideStartTime = new Date().getTime();
    menuInStartHeight = catSelection.style.height.replace(/px/, "");;
  }

  // Calculate new height
  var elapsedTime = new Date().getTime() - menuSlideStartTime;
  menuInCurrentHeight = Math.floor(menuInStartHeight * (1.0 - (elapsedTime / menuSlideInTime)));
  menuInCurrentHeight = menuInCurrentHeight < 0 ? 0 : menuInCurrentHeight;

  // Resize and set new timer.
  catSelection.style.height = "" + menuInCurrentHeight + "px";
  if (menuInCurrentHeight > 0) {
    hideMenuTimer = setTimeout("mozHideCatSel('" + catCat + "')", menuSlidingStep);
  }
  else {
    // Hide.
//    catSelection.style.display = 'none';
	cMS('hide');
	menuInCurrentHeight = 0;
	menuInStartHeight = 0;
	currentMenu = "";
  }

}

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);
}
