var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;
var ddparentitem = 0;
var wLoc = window.location.toString();

// open hidden layer
function mopen(id) {	
  // cancel close timer
  mcancelclosetime();

  // close old layer
  mclose();

  // get new layer and show it
  ddmenuitem = document.getElementById('m' + id);
  var dsfjl = id.substring(0, id.indexOf("_") + 1);
  //ddparentitem = document.getElementById('f' + id.replace(dsfjl, ""));
  ddparentitem = document.getElementById('f' + id);
  //alert('f' + id.replace(dsfjl, ""));
  
  if (ddmenuitem) {
	var rel = ddmenuitem.getAttribute("rel");
	if (rel) {
	  rel = rel.replace(/t/, "");
	  rel = parseInt(rel);
	}
	else {
	  rel = 0;
	}

	var t = id.substring(0, id.indexOf("_"));
	var top = 19 * rel;
	top += t * 22;
	id = id.substring(id.indexOf("_") + 1);
	id = id - 1;
	top += id * 17;
	
	if (t == 2) {
	  top -= 28;	
	}
	else if (t == 3) {
	  top -= 40;	
	}
	else if (t == 4) {
	  top -= 48;	
	}
	
	
	ddmenuitem.style.top = top + "px";
    ddmenuitem.style.display = 'block';
	
	//keep the top level link activated
	ddparentitem.style.color = "#fff";
	if (wLoc.indexOf("file://") != -1) {
	  ddparentitem.style.background = "#131313 url('../section-link-black.jpg') repeat-x";
	}
	else if (wLoc.indexOf("cgi-bin") != -1) {
		ddparentitem.style.background = "#131313 url('/acatalog/section-link-black.jpg') repeat-x";
	}
	else if (wLoc.indexOf("acatalog") == -1) {
	  ddparentitem.style.background = "#131313 url('acatalog/section-link-black.jpg') repeat-x";
	}
	else {
      ddparentitem.style.background = "#131313 url('section-link-black.jpg') repeat-x";
	}
    ddparentitem.style.textDecoration ="none";
  }
}

// close showed layer
function mclose() {
  if (ddmenuitem) ddmenuitem.style.display = 'none';
  //reset the top level link
  if (ddparentitem) {
    ddparentitem.style.color = "#666";
	if (wLoc.indexOf("file://") != -1) {
      ddparentitem.style.background = "url('../section-link-blue.jpg') repeat-x";
	}
	else if (wLoc.indexOf("cgi-bin") != -1) {
		ddparentitem.style.background = "url('/acatalog/section-link-blue.jpg') repeat-x";
	}
	else if (wLoc.indexOf("acatalog") == -1) {
	  ddparentitem.style.background = "url('acatalog/section-link-blue.jpg') repeat-x";
	}
	else {
	  ddparentitem.style.background = "url('section-link-blue.jpg') repeat-x";
	}
  }
}

// go close timer
function mclosetime() {
  closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
  if (closetimer) {
    window.clearTimeout(closetimer);
	closetimer = null;
  }
}

// close layer when click-out
document.onclick = mclose; 


// JavaScript Document
var $d = function(id) {
  return document.getElementById(id);
};

function teclanLoad() {
  //fix the border to run the full height of the center div
  var left = $d("left"), content = $d("center");
  if (left && content) {
    var hArr = new Array(left.offsetHeight, content.offsetHeight);
	  var theHighest = 0;
	  for (var i = 0; i < hArr.length; i++) {
	    if (hArr[i] > theHighest) theHighest = hArr[i];
	  }
	  left.style.height = theHighest + "px";
	  content.style.height = theHighest + "px";
  }
  initRollovers();
}

if (window.attachEvent) { 
  window.attachEvent("onload", teclanLoad); 
} 
else {  
  window.addEventListener("load", teclanLoad, false); 
}

function initRollovers() {
  var container = document.getElementById("rollovers");
  var mainImg = document.getElementById("main-img");
  mainImg.onclick = function(){teclanShowPopUp(mainImg.src,400,500)};
  if (container) {
    var imgs = container.getElementsByTagName("img");
    for (var i = 0; i < imgs.length; i++) {
      imgs[i].onclick = function() {
        if (mainImg) {
          //hold temp image
		  var tempImgsrc = mainImg.src;
		  mainImg.src = this.src;
		  mainImg.width = 200;
		  mainImg.onclick = imageThumbPopup;
		  this.src = tempImgsrc;
		  this.width = 100;
		  
        }
      }
    }
  }
}

function imageThumbPopup() {
	teclanShowPopUp(this.src,400,500);
}

/**
 * Provides functionality on section pages to filter/sort products.
 * 
 * @author Lewis A MacKenzie
 * @copyright teclan 2009
 * @version 1.0.0
 */

//main HTTP request
var request = null;
var currentPageNumber = 1;
var proposedPage = 1;

function getCustomVarValues(customVars) {
  //Mozilla
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  }
  //Microsoft
  else if (window.ActiveXObject) {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    if (!request) request = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (request) {
    request.onreadystatechange = allBrandsCallBack;
    try {
      request.open("POST", theURL, true);
      request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      request.send("action=allVarValues&customVars=" + customVars);
    }
    catch (e) {
      alert(e);
    }
  }
}

