/* global resources */

/* function to simply multiple functions in window.onload
* DOM Scripting (103) :: Simon Willison (http://simon.incution.com) */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}
	else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}


/* site specific */
//rollovers
nav_taste_on=new Image();
nav_taste_off=new Image();
storyRealGoodness_on=new Image();
storyRealGoodness_off=new Image();
lactoseFreeTech_on=new Image();
lactoseFreeTech_off=new Image();
productLocator_on=new Image();
productLocator_off=new Image();
nav_products_on=new Image();
nav_products_off=new Image();
nav_cooking_on=new Image();
nav_cooking_off=new Image();
nav_FAQ_on=new Image();
nav_FAQ_off=new Image();
glossary_on=new Image();
glossary_off=new Image();
nav_healthy_on=new Image();
nav_healthy_off=new Image();
nav_insiders_on=new Image();
nav_insiders_off=new Image();
nav_connect_on=new Image();
nav_connect_off=new Image();

nav_taste_on.src="images/nav_taste_over.png";
nav_taste_off.src="images/nav_taste.png";
storyRealGoodness_on.src="images/subnav/storyRealGoodness_over.png";
storyRealGoodness_off.src="images/subnav/storyRealGoodness.png";
lactoseFreeTech_on.src="images/subnav/lactoseFreeTech_over.png";
lactoseFreeTech_off.src="images/subnav/lactoseFreeTech.png";
productLocator_on.src="images/subnav/productLocator_over.png";
productLocator_off.src="images/subnav/productLocator.png";
nav_products_on.src="images/nav_products_over.png";
nav_products_off.src="images/nav_products.png";
nav_cooking_on.src="images/nav_cooking_over.png";
nav_cooking_off.src="images/nav_cooking.png";
nav_FAQ_on.src="images/nav_FAQ_over.png";
nav_FAQ_off.src="images/nav_FAQ.png";
glossary_on.src="images/subnav/glossary_over.png";
glossary_off.src="images/subnav/glossary.png";
nav_healthy_on.src="images/nav_healthy_over.png";
nav_healthy_off.src="images/nav_healthy.png";
nav_insiders_on.src="images/nav_insiders_over.png";
nav_insiders_off.src="images/nav_insiders.png";
nav_connect_on.src="images/nav_connect_over.png";
nav_connect_off.src="images/nav_connect.png";

function rollPhase(imageName) {
	imageHolder=new Image();
	imageHolder.src=eval(imageName + "_on.src");
	document[imageName].src=imageHolder.src;
	}
        
function outPhase(imageName) {
	imageHolder=new Image();
	imageHolder.src=eval(imageName + "_off.src");
	document[imageName].src=imageHolder.src;
	}



//drop down menu lists
function dropDown(whichNav) {
	document.getElementById(whichNav).style.display='block';
	}

function dropDownRetract(whichNav) {
	document.getElementById(whichNav).style.display='none';
	}



//auto rotation feature for feature content swap
function swapLayers1() {
	changeFeature('1'); 
	layerSwap = setTimeout("swapLayers2()", 12000);
	}

function swapLayers2() {
	changeFeature('2'); 
	layerSwap = setTimeout("swapLayers3()", 12000);
	}

function swapLayers3() {
	changeFeature('3'); 
	layerSwap = setTimeout("swapLayers4()", 12000);
	}

function swapLayers4() {
	changeFeature('4'); 
	layerSwap = setTimeout("swapLayers5()", 12000);
	}

function swapLayers5() {
	changeFeature('5'); 
	layerSwap = setTimeout("swapLayers1()", 12000);
	}

function stopRotator() {
	clearTimeout(layerSwap);
	}


