/* js functions */

// check for macIE
var browser = navigator.userAgent.toLowerCase();
var isMacIE = false;
if (browser.indexOf('mac') != -1 && browser.indexOf('msie') != -1){
	var isMacIE = true;
}


// preload rollovers
function preload(){
// rollover images
	var imgArray = new Array(
		'images/home_main_equipment.jpg',
		'images/home_main_device.jpg',
		'images/home_main_service.jpg',
		'images/n1_home_on.gif',
		'images/n1_overview_on.gif',
		'images/n1_services_on.gif',
		'images/n1_products_on.gif',
		'images/n1_company_on.gif',
		'images/n1_careers_on.gif',
		'images/n1_contact_on.gif',
		'http://www.aricent.com/images/n2_arrow_up.gif',
		'http://www.aricent.com/images/n2_arrow_dn.gif',
		'images/tmlogo_cisco_on.gif',
		'images/tm_cisco.gif',
		'images/tmlogo_sprint_on.gif',
		'images/tm_sprint.gif',
		'images/tmlogo_nokia_on.gif',
		'images/tm_nokia.gif',
		'images/tmlogo_samsung_on.gif',
		'images/tm_samsung.gif',
		'images/btn_closewin_on.gif'
	);
	//cache the images
	loadedImgs = [];
	for(i=0; i<imgArray.length; i++){
		loadedImgs[i] = new Image;
		loadedImgs[i].src = imgArray[i];
		//alert(loadedImgs[i].src);
	}
}

// The right way to add load events. By Simon Willison
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// rollovers
function roll(img,state,ext){
	document.images[img].src = state ? "/images/"+img+"_"+ext+".gif" : "/images/"+img+"_off.gif";
}

// function to open external links in new window
function openInNewWin() {
	var newWindow = window.open(this.getAttribute('href'), 'newWindow');
	newWindow.focus();
	return false;
}

// function to set external links opening option
function setExtLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "ext"){
			newImg = document.createElement('img');
			newImg.setAttribute('src','images/arrow_ext.gif');
			newImg.setAttribute('alt',' [external link]');
			newImg.setAttribute('title','external link');
			newImg.setAttribute('height','10');
			newImg.setAttribute('width','14');
			newImg.setAttribute('class','extImg');
			newImg.setAttribute('className','extImg');
			anchor.appendChild(newImg);
			anchor.onclick = openInNewWin;
		}
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "pdf"){
			newImg = document.createElement('img');
			newImg.setAttribute('src','images/icon_pdf.gif');
			newImg.setAttribute('alt',' [pdf link]');
			newImg.setAttribute('title','pdf link');
			newImg.setAttribute('height','16');
			newImg.setAttribute('width','15');
			newImg.setAttribute('class','dloadImg');
			newImg.setAttribute('className','dloadImg');
			anchor.appendChild(newImg);
			anchor.onclick = openInNewWin;
		}
		
		//moved to combine all scripts gary s.
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "pop"){
			anchor.onclick = openPopWin;
		}
	}
}

//trigger for openPopWin
//pops a 760 x 600 window
// function to set external links opening option
function setPopWin() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "pop"){
			anchor.onclick = openPopWin;
		}
	}
}

// gary added duplicate for pdf icon
// if he could write javascript he probably could make just 1 function

// take off the open in new window ???
// function to set pdf links opening option
function setPdfLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "pdf"){
			newImg = document.createElement('img');
			newImg.setAttribute('src','images/icon_pdf.gif');
			newImg.setAttribute('alt',' [download pdf]');
			newImg.setAttribute('title','download pdf');
			newImg.setAttribute('height','16');
			newImg.setAttribute('width','15');
			newImg.setAttribute('border','5');
			newImg.setAttribute('class','pdflink');
			anchor.appendChild(newImg);
			anchor.onclick = openInNewWin;
		}
	}
}




// require time elapse before rollover/fader works
var rollTime = 0;
function startRollTimer(){
	setTimeout('setRollTimer()', 500);
}
function setRollTimer(){
	rollTime = 1;
}