function allBrandsCallBack() {
  if (request.readyState == 4) {
    if (request.status == 200) {
	
		var iexplorer = navigator.appName.indexOf("Microsoft") != -1; //Verifiy explorer
	  if (request.responseText.indexOf('[Brand]') != -1) {
		var start = request.responseText.indexOf('[Brand]') + 7;
		var end = request.responseText.indexOf('[/Brand]');
	    var brands = request.responseText.substring(start, end);
		//handling of IE8
		var brandSelect = document.getElementById("BrandList");// this is the select box
		if (iexplorer) {
			brandSelect.outerHTML = '<select name="Brand" id="BrandList"><option value="None">Brand</option>' + brands + '</select>';
		}
		else {
			brandSelect.innerHTML = '<option value="None">Brand</option>' + brands; // Normally
		}
	  }
	  if (request.responseText.indexOf('[Colour]') != -1) {
		var start = request.responseText.indexOf('[Colour]') + 8;
		var end = request.responseText.indexOf('[/Colour]');
	    var colours = request.responseText.substring(start, end);
		//handling of IE8
		var colourSelect = document.getElementById("ColourList");// this is the select box
		if (iexplorer) {
			colourSelect.outerHTML = '<select name="Colour" id="ColourList"><option value="None">Colour</option>' + colours + '</select>';
		}
		else {
			colourSelect.innerHTML = '<option value="None">Colour</option>' + colours; // Normally
		}
	  }
	  
	  if (request.responseText.indexOf('[Size]') != -1) {
		var start = request.responseText.indexOf('[Size]') + 6;
		var end = request.responseText.indexOf('[/Size]');
	    var sizes = request.responseText.substring(start, end);
		//handling of IE8
		var sizeSelect = document.getElementById("SizeList");// this is the select box
		if (iexplorer) {
			sizeSelect.outerHTML = '<select name="Size" id="SizeList">' + sizes + '</select>';
		}
		else {
			sizeSelect.innerHTML = sizes; // Normally
		}
	  } 
	  if (request.responseText.indexOf('[Width]') != -1) {
		var start = request.responseText.indexOf('[Width]') + 7;
		var end = request.responseText.indexOf('[/Width]');
	    var widths = request.responseText.substring(start, end);
		//handling of IE8
		var widthSelect = document.getElementById("WidthList");// this is the select box
		if (iexplorer) {
			widthSelect.outerHTML = '<select name="Width" id="WidthList"><option value="None">Width</option>' + widths + '</select>';
		}
		else {
			widthSelect.innerHTML = '<option value="None">Width</option>' + widths; // Normally
		}
	  }
	  if (request.responseText.indexOf('[Height]') != -1) {
		var start = request.responseText.indexOf('[Height]') + 8;
		var end = request.responseText.indexOf('[/Height]');
	    var heights = request.responseText.substring(start, end);
		//handling of IE8
		var heightSelect = document.getElementById("HeightList");// this is the select box
		if (iexplorer) {
			heightSelect.outerHTML = '<select name="Height" id="HeightList"><option value="None">Height</option>' + heights + '</select>';
		}
		else {
			heightSelect.innerHTML = '<option value="None">Height</option>' + heights; // Normally
		}
	  }
	}
  }
}

function dalzellsSearch(pageNumber) {
  setLoaderVisible(true);
  
  var queryString = "";
  var theForm = document.getElementById("dalz-search");
  if (theForm) {
    for (var i = 0; i < theForm.elements.length; i++) {
	  if (i > 0) {
	    queryString += "&";
	  }
	  queryString += theForm.elements[i].name + "=" + encodeURIComponent(theForm.elements[i].value);
	}
  }

  if (pageNumber == -1) {
	proposedPage = currentPageNumber;
    queryString += "&page=" + currentPageNumber;
  }
  else if (pageNumber == -2) {
	proposedPage = currentPageNumber + 1;
    queryString += "&page=" + (currentPageNumber + 1);
  }
  else if (pageNumber == -3) {
	proposedPage = currentPageNumber - 1;
    queryString += "&page=" + (currentPageNumber - 1);
  }
  else {
	proposedPage = pageNumber;
    queryString += "&page=" + pageNumber;
  }
  
  queryString += "&proppage=" + proposedPage;

  //Mozilla
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
  }
  //Microsoft
  else if (window.ActiveXObject) {
    request = new ActiveXObject("Msxml2.XMLHTTP");
    if (!request) request = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (request) {
    request.onreadystatechange = searchCallBack;
    try {
      request.open("POST", theURL, true);
      request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      request.send(queryString);
    }
    catch (e) {
      alert(e);
    }
  }
}

function searchCallBack() {
  if (request.readyState == 4) {
    if (request.status == 200) {
	  document.getElementById("searchresults").innerHTML = request.responseText;
	  setLoaderVisible(false);
	  currentPageNumber = proposedPage;
	}
  }
}

function setLoaderVisible(visible) {
  var loader = document.getElementById("loader");
  if (loader) {
    loader.style.visibility = visible ? "visible" : "hidden";
  }
}

function teclanShowPopUp(sUrl, nWidth, nHeight)
  	{  
	window.open(sUrl, 'ActPopup', 'width=' + nWidth + ',height=' + nHeight + ',scrollbars, resizable');
	}
