var oActiveButton = '';
var Buttons = new Array();
var n00 = new Image(); n00.src = '/images/images/common/nav_0-0.gif';
var n01 = new Image(); n01.src = '/images/images/common/nav_0-1.gif';
var n10 = new Image(); n10.src = '/images/images/common/nav_1-0.gif';
var nav_left = new Image(); nav_left.src = '/images/images/common/nav_left.gif;'
var nav_left_on = new Image(); nav_left_on.src = '/images/images/common/nav_left_on.gif;'
var nav_right = new Image(); nav_right.src = '/images/images/common/nav_right.gif;'
var nav_right_on = new Image(); nav_right_on.src = '/images/images/common/nav_right_on.gif;'

var hideTimer = null;

function Button(id, seps) {
	this.id = id;
	this.img = document.getElementById(id);
	this.seps = [
		document.getElementById(seps[0]), 
		document.getElementById(seps[1])
	];
	
	this.setActiveState = function() {
		this.img.src = window['nav_' + this.id.toLowerCase() + '_on'].src;
		this.seps[0].src = (this.id == 'Home') ? nav_left_on.src : n01.src;
		this.seps[1].src = (this.id == 'Transportation') ? nav_right_on.src : n10.src;
	}
	
	this.restoreDefaultState = function() {
		this.img.src = window['nav_' + this.id.toLowerCase()].src;
		this.seps[0].src = (this.id == 'Home') ? nav_left.src : n00.src;
		this.seps[1].src = (this.id == 'Transportation') ? nav_right.src : n00.src;
	}
}

function delayed_MouseOut(id) {
	var oBtn = Buttons[id];
	if(!oBtn) return false;
	oBtn.restoreDefaultState();
	if(defaultElementExists()) {
		var oDefault = Buttons[oDefaultElement];
		oDefault.setActiveState();
	}
}

function init() {
	var oImages = document.getElementById('PrimaryNav').getElementsByTagName('IMG');
	var iCount = 0;
	for(var i=0; i<oImages.length; i++) {
		// skip the separator images.  only nav items will be nested in anchors, of course
		if(oImages[i].parentNode.nodeName != 'A') {
			continue;
		} else {
			// initialize button
			Buttons[oImages[i].id] = new Button(oImages[i].id, ['s' + iCount, 's' + (iCount +1) ]);
			
			sTempImg = 'nav_' + oImages[i].id.toLowerCase();
			// preload button images
			window[sTempImg] = new Image();
			window[sTempImg].src = '/images/images/common/' + sTempImg + '.gif';
			window[sTempImg + '_on'] = new Image();
			window[sTempImg + '_on'].src = '/images/images/common/' + sTempImg + '_on.gif';
			
			oImages[i].onmouseover = function() { 
				clearTimeout(hideTimer);
				
				if(!Buttons[this.id]) {
					return false;
				} else {
					var oBtn = Buttons[this.id];
				}
				if(defaultElementExists()) {
					var oDefault = Buttons[oDefaultElement];
					oDefault.restoreDefaultState();
				}
				if(oActiveButton != '') {
					var oActive = Buttons[oActiveButton];
					oActive.restoreDefaultState();
				}
				oBtn.setActiveState();
				oActiveButton = this.id;			
			}
			
			oImages[i].onmouseout = function() { 
				hideTimer = setTimeout("delayed_MouseOut('" + this.id + "')", 300);
			}
			
			iCount ++;
		}
	}

	if(defaultElementExists()) Buttons[oDefaultElement].setActiveState();
}

function defaultElementExists() {
	// oDefaultElement is defined in snippet: channels_primary_nav
	return (typeof oDefaultElement != 'undefined' &&
			oDefaultElement != '' &&
			oDefaultElement != '$channel');
}


function captureKeyEvents(e) {
	var intIsAltKey = 0;
	if(!e) e = window.event;
	
	if(e.which && e.which == 18) intIsAltKey = true;
	if(e.altKey) intIsAltKey = 18;
	
	if(intIsAltKey == 0 || e.keyCode == 18) return true;
	
	aLocations = new Array();
		aLocations[67] = '/';
		aLocations[68] = '/magazineindex';
		aLocations[69] = '/Bioengineering';
		aLocations[70] = '/Computing';
		aLocations[71] = '/Consumer';
		aLocations[72] = '/PowerEnergy';
		aLocations[73] = '/Semiconductor';
		aLocations[74] = '/Communications';
		aLocations[75] = '/Transportation';
	
	intLocation = intIsAltKey + e.keyCode;
	if(aLocations[intLocation] ) {
		top.location.href = aLocations[intLocation];
	}
}

addEvent(document, 'keydown', captureKeyEvents);
addEvent(window, 'load', init);



function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

addEvent(window, "unload", function() {
  if (spectrumFontStyle != getActiveStyleSheet()) {
    setCookie("SpectrumFontStyle", getActiveStyleSheet(), daysInFuture(365));
  }
});

var spectrumFontStyle = getCookie("SpectrumFontStyle");
setActiveStyleSheet(spectrumFontStyle ? spectrumFontStyle : getPreferredStyleSheet());


// written by Dean Edwards, 2005
// with input from Tino Zijdel

// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	// assign each event handler a unique ID
	if (!handler.$$guid) handler.$$guid = addEvent.guid++;
	// create a hash table of event types for the element
	if (!element.events) element.events = {};
	// create a hash table of event handlers for each element/event pair
	var handlers = element.events[type];
	if (!handlers) {
		handlers = element.events[type] = {};
		// store the existing event handler (if there is one)
		if (element["on" + type]) {
			handlers[0] = element["on" + type];
		}
	}
	// store the event handler in the hash table
	handlers[handler.$$guid] = handler;
	// assign a global event handler to do all the work
	element["on" + type] = handleEvent;
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	// delete the event handler from the hash table
	if (element.events && element.events[type]) {
		delete element.events[type][handler.$$guid];
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(window.event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};

//Added by Rob Moskal to get arguments off the request
function getURLParameters(paramName) 
{
    var sURL = window.document.URL.toString();
    
    if (sURL.indexOf("?") > 0)
    {
        var arrParams = sURL.split("?");
            
        var arrURLParams = arrParams[1].split("&");
 
        for (i=0;i<arrURLParams.length;i++)
        {
            var sParam =  arrURLParams[i].split("=");
            if (sParam[0]== paramName) {
                var result =  unescape(sParam[1]);
                rExp = /\+/g;
                return result.replace(rExp," ");
                
             }
        }
    }
}

function trimString(sStr, iLength) {
	return sStr.substring(0, iLength);
}

/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn() {
	var xmlhttp, bComplete = false;
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch (e) { try { xmlhttp = new XMLHttpRequest(); }
	catch (e) { xmlhttp = false; }}}
	if (!xmlhttp) return null;

	this.connect = function(sURL, sMethod, sVars, fnDone) {
		if (!xmlhttp) return false;
		bComplete = false;
		sMethod = sMethod.toUpperCase();
		
		try {
			if (sMethod == "GET") {
				xmlhttp.open(sMethod, sURL+"?"+sVars, true);
				sVars = "";
			} else {
				xmlhttp.open(sMethod, sURL, true);
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && !bComplete) {
					bComplete = true;
					fnDone(xmlhttp);
		        }
			};
			xmlhttp.send(sVars);
		}
		catch(z) { return false; }
		return true;
	};
	return this;
}
