var ie  = document.all ? true:false;

function popwin(url, width, height){
	var _width = screen.availWidth; 
	var _height = screen.availHeight; 
	var centered_width = 0;
	var centered_height = 0;	
	if (_width > width) centered_width = (_width - width) / 2;
	if (_height > height) centered_height = (_height - height) / 2;
	viewer = window.open(url,"viewer","top=" + centered_height + ",left=" + centered_width + ",status=yes,scrollbars=yes,menubar=no,resizable=yes,width=" + width + ",height=" + height);
	viewer.focus();
}

function pw(a, width, height) {
	//var a = (ie) ? window.event.srcElement : window.event.target;
	popwin(a.href, width, height);
	return false;
}

function mimicAnchorHover(elem){dynamicUI.mimicAnchorHover(elem)}
function mimicAnchorHoverOff(elem) {dynamicUI.mimicAnchorHoverOff}

window.DOMHelper = {
	removeChildNodes : function (element) {
		if(element.childNodes.length > 0) {
			var currentChild = element.childNodes.item(0);
			var nextChild;
			while(currentChild){
				nextChild = currentChild.nextSibling;
				element.removeChild(currentChild);
				currentChild = nextChild;
			}
		}
	},
	setText : function(elem, text){
		DOMHelper.removeChildNodes(elem);
		elem.appendChild(document.createTextNode(text));
	}
}