// function to do rollover/fade on homepage
function showLayer(theDiv){
	if (rollTime < 1) return;
	if (!document.getElementById) return;
	var topImg = document.getElementById('foreground');
	var btmImg = document.getElementById('background');
	var overlay = document.getElementById('overlay');
	
	// if not macIE, do fade steps...
	if (!isMacIE){
	
		if (fader != ""){
			fader.haltFade();
		}
		// move visible image up to foreground if not already
		topImg.style.backgroundImage = 'url(/images/home_main_'+topLayer+'.jpg)'
		
		// increment z-index of foreground, background, and overlay divs
		divIndex++;
		topImg.style.zIndex = divIndex+1;
		btmImg.style.zIndex = divIndex-1;
		olayIndex++;
		overlay.style.zIndex = olayIndex;
		
		// set new image as bg of background layer
		btmImg.style.backgroundImage = 'url(/images/home_main_'+theDiv+'.jpg)';
		
		// instantiate foreground div as new fade object & then fadeout
		fader = new Fadomatic(topImg, 10, 100);
		fader.fadeOut();
		
		// set title attribute
		overlay.setAttribute('title',titles[theDiv]);
		
		// reassign toplayer
		topLayer = theDiv;
	
	// else if macIE, do simple rollover...
	} else {
		topImg.style.backgroundImage = 'url(/images/home_main_'+theDiv+'.jpg)'
	}
	
	// hide default callout first time through
	var callout = document.getElementById("home-callout");
	callout.style.display = "none";
}

/* 
// function to set background rollovers
function setBg(div,map){
	if (map) div = document.getElementById(div);
	var imgName = div.getAttribute("id");
	var mainDiv = document.getElementById("home-main");
	mainDiv.style.backgroundImage = "url(/images/home_main_"+imgName+".jpg)";
	var callout = document.getElementById("home-callout");
	callout.style.display = "none";
}
	
// function to prepare rollovers
function prepRolls(){
	var segments = new Array("equipment","device","service");
	for (i=0; i<segments.length; i++){
		if (!document.getElementById || !document.getElementById(segments[i])) return false;
		rollDiv = document.getElementById(segments[i]);
		rollDiv.onmouseover = function() {
			setBg(this);
		}
	}
}
*/

// function to add class dynamically
function addClass(element,value){
	if (!element.className){
		element.className = value;
	} else {
		newClassName  = element.className;
		newClassName += " ";
		newClassName += value;
		element.className = newClassName;
	}
}

// function to add state class dynamically
// by slicing off any previously added state classes
function addStateClass(element,cName,value,state){
	if (!element.className){
		element.className = value;
	} else {
		if (state == "updown"){
			classArr = element.className.split(" ");
			newClassName = classArr[0].replace(/-up|-dn/gi,""); 
		} else if (state = "hover"){
			newClassName = element.className.replace(/\sieon|\sieoff/gi,"");
			newClassName += " ";
		}
		newClassName += value;
		element.className = newClassName;
		//alert(newClassName);
	}
}

