function whit_load() {
	initMenus();
	formatDates();
	
	// Figure out exactly where we are on the site
	var is_story = false; 
	var is_sect = false; 
	
	try {
		is_story = !!(story_id != "");
		is_sect = !!(section_name != "");
	} catch (err) {}
	
	if ((!is_sect) && (!is_story)) {
		startController();
		setupBelt();
	}
	
};

Element.observe($(window),'load',whit_load);

function formatDates() {
	$$("span.whit_date").each(
		function(ele) {			
			var oldDate = ele.innerHTML;
			var newDate = Date.parse(oldDate);
			ele.innerHTML = newDate.toString('MMMM dd, yyyy');
		}
	);
}

function setupBelt() {										
	var belt = document.getElementById('whit_fp_lead_multi_inside');
	if (!belt)	{
		return false;
	}										
	
	/* remove blank children */
	for (var x = 0; x < belt.childNodes.length; x++) {
		var child = belt.childNodes[x];
		if (child.nodeType == 3 && !/\S/.test(child.nodeValue)) {
			belt.removeChild(belt.childNodes[x]);
			x--;
		}
	}
	
	var entries = belt.childNodes;
	if (entries.length == 0) {
		return false;
	}
	
	function moveBelt(directionChange)
	{										
		if (directionChange > 0) {
			var clonedTout = entries.item(entries.length - 1).cloneNode(true);
			belt.insertBefore (clonedTout, belt.firstChild);
			belt.removeChild (belt.lastChild);
		} else {
			var clonedTout = entries.item(0).cloneNode(true);
			belt.appendChild (clonedTout);
			belt.removeChild (belt.firstChild);
		}
	}

	var leftArrow = $('whit_fp_lead_multi_left');
	var rightArrow = $('whit_fp_lead_multi_right');
	leftArrow.href = 'javascript:{}';
	rightArrow.href = 'javascript:{}';										
	Event.observe('whit_fp_lead_multi_left', 'click', function () { moveBelt(-1); });
	Event.observe('whit_fp_lead_multi_right', 'click', function () { moveBelt(1); });
}

//
/****************************
* Breakout Controller	    *
****************************/
storyOffset = 0;
var fader = null;
var timer = null;
var marker_on = null;
var marker_off = null;

function startController() {

	if (!$("whit_fp_top_article")) { return; }
	
	// Preload the images for each item
	for (i = 0; i < stories.length; i++) {
		stories[i]['img'] = new Asset.image(stories[i]['img']);
	}
	
	// Inject the marker navigation elements in the layout
	for (i = 0; i < stories.length; i++) {
		link = Builder.node('a', {href:"javascript:{}",rel:i});
		Element.observe(link,'click',function() { fadeOut(this.rel); return false;});
		if (i == 0) {
			link.addClassName("marker_on");
			//link.appendChild(marker_on.cloneNode(true));
			$("whit_fp_top_article_count_markers").appendChild(link)
		} else {
			link.addClassName("marker_off");
			//link.appendChild(marker_off.cloneNode(true));
			$("whit_fp_top_article_count_markers").appendChild(link)
		}
	}
	
	// Start the fading process
	timer = window.setTimeout("fadeOut()", 8000);
}

// Fade out the current block 
function fadeOut(offset) {
	// Make the navigation element show not active
	marker = $$("#whit_fp_top_article_count_markers a")[storyOffset];
	
	if ( marker ) {
		marker.removeClassName("marker_on");
		marker.addClassName("marker_off");
	
		// If the change was requested bye the user, reset the timer
		if (offset != null) { 
			window.clearTimeout(timer)
			storyOffset = offset - 1;
		} 
		new Effect.Opacity('whit_fp_top_article', { duration:0.5, from:1.0, to: 0.0} );
		timer = window.setTimeout("swapContent()", 800);		
	}
}

// Swap out the content for the next item
function swapContent() {
	// increment the story ofset
	storyOffset = (storyOffset + 1 ) % stories.length;
	
	//Change out all of the necessary content
	//$$("#whit_fp_top_article_img img")[0].remove();
	//$("#whit_fp_top_article_img a").appendChild(stories[storyOffset]['img']);
	$$("#whit_fp_top_article a").each(function(ele) { ele.href = stories[storyOffset]["link"]});
	$$("#whit_fp_top_article_img a")[0].href = stories[storyOffset]["link"];
	$$("#whit_fp_top_article_img a img")[0].src = stories[storyOffset]["img"].src;
	//$("di_featured_scroller_date").innerHTML = stories[storyOffset]["timelastUpdated"]+" "+stories[storyOffset]["datelastUpdated"];
/*	if (stories[storyOffset]['commentsEnabled']) {
		$("di_fp_scroller_comment").innerHTML = commentTemplate.evaluate({count:stories[storyOffset]["commentCount"]});
		$('di_featured_scroller_comment').show();

	} else {
		$('di_featured_scroller_comment').hide();
	}*/
	
	// Activate the correct marker in the navigation element
	marker = $$("#whit_fp_top_article_count_markers a")[storyOffset];
	
	marker.removeClassName("marker_off");
	marker.addClassName("marker_on");
	
	timer = window.setTimeout("fadeIn()", 15);
}

// Fade the content back in
function fadeIn() {
	new Effect.Opacity('whit_fp_top_article', { duration:0.5, from:0.0, to: 1.0} );
	
	timer = window.setTimeout("fadeOut()", 8000);
}
//

var Asset = {
	image: function(src,properties) {
		var image = new Image();
		image.src = src;
		var element = Builder.node('img',{src: src});
		return element;
	}
}

/*
	Expandable Listmenu Script
	Author : Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
*/

function initMenus() {
	if (!document.getElementsByTagName) return;
	
	var aMenus = document.getElementsByTagName("LI");
	for (var i = 0; i < aMenus.length; i++) {
		var mclass = aMenus[i].className;
		if (mclass.indexOf("whit_fp_expand_headlines") > -1) {
			var submenu = aMenus[i].childNodes;
			for (var j = 0; j < submenu.length; j++) {
				if (submenu[j].tagName == "A") {
					
					submenu[j].onclick = function() {
						var node = this.nextSibling;
											
						while (1) {
							if (node != null) {
								if (node.tagName == "UL") {
									var d = (node.style.display == "none")
									node.style.display = (d) ? "block" : "none";
									this.className = (d) ? "whit_fp_expand_headlines_open" : "whit_fp_expand_headlines_closed";
									return false;
								}
								node = node.nextSibling;
							} else {
								return false;
							}
						}
						return false;
					}
					
					submenu[j].className = (mclass.indexOf("open") > -1) ? "whit_fp_expand_headlines_open" : "whit_fp_expand_headlines_closed";
				}
				
				if (submenu[j].tagName == "UL")
					submenu[j].style.display = (mclass.indexOf("open") > -1) ? "block" : "none";
			}
		}
	}
}