

// Image preloader and mouseovers
var W3CDOM = (document.createElement && document.getElementsByTagName);

// catch any errors so they don't annoy the user
//window.onerror = function () {return true;};

/* stop IE6 flicker */
try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}

/* for Mozilla/Opera 9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer (do not delete comments, they are part of this IE code) */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			init(); // call the onload handler
		}
	};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			init(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
window.onload = init;

var mouseOvers = new Array();
var mouseOuts = new Array();


function init() {
	if (!W3CDOM) return;
	
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;

	// kill the Safari timer
	if (_timer) {
		clearInterval(_timer);
		_timer = null;
	}
	
	//alert("init");
	
	var nav = document.getElementById('mainbuttons2');
	
	var imgs = nav.getElementsByTagName('img');

	
 // Find the first subnav li's and remove their border
	var subnavcollection = new Array(); 
	function getElementbyClass(classname){ 
 		var inc=0; 
 		var alltags=document.getElementsByTagName("*"); 
 		for (i=0; i<alltags.length; i++){ 
   		if (alltags[i].className==classname) 
     	subnavcollection[inc++]=alltags[i]; 
 		} 
	}
	getElementbyClass('subnav');
	for (var i=0;i<subnavcollection.length;i++) {
		// see if the subnav's parent link is set to "active", if so then make this ul active as well
		if (subnavcollection[i].parentNode.getElementsByTagName('A')[0].className == "active") {
			subnavcollection[i].className += " active";
		}
		// remove border on the first subnav li
		var thelis = subnavcollection[i].getElementsByTagName('Li');
		thelis[0].className += " firstsublink";
		for (j=0; j<thelis.length; j++){ 
			// If we find a link inside this subnav with a class of "active", then put a class of active on the subnav too and it's parent (the main link)
			var theas = thelis[j].getElementsByTagName('A');
			for (k=0; k<theas.length; k++) {
				if (theas[k].className == "active") {
					subnavcollection[i].className += " active";
					// if there is already an active class
					if (subnavcollection[i].parentNode.getElementsByTagName('A')[0].className != "active") {
						subnavcollection[i].parentNode.getElementsByTagName('A')[0].className = "active";
					}					
				}
			}
 		}
	}
	
	
	
	// Add mouseover events to our navigation images
	for (var i=0;i<imgs.length;i++) {
		// If the image has a class of blank, skip it
		if (imgs[i].className == "blank") continue;
		// Give the mouseovers to the li tag that holds the image. Ex: <li><a><img></a></li>
		imgs[i].parentNode.parentNode.onmouseover = mouseGoesOver;
		imgs[i].parentNode.parentNode.onmouseout = mouseGoesOut;
		// While we're here, find the sublinks and then give a certain class to the very last sublink (to remove the right border)
		var sublinks = imgs[i].parentNode.parentNode.getElementsByTagName('a');
		if (sublinks.length > 0) { 
			if (sublinks[sublinks.length-1].className == "active") {
				sublinks[sublinks.length-1].className += " lastsublinkactive";
				sublinks[sublinks.length-1].parentNode.className += " lastsublinkactive";
			} else {
				sublinks[sublinks.length-1].className += " lastsublink";
				sublinks[sublinks.length-1].parentNode.className += " lastsublink";
				if (sublinks[sublinks.length-1].parentNode.parentNode.className=="tertiary_nav") {
					sublinks[sublinks.length-1].parentNode.parentNode.parentNode.className += " lastsublink";
				}
			}
		};
		// Load our arrays with the new images we'll use for our rollovers
		var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
		mouseOuts[i] = new Image();
		mouseOuts[i].src = imgs[i].src;
		mouseOvers[i] = new Image();
		mouseOvers[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + "_on" + suffix;
		imgs[i].parentNode.parentNode.number = i;
		// If the image parent (surrounding A tag) has a class of "active" then show the active state at all times
		if (imgs[i].parentNode.className == "active") { 
			var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
			imgs[i].src = mouseOuts[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + "_on" + suffix;
		}
	}
	
	
	// Add rollover for tertiary nav
	function addTertiaryNavRollovers() { 
		var classname = "tertiary_nav";
 		var inc=0; 
 		var alltags=document.getElementsByTagName("*"); 
		var tertiaryLinks = "";
 		for (i=0; i<alltags.length; i++){ 
   		if (alltags[i].className==classname) {
				alltags[i].parentNode.onmouseover = function () {
					var divs = this.getElementsByTagName('div');
					for (var j = 0; j<divs.length; j++) {
						divs[j].style.display = 'block';
					}
					if (this.childNodes[0].className != "active") {
						this.childNodes[0].className = "activetemp";
						if (this.className.length > 0) {
							this.className += " activetempli";
						} else {
							this.className = "activetempli";
						}
					} else {
						this.className = "activeli";
					}
				}
				alltags[i].parentNode.onmouseout = function () {
					var divs = this.getElementsByTagName('div');
					for (var j = 0; j<divs.length; j++) {
						divs[j].style.display = 'none';
					}
					if (this.childNodes[0].className.indexOf('activetemp') >= 0) {
						this.childNodes[0].className = this.childNodes[0].className.replace('activetemp', '');
						this.className = this.className.replace('activetempli', '');
					} else if (this.childNodes[0].className.indexOf('active') >= 0) {
						this.className = this.className.replace('activeli', '');
					}
				}
				tertiaryLinks = alltags[i].childNodes[0].getElementsByTagName("*");
				alltags[i].childNodes[0].childNodes[tertiaryLinks.length-1].className = "last";
			}     		
 		} 
	}
	addTertiaryNavRollovers();
	
	
	// Add rollover for any other button
	function addRollover (daID) {
		if (document.getElementById(daID)) {
			var custconn = document.getElementById(daID);
			// If the image already ends with "_on.gif" we assume the button does not need a rollover state
			if ((custconn.src.substring(custconn.src.lastIndexOf('.')-3,custconn.src.lastIndexOf('.'))) == "_on") {
				return;
			}
			var custconnover = new Image();
			var custconnout = new Image();
			// Add to our new image the rollover version of the image (the image name plus "_on")
			custconnover.src = custconn.src.substring(0,custconn.src.lastIndexOf('.')) + "_on.gif";
			custconnout.src = custconn.src;
			custconn.onmouseover = function() {
				this.src = custconnover.src;
			}
			custconn.onmouseout = function() {
				this.src = custconnout.src;
			}
		}
	}
	
	// Add rollovers
	addRollover('customerconnection');
	addRollover('viewproductdemo');
	addRollover('performanceondemand');
	addRollover('uclSearch_btnSearch');
	addRollover('silverbutton');
	addRollover('nav2whitepapers');
	addRollover('nav2webinars');
	addRollover('nav2casestudies');
	addRollover('nav2videos');
	addRollover('nav2datasheets');
	addRollover('blue');
	addRollover('red');
	
	
	// Add pop-up window functionality to all links with the class of "popup"
	var links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		if (links[i].getAttribute("class") == "popup") {
			links[i].onclick = function() {
				window.open(this.getAttribute("href"),"popup","width=452,height=294");
				return false;
			}
		}
	}
	
	// Move the welcome title into position
	var username = document.getElementById("h3username");
	var welcomebox = document.getElementById("welcomebox");
	var nestedbox = document.getElementById("firstnestedbox");
	if (username && welcomebox && nestedbox) {
		username.style.display = "block";
		welcomebox.insertBefore(username,nestedbox);
	}
	

}

// Preload some other images
var star1 = new Image();
star1.src = '/images/layout/img/stars_on.gif';
var star2 = new Image();
star2.src = '/images/layout/img/stars2_on.gif';
var star3 = new Image();
star3.src = '/images/layout/img/stars3_on.gif';
var subnavbackgrounds1 = new Image();
subnavbackgrounds1.src = '/images/layout/img/background_subnavigation_left.gif';
var subnavbackgrounds2 = new Image();
subnavbackgrounds2.src = '/images/layout/img/background_subnavigation_left2.gif';
var subnavbackgrounds3 = new Image();
subnavbackgrounds3.src = '/images/layout/img/background_subnavigation_right.gif';
var subnavbackgrounds4 = new Image();
subnavbackgrounds4.src = '/images/layout/img/background_navigation_separator.gif';
var yellowboxarrow = new Image();
yellowboxarrow.src = '/images/layout/img2/bg_yellowboxlink.gif';
var headingarrow = new Image();
headingarrow.src = '/images/layout/img2/bg_arrow_on.gif';
var boxedlink = new Image();
boxedlink.src = '/images/layout/img2/bg_boxedarrow_on.gif';
var red = new Image();
red.src = '/images/layout/img4/red_on.gif';
var blue = new Image();
blue.src = '/images/layout/img4/blue_on.gif';



// variables for our mouseouts and mouseactions
var mouseAction;
var mouseActionOut;
var rolloverDelay = 50;

// Mouseover function
function mouseGoesOver() {
	var nav = document.getElementById('mainbuttons2');
	var lis = nav.getElementsByTagName('li');
	for (var i = 0; i<lis.length; i++) {
		if (lis[i]==this) var danumber = i;
	}
	if (lis[danumber].className.indexOf('over') < 0) {
		mouseAction = setTimeout('mouseGoesOverAction("' + danumber + '")',rolloverDelay);
	} else {
		clearTimeout(mouseAction);
		clearTimeout(mouseActionOut);
	}
}
function mouseGoesOverAction(danumber) {
	var nav = document.getElementById('mainbuttons2');
	var lis = nav.getElementsByTagName('li');
	var dali = lis[danumber];
	var daimage = dali.getElementsByTagName('img')[0];
	//alert('yes');
	daimage.src = mouseOvers[dali.number].src;
	dali.className += " firefoxfix";
	dali.className += " over";
}

// Mouseout function
function mouseGoesOut() {
	var nav = document.getElementById('mainbuttons2');
	var lis = nav.getElementsByTagName('li');
	for (var i = 0; i<lis.length; i++) {
		if (lis[i]==this) var danumber = i;
	}
	clearTimeout(mouseAction);
	clearTimeout(mouseActionOut);
	mouseActionOut = setTimeout('mouseGoesOutAction()',rolloverDelay);
}
function mouseGoesOutAction() {
	var nav = document.getElementById('mainbuttons2');
	var liimages = nav.getElementsByTagName('img');
	for (var i = 0; i<liimages.length; i++) {
		liimages[i].src = mouseOuts[liimages[i].parentNode.parentNode.number].src;
		liimages[i].parentNode.parentNode.className = liimages[i].parentNode.parentNode.className.replace(" over", "");
	}
}

