// Zee & Co. javascript 

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function getElementsByClassName(classname) {
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = document.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++){
			if(re.test(els[i].className))a.push(els[i]);
	}
    return a;
}

addLoadEvent(runBookmark);
function runBookmark(){
	if(getElementsByClassName("bookmark") == 'undefined'){ return false; }
	var bookmark = getElementsByClassName("bookmark");
	
	var location = window.location;
	for(var i=0; i < bookmark.length; i++){
		var title = bookmark[i].getAttribute("title");
		bookmark[i].onclick = function(){
			return addBookmark(title, location);
		}
	}
	return false;
}
// Add bookmark for both browser engines...
function addBookmark(title,url) {
  
  //alert(title); IE7 issue with window.external...
  if (window.sidebar) {
    window.sidebar.addPanel(title, url,"");
  } else if( document.all ) {
    window.external.AddFavorite(url, title);
  } else if( window.opera && window.print ) {
    return true;
  }
} 


addLoadEvent(openPopup);
function openPopup(){
	
	var popupLinks = getElementsByClassName("openPopup");
	
	for ( var i=0; i < popupLinks.length; i++ ){
		var trigger = popupLinks[i].getAttribute("className") || popupLinks[i].getAttribute("class");

		switch(trigger) {
			case "openPopup prodPopup":
			popupLinks[i].onclick = function(){
				return popup( this, "Product", "600", "744", "yes" );
			}
		break;
		case "openPopup sizes":
			popupLinks[i].onclick = function(){
				return popup( this, "SizeGuide", "593", "744", "yes" );
			}
		break;
		case "openPopup termsPopup":
			popupLinks[i].onclick = function(){
				return popup( this, "Terms", "600", "744", "yes" );
			}
		break;
		case "openPopup emailPopup":
			popupLinks[i].onclick = function(){
				return popup( this, "EmailFriend", "440", "450", "" );
			}
		break;
		case "openPopup forgotten":
			popupLinks[i].onclick = function(){
				return popup( this, "forgotten", "440", "298", "" );
			}
		break;
		case "openPopup security":
			popupLinks[i].onclick = function(){
				return popup( this, "forgotten", "440", "368", "" );
			}
		break;
		case "openPopup payPal":
			popupLinks[i].onclick = function(){
				return popup( this, "PayPal", "440", "360", "" );
			}
		break;
		}
	}
}

function popup( vURL, vWinName, vWidth, vHeight, vScrollbar) {
//vWinName cannot contain spaces, leave vScrollbar blank for no scrollba
	if (! window.focus)return true;
	
	var vhref;
	var vSWidth = screen.width
	var vSHeight = screen.height
	
	if(vScrollbar == "yes") {
		vWidth = new Number(vWidth) + 16
	}
	
	var vleftPos = (vSWidth / 2) - (vWidth / 2)
	var vtopPos = (vSHeight / 2) - (vHeight / 2)
	
	if (typeof(vURL) == 'string') {
		vhref = vURL;
	} else {
		vhref = vURL.href;
	}
	window.open(vhref, vWinName, 'scrollbars=' + vScrollbar + ',statusbar=no,menubar=0,width=' + vWidth + ',height=' + vHeight + ',left=' + vleftPos + ',top=' + vtopPos);	
	
	//cancel href of link to stop natural link to popup
	return false;
}

function popupProduct( vURL, vWinName, vWidth, vHeight, vScrollbar) {
//vWinName cannot contain spaces, leave vScrollbar blank for no scrollba

	if (! window.focus)return true;
	
	var vhref;
	var vSWidth = screen.width
	var vSHeight = screen.height
	
	if(vScrollbar == "yes") {
		vWidth = new Number(vWidth) + 16
	}
	
	var vleftPos = (vSWidth / 2) - (vWidth / 2)
	var vtopPos = (vSHeight / 2) - (vHeight / 2)
	
	if (typeof(vURL) == 'string') {
		vhref = vURL;
	} else {
		vhref = vURL.href;
	}
	window.open(vhref, vWinName, 'scrollbars=' + vScrollbar + ',statusbar=no,menubar=0,width=' + vWidth + ',height=' + vHeight + ',left=' + vleftPos + ',top=' + vtopPos);	
	//cancel href of link to stop natural link to popup
	return false;
}

