// for site specific functions. either grab js from code-library.js or write your own.

/* =================================================================== */
//Browser check - better to check for method availability than browsers
var IS_DOM = (document.getElementById) ? true : false;
var IS_IE = (document.all) ? true : false;
var IS_IE50 = (navigator.userAgent.indexOf("IE 5.0") != -1);
var IS_Mac = (navigator.appVersion.indexOf("Mac") != -1);
/* =================================================================== */

/* =================================================================== */
// add a class of "js" to the body tag, to allow for css that gets
// implemented only when js is available
// dependencies: cssjs(), addEvent()
// could change this to the html tag as well pretty easy
function addJSClass () {
	var oBody = document.getElementsByTagName('body')[0];
	if (!oBody) { return; }
	cssjs('add',oBody,'js');
}

/* =================================================================== */

/* =================================================================== */
// function to add/remove classes from elements
// written by Christian Heilmann 
// more info here http://www.onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
// call like cssjs('add',containerOBJ,classname);
function cssjs(a,o,c1,c2){
	switch (a){
		case 'swap':
			o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
		break;
		case 'add':
			if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
		break;
		case 'remove':
			var rep=o.className.match(' '+c1)?' '+c1:c1;
			o.className=o.className.replace(rep,'');
		break;
		case 'check':
			return new RegExp("(^|\\s)" + c1 + "(\\s|$)").test(o.className);
		break;
	}
}

/* =================================================================== */

/* =================================================================== */
// function to add events crossbrowser
// from: http://www.dustindiaz.com/rock-solid-addevent/
// needs to be higher in the code than any calls to it.
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

// use only with addEvent function
// this is here to clear the event cache to prevent memory leaks
// for more info:  http://novemberborn.net/javascript/event-cache
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);
/* =================================================================== */

/* =================================================================== */
// various link functionality - popups, external, onstate
// original script taken from Jeremy Keith
// dependencies: cssjs(), addEvent()
var anchors = {
	a : Object,
	doPopups : function() {
		if (!document.getElementsByTagName) { return false; }
		var links = document.getElementsByTagName("a");
		for (var i=0; i < links.length; i++) {
			var anchor = links[i];
			if (anchor.getAttribute('href') && anchor.rel.match('external')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"");
				};
				cssjs('add',anchor,'external');
			}
		    if (cssjs('check',anchor,'popup')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"toolbar=no");
				};
		    }
			if (cssjs('check',anchor,'popupPrintSmall')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"toolbar=no,width=750,height=600");
				};
		    }
		    if (cssjs('check',anchor,'popupPrint')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"toolbar=no,width=1050,height=825");
				};
		    }
			if (cssjs('check',anchor,'popupPrintLarge')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"toolbar=no,width=1275,height=1175,scrollbars=yes");
				};
		    }
		    if (cssjs('check',anchor,'popupFull')) {
				anchor.onclick = function() {
					return anchors.openWin(this,"");
				};
		    }
			if (anchor.href == location.href) {
				cssjs('add',anchor,'onstate');
			}
		}
	},
	openWin : function(o,params) {
		window.open(o.href, "newwin","" + params + "");
		return false;
	}
};
addEvent(window,'load',anchors.doPopups);
/* =================================================================== */

function switchApplication(strToggleState, strID) {

	aryIcons = new Array("","graphics","electronics","displays","life-sciences","chemical","3d-mechanical","optical","photovoltaic");

	for (i=1;i<aryIcons.length;i++) {
		eval("IconAPP"+i+"On = new Image();");
		eval("IconAPP"+i+"Off = new Image();");
		eval("ThumbAPP"+i+"On = new Image();");
		eval("IconAPP"+i+"On.src = \"../img/misc/icon_apps_"+aryIcons[i]+"_r.gif\";");
		eval("IconAPP"+i+"Off.src = \"../img/misc/icon_apps_"+aryIcons[i]+"_0.gif\";");
		eval("ThumbAPP"+i+"On.src = \"../img/misc/thumb_apps_"+aryIcons[i]+".jpg\";");
		eval("document.getElementById(\"IconAPP"+i+"\").src = IconAPP"+i+"Off.src;");
		eval("document.getElementById(\"textAPP"+i+"\").style.display = \"none\";"); 
	}

	eval("document.getElementById(\"text"+strID+"\").style.display = \"block\";");
	eval("document.getElementById(\"Thumbnail\").src = Thumb"+strID+"On.src;");
	eval("document.getElementById(\"Icon"+strID+"\").src = Icon"+strID+"On.src;");

}

function preloadConstantsNavigation() {

	aryButtons = new Array("", "home","ecommerce","careers","contact-us");

	for (i=1;i<aryButtons.length;i++) {
		eval("B" + i + "Off = new Image();");
		eval("B" + i + "On = new Image();");
		eval("B" + i + "Off.src = \"" + strImageLocation + "/navigation/cnst_" + aryButtons[i] + "_0.gif\"");
		eval("B" + i + "On.src = \"" + strImageLocation + "/navigation/cnst_" + aryButtons[i] + "_r.gif\"");
	}

}

function imgOn(strImageName) {
	eval("document." + strImageName + ".src = " + strImageName + "On.src;");
}

function imgOff(strImageName) {
	if (document.getElementById(strImageName).attributes["buttonState"].value == "0") {
	eval("document." + strImageName + ".src = " + strImageName + "Off.src;");
	}
}