

$("html").addClass("js");

// global namespace
var NWI = NWI || {};

NWI.Banner = {
	fadeSpeed : "normal",
	transition: 5000,	 
	current : 0,		 
	count : 1			 
}
NWI.Banner.slide = function(imgSrc, imgAlt, imgUrl, textX, textUrl){
	this.imgSrc = imgSrc;
	this.imgAlt = imgAlt;
	this.imgUrl = imgUrl;
	this.textX = textX;
	this.textUrl = textUrl;
}
NWI.Banner.slide.prototype.loaded = false;
NWI.Banner.allLoaded = function(){
	x = true;
	$.each(NWI.Banner.slides, function(i){
		if ( NWI.Banner.slides[i].loaded === false ) {
			x = false;
		}
	})
	return x;
}
NWI.Banner.showNext = function(x){
	var nextItem;
	// just to peek ahead
	if ( x+1 == NWI.Banner.count) {
		nextItem = 0;
	} else {
		nextItem = x+1;
	}
	$("#banner img:eq(" + x + ")").fadeOut(NWI.Banner.fadeSpeed, function(){
		$("#banner img:eq(" + (nextItem) + ")").fadeIn(NWI.Banner.fadeSpeed, function(){
			if ( nextItem != 0 ) {
				$("#story-link1").attr("href", NWI.Banner.slides[x].imgUrl);
				//$("#story-link2").html(NWI.Banner.slides[x].textX);
				//$("#story-link2").attr("href", NWI.Banner.slides[x].textUrl);
			} else {
				$("#story-link1").attr("href", $("#story-link1").data("href"));
				//$("#story-link2").html($("#story-link2").data("text"));
				//$("#story-link2").attr("href", $("#story-link2").data("href"));
			}
			if ( (NWI.Banner.current + 1) >= NWI.Banner.count ) {
				NWI.Banner.current = 0;
			} else {
				NWI.Banner.current++;
			};
			NWI.Banner.loadNext(NWI.Banner.current);
		})
	});
}
NWI.Banner.loadNext = function(x){
	if ( NWI.Banner.allLoaded() === false ) {
		var slideIndex = x;
		var source = NWI.Banner.slides[slideIndex].imgSrc;
		var alt = NWI.Banner.slides[slideIndex].imgAlt;
		$("#banner img:eq(" + (slideIndex+1) + ")").load(function(){
			NWI.Banner.slides[slideIndex].loaded = true;
			setTimeout(function(){
				NWI.Banner.showNext(x);
			}, NWI.Banner.transition);
		})
		$("#banner img:eq(" + (slideIndex+1) + ")").attr("src", source).attr("alt", alt);
	} else {
		setTimeout(function(){
			NWI.Banner.showNext(NWI.Banner.current);
		}, NWI.Banner.transition)
	}
}
NWI.Banner.setup = function(){
	if ( $("#story-link1").size() === 1  ){
 //&& $("#story-link2").size() === 1) {
		/* bug B-63; FF2, the slideshow alters the anti-aliasing of all fonts, this prevents that. */
		if ($.browser.mozilla === true && parseFloat($.browser.version) < 1.9) {
		    $('body').css('opacity', 0.9999);
		}
	
		$("#story-link1").data("href", $("#story-link1").attr("href"));
		//$("#story-link2").data("text", $("#story-link2").html());
		//$("#story-link2").data("href", $("#story-link2").attr("href"));
		
		NWI.Banner.count = NWI.Banner.slides.length + 1;
		$.each(NWI.Banner.slides, function(i){
			$("#banner img:eq(0)").after("<img title='Sample Projects' />");
			$("#banner img[title='Sample Projects']").hide();
		});
		NWI.Banner.loadNext(NWI.Banner.current);
	}
}
$(window).load(function(){
	NWI.Banner.setup();
})
NWI.Banner.slides = [];