//faqs
addLoadEvent(faqClick);
function faqClick(){
	//loop through all question links in container and attach onclick to execute activateTip
	//pass question's id to function and append _a to get name of answer id
	if (!document.getElementById("questions")){ return false; }
	var QBox = document.getElementById("questions");
	var questionList = QBox.getElementsByTagName("a");
	if (questionList.length < 1) return false;
		for ( var i=0; i < questionList.length; i++){
			var thisq = i+1;
			//alert(which);
			questionList[i].onclick = function(){
				activateTip(this.id);
			return true;
			}
		}
}
//declare the last var and set an initial state (to stop errors)
var last = 'q1';
function activateTip(which){
	//remove any existing highlight
	toTheTop();
	//apply the new highlight
	document.getElementById(which+'_a').className='question on';
	//set the last tag so we know which to remove next time
	last = which;
}

function toTheTop(){
//reset the text color
	document.getElementById(last+'_a').className="question";
}

addLoadEvent(addListener);
function addListener() {
	
	//set zIndex on main image so can update it later
	//if (document.getElementById("ctl00_mainContent_aMainImage")) {
//	    var mainImg = document.getElementById("ctl00_mainContent_imgMain");
//			mainImg.style.zIndex = 1;
//	    mainImg.style.position = "relative";
//    	
	    var el = document.getElementById("ctl00_mainContent_ThumbnailContainer");
	    var subEls = el.getElementsByTagName("li");
//    	
	    for (var x = 0; x < subEls.length; x++) {
//    		
//		    //set zIndex of thumbs
//		    var thumbs = subEls[x].getElementsByTagName("img");
//		    thumbs[1].style.zIndex = -1;
    		
		    //add event handler		
		    if (subEls[x].attachEvent) {
			    subEls[x].attachEvent("onmouseover", imageOver);
			    subEls[x].attachEvent("onmouseout", imageOut);
		    } else {
			    subEls[x].addEventListener("mouseover", imageOver, false);
			    subEls[x].addEventListener("mouseout", imageOut, false);
		    }
	    }
//	}
}

var imageOver = function(e) { 

	if (e.target) {
		if (e.target.tagName == "A") {
		  var image = e.target.childNodes[3];
  	} else if (e.target.tagName == "IMG") {
			var image = e.target.parentNode.childNodes[3];
		}		
	} else if (e.srcElement) {
		if (e.srcElement.tagName == "A") {
		  var image = e.srcElement.childNodes[2];
  	} else if (e.srcElement.tagName == "IMG") {
			var image = e.srcElement.parentNode.childNodes[2];
		}
		//mainImg.style.zIndex = -1;
	}
	
	var mainImg = document.getElementById("ctl00_mainContent_imgMain");
	
	mainImg.style.display = "none";
	image.style.display = "block";
}
	
//change it back
var imageOut = function(e) {
	if (e.target) {
		if (e.target.tagName == "A") {
		  var image = e.target.childNodes[3];
  	} else if (e.target.tagName == "IMG") {
			var image = e.target.parentNode.childNodes[3];
		}		
	} else if (e.srcElement) {
		if (e.srcElement.tagName == "A") {
		  var image = e.srcElement.childNodes[2];
  	} else if (e.srcElement.tagName == "IMG") {
			var image = e.srcElement.parentNode.childNodes[2];
		}
		//mainImg.style.zIndex = 1;
	}
	
	var mainImg = document.getElementById("ctl00_mainContent_imgMain");
	mainImg.style.display = "block";
	image.style.display = "none";
}