window.onload = init;

var openFirstAccordion = false; // on moofx pages, if no accordion is specified in the URL, should we open the first one by default?
var moofx = false; // does the page use moofx?
var home = false; // are we on the home page?

// Sets a cookie
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

// Returns a cookie value
function readCookie(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;
}

// Erases a cookie
function eraseCookie(name) {
	createCookie(name,"",-1);
}

// Function to record the page scroll location in a cookie
function checkPageScroll() {
	if (self.pageYOffset) {
		sY = self.pageYOffset;
		sX = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	
	        sY = document.documentElement.scrollTop;
        sX = document.documentElement.scrollLeft;
    } else if (document.body) {
    	sY = document.body.scrollTop; 
    	sX = document.body.scrollLeft;
    }
	createCookie ('scrollLocX', sX, 0)
	createCookie ('scrollLocY', sY, 0)
}

// Function to scroll window to a specified horizontal/vertical location based on an amt. in a cookie
function scrollToLocation() {
	var xLoc = readCookie('scrollLocX');
	var yLoc = readCookie('scrollLocY');
	window.scrollTo(xLoc,yLoc);
}

// Cross-browser function to measure window height
function getWindowHeight() {
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body&&document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

// Cross-browser function to measure window width
function getWindowWidth() {
	var windowWidth=0;
	if (typeof(window.innerWidth)=='number') {
		windowWidth=window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body&&document.body.clientWidth) {
				windowWidth=document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}


// Function to show/hide Previous Versions and Version history for products
function expandCollapse(text, link, show, hide) {
	if (document.getElementById(text).style.display == "none") {
		document.getElementById(text).style.display = "block";
		document.getElementById(link).innerHTML = hide;
	}else{
		document.getElementById(text).style.display = "none";
		document.getElementById(link).innerHTML = show;
	}
}

// Old browser compatible element creation
// Sample use: var element = new getObj('id');
// Now you can set styles with element.style property
function getObj(name) {
	this.exists = false;
	var y;
	if (document.getElementById && document.getElementById(name) != null) { 
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
		this.hidden = "";
		this.exists = true;
	}
	else if (document.all && document.all[name] != y) {
//		alert (document.all[name]);
		this.obj = document.all[name];
		this.style = document.all[name].style;
		this.hidden = "";
		this.exists = true;
	}
	else if (document.layers) {
		this.obj = document.layers[name];
		this.style = document.layers[name];
		this.hidden = "";
		this.exists = true;
	}
}

// Pop-up window function
function popWin(url,width,height) {
	var x;
	if (width==x) {
		width=475;
	}
	if (height==x) {
		height=220;
	}
	eval ("popupWin = window.open('', 'mainwindow', 'scrollbars=yes,resizable=yes,toolbar=no,width=" + width + ",height=" + height + ",left=10,top=10,screenX=10,screenY=10')");
	popupWin.location=url ;                
}

//LOADING MESSAGE FOR HOME FLASH

function loadingMessage() {
//	alert (flashinstalled);
	document.getElementById('loading_div').style.display = "none";
	document.getElementById('banner').style.display = "block";
	document.getElementById('banner_float_wrapper').style.display = "block";
}

function toggleProductsHome(link) {
	if (!(detected.os == "MAC" && detected.browser == "IE") && !(detected.browser=="NETSCAPE")) {
//		alert('inside toggleProductsHome');
		var byNameLink = document.getElementById('products_by_name_link');
		var byHostLink = document.getElementById('products_by_host_link');
		var matrixLink = document.getElementById('product_support_matrix_link');
//		alert (link);
		if (link.id == 'products_by_name_link' || link == "Products by Name") {
			byHostLink.style.backgroundImage = 'none';
			matrixLink.style.backgroundImage = 'none';
			if (byNameLink.style.backgroundImage.indexOf('/images/leftnav_selected.gif') > 0 ) {
				byNameLink.style.backgroundImage = "none";
			} else {
				byNameLink.style.backgroundImage = "url('/images/leftnav_selected.gif')";
			}
		}
		if (link.id == 'products_by_host_link' || link == "Products by Host") {
			if (byHostLink.style.backgroundImage.indexOf('/images/leftnav_selected.gif') > 0 ) {
				byHostLink.style.backgroundImage = "none";
			} else {
				byHostLink.style.backgroundImage = "url('/images/leftnav_selected.gif')";
			}
			matrixLink.style.backgroundImage = 'none';
			byNameLink.style.backgroundImage = 'none';
		}
		if (link.id == 'product_support_matrix_link' || link == "Product Support Matrix") {
			byHostLink.style.backgroundImage = 'none';
			if (matrixLink.style.backgroundImage.indexOf('/images/leftnav_selected.gif') > 0 ) {
				matrixLink.style.backgroundImage = "none";
			} else {
				matrixLink.style.backgroundImage = "url('/images/leftnav_selected.gif')";
			}
			byNameLink.style.backgroundImage = 'none';
		}
	}
}	

// onLoad function
function init(){

	function resizeHandler() {
		if (moofx) {
			var bottomDiv = new getObj('bottom_spacer');
			windowHeight = getWindowHeight();
			bottomDiv.style.height=windowHeight + 'px';
		}
		document.body.style.marginRight = '';
//		fixCentering();
	}
	
	function fixCentering(){
		if (!(detected.os == "WINDOWS" && detected.browser == 'IE')) {
			var curNode = document.body.firstChild;
			while (curNode.nodeType != 1) {
				curNode = curNode.nextSibling;
			}
			if (curNode.nodeType == 1) {
				if (getWindowHeight() > curNode.clientHeight + 60) {
					document.body.style.marginRight = "15px";
//					alert (document.body.style.width);
				}
			}
		}
	}

	//hash function
	function checkHash(doit){
		var found = false;
		toggles.each(function(a, i){
			if (window.location.href == a.href) {
				if (doit) {
					myAccordion.showThisHideOpenNoAnim(stretchers[i]);
					toggleProductsHome(a.title);
				}
				found = true;
			}
		});
		return found;
	}

	if (moofx) {
		resizeHandler();
	}
	
//	fixCentering();
//	window.onresize = resizeHandler;
	
	// page auto-scrolling:
	
	var urlArr = window.location.href.split('/');
	var curSection = urlArr[3];
	urlArr = document.referrer.split('/');
	var lastSection = urlArr[3];

	if (!(detected.os == "MAC" && detected.browser == "IE") && curSection == lastSection) { // don't attempt to scroll page if browser is mac IE, or if the last page was in another section
		scrollToLocation();
	}
	
	window.onunload = checkPageScroll;

	if (moofx && modernBrowser) { // if there are moofx in the page and the browser can handle it, then load them up
		stretchers = document.getElementsByClassName('stretcher'); //div that stretches
		toggles = document.getElementsByClassName('accordion_title'); //links where you click to open the accordion

		//accordion effect
		myAccordion = new fx.Accordion(
			toggles, stretchers, {opacity: false, duration: 400}
		);
		
		var uri = window.location.href.substring(window.location.href.indexOf('.com')+4, window.location.href.length);
		if (openFirstAccordion && !checkHash(false)) { // if page specifies to open the first accordion and there's no hash mark in the current URL
			if (uri == '/products/' || uri == '/products/index.php') { // if products home, make the left nav link grey, and open the first accordion
				if (document.getElementById) {
					toggleProductsHome(document.getElementById('products_by_name_link'));
					myAccordion.showThisHideOpenNoAnim(stretchers[0]);
				}
			} else {
				myAccordion.showThisHideOpenNoAnim(stretchers[0]); // otherwise just open the first accordion
			}
		} else {
			checkHash(true); // otherwise open the accordion specified by the hash in the URL
		}
		
		stretchers.each(function(div, i){
			if (div.firstChild.className == 'content') {
				div.firstChild.style.display = 'block';
			}
		});
	}


	// overviewMovDiv = new getObj('overview_mov');
	
//	alert (overviewMovDiv.exists);
	
//	alert (detected.browser);
//	alert (detected.os);

	// if (overviewMovDiv.exists) {
	// 	overviewMovDiv.style.display = 'block';
	// 	if (document.getElementById) {
	// 		document.getElementById('overview').style.height = '';
	// 	} else {
	// 		overviewMov = new getObj('overview');
	// 		if (overviewMov.exists) {
	// 			overview.style.height = '';
	// 		}
	// 	}
	// }


	if (home) {
		loadingMessage();
	}
	

}