// get current style properties
function getStyle(el,styleProp){
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

// function to add onclick handler to list items
function prepLists(listHeadClass,subListClass,hov,all){
	if (!document.getElementById) return;
	headEls = document.getElementsByTagName("h5");
	for (i=0;i<headEls.length;i++){
		if (headEls[i].className && headEls[i].className.indexOf(listHeadClass) != -1){
			elRef = headEls[i];
			elRef.onclick = function() {
				showHideList(this,subListClass);
			}
			// set iehover states if called for
			if (document.all && hov > 0){
				elRef.onmouseover = function () {
					setBgColor(this,1);
				}
				elRef.onmouseout = function () {
					setBgColor(this,0);
				}
			}
			// for javascript enabled users, collapse all sublists to start
			// noscript folks see all open as defined in css
			list = getSubList(elRef,subListClass);
			if (list != null){
				list.style.display = "none";
			}
		}
	}
	// setup expand/collapse all buttons if 'all' param is true
	if (all < 1) return;
	// if both expander and collapser exist
	if (document.getElementById("expander") && document.getElementById("collapse")){
		xBtn = document.getElementById("expander");
		cBtn = document.getElementById("collapse");
		xBtn.onclick = function(){
			expandCollapseList(listHeadClass,subListClass,1);
			xBtn.style.display = "none";
			cBtn.style.display = "block";
		}
		cBtn.onclick = function(){
			expandCollapseList(listHeadClass,subListClass,0);
			xBtn.style.display = "block";
			cBtn.style.display = "none";
		}
	// if only expander exists
	} else if (document.getElementById("expander")){
		xBtn = document.getElementById("expander");
		xBtn.onclick = function(){
			expandCollapseList(listHeadClass,subListClass,1);
		}
	}
}

// find reference to sublist from header element
function getSubList(el,listClass){
	nextEl = el.nextSibling;
	while (nextEl.nodeName.toLowerCase() != "ul"){
		nextEl = nextEl.nextSibling;
	}
	if (nextEl.className == listClass){
		return nextEl;
	} else {
		return null;
	}
}

// function to show/hide embedded list
function showHideList(el,cName){
	newHdClass = el.className.indexOf("dn") != -1 ? "-up" : "-dn";
	addStateClass(el,cName,newHdClass,"updown");
	//alert(el.className);
	list = getSubList(el,cName);
	if (list != null){
		list.style.display = list.style.display != "block" ? "block" : "none";
	}
}

// function to expand all contained lists
function expandCollapseList(headClass,listClass,state){
	//alert(state);
	// open/close lists
	headArray = document.getElementsByTagName("h5");
	for (i=0;i<headArray.length;i++){
		if (headArray[i].className && headArray[i].className.indexOf(headClass) != -1){
			headRef = headArray[i];
			newHdClass = state > 0 ? addStateClass(headRef,headClass,"-dn","updown") : addStateClass(headRef,headClass,"-up","updown");
			list = getSubList(headRef,listClass);
			list.style.display = state > 0 ? "block" : "none";
		}
	}
}

// function to set hover color for ie browers
function setBgColor(el,state){
	if (state > 0){
		addStateClass(el,"","ieon","hover");
	} else {
		addStateClass(el,"","ieoff","hover");
	}
}

// function to prep more links on team page
function prepMoreLinks(){
	if (!document.getElementById) return;
	headArray = document.getElementsByTagName("h5");
	for (i=0;i<headArray.length;i++){
		if (headArray[i].className && headArray[i].className.indexOf('mlink') != -1){
			headArray[i].onclick = function(){
				showHideMore(this);
			}
			hideMore(headArray[i]);
		}
	}
}

// function to hide team text for js enabled browsers
function hideMore(el){
	nextEl = el.nextSibling;
	while (nextEl.nodeName.toLowerCase() != "p"){
		nextEl = nextEl.nextSibling;
	}
	if (nextEl.className == "txt-team"){
		nextEl.style.display = "none";
	}
}

// function to show/hide team text
function showHideMore(el,cName){
	newHdClass = el.className.indexOf("dn") != -1 ? "-up" : "-dn";
	addStateClass(el,cName,newHdClass,"updown");
	//alert(el.className);
	list = getSubList(el,cName);
	if (list != null){
		list.style.display = list.style.display != "block" ? "block" : "none";
	}
}

// testimonial array
function Testimonial(h, txt){
 this.h = h;
 this.txt = txt;
}
var tmArray = new Array('cisco','nokia','sprint','samsung');
var cisco = new Testimonial('114px','Cisco loves this service');
var nokia = new Testimonial('114px','Nokia loves this service');
var sprint = new Testimonial('114px','Sprint loves this service');
var samsung = new Testimonial('114px','Samsung loves this service');


// function to swap testimonials
function tmSwap(cid){
	if (!document.getElementById || !document.getElementById("tm-text") || !document.getElementById(cid.id)) return false;
	// grab elements
	txtBox = document.getElementById("tm-text");
	lgoBox = document.getElementById(cid.id);
	// swap testimonial text
	cname = eval(cid.id);
	txtBox.title = cname.txt;
	txtBox.childNodes[0].childNodes[0].nodeValue = cname.txt;
	txtBox.style.backgroundImage = "url(/images/tm_"+cid.id+".gif)";
	txtBox.style.height = cname.h;
	//txtBox.className = cid.id; //ie dont play this
	for (i=0; i<tmArray.length; i++){
		if (document.getElementById(tmArray[i])){
			tmBox = document.getElementById(tmArray[i]);
			tmBox.className = "";
		}
	}
	cid.className = "tm-on";
}

// preset testimonials
function prepTMs(arr){
	for (i=0; i<arr.length; i++){
		if (document.getElementById && document.getElementById(arr[i])){
			logoLink = document.getElementById(arr[i]);
			logoLink.onclick = function(){
				tmSwap(this);
				return false;
			}
		}
	}
}

// function to prep office locations listings
function prepTabList(tabList){
	if (!document.getElementById) return;
	var oList = document.getElementById('tabs');
	for (i=0; i< oList.childNodes.length; i++){
		if (oList.childNodes[i].nodeName.toLowerCase() == "li"){
			oList.childNodes[i].onclick = function(){
				showTabs(this.getAttribute('id'),tabList);
				return false;
			}
			if (oList.childNodes[i].className == "taboff"){
				locOffObj = document.getElementById(oList.childNodes[i].getAttribute('id').substr(4));
				locOffObj.style.display = "none";
			}
		}
	}
}

// initiate tab layers
var officeTabs = new Array ('sales','development');
var allianceTabs = new Array ('standards','products','systems');
// add by gary
var newsTabs = new Array ('pressrelease', 'media');

// function to switch tab & layer display
function showTabs(tabName,tabs){
	sufx = tabName.substr(4);
	for (i=0; i<tabs.length; i++){
		tabObj = document.getElementById('tab_'+tabs[i]);
		tabObj.className = 'taboff';
		locObj = document.getElementById(tabs[i]);
		locObj.style.display = "none";
	}
	tabOnObj = document.getElementById('tab_'+sufx);
	tabOnObj.className = 'tabon';
	locOnObj = document.getElementById(sufx);
	locOnObj.style.display = "block";
}


// setup map links
function setMapLinks(){
	links = document.getElementsByTagName('a');
	for(i=0; i<links.length; i++){
		if(links[i].className == 'map'){
			if (links[i].parentNode.nodeName.toLowerCase() == 'p'
				&& links[i].parentNode.parentNode.className == 'maplink'){
				div = links[i].parentNode.parentNode;
				div.onclick = function(){
					getDivLoca(this);
					return false;
				}
			} else {
				links[i].onclick = function(){
					openMap(this.getAttribute('rel'));
					return false;
				}
			}
		}
	}
}
function getDivLoca(divObj){
	for (i=0; i<divObj.childNodes.length; i++){
		if (divObj.childNodes[i].nodeName == "P"){
			childObj = divObj.childNodes[i];
			for (j=0; j<childObj.childNodes.length; j++){
				if (childObj.childNodes[j].nodeName == "A"){
					loc = childObj.childNodes[j].getAttribute('rel');
				} else {
					loc = "usa_paloalto";
				}
			}
		}
	}
	openMap(loc);
}
function openMap(loca){
	var pop = window.open('map-'+loca+'.html','mapWin','width=760,height=600,directories=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,toolbar=no,status=no,screenX=50,screenY=50,left=50,top=50');
	pop.focus();
}


// added for templates
// function to open enlarged images
function openPopWin() {
	var params = 'height=600,width=760,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,toolbar=no,screenX=5,screenY=5,left=50,top=50';
	var newWindow = window.open(this.getAttribute('href'), 'popWindow', params);
	newWindow.focus();
	return false;
}


// prep window closer
function setWinCloser(){
	var links = document.getElementsByTagName('a');
	for(i=0; i<links.length; i++){
		if(links[i].getAttribute('rel') == 'close'){
			links[i].onclick = function(){
				closeWindow();
				return false;
			}
		}
	}
}
function closeWindow(){
	window.close();
}