//feature content swapper
function changeFeature(whichFeature) {
	if(navigator.appVersion.indexOf('MSIE 8.0') > 0){
		document.getElementById("rotatorDivText").style.opacity=0;
		document.getElementById("rotatorDivImage").style.opacity=0;
		document.getElementById('rotatorDivText').innerHTML=eval("featureText_" + whichFeature);
		document.getElementById('rotatorDivImage').innerHTML=eval("featureImage_" + whichFeature);
		document.getElementById("rotatorDivText").style.opacity=100;
		document.getElementById("rotatorDivText").style.opacity=100;
	}
	else {
	document.getElementById("rotatorDivText").style.opacity=0;
	document.getElementById("rotatorDivImage").style.opacity=0;
	document.getElementById('rotatorDivText').innerHTML=eval("featureText_" + whichFeature);
	document.getElementById('rotatorDivImage').innerHTML=eval("featureImage_" + whichFeature);
	opacity("rotatorDivText", 0, 100, 500);
	opacity("rotatorDivImage", 0, 100, 500);
	}
	var swapNavLinks = document.getElementById("center_switch").getElementsByTagName("a");
		for (var i=0; i<swapNavLinks.length; i++){
			if((whichFeature.substr(-1, 1)) == (i + 1)){
				swapNavLinks[i].firstChild.src = "images/index/rotator_button_on.png";
			}
			else {
			swapNavLinks[i].firstChild.src = "images/index/rotator_button_off.png";
			}
		}
			
}

//tell a friend callout open/close
function calloutExpanderHome(whichState) {
	document.getElementById('friendCallout2').style.opacity=0;
	if(navigator.appVersion.indexOf('MSIE 6.0') > 0){
	document.getElementById('friendCallout2').style.backgroundImage = "url(images/callouts/callout_friend_background2.jpg)";
	}
	else {
		document.getElementById('friendCallout2').style.backgroundImage = "url(images/callouts/callout_friend_background2.png)";
	}
	document.getElementById('friendCallout2').style.backgroundRepeat = "no-repeat";
	document.getElementById('friendCallout2').innerHTML=whichState;
	document.getElementById('friendCallout2').style.marginTop = "3px";
	document.getElementById('friendCallout2').style.marginLeft = "8px";
	document.getElementById('friendCallout2').style.width = "390px";
	document.getElementById('friendCallout2').style.height = "335px";
	document.getElementById('friendCallout2').style.padding = "25px 40px 0 20px";
	if(navigator.appVersion.indexOf('MSIE') > 0){
		document.getElementById('friendCallout2').opacity = 100;
		document.getElementById('friendCallout2').filter = "alpha(opacity=100)"; 
	}
	else {
	opacity("friendCallout2", 0, 100, 500);
	}
	}		

function calloutCollapseHome(whichState) {
	document.getElementById('friendCallout2').style.opacity=0;
	document.getElementById('friendCallout2').style.backgroundImage = "";
	document.getElementById('friendCallout2').innerHTML=whichState;
	document.getElementById('friendCallout2').style.marginTop = "0";
	document.getElementById('friendCallout2').style.marginLeft = "0";
	document.getElementById('friendCallout2').style.width = "177px";
	document.getElementById('friendCallout2').style.height = "194px";
	document.getElementById('friendCallout2').style.padding = "0 0 0 0";
	//opacity("friendCallout2", 0, 100, 500);
	//if(navigator.appVersion.indexOf('MSIE 6.0') > 0){
	if(navigator.appVersion.indexOf('MSIE') > 0){
		document.getElementById('friendCallout2').style.opacity=100;
	}
	/* else if(navigator.appVersion.indexOf('MSIE 7.0') > 0){
		document.getElementById('friendCallout2').style.opacity=100;
	} */
	else {
		opacity("friendCallout2", 0, 100, 500);
	}	
	}		

function calloutExpander(whichState) {
	document.getElementById('friendCallout').style.opacity=0;
	//alert(navigator.appVersion.indexOf('MSIE'));
	if(navigator.appVersion.indexOf('MSIE') > 0){
		document.getElementById('friendCallout').style.backgroundImage = "url(images/callouts/callout_friend_background.jpg)";
	}
	else {
		document.getElementById('friendCallout').style.backgroundImage = "url(images/callouts/callout_friend_background.png)";
	}
	document.getElementById('friendCallout').style.backgroundRepeat = "no-repeat";
	document.getElementById('friendCallout').innerHTML=whichState;
	document.getElementById('friendCallout').style.width = "456px";
	document.getElementById('friendCallout').style.height = "335px";
	document.getElementById('friendCallout').style.padding = "25px 40px 0 20px";
	if(navigator.appVersion.indexOf('MSIE') > 0){
		document.getElementById('friendCallout').opacity = 100;
		document.getElementById('friendCallout').filter = "alpha(opacity=100)"; 
	}
	else {
	opacity("friendCallout", 0, 100, 500);
	}
}		

