var splashTimeout = null;
var sectionTypes = ["home"];
var scrollRate = 22;
$j = jQuery.noConflict();

$j(document).ready(function(){
	Initialise();
});

// Initialise all javascript enhancements for the current page
function Initialise(){
	// Enable PNG transparency fix for IE6
	$j('body').supersleight();
	
	// Enable Rollover effects
	startFlowplayer();
	setRollovers();
	scrollNews();
	
	if(currentStateIs("section", "home")&&currentStateIs("page", "home")) 
		InitialiseSplashPane();
	
	/*// Enable section specific enhancements 
	switch(getSectionType())
	{
		case "home":	
			InitialiseHomeSection();
			break;
		
		case "default":
			break;
	}*/
}

// Initialise the looping splash pane on the home page
function InitialiseSplashPane(){
	setSplashButtons("splashpane1");
	splashTimeout = setTimeout(function(){rotateSplashNews("splashpane1", 0)}, 7000);
}

// Set the splash buttons to force a selected item on hover and start loop again on mouse off
function setSplashButtons(source){
	$j("#"+source+" .button").each(function (i, splashbutton) {
		$j(this).mouseover(function(){
			focusSplashNewsItem(source, i);
		});	
		
		$j(this).mouseout(function(){
			splashTimeout = setTimeout(function(){rotateSplashNews(source, i)}, 3000);
		});			
	});
	
	$j("#"+source+" .mainimage").attr("href", $j("#"+source+" .item1button").attr("href"));
	$j("#"+source+" .text").attr("href", $j("#"+source+" .item1button").attr("href"));	
}

// Stop transition rotation in splash news and focus one selected item
function focusSplashNewsItem(source, target){
	clearTimeout(splashTimeout);
	
	$j("#"+source+" .item").each(function (i, newsitem) {
		if(i==target){
			$j("#"+source+" #"+newsitem.id+" .image").css('opacity', 1);
			$j("#"+source+" ."+newsitem.id+"button").addClass(newsitem.id+"active");
			$j("#"+source+" #"+newsitem.id).addClass("active");	
			$j("#"+source+" .mainimage").attr("href", $j("#"+source+" ."+newsitem.id+"button").attr("href"));
			$j("#"+source+" .text").attr("href", $j("#"+source+" ."+newsitem.id+"button").attr("href"));
		}
		else{
			$j("#"+source+" #"+newsitem.id+" .image").css('opacity', 0);
			$j("#"+source+" ."+newsitem.id+"button").removeClass(newsitem.id+"active");
			$j("#"+source+" #"+newsitem.id).removeClass("active");				
		}
	});
}

// Rotate transitions between splash news items
function rotateSplashNews(source, current){
	var target = current+1;
	var items = $j("#"+source+" .item").toArray();
	
	if(target >= $j("#"+source+" .item").length)
		target=0;
	
	var infunc = function () {
			$j("#"+source+" ."+items[current].id+"button").removeClass(items[current].id+"active");
			$j("#"+source+" #"+items[current].id).removeClass("active");
		};
	
	var outfunc = function () {
			$j("#"+source+" ."+items[target].id+"button").addClass(items[target].id+"active");
			$j("#"+source+" #"+items[target].id).addClass("active");
			$j("#"+source+" .mainimage").attr("href", $j("#"+source+" ."+items[target].id+"button").attr("href"));
			$j("#"+source+" .text").attr("href", $j("#"+source+" ."+items[target].id+"button").attr("href"));			
			splashTimeout = setTimeout(function(){rotateSplashNews(source, target)}, 7000);
		};
	
	transition("#"+source+" #"+items[current].id+" .image", "#"+source+" #"+items[target].id+" .image", infunc, outfunc, 400);
}

// Setup news scroller
function scrollNews(){
	var source = $j(".latestnewsscroll .scroller");
	var duration = (source.width()/scrollRate)*1000;
	
	scroll(source, duration, $j(".latestnewsscroll .scrollcontainer").width());	
}
