// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0


function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.documentElement.scrollLeft;
    tempY = event.clientY + document.documentElement.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
}


function showAlertBox(thisObj){
	
	var titleText = thisObj.getAttribute('name');
	var alertBox = document.getElementById('alertTextBox');
	alertBox.style.display = "block";
	alertBox.innerHTML = titleText; 

	/* if (!e) { 
	var e = window.event;
	}
	if (thisObj.pageX || thisObj.pageY)
	{
		posx = thisObj.pageX;
		posy = thisObj.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	} */
	
	alertBox.style.top = (tempY + 10) + 'px';
	alertBox.style.left = (tempX - 110) + 'px';
	return false;	
}

function hideAlertBox(){
	var alertBox = document.getElementById('alertTextBox');
	alertBox.style.display = "none";
	alertBox.innerHTML = "";	
}

function initProducts(){
	
	hideItems();
	
	if(!document.getElementsByTagName) return false;
	var rows = document.getElementsByTagName("tr");
	var a = document.getElementsByTagName("a");
	
	for (var i=0; i<a.length; i++) {
		var aTitleName = a[i].getAttribute("title");
		if(aTitleName == "size") {
			a[i].className = 'close';
		}
	}
	for (var j=0; j<rows.length; j++) {
		var thisRow = rows[j].getAttribute("title");
		if (thisRow == "size") {
			rows[j].className = 'show';
		}
	}
}

function showHideItem(thisObj) {
	theObject = thisObj.getAttribute('title');
	var rows = document.getElementsByTagName("tr");
	for (var i=0; i<rows.length; i++) {         
		var thisRow = rows[i].getAttribute("title");
		if(thisRow == theObject) {
			if (rows[i].className == "hide") {
				rows[i].className = "show";
				thisObj.className = 'close';
			} else {
				rows[i].className = "hide";
				thisObj.className = 'open';
			}
		}
	}
	return false;
}


function hideItems() {
	if(!document.getElementsByTagName) return false;
	var rows = document.getElementsByTagName("tr");
	for (var i=0; i<rows.length; i++) {
		var thisRow = rows[i].getAttribute("title");
		if(!thisRow == "") {
			rows[i].className = 'hide';
		}
	}
	var a = document.getElementsByTagName("a");
	for (var i=0; i<a.length; i++) {
		var aClassName = a[i].className;
		//alert(aClassName);
		//alert(a[i].nodeName);
		if (aClassName == "close"){
			a[i].className = "open";
		}
	}
	return false;
}

function showItems() {
	if(!document.getElementsByTagName) return false;
	var rows = document.getElementsByTagName("tr");

	for (var i=0; i<rows.length; i++) {
		var thisRow = rows[i].getAttribute("title");
		if(!thisRow == "") {
			rows[i].className = 'show';
		}
	}
	var a = document.getElementsByTagName("a");
	for (var i=0; i<a.length; i++) {
		var aClassName = a[i].className;
		//alert(aClassName);
		//alert(a[i].nodeName);
		if (aClassName == "open"){
			a[i].className = "close";
		}
	}
	return false;
}

function showHideAllItems(thisObj) {
	if(!document.getElementsByTagName) return false;
	
	var theText = thisObj.firstChild.nodeValue;
	if(theText == 'Collapse All'){
		thisObj.firstChild.nodeValue = 'Expand All';
		thisObj.className = "openAllFeatures";
		hideItems();
	}
	else {
		thisObj.firstChild.nodeValue = 'Collapse All';
		thisObj.className = "closeAllFeatures";
		showItems();
	}
	return false;
}


function showHideCaseContent() {
	if(!document.getElementsByTagName) return false;
	
	var divs = document.getElementsByTagName("div");
	var a = document.getElementsByTagName("a");
	//hide all divs with product content
	for (var i=0; i<a.length; i++) {
		var divTitle;
		var aTitle = a[i].getAttribute("title");
		for (var j=0; j<divs.length; j++) {
			divTitle = divs[j].getAttribute("title");
			if(!divTitle == "") {
				if (aTitle == divTitle){
					divs[j].className = "";
				}
			}
		}
	}
}

var thisHref = "";