function calloutCollapse(whichState) {
	document.getElementById('friendCallout').style.opacity=0;
	document.getElementById('friendCallout').style.backgroundImage = "";
	document.getElementById('friendCallout').innerHTML=whichState;
	document.getElementById('friendCallout').style.width = "287px";
	document.getElementById('friendCallout').style.height = "120px";
	document.getElementById('friendCallout').style.padding = "0 0 0 0";
	if(navigator.appVersion.indexOf('MSIE 6.0') > 0){
		document.getElementById('friendCallout').style.opacity=100;
	}
	else if(navigator.appVersion.indexOf('MSIE 7.0') > 0){
		document.getElementById('friendCallout').style.opacity=100;
	}
	else {
		opacity("friendCallout", 0, 100, 500);
	}	
}


function calloutExpanderConnect(whichState) {
	document.getElementById('connectCallout').style.opacity=0;
	//alert(navigator.appVersion.indexOf('MSIE'));
	if(navigator.appVersion.indexOf('MSIE') > 0){
		document.getElementById('connectCallout').style.backgroundImage = "url(images/callouts/callout_share_background.jpg)";
	}
	else {
		document.getElementById('connectCallout').style.backgroundImage = "url(images/callouts/callout_share_background.png)";
	}
	document.getElementById('connectCallout').style.backgroundRepeat = "no-repeat";
	document.getElementById('connectCallout').innerHTML=whichState;
	document.getElementById('connectCallout').style.width = "456px";
	document.getElementById('connectCallout').style.height = "335px";
	document.getElementById('connectCallout').style.padding = "25px 40px 0 20px";
	if(navigator.appVersion.indexOf('MSIE') > 0){
		document.getElementById('connectCallout').opacity = 100;
		document.getElementById('connectCallout').filter = "alpha(opacity=100)"; 
	}
	else {
	opacity("connectCallout", 0, 100, 500);
	}
}		



function calloutCollapseConnect(whichState) {
	document.getElementById('connectCallout').style.opacity=0;
	document.getElementById('connectCallout').style.backgroundImage = "";
	document.getElementById('connectCallout').innerHTML=whichState;
	document.getElementById('connectCallout').style.width = "287px";
	document.getElementById('connectCallout').style.height = "120px";
	document.getElementById('connectCallout').style.padding = "0 0 0 0";
	if(navigator.appVersion.indexOf('MSIE 6.0') > 0){
		document.getElementById('connectCallout').style.opacity=100;
	}
	else if(navigator.appVersion.indexOf('MSIE 7.0') > 0){
		document.getElementById('connectCallout').style.opacity=100;
	}
	else {
		opacity("connectCallout", 0, 100, 500);
	}	
}


//fade up opacity of an element
function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
}


function showMe(myTarget, inline){
	if(!document.getElementById){
		return false;
	}
	if(document.getElementById(myTarget)){
		if(inline){
			document.getElementById(myTarget).className = "showMeInline";
		}
		else {
			document.getElementById(myTarget).className = "showMe";
		}
		return false; // keep my link from doing anything
	}
	return false;
}

/*
 * function to toggle another element based on the selection of a <select>s <option>
 * me = the parent <select>
 * target = the <option> to be checked for
 * remote = the the of the remote object to be shown
 */

function selectToggleOn(me, target, remote){
	if(!document.getElementById){
		return false;
	}
	if(document.getElementById(target).selected){
		showMe(remote);
	}
}


//used by opacity function to account for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}
				
				
function prep_devInquiry(){
	if(!document.getElementsByTagName || !document.getElementById){
		return false;
	}
	var theH = document.getElementById("col_center").scrollHeight;
	//alert(theH);
}addLoadEvent(prep_devInquiry)
