var popupCount = 100;
var tooltips = false;
var thumbCount;
var thumbTitles = new Array(100);
var thumbURLs = new Array(100);
var currentThumb;
var loadDone = false;

function GcreateElement(element)
{
 if (typeof document.createElementNS != 'undefined') { return document.createElementNS('http://www.w3.org/1999/xhtml', element); }
 if (typeof document.createElement != 'undefined') { return document.createElement(element); }
 return false;
}

function loadGoogleAnalytics()
{
 var head = document.getElementsByTagName('head').item(0);
 script = GcreateElement('script');
 script.src = "http://www.google-analytics.com/urchin.js";
 script.type = 'text/javascript';
 script.id = 'googleAnalytics';
 void(head.appendChild(script));
}

function pingGoogleAnalytics()
{
 if(typeof(_uacct) == "undefined") {
  setTimeout("pingGoogleAnalytics()", 100);
 } else {
  _uacct = "UA-522059-1";
  urchinTracker();
 }
}

function compShipCancel()
{
 var head = document.getElementsByTagName('head').item(0);
 var old  = document.getElementById('lastLoadedCmds');
 if (old) head.removeChild(old);
 document.body.removeChild(document.getElementById("shipPop"));
}
function compShip()
{
 var head = document.getElementsByTagName('head').item(0);
 var old  = document.getElementById('lastLoadedCmds');
 if (old) head.removeChild(old);
 script = document.createElement('script');
 script.src = "./test.js?"+ Math.floor(Math.random()*999999999) ;
 script.type = 'text/javascript';
 script.defer = true;
 script.id = 'lastLoadedCmds';
 document.getElementById("compship").disabled = true;
 document.getElementById("compship").value = "Please wait...";
 document.getElementById("shipPop").innerHTML = "Contacting server to get the estimate...";
 head.appendChild(script);
}
function compShipOpen(oNode)
{
 var prodid = oNode.parentNode.parentNode.id.substr(5);
 var iX = 0;
 var iY = 0;
 var oElement = oNode;
 while( oElement != null ) {
  iX += oElement.offsetLeft;
  iY += oElement.offsetTop;
  oElement = oElement.offsetParent;
 }
 var oPop = createPopup("shipPop","shipOut","Shipping Estimator",500,200,compShipCancel);
 centerDivVertical(oPop,500,200);
 document.getElementById("shipOut").innerHTML = prodid + "<h3>Loading product shipping specifications...</h3><br /><img alt=\"loading\" src=\"gc2/bluebarloader.gif\" />";
 oPop.style.visibility = "visible";
}  
function ttShipSetup(oNode) {
 oNode.onmouseover = function() { ttShipOver(oNode); }
 oNode.onmouseout = function() { ttOut(); }
 oNode.onclick = function() { compShipOpen(oNode); }
 //return false;
}
function ttShipOver(oNode) {
 ttPosition(oNode.parentNode, "Click to calculuate<br />a shipping estimate.",10,-50);
}

function ttPosition(oNode,sText, extraX, extraY)
{
 if(!tooltips)
 {
  var ie=document.all &&!window.opera
  var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
  var docwidth=(ie)? standardbody.clientWidth : window.innerWidth-16
  if(docwidth < 900) { return; } //disable because small resolutions cause problems with tooltips!
  tooltips = true;
 }
 if(!extraX) { extraX = 0; }
 if(!extraY) { extraY = 0; }
 var iX = 0;
 var iY = 0;
 var oElement = oNode;
 while( oElement != null )
 {
  iX += oElement.offsetLeft;
  iY += oElement.offsetTop;
  oElement = oElement.offsetParent;
 }
 try { //error catching  - just in case the element doesn't exist (yet!) eg: document onload hasn't run yet
 document.getElementById("tooltip").innerHTML = sText;
 document.getElementById("tooltip").style.left = (iX+extraX) + "px";
 document.getElementById("tooltip").style.top = (iY+extraY) + "px";
 document.getElementById("tooltip").style.visibility = "visible";
 } catch (excp) { }
}
function ttOut()
{
 try { //error catching  - just in case the element doesn't exist (yet!) eg: document onload hasn't run yet
  document.getElementById("tooltip").style.visibility = "hidden";
 } catch (excp) { }
}