function initProductPage(){

	if(!document.getElementById) return false;	
	if(!document.getElementById('productContents')) return false;
	
	var navTable = document.getElementById("product-nav");
	var aLinks = navTable.getElementsByTagName("a");
	if(aLinks <= 0) return false;
	
	// Set onclick event for main links
	for(var i=0; i < aLinks.length; i++){
		thisTitle = aLinks[i].getAttribute("title");
		if(thisTitle != "Accessories") {
			aLinks[i].onclick = function(){
				return showContent(this);	
			}
		}
	}
	
	// Add 'class="selected"' to first link in product-nav table
	aLinks[0].className = "selected";
	
	// Hide all content except first page 
	var productContents = document.getElementById('productContents');
	var contentAreas = productContents.getElementsByTagName('div');
	for(i=1; i<contentAreas.length; i++) {
	contentAreas[i].style.display = "none";

	}
	
	//Set onClick event for page content links
	var overallContentArea = document.getElementById('productContents');
	var contentLinks = overallContentArea.getElementsByTagName("a");
	
	if(contentLinks <= 0) return false;
	
	// Set onclick event for main links
	for(var i=0; i < contentLinks.length; i++){
		
		// Get href value
		thisHref = contentLinks[i].getAttribute('href');
		if(!thisHref == "") {
			
			if(thisHref.indexOf("#") > -1) {
				
				contentLinks[i].onclick = function(){
					thisHref = this.getAttribute('href',2);
					thisHref = thisHref.replace("#","");
					//Loop through nav links and select one if relevant
					var navTable = document.getElementById("product-nav");
					var aLinks = navTable.getElementsByTagName("a");
					for(var i=0; i < aLinks.length; i++){
			
						if(aLinks[i].getAttribute("title") == thisHref) {
							return showContent(aLinks[i]);
						}
					}
				}
			}
		}
	}
}

