// preload images
// adapted from http://www.mattfarina.com/2007/02/01/preloading_images_with_jquery
jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

// main menu
$(document).ready(function() {
	// preload menu images
	$("#navigation ul li img").load(function() {
		$.preloadImages($(this).attr("src").replace("up", "over"));
	});
	// rollovers
	$("#navigation ul li img").hover(function(){
		$(this).attr("src", $(this).attr("src").replace("up", "over"));
		return false;
	},function(){
		$(this).attr("src", $(this).attr("src").replace("over", "up"));
		return false;
	});
});

// sub menu
// adapted from http://jquery.com/blog/2006/11/14/expandable-sidebar-menu-screencast/
$(document).ready(function(){			
	$("#content-side-menu ul li ul:not(.currentsection)").hide();
//	$("#content-side-menu ul li a.section").click(function(){
//		return false;
//	});
	$("#content-side-menu ul li a.section img.arrow").click(function(){
		$("#content-side-menu ul li ul:visible").slideUp("normal");
		$("#content-side-menu ul li img.arrow").attr("src", "assets/img/sub-menu/arrow-right.gif");
		$(this).parent().next().slideDown("normal");
		$(this).attr("src", "assets/img/sub-menu/arrow-down.gif");
		return false;
	});
});

// news scroller
// adapted from http://www.learningjquery.com/2006/10/scroll-up-headline-reader
var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

$(document).ready(function(){
	headline_count = $("div.article").size();
	$("div.article:eq("+current_headline+")").css('top','5px');
	headline_interval = setInterval(headline_rotate,10000); //time in milliseconds
	$('#content-news').hover(function() {
		clearInterval(headline_interval);
	}, function() {
		headline_interval = setInterval(headline_rotate,10000); //time in milliseconds
		headline_rotate();
	});
});

function headline_rotate() {
	current_headline = (old_headline + 1) % headline_count;
	$("div.article:eq(" + old_headline + ")").fadeOut("slow", function() {
		$(this).css('top','210px');
	});
	$("div.article:eq(" + current_headline + ")").show().animate({top: 5},"slow");
	old_headline = current_headline;
}

$(document).ready(function(){
	// drop shadow
	$(".dropshadow").wrap("<div class='wrap0'><div class='wrap1'><div class='wrap2'>" + "<div class='wrap3'></div></div></div></div>");
	// add target attribute to make links with class of newwindow open in a new window.
	$("a.newwindow").attr("target", "_blank");
});

// clickable search button
$(document).ready(function(){
	$("a.searchTxt").click(function(){
		$("form")[0].submit();
		return false;
	});
});



// sifr dynamic text replacement
sIFR.replaceElement(named({sSelector:"h1",sFlashSrc:"assets/swf/rotissemiserif.swf",sColor:"#0d4880",sCase:"upper"}));