function ttPictSetup(oNode) {
 oNode.onmouseover = function() { ttPictOver(oNode); }
 oNode.onmouseout = function() { ttOut(); }
 oNode.onclick = function() { viewerOpen(oNode); }
}
function ttPictOver(oNode) {
 ttPosition(oNode.parentNode, "Click to view<br />pictures of product.",10,-50);
}
function thumbOver(oNode,sText) { ttPosition(oNode, sText,0,104); }
function pictReady(oNode)
{
 try
 {
  document.getElementById("circleload").style.visibility = "hidden";
  document.getElementById("circleload").parentNode.removeChild(document.getElementById("circleload"));
 } catch (excp) { }
 oNode.style.visibility = "visible";
 oNode.style.height = "auto";
}
function movePict(amt) //eg: go to next or prev picture
{
 var newLoc = currentThumb + amt;
 if(newLoc < 0)
 {
  newLoc = thumbCount -1;
 }
 else if(newLoc >= thumbCount)
 {
  newLoc = 0;
 }
 thumbClick(newLoc);
}
function thumbClick(thumbNum)
{
 currentThumb = thumbNum;
 var oPop = createPopup("bigpictPop","bigpictOut",thumbTitles[thumbNum] + " &nbsp;<small>["+(thumbNum+1)+"/"+thumbCount+"]</small>",670,475, function() { document.body.removeChild(document.getElementById("bigpictPop")); }, (thumbCount > 1) ); //thumbcount > 1 becomes true if theres mroe than 1 pict thus setting makePictBtn = true -- turns on the prev/next buttons
 centerDivVertical(oPop,670,475,-50);
 document.getElementById("bigpictOut").innerHTML = "<img id=\"circleload\" class=\"bigload\" alt=\"loading\" src=\"gc2/bluecircleloader.gif\" /><img class=\"notloaded\" onload=\"pictReady(this);\" src=\"gc2/prodpics/"+thumbURLs[thumbNum]+"\" />";
 
 oPop.style.visibility = "visible";
}
function thumbEvents(thumb,i)
{
 thumb.onmouseover = function() { this.style.borderColor = "#f00"; thumbOver(this, thumbTitles[i]); };
 thumb.onmouseout = function() { this.style.borderColor = "#00f"; ttOut(); };
 thumb.onclick = function() { thumbClick(i); };
}
function loadThumbs()
{
 if(thumbCount==1)
 {
   viewerClose();
   thumbClick(0); //if there is only 1 picture available then just go straight to it - skip the thumbnail window
   return;
 }
 
 document.getElementById("pictOut").innerHTML = "<h3>Click on a picture to enlarge:</h3>";
 var counter = 1;
 for(var i = 0; i < thumbCount; i++)
 {
  var thumb = document.createElement("img");
  thumb.className = "thumb";
  thumb.setAttribute("src","gc2/prodpics/t_"+thumbURLs[i]);
  thumbEvents(thumb,i);
  document.getElementById("pictOut").appendChild(thumb);
  if(counter == 4)
  {
   document.getElementById("pictOut").appendChild(document.createElement("br"));
   counter = 1;
  } else { counter++; }
 }
}
function viewerClose()
{
 var head = document.getElementsByTagName('head').item(0);
 var old  = document.getElementById('lastLoadedCmds');
 if (old) head.removeChild(old);
 var oldP = document.getElementById("bigpictPop");
 if(oldP) { document.body.removeChild(oldP); }
 oldP = document.getElementById("pictPop");
 if(oldP) { document.body.removeChild(oldP); }
}
function viewerOpenByProdID(prodid)
{
 viewerClose();
 var head = document.getElementsByTagName('head').item(0);
 var old  = document.getElementById('lastLoadedCmds');
 if (old) head.removeChild(old);
 script = document.createElement('script');
 script.src = "gc2/backend/pictures.php?sProdID="+prodid+"&rand="+ Math.floor(Math.random()*999999999) ;
 script.type = 'text/javascript';
 script.defer = true;
 script.id = 'lastLoadedCmds';
 head.appendChild(script);
 var oPop = createPopup("pictPop","pictOut","Picture Viewer",600,400,viewerClose);
 centerDivVertical(oPop,600,500);
 document.getElementById("pictOut").innerHTML = "<h3>&nbsp;<br />Loading product picture thumbails...</h3><br /><img alt=\"loading\" src=\"gc2/bluebarloader.gif\" />";
 oPop.style.visibility = "visible";	
}
function viewerOpen(oNode)
{
 var prodid = oNode.parentNode.parentNode.id.substr(5);
 viewerOpenByProdID(prodid);
}
function CatThumbClick(thumbFile,thumbName)
{
 var oPop = createPopup("bigpictPop","bigpictOut",thumbName,670,475, function() { document.body.removeChild(document.getElementById("bigpictPop")); } );
 centerDivVertical(oPop,670,475,-50);
 document.getElementById("bigpictOut").innerHTML = "<img id=\"circleload\" class=\"bigload\" alt=\"loading\" src=\"gc2/bluecircleloader.gif\" /><img class=\"notloaded\" onload=\"pictReady(this);\" src=\"gc2/prodpics/"+thumbFile+"\" />";
 //document.getElementById("bigpictOut").innerHTML += "<br /><span onclick=\"document.body.removeChild(document.getElementById('bigpictPop'));\" class=\"fakelink\">[Exit]</span>";
 oPop.style.visibility = "visible";
}


