google.load("jquery", "1.4");

google.setOnLoadCallback(function() {
	
	accessibility_init();
	
	nav_init();
	
	banners_init();
		
});

function banners_init () {
	var banners = $('#banner div');
	
	banners.css('position', 'absolute');
	
	if(banners.length > 1){
		//Bring the first banner to the front
		banners.eq(0).css({'zIndex': '2'});
		
		animate_banner(banners);
	}
}

var banner_position = 0;
var banner_delay = 5000;
var banner_transition = 2000;

function animate_banner (banners) {
	
	//If the current position is the last item, next item is first array position
	if(banner_position == (banners.length-1)){
		var next = banners.eq(0);
		var current = banners.eq(banner_position);
		
		banner_position = 0;
	}else{
		var next = banners.eq(banner_position + 1);
		var current = banners.eq(banner_position);
		
		banner_position++;
	}
	
	
	next.css({'zIndex': '1'}).fadeIn(0);
	
	current.css('zIndex', '2').delay(banner_delay).fadeOut(banner_transition, function(){
		$(this).css({'zIndex': '0'})
		
		animate_banner(banners);
	});

}

function accessibility_init()
{
	//Add the buttons
	$('#nav').after('<div id="accessibility"><h2 class="hide">Accessibility Controls</h2><a id="text_only" href="#">Text only</a> <a href="#" id="a1">A</a> <a href="#" id="a2">A</a> <a href="#" id="a3">A</a></div>');
	
	//Font size buttons
	$('#a1').click(function(){
		$('body').css('font-size', '75%');
		return false;
	});
	
	$('#a2').click(function(){
		$('body').css('font-size', '85%');
		return false;
	});
	
	$('#a3').click(function(){
		$('body').css('font-size', '100%');
		return false;
	});
	
	//Text only button
	var css = "";
	
	$('#text_only').click(function(){
		if(css == "")
		{
			css = $('link#css').attr('href');
			$('link#css').attr('href', '');
			
			//Change the buttons
			$(this).text('Styled version');
			$("#a1").text('(Normal text - ');
			$("#a2").text('Medium text - ');
			$("#a3").text('Large text)');
		}else{
			$('link#css').attr('href', css);
			css = "";
			
			//Change the buttons back
			$(this).text('Text only');
			$("#a1").text('A');
			$("#a2").text('A');
			$("#a3").text('A');
		}
		
		return false;
	});
}

function nav_init () {
	//Add the parent class to each li element above the "active" li
	$("#subnav li.active").parents('li').each( function(){
		$(this).addClass("parent");
	});
	
	//Get rid of any empty ul elements (this is due to the recursive way we are embedding the templates results in empty ul elements)
	$("#subnav ul").each(function(){
		if($(this).children().length == 0)
		{
			$(this).remove();
		}
	});
	
	//Add a special flag to top level navigation with no children
	$("#subnav > li").each(function(){
		if($(this).children('ul').length == 0)
		{
			$(this).addClass('noChildren');
		}
	});
	
	//We want to inject the parent of the current page into the main page title, EE has no idea how to do this
	// so we check the navigation with JS and do it here
	/*
	if($("#subnav > li.active").length != 1)
	{
		var a = $("#subnav li.active").closest("#subnav > li").children("a");
		
		if($(a).text() != "")
		{
			$(".header h1").prepend("<span>" + $(a).text() + "</span>");
		}
	}*/
}




