window.dynamicUI = {
	mimicAnchorHover : function(elem) {
		elem.style.textDecoration = "underline";
	},
	
	mimicAnchorHoverOff : function(elem) {
		elem.style.textDecoration = "none";
	},
	
	toggle : function(id) {
		var container = document.getElementById(id);
		if(container.style.display == "none"){
			container.style.display = "";
			return true;
		}
		else {
			container.style.display = "none";
			return false;
		}
	},
	
	hide : function(id) {
		var container = document.getElementById(id);
		container.style.display = "none";
		return false;
	},
	
	show : function(id) {
		var container = document.getElementById(id);
		container.style.display = "";
		return false;
	},
	
	resizeFrame : function(id) {
		try {
			var calcElem = document.getElementById(id);
			var calcFrame = window.frames[0].document;
			var calcFrameHeight = calcFrame.body.scrollHeight + 30;
			var sty = (calcElem.style) ? calcElem.style : calcElem;
			sty.height = calcFrameHeight + "px";
		}
		catch(e) {}
	},
	
	findPos : function (obj) { /* returns a coordinate of where an element exists on screen*/
		var curleft = 0;
		var curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
}

window.mathExtention = {

	/* for XSLT encoding problems */
	
	isGt : function(val1, val2) {
		return (val1 > val2);
	},

	isLt : function(val1, val2) {
		return (val1 < val2);
	},

	isNumeric : function(val) {
		var nums = "01234567890";
		for (var i = 0; i < val.length; i++) {
			if (nums.indexOf(val.charAt(i), 0) == -1){return false;}
			else {return true;}
		}
	}
}

window.tabControl = {

	hideAll : function() {
		var aElem = document.getElementById("infopods");
		var aDivs = aElem.getElementsByTagName("div");
		var bmk = stateManager.getCurrentBookmark();
		var s = (/^tabs=section[0-9]$/.test(bmk)) ? 0 : 1;
		for (i = s; i < aDivs.length; i++){
			if ((s == 1 || (s == 0 && ("tabs=" + aDivs[i].id) != bmk)) && (aDivs[i].className == "infopod" || aDivs[i].className == "infopod jsshow")){	
				aDivs[i].className = "infopod jshide";
			}
		}
		
		if(s == 0){
			var tab;
			var tabId = bmk.substring(5) + "link";
			tab = document.getElementById("section1link");
			tab.className = "off";
			tab = document.getElementById(tabId); 
			tab.className = "on";
			//this.trackEvent(bmk);	
		}
	},
	
	show : function(sId) {
		var aElem = document.getElementById("infopods");
		var aDivs = aElem.getElementsByTagName("div");
		for (i=0; i<aDivs.length; i++){
			if (aDivs[i].className == "infopod" || aDivs[i].className == "infopod jsshow"){
				aDivs[i].className = "infopod jshide";
			}
			if (aDivs[i].id == sId){
				aDivs[i].className = "infopod jsshow";
			}
		}
		var aElem = document.getElementById("infopods_nav");
		var tabs = aElem.getElementsByTagName("a");
		for (i=0; i < tabs.length; i++){
			var sLink = tabs[i].id;
			if(sLink == sId + "link"){
				tabs[i].className = "on";
			}
			else {
				tabs[i].className = "off";
			}
		}
		window.scroll(0, 0); //back to top
		this.trackEvent(sId);
	},
	
	trackEvent : function(id) {
		try {
			var pn = (productType != "") ? id + "/" + productType : id;
			_hbPageView(pn, hbx.mlc);
		}
		catch(e){}
	},
	
	currentTab : "section1"
}

window.stateManager = {
		
	createCookie : function(name,value,days) {
		var expires;
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = ";expires=" + date.toGMTString();
		}
		else {
			expires = "";
		}
		document.cookie = name+"="+value+expires+"; path=/";
	},

	readCookie : function (name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	eraseCookie : function(name) {
		createCookie(name,"",-1);
	},
	
	getCurrentBookmark : function() {
		var str_url = new String(document.location.href);
		var ar = str_url.split(/#/);
		var bmrk = (ar.length > 1) ? ar[1] : null;
		return bmrk;
	}
}

window.snapManager = {

	"snapThreshold" : 880,
	"currentWidth" : 0,
	"clientWidth" : null,
	"currentMode" : null,
	
	initialise : function() {
		
		if (!(ie && screen.width > this.snapThreshold)) {
			this.currentWidth = window.innerWidth;
			if (this.currentWidth < this.snapThreshold) {
				if (document.getElementsByTagName && document.getElementsByTagName("link")[2]) document.getElementsByTagName("link")[2].disabled = true;
			}
		}
		
		this.clientWidth = stateManager.readCookie("screenWidth");

		if(this.clientWidth != null) {
			if(this.clientWidth < this.snapThreshold) {
				this.disableCSS(false);
			} else {
				this.disableCSS(true);
			}
		}
	},
	
	disableCSS : function(par){
		var i;
		var a;

		for(i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
				if(a.getAttribute("title") == "alternate") a.disabled = par;
			}
		}
	},
	
	eventResize : function(){
		this.currentWidth = (!ie) ? window.innerWidth + 4 : document.body.offsetWidth;
		
		if (this.currentWidth < this.snapThreshold){
			this.disableCSS(false);
			if(this.currentMode != "min") stateManager.createCookie("screenWidth", this.currentWidth, 1);
			this.currentMode = "min";
		} else {
			this.disableCSS(true);
			if(this.currentMode != "max") stateManager.createCookie("screenWidth", this.currentWidth, 1);
			this.currentMode = "max";
		}
	}
}

snapManager.initialise();

function initialise() {
	snapManager.eventResize();
	if(ie) {parseStylesheets();}
}

function eventResize() {snapManager.eventResize();}
function showInfopod(id) {YAHOO.util.History.navigate("tabs", id);}

function showTab() {
	var id = YAHOO.util.History.getCurrentState("tabs");
	tabControl.show(id);
}

/*** toggle visibility ***/


function toggleVisibility(theTag, theClass, theAction) {

	var tTag=theTag;
	var tClass=theClass;
	var tAction= theAction;
	var aElem = document.getElementsByTagName(tTag);
	
	for (i=0; i<=aElem.length;i++) 	{
		if (aElem[i].className==tClass)	{
			aElem[i].style.visibility=tAction;
		}
	}
 }
 
 function createAtlasTag(id) {
 	document.write("<s" + "cript language=\"JavaScript\" src=\"http://switch.atdmt.com/jaction/" + id + "\"></s" + "cript>");
 }

window.onload = initialise;
window.onresize = eventResize;