function showContent(thisLink) {
	// Hide all content first
	var productContents = document.getElementById('productContents');
	var contentAreas = productContents.getElementsByTagName('div');
	for(i=0; i<contentAreas.length; i++) {
	contentAreas[i].style.display = "none";
	}
	
	// Deselect Links
	var navTable = document.getElementById("product-nav");
	var aLinks = navTable.getElementsByTagName("a");
	for(var i=0; i < aLinks.length; i++){
		aLinks[i].className = "";
	}
	
	thisLink.className = "selected";
    var href = thisLink.getAttribute('href');
    //var val = href.substr(1); // skip initial #
    var hash = href.search(/#/);
    var val = href.substr(hash+1); // AFTER hash
	//var val = thisLink.firstChild.nodeValue; // content of the a element
	var thisContent = document.getElementById(val); // element with that ID
//alert(val);
    if(thisContent) {
        thisContent.style.display = "block";
    }
	return false;
}


function addProductImage() {
	//Get Product Name
	var thisProductName = document.getElementById('productName').firstChild.nodeValue;
	//Get FlashFrame
	var flashFrame = document.getElementById('flashFrame');
	var thisImgSrc = "uploads/products/" + thisProductName + "NoFlash.jpg";
	//Create Image node
	
	var myDiv = document.createElement("div");
	var text = "<p>You need Flash Player 8 to watch our presentation, <a href=\"http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\">Click Here To Upgrade Your Flash Player</a></p><img src=\""+thisImgSrc+"\" />";
	
	myDiv.innerHTML = text;
	//myPara.appendChild(myImg);
	
	myDiv.style.background = "";
	//myDiv.style.background = thisImgSrc;
	
	myDiv.setAttribute('id','flashFrame');

	// get FlashFrame parent
	flashFrameParent = flashFrame.parentNode;

	//replace flashFrame with myImg
	flashFrameParent.replaceChild(myDiv,flashFrame);
	
}

// Expand and Collapse Tables

// set global variables
var thisTable = "";
var thisBody = "";
var arrRows = "";

var expandAllText = "Expand All";
var expandAllClass = "expandAll";
var hideAllText = "Hide All";
var hideAllClass = "hideAll";

function initExpandableTables(defaultOpen,pagename)
{
	if(!document.getElementById) return false;
	if(!document.getElementById("expandingTable")) return false;
	thisTable = document.getElementById("expandingTable");
	
	// Get and save current table class
	var savedTableClass = thisTable.className;
	
	//temporarily hide the table
	thisTable.setAttribute("class","hide");
	thisTable.className = "hide"; //For IE - stupid browser!
	
	// Do row manipulation
	thisBody = thisTable.tBodies[0];
	arrRows = thisBody.rows;
	
	// Create popup onclick function for all images
	if(pagename != 'tetra-portal'){
		setImagePopus();
	}
	
	var currentSection = "";
	//arrRows = thisBody.getElementsByTagName("tr");
	
	for(i=0; i < arrRows.length; i++){
		if(arrRows[i].getAttribute("id")){
			currentSection = arrRows[i].getAttribute("id");
			// Create "a" element
			var aTag = document.createElement("a");
			aTag.setAttribute("href","#");
			aTag.setAttribute("id",currentSection+"A");
			aTag.setAttribute("title",currentSection);
			aTag.setAttribute("class","expand");
			aTag.className = "expand"; //For IE - stupid browser!
			
			// Create "span" element
			var spanTag = document.createElement("span");
			spanTagText = document.createTextNode("Expand");
			spanTag.appendChild(spanTagText);
			
			spanTag.setAttribute("class","hide");
			spanTag.className = "hide"; //For IE - stupid browser!
			
			aTag.appendChild(spanTag);
			
			// Set onClick event
			aTag.onclick = function(){
				showHideChildren(this);
				return false;
			}
			
			
			// Place new "a" element as first child of first TD
			firstTdTag = arrRows[i].getElementsByTagName("td")[0];
			firstTdTag.insertBefore(aTag,firstTdTag.firstChild);
		}
		else {
			arrRows[i].setAttribute("title",currentSection);
			arrRows[i].setAttribute("class", "hide");
			arrRows[i].className = "hide"; //For IE - stupid browser!
		}
	}
	
	
	// If there is a default open value set then open it
	if(defaultOpen){
		currentSection = defaultOpen;
		for(i=0; i < arrRows.length; i++){
			var thisRowTitle = arrRows[i].getAttribute("title");
			if(thisRowTitle == currentSection) {
				arrRows[i].setAttribute("class","");
				arrRows[i].className = ""; //For IE - stupid browser!	
			}
		}
		headerTr = document.getElementById(defaultOpen+"A");
		headerTr.setAttribute("class","contract");
		headerTr.className = "contract"; //For IE - stupid browser!
	}
	
	
	// Create and Add Expand/Hide all link
	var aTagExpandAll = document.createElement("a");
	aTagExpandAll.setAttribute("href","#");
	aTagExpandAll.setAttribute("class",expandAllClass);
	aTagExpandAll.className = expandAllClass; //For IE - stupid browser!
	aTagExpandAllText = document.createTextNode(expandAllText);
	aTagExpandAll.appendChild(aTagExpandAllText);
	
	var aTagHideAll = document.createElement("a");
	aTagHideAll.setAttribute("href","#");
	aTagHideAll.setAttribute("class",hideAllClass);
	aTagHideAll.className = hideAllClass; //For IE - stupid browser!
	aTagHideAllText = document.createTextNode(hideAllText);
	aTagHideAll.appendChild(aTagHideAllText);
	
	//Set onclick events 
	aTagExpandAll.onclick = function(){
		showHideAll(this);
		return false;
	}
	aTagHideAll.onclick = function(){
		showHideAll(this);
		return false;
	}
	
	// Get table head row
	firstThTag = thisTable.tHead.rows[0].cells[0]; 

	// Place the new "a" elements in the first TH
	firstThTag.insertBefore(aTagExpandAll,firstThTag.firstChild);
	firstThTag.insertBefore(aTagHideAll,firstThTag.firstChild);
	
	//replace saved table class
	thisTable.setAttribute("class",savedTableClass);
	thisTable.className = savedTableClass; //For IE - stupid browser!
}




Event.observe(window, 'load', function (){
											findName();
									     });
	
function findName() {
	var HeadTag = document.getElementsByClassName('titleText');
	for(i=0; i < HeadTag.length; i++){
		HeadTag[i].onclick = function(){
							var theid = this.title;
							if(document.all){
								var theitem = $(theid).nextSibling;
							}else{
								var theitem = $(theid).nextSibling.nextSibling;
							}
							if(theitem.className=='hide'){
								theitem.className = '';
							}else{
								theitem.className = 'hide';
							}
							return false;
						
		}
	}		
}
		
		

function showHideChildren(thisA){

	currentSection = thisA.getAttribute('title');
	for (var i=0; i<arrRows.length; i++) {         
		var thisRowTitle = arrRows[i].getAttribute("title");
		if(thisRowTitle == currentSection) {
			if(arrRows[i].getAttribute("class") == "hide"){
				arrRows[i].setAttribute("class","");
				arrRows[i].className = ""; //For IE - stupid browser!
				thisA.setAttribute("class","contract");
				thisA.className = "contract"; //For IE - stupid browser!
			} else {
				arrRows[i].setAttribute("class","hide");
				arrRows[i].className = "hide"; //For IE - stupid browser!
				thisA.setAttribute("class","expand");
				thisA.className = "expand"; //For IE - stupid browser!
			}
		}
	}
}

function showHideAll(thisA){
	action = thisA.firstChild.nodeValue;
	
	for (var i=0; i<arrRows.length; i++) {
		if(arrRows[i].getAttribute("title")){
			if(action == expandAllText) {
				arrRows[i].setAttribute("class","");
				arrRows[i].className = ""; //For IE - stupid browser!
			} else {
				arrRows[i].setAttribute("class","hide");
				arrRows[i].className = "hide"; //For IE - stupid browser!	
			}				 
		} else {
			if(action == "Expand All"){
				arrRows[i].getElementsByTagName("a")[0].className = "contract";
			} else {
				arrRows[i].getElementsByTagName("a")[0].className = "expand";
			}
		}
	}
}

// set fucntion for image pop ups
function setImagePopus() {
	allAs = thisBody.getElementsByTagName("a");
	for (var i=0; i<allAs.length; i++) {
		allAs[i].onclick = function(){
			if(this.getAttribute('href').indexOf("mailto")>=0){
				//enlargeImage(this, true, 800, 600);
			}
			else if(this.getAttribute('href').indexOf("products-detail.php")>=0){
				enlargeImage(this, true, false, 600, 800);
				return false;
			}
			else{
				enlargeImage(this, false, true, 700, 600);
				return false;
			}
			
		}
	}
}


// enlarge images in new window
function enlargeImage(thisObj, resizable, scrollbars, width, height){
	//Get Image 
	var myImageSrc = thisObj.getAttribute('href');
	stringResizable = (resizable==true)?",resizable=1":",resizable=no";
	//stringScrollbars = ",scrollbars=1";
	stringScrollbars = (scrollbars=true)?",scrollbars=none":"";
	stringWidth = (width>0)?",width="+width:",width=600";
	stringHeight = (height>0)?",height="+height:",width=600";
	stringSize = (width!="" && height!="")?stringWidth+stringHeight:"";
	//alert(">>"+stringScrollbars);
	window.open(myImageSrc,"myWindow",stringSize+stringResizable+stringScrollbars);
}

//Scenario Highlight 
function showHideHighlight (thisAreaTag) {
	caseImage = document.getElementById('caseTransImage');
	caseImageBackground = caseImage.style.backgroundImage;
	coords = thisAreaTag.getAttribute("coords");
	count = 0;
	if(!caseImageBackground || caseImageBackground == "") {
		
	    var coords_array=coords.split(",");
		xCoord = coords_array[0];
		yCoord = coords_array[1];
		offset = coords_array[2];
		
		xCoord = (xCoord - offset);
		yCoord = (yCoord - offset);

		caseImage.style.backgroundPosition = xCoord + "px " + yCoord + "px";
		caseImage.style.backgroundRepeat = "no-repeat";
		caseImage.style.backgroundImage = "url(uploads/products/scenario_highlight.gif)";
		
	} else {
	caseImage.style.backgroundImage = "";
	}
}

