//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');
	for (i=0; i<links.length; i++) {
		if (links[i].parentNode.className == 'end') {
			links[i].parentNode.addClassName("current");
			var helper = links[i].parentNode;
			var front = links[i].parentNode;
			while (front = front.nextSibling) {
				if (front.nodeName && front.nodeName.toLowerCase()=="li")
				break;
			}
			front.addClassName("front", front);
		}
		else if (links[i].hasClassName(bodyId)) {
			links[i].parentNode.addClassName("current");
			var helper = links[i].parentNode;
		}
		if (helper) {
			while (helper = helper.previousSibling) {
				if (helper.nodeName && helper.nodeName.toLowerCase()=="li")
				break;
			}
		}
		if (helper) {
			helper.addClassName("helper");
		}
	}
}

function addHoverState() {
	function swapImgIn() {
		var imgPath = this.getElementsByTagName('img')[0];
		imgPath.src = imgPath.src.replace('.gif', '_h.gif');
	}
	function swapImgOut() {
		var imgPath = this.getElementsByTagName('img')[0];
		imgPath.src = imgPath.src.replace('_h.gif', '.gif');
	}
	$$('.arrow', '.hover', 'a.go').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
		try {
            // Get column heights
			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 = document.getElementsByClassName('gradient', $('column1'));
				bords = document.getElementsByClassName('top_border', $('column1'));
				last = hites.last();
			} else {
				diff = coneH - ctwoH;
				hites = document.getElementsByClassName('gradient', $('column2'));
				bords = document.getElementsByClassName('top_border', $('column2'));
				last = hites.last();
			}
			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";
			}
		} catch(err) {
			//Handle errors here
		}	
	//}
}



showTP = function() {
  if ($('tooltip').style.display == 'none') $('tooltip').show();
}
hideTP = function() {
  //console.log($('tooltip').style.display)
  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() {
    urchinTracker('/eats-and-treats/nutrition-calculator-button');
    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) {
	urchinTracker(track);
	window.location = url;
}


function swapFlashBanner(swfFile) {
    sn.setAttribute('swf', swfFile);
    sn.write('flash_banner');
}
