var homebg = ["bg1", "bg2", "bg3"];
var fader;
var current = 0;
function xfade(){
	var next = current + 1;
	if(next == homebg.length){
		next = 0;
	}
	fader.attr("class",homebg[next]);
	fader.fadeIn(1500, function(){
		$('body').removeClass(homebg[current]);
		current = next;
		$('body').addClass(homebg[current]);
		fader.hide();
		if(current != homebg.length-1){
			setTimeout("xfade()",10000);
		}
	});
}


$(document).ready(
	function() {

		// Animated mainnav backgrounds - uses jquery.bgpos plugin.
		$('.mainnav li:not(.active) a').each(function(){
			var inactivepos = eval($(this).width()-540)+"px";
			$(this).css( {backgroundPosition: inactivepos+" 0"} )
				.mouseover(function(){
					$(this).stop().animate({backgroundPosition:"(0 0)"}, {duration:600});
				})
				.mouseout(function(){
					$(this).stop().animate({backgroundPosition:"("+ inactivepos +" 0)"}, {duration:200, complete:function(){
						$(this).css({backgroundPosition: inactivepos+" 0"});
					}
				})
			});
		});


		// Open links with rel="external" in new window - like ye ole' target="_blank"
		$('a[rel="external"]').click(function() {window.open( $(this).attr('href') ); return false;});


		// Zebra-stripe data tables
		$("table.data tbody").each(function() {
			$(this).removeClass("odd even");
			$("tr:odd", this).addClass("odd");
			$("tr:even", this).addClass("even");
		});


		// Initialize Lightbox
		$(".lightbox").lightbox();


		// HomeBG Fader
		$("body").prepend('<div id="homefader1"></div>');
		fader = $("#homefader1");
		setTimeout("xfade()",10000);
		$(".homefader.bg1").fadeOut('slow');


	}
);
