/**
 * @author bao.nguyen
 */
// Shows the containing div

var originalTopLocation = null;
var originalLeftLocation = null;
function grapeTipGrow(){
	// calculate top location for the grape-tip containing div...current = current - 140
	if (originalTopLocation == null) {
  	originalTopLocation = document.getElementById('grape-tip-container').style.top;
  }
	var topLocation = originalTopLocation.split('px');
	var topLocation = parseInt(topLocation[0]) - 165;
	document.getElementById('grape-tip-container').style.top = topLocation + 'px';
	//check if grape announces to the left
	if (document.getElementById('grape-tip-container').className == 'grape-tip-left'){
		//calulate left movement
		if (originalLeftLocation == null) {
		  originalLeftLocation = document.getElementById('grape-tip-container').style.left;
	  }
		var leftLocation = originalLeftLocation.split('px');
		var leftLocation = parseInt(leftLocation[0]) - 243;
		document.getElementById('grape-tip-container').style.left = leftLocation + 'px';
		document.getElementById('grape-tip').style.left = '243px';
	}
	// set the size of the containing div
	document.getElementById('grape-tip-container').style.width = '300px';
	document.getElementById('grape-tip-container').style.height = '235px';	
	document.getElementById('grape-tip').style.top = '165px';
}

function grapeTipShrink(){
	// calculate top location for the grape-tip containing div...current = current + 140

	document.getElementById('grape-tip-container').style.top = originalTopLocation;
	
	//check if grape announces to the left
	if (document.getElementById('grape-tip-container').className == 'grape-tip-left'){
		//calulate left movement
		document.getElementById('grape-tip-container').style.left = originalLeftLocation;
		document.getElementById('grape-tip').style.left = '0px';
	}
	// reset the size of the containing div
	document.getElementById('grape-tip').style.top = '0px';	
	document.getElementById('grape-tip-container').style.width = '50px';
	document.getElementById('grape-tip-container').style.height = '50px';	
}

function autoInstallFlashPlayer(){
	var version = deconcept.SWFObjectUtil.getPlayerVersion();
	
	// if version[major] == 0 - no flash player installed
	if (version["major"] == 0) {
    // use run ActiveContent
		  AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','name','empty-swf','width','1','height','1','src','media/swf/empty','quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer','wmode','transparent','movie','media/swf/empty' ); //end AC code
		}
		// elseif version major < 0
		else if (version["major"] < 10) {
		  if(version['rev'] < 115){
			// do express install
			var htmlToWrite = '<div id="express-install" style="position:absolute;z-index: 600000; margin: 0 0;top:0; width: 100%; height: 100%;"></div>';			
			document.write(htmlToWrite);
			var fo = new SWFObject("/media/swf/empty.swf", "empty-swf", "100%", "100%", "9.0.115", "#ffffff", "high");
			fo.useExpressInstall("/media/swf/expressinstall.swf");
			fo.addParam("wmode", "transparent");
			fo.write("express-install");			
			}
		}
}

function toggleTreeMenu(toggleElement){
	// (check to see if menu is opened or closed (get class name))
	// get class name for anchor tag
	// if opened (menu-collapse)
	try{
	if(Element.hasClassName($(toggleElement),'menu-collapse')){
		// (close)
		// get ul tag (sibling?) or get parent li, and get ul, set display to none
		$(toggleElement).up('li').down('ul').hide();
		
		// set anchor class to collapsed
		Element.removeClassName($(toggleElement),'menu-collapse');
		Element.addClassName($(toggleElement),'menu-expand');
	}
	// else closed (menu-expand)
	else if(Element.hasClassName($(toggleElement),'menu-expand')){
		// (open)
		// get ul tag (sibling?) or get parent li, and get ul, set display to block
		$(toggleElement).up('li').down('ul').show();		
		
		// set anchor class to expanded
		Element.removeClassName($(toggleElement),'menu-expand');
		Element.addClassName($(toggleElement),'menu-collapse');	
	}
	}
	catch(err){}

}

function clearField(formField, defaultValue){
	if (formField.value == defaultValue){
		formField.value = ('');
	}
}



function toggleMap(displayJobsType){
	if (displayJobsType == 'jobsByFunction'){
		Element.hide($('jobs-by-location-nav'));
		Element.hide($('content-body-location'));
		Element.setStyle('jobs-by-function-nav', {display: 'block'});
		Element.setStyle('content-body-function', {display: 'block'});
		//Element.show($('jobs-by-function-nav'));
		//Element.show($('content-body-function'));		
	}
	else{
		Element.setStyle('jobs-by-location-nav', {display: 'block'});
		Element.setStyle('content-body-location', {display: 'block'});		
		Element.hide($('jobs-by-function-nav'));
		Element.hide($('content-body-function'));
	}
	
}

function loadJobLocation(jobLocation){
	new Ajax.Updater('job-display', '/careers/joblocationlistings/' + jobLocation + '.aspx', {asynchronous:true});	
}

function openNutritionInfo(urlToLoad){
	if (urlToLoad != ''){
		window.open(urlToLoad,'_blank','width=400,height=650');
	}
}


function hideOtherSubmenus(){
	$$('ul.header-menu li.active .header-submenu')[0].hide();
}

function showOtherSubmenus(){
	$$('ul.header-menu li.active .header-submenu')[0].show();
}

startList = function() {

		if (document.getElementById("main-menu")){
			cssdropdownRoot = document.getElementById("main-menu");
			for (x=0; x<cssdropdownRoot.childNodes.length; x++) {
				node = cssdropdownRoot.childNodes[x];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
						if(this.className.search(/active/) == -1){
							hideOtherSubmenus();
						}
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
						if(this.className.search(/active/) == -1){
							showOtherSubmenus();
						}
					}
				}
			}
		}

}


window.onload = function(){
	startList();
	var hashValue = window.location.hash;
	if (hashValue == '#health'){
		toggleTreeMenu('section1');
	}
	else if (hashValue == '#product'){
		toggleTreeMenu('section2');
	}
	else if (hashValue == '#company'){
		toggleTreeMenu('section3');
	}
}

