var fadeIn;
//loaded is the div that holds the loading/progress image
var loaded
//results is the div that the async data gets pulled into
var results;
var mixNav;


window.addEvent('domready',function(){
	loaded = $('loaded');
	results = $('results');
	loaded.setStyle('visibility', 'hidden');
	results.setStyle('opacity', '0');
	results.setStyle('visibility', 'visible');
});

window.addEvent('load',function(){
	fadeIn = new Fx.Style('results', 'opacity',{duration:600,transition:Fx.Transitions.Expo.easeIn});
	mixNav = new Fx.Style('mixNavContainer', 'opacity',{duration:600,transition:Fx.Transitions.Expo.easeIn});

	//new class for pulling required parameter ("article") off of query string.  each state gets its own param.
	var cleanUrl = new Class({
		//strip = method that does the magic
		strip: function(url){
			var paramss = url.slice(url.indexOf("?") + 1);
			var article = paramss.slice(paramss.indexOf("article=") + 8);
			var articleParam
			//test for multiple parameters and slice out needed param.  
			if(article.contains("&")){
				articleParam = article.slice(0, article.indexOf("&"));
			}
			else{
				articleParam = article.slice(0);
			}
			
			var full = "/xml/" + articleParam + ".xml";
			
			//return name of xml file.  test for empty first.
			return full;
		}
	});
	
	//instantiate above url cleansing class
	var cleansedObj = new cleanUrl();
			
	//loop through links in a given container element
	$$('#purposeLinks a').each(function(item){
		//attach click events to each link
		item.addEvent('click',function(e){
			//stop the event
			e = new Event(e).stop();
			//set the href property for each link to a variable
			var url = this.getProperty('href');
			//set variable to the return value of the instantiated class's method
			url = cleansedObj.strip(url);
			//call ajax function!
			var ajaxObj = ajaxCall(url);
		});
	});

});

//keep function outside of event assignments so it can be called via flash (getURL()) if neccessary
function ajaxCall(url){
	new Ajax(url, { method: 'get', onRequest: function(){results.setStyle('opacity', '0');whileLoading(loaded);}, onFailure: function(){whileLoading(loaded);}, onComplete: function(){whileLoading(loaded);fadeIn.start(0,1);}, update: results }).request();
}

//function to display loading graphic while request fires and hide it on completion.
function whileLoading(obj){
	if(obj.style.visibility == 'hidden'){
		obj.setStyle('visibility', 'visible');
	}
	else{
		obj.setStyle('visibility', 'hidden');
	}
}

function hideBG(){
	$("defaultBG").setStyle("visibility", "hidden");
}

var adArray = ['modal.html','modal2.html','modal3.html'];
var adCount = 0;
var adLink;

function adPicker(){
	if(adCount < 2){
		adLink = adArray[adCount];
		adCount++;
	}
	else{
		adCount = 0;
		adLink = adArray[adCount];
		adCount++;
	}
	return adLink;
}

function modalUpdate(){
	MOOdalBox.open(adPicker(),"<strong>NUT&#183;rition Print Ads <a href='#' onclick='modalUpdate()' style='color: #689833;margin-left: 10px'>next ></a></strong>", "339 450");
}

function showTheNut(n,m,o){
	$(n).style.visibility = "visible"
	if(m != 'nil'){
		if(m=="cranberries" || m=="sesamemix"){
			$(m).style.backgroundImage = 'url(/images/home/mix_nav_bg_over2.gif)';
		}
		else if(m=="soynuts"){
			$(m).style.backgroundImage = 'url(/images/home/mix_nav_bg_over3.gif)';
		}
		else{
			$(m).style.backgroundImage = 'url(/images/home/mix_nav_bg_over.gif)';
		}
		$(m).className = 'mixItalic';
		if(o != 'nil'){
			$(o).className = 'mix';
			$(o).style.backgroundImage = 'url(/images/home/mix_nav_bg.gif)';
		}
		$('sneak').style.visibility = 'hidden';
	}
}

function hideTheNut(n,m,o){
	if(n != 'nil'){
		$(n).style.visibility = "hidden"
		if(m=="cranberries" || m=="sesamemix"){
			$(m).style.backgroundImage = 'url(/images/home/mix_nav_bg2.gif)';
		}
		else if(m=="soynuts"){
			$(m).style.backgroundImage = 'url(/images/common/blank.gif)';
		}
		else{
			$(m).style.backgroundImage = 'url(/images/home/mix_nav_bg.gif)';
		}
	}
	$(m).className = 'mix';
	$(o).className = 'mixItalic';
	$(o).style.backgroundImage = 'url(/images/home/mix_nav_bg_over.gif)';
}