function createPopup(ContainerID, TextID, Title, Width, Height, CloseBtnHandler,makePictBtns)
{
 try {  //try to call the close button
  if(document.getElementById(ContainerID)) { document.getElementById(ContainerID + "_closebtn").onclick(); }
 } catch(excp) { }

 if(document.getElementById(ContainerID)) { //if its still there then just delete the old popup
  document.body.removeChild(document.getElementById(ContainerID));
 }

 ttOut(); //this fixes a bug in safari where onmouseout doesn't trigger when you click on a thumb

 var pcontainer = document.createElement("div");
 var ptitlebar = document.createElement("div");
 var pcontent = document.createElement("div");
 var pbottombar = document.createElement("div");
 
 pcontainer.id = ContainerID;
 pcontainer.className = "pContainer";
 pcontainer.style.zIndex = popupCount++; //keep counting up so new popups ALWAYS show up on top
 
 ptitlebar.className = "pTitleBar";
 var ptitletext = document.createElement("span");
 ptitletext.innerHTML = Title;
 ptitletext.style.position = "relative";
 ptitletext.style.top = "4px";
 ptitlebar.appendChild(ptitletext);
 //ptitlebar.innerHTML = Title;

 pcontent.className = "pContent";
 pcontent.id = TextID;

 pbottombar.className = "pBottomBar";
 
 if(makePictBtns)
 {
  var ptemp = document.createElement("input");
  ptemp.type = "button";
  ptemp.value = "Previous Picture";
  ptemp.className = "pButton";
  ptemp.onmouseover = function() { this.style.backgroundColor = '#990'; }
  ptemp.onmouseout = function() { this.style.backgroundColor = '#3b5998'; }
  ptemp.onclick = function() { movePict(-1); }
  pbottombar.appendChild(ptemp);
  var ptemp = document.createElement("input");
  ptemp.type = "button";
  ptemp.value = "Next Picture";
  ptemp.className = "pButton";
  ptemp.onmouseover = function() { this.style.backgroundColor = '#990'; }
  ptemp.onmouseout = function() { this.style.backgroundColor = '#3b5998'; }
  ptemp.onclick = function() { movePict(1); }
  pbottombar.appendChild(ptemp);
 }
 var pclosebtn = document.createElement("input");
 pclosebtn.type = "button";
 pclosebtn.value = "CLOSE";
 pclosebtn.onclick = CloseBtnHandler;
 pclosebtn.id = ContainerID + "_closebtn";
 pclosebtn.className = "pButton";
 pclosebtn.onmouseover = function() { this.style.backgroundColor = '#990'; }
 pclosebtn.onmouseout = function() { this.style.backgroundColor = '#3b5998'; }
 pbottombar.appendChild(pclosebtn);
 
 
 pcontainer.style.width = (Width+20) + "px";
 pcontent.style.height = (Height+8) + "px";
 
 pcontainer.appendChild(ptitlebar);
 pcontainer.appendChild(pcontent);
 pcontainer.appendChild(pbottombar);

 document.body.appendChild(pcontainer);
 return pcontainer;
}


