//doSubnav = function() {
function doSubnav() {
	// Uncomment these lines to pull selector from URL
	//var urlId	= window.location.href.split('/')[4].split('.')[0];
	//var bodyId	= urlId;
	var bodyTag = document.getElementsByTagName('body');
	var bodyId	= bodyTag[0].id;
	//var links	= document.getElementsByClassName(bodyId, 'a');
	var links	=$$('a');

	for (var i=0; i<links.length; i++) {
		if (links[i].parentNode.className == 'end') {
			Element.addClassName(links[i].parentNode, 'current');
			var helper = links[i].parentNode;
			var front = links[i].parentNode;
			while (front = front.nextSibling) {
				if (front.nodeName && front.nodeName.toLowerCase()=="li") {
				  break;
			  }
			}
			Element.addClassName(front, "front");
		} else if (links[i].hasClassName(bodyId)) {
      Element.addClassName(links[i].parentNode, 'current');
			var helper = links[i].parentNode;
		}
		if (helper) {
			while (helper = helper.previousSibling) {
				if (helper.nodeName && helper.nodeName.toLowerCase()=="li") {
				  break;
			  }
			}
		}
		if (helper) {
			Element.addClassName(helper, "helper");
		}
	}
}

function addHoverState() {
	function swapImgIn() {
		var imgPath = this.getElementsByTagName('img')[0];
		imgPath.src = imgPath.src.replace(/\.(gif|png)$/, '_h.$1');
	}
	function swapImgOut() {
		var imgPath = this.getElementsByTagName('img')[0];
		imgPath.src = imgPath.src.replace(/_h\.(gif|png)$/, '.$1');
	}
	$$('.arrow', '.hover').each(function(btn) {
		btn.observe('mouseover', swapImgIn.bind(btn));
		btn.observe('mouseout', swapImgOut.bind(btn));
	});
}

function fixColumns() {
	// NEED CONDITIONAL THAT DOES NOT EXECUTE ON about PAGE AND store locator PAGE
	// if (document.getElementsByClassName('gradient').length > 1) {
	// TODO: try catch for now, but this is hack
	// Get column heights

	// If #columns exists
	if($('columns') != undefined) {
		
		// If #column2 exists
		if($('column2') != undefined) {
			var coneH = $('column1').offsetHeight;
			var ctwoH = $('column2').offsetHeight;
			var diff;
			var hites;
			var bords;
			var last;

			// Compare column heights and set box insides inside of shorter column to taller height
			if (coneH < ctwoH) {
				diff = ctwoH - coneH;
				hites = $$('#column1 .gradient');
				bords = $$('#column1 .top_border');
				last = hites[0];
			} else {
				diff = coneH - ctwoH;
				hites = $$('#column2 .gradient');
				bords = $$('#column2 .top_border');
				last = hites[hites.length-1]
			}
			if (navigator.appName != "Microsoft Internet Explorer") {
				oldH = last.getStyle('height').replace("px", "") * 1.0;
				last.setStyle({height: (oldH + diff) + "px"});
			} else {
				oldH = last.offsetHeight;
				for (i=0; i<bords.length; i++) {
					oldH -= 10;
				}
				last.style.height = oldH + diff - 36 + "px";
			}
		}
	}
}

showTP = function() {
  if ($('tooltip').style.display == 'none') $('tooltip').show();
}
hideTP = function() {
  if ($('tooltip').style.display == '') $('tooltip').hide();
}

function reveal() {
	// NEED TO SLIDE MAP UP WITH TRIP PLANNER--SAME TIME, SAME SPEED
	new Effect.Move('trip-planner', {
		//scaleMode:'content'
		x:0,
		y:-568,
		mode:'relative',
		afterFinish:showTP
	});
	new Effect.Scale('map', {
		scaleX:false,
		//scaleContent:false,
		scaleMode:{ originalHeight:223, originalWidth:588 }
	});
}

// This funtion is a generic function that contains all functions to be triggered on window load.
inits = function() {
    var page = document.body.id;
	doSubnav();
	addHoverState();
	if (page != ('store-locator' || 'about')) fixColumns();
}

function openNutriCalc() {
    window.open('http://www.idq.com/DQCNCPublic/', 'newwindow', 'height=750,width=650,scrollbars=yes');
};

function allowInt(f) {
	if (!/^\d*$/.test(f.value)) {
		f.value = f.value.replace(/[^\d]/g,"");
	}
}

function trackAndRedirect(track, url) {
	window.location = url;
}


function swapFlashBanner(swfFile) {
    sn.setAttribute('swf', swfFile);
    sn.write('flash_banner');
}

function toggleLocator() {
	$('navigation_header').toggleClassName('expanded');
	getLocationStores();
}
function expandLocator() {
	$('navigation_header').addClassName('expanded');
	getLocationStores();
}

function setFlashCookie(c_name,value) {
	document.cookie = c_name + "=" + escape(value) + ";expires=";
}

function getFlashCookie(c_name) {
	
	var strCookie = ""
	
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    strCookie =  unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	  
	setFlash(strCookie);
}

function setFlash(str) {
	
	document.getElementById("flash_banner").onFlashCookieResult(str);
	alert("cookie: " + str)
}

function getLocationStores() {
	// We only want to run this if the locations div is expanded, and this is the first run.
	if ($('navigation_header').hasClassName('expanded') && firstRun == true){
		new Ajax.Request('/services/getHomepageStores.cfm', {
			method: 'get',			 
			asynchronous: true,
			timeout: 10000,
			onTimeout: function(t) {
	        	$('closest-locations').update('<ul id="search_results" class="one-result"><li>Oops, something went wrong.</li></ul>');
			},
			onSuccess: function(t) {
				$('closest-locations').update(t.responseText);
				// Set firstRun = false so that subsequent requests do not call ajax.
				firstRun = false;
	        },
	        onFailure: function(t) {
	        	$('closest-locations').update('<ul id="search_results" class="one-result"><li>Oops, something went wrong.</li></ul>');
	        }
		});
	}
}

Event.observe(window, 'load', function() {
	firstRun = true;
	// ain access to the body element and determine if the class attribute is set to "homepage".
	//Event.observe('dq_stores_toggle', 'click', toggleLocator);
	//Event.observe('dq_stores_toggle', 'click', getLocationStores);
	//Event.observe('address1', 'focus', expandLocator);
	//Event.observe('address1', 'focus', getLocationStores);
});