function bbOver(oNode)
{
 oNode.src='gc2/customizebuy_over.gif';
 ttPosition(oNode, "Click to customize,<br />estimate shipping,<br />or purchase<br />this product online.",-30,-80);
}

function bbOut(oNode)
{
 oNode.src='gc2/customizebuy.gif';
 ttOut();
}

function bbClick(oNode)
{
 var prodid = oNode.parentNode.parentNode.id.substr(5);
 location.href="./?sAction=OrderForm&sProdID="+prodid;
 //alert(prodid);
}

/////////////////////////////////////////////////

function centerDivVertical(divobj,objwidth,objheight,extraY)
{ //Centers a div element on the page
 if(!extraY) { extraY = 0; }
 var ie=document.all &&!window.opera
 var dom=document.getElementById
 var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
 var scroll_top=(ie)? standardbody.scrollTop : window.pageYOffset
 var scroll_left=(ie)? standardbody.scrollLeft : window.pageXOffset
 var docwidth=(ie)? standardbody.clientWidth : window.innerWidth-16
 var docheight=(ie)? standardbody.clientHeight: window.innerHeight
 var docheightcomplete=(standardbody.offsetHeight>standardbody.scrollHeight)? standardbody.offsetHeight : standardbody.scrollHeight //Full scroll height of document
 if(!objheight) { objheight = divobj.offsetHeight; }
 if(!objwidth) { objwidth = divobj.offsetWidth; }
 var topposition=(docheight>objheight)? scroll_top+docheight/2-objheight/2+"px" : scroll_top+10+"px" //Vertical position of div element: Either centered, or if element height larger than viewpoint height, 10px from top of viewpoint
 divobj.style.left=parseInt(docwidth/2-objwidth/2)+"px" //center div element horizontally
 divobj.style.top=extraY + Math.floor(parseInt(topposition))+"px"
}

function setupOptions(oNode) { oNode.parentNode.style.textAlign="center"; oNode.className += "fakelink"; }
function doload() {
 if(loadDone) { return; }
 
 loadGoogleAnalytics();
 pingGoogleAnalytics();
 
 loadDone = true;

 try {

 var links = document.getElementsByTagName("span");
 for(var i=0; i<links.length; i++) {
  if(links[i].innerHTML == "[Shipping]") { ttShipSetup(links[i]); setupOptions(links[i]); }
  else if(links[i].innerHTML == "[Pictures]") { ttPictSetup(links[i]); setupOptions(links[i]); }
 }

 var links = document.getElementsByTagName("img");
 for(var i=0; i<links.length; i++) {
  if(links[i].className == "buybutton") {
   links[i].src = "gc2/customizebuy.gif";
   links[i].onmouseover = function() { bbOver(this); };
   links[i].onmouseout = function() { bbOut(this); };
  }
 }

 var tt = document.createElement("div");
 tt.id ="tooltip";
 document.body.appendChild(tt);

 IEMinHeight();
 
 } catch(ex) { loadDone = false; }

}

function IEMinHeight()
{
 if(document.all && !window.opera) { //IE ONLY - enforce the min-height (sort of)
  var cc = document.getElementById("centercontent");
  var lc = document.getElementById("leftcontent");
  if(cc.offsetHeight < lc.offsetHeight) {
   cc.style.height = (lc.offsetHeight-2) + "px";
  }
 }
}

function init() {
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;

	// kill the timer
	if (_timer) clearInterval(_timer);

	doload();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			init(); // call the onload handler
		}
	};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			init(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
window.onload = init;