// Rotating banner javascript

var currentBanner = 0;
var aniTime = 5000;
var stopped = false;
var active = false;
var menuWidth = 0;
var flyoutWidth = 0;
var tID = 0;
var aLink = "";
var simpleUI = false;
	
$(document).ready(function( ) {
 	if ($('#RotBannerContainer').length == 0) {
		return;
	}
 	if ($.browser.msie && $.browser.version < 6)  {
		 
		return;
	}
	initializeBanners();
	initializeImages();
 	$('#RotBannerMenu li a').bind('click', rotMenu); 
	$('#RotBannerMediaContainer').bind('click', goLink); 
	menuWidth = $('#RotBannerMenuContainer').css('width');
	flyoutWidth = $('#RotBannerMenuFlyout').css('width');
	showBanner(1);
	//showFlyout();
	//$('#RotBannerContainer').bind('mouseenter', entered);
	//$('#RotBannerContainer').bind('mouseleave', left);
	$('#RotBannerContainer').hover(entered, left);
	startTimer();
});

function initializeBanners() {
	if (!simpleUI) {
		$('.RotBanner p').hide();
	}
	//$('.RotBanner:gt(0)').hide();
	$('.RotBanner').hide();
 
}

function entered() {
	stopTimer();
	//hideFlyout();
	//showMenu();
}

function left() {	
	//hideMenu();
	//showFlyout();
	startTimer();
}

function startTimer() {
	stopped = false;
	 tID = window.setTimeout(function() {
 		fireTimeout();
	}, aniTime);
};

function stopTimer() {
	stopped = true;
	clearTimeout(tID);
	tID = 0;
}

function fireTimeout() {
	if (stopped) {
		return;
	}
	var nextBanner = currentBanner + 1;
	if (nextBanner > ($('.RotBanner').size()) ) {
		nextBanner = 1;
	}
	showBanner(nextBanner);
	if (!stopped) {
		tID = window.setTimeout(function(){
			fireTimeout();
		}, aniTime);
	};		
};

function rotMenu( evt ) {
	var menuID = $(this).attr('id');
	var newBanner = menuID.substring((menuID.length-1), menuID.length);
	showBanner(newBanner);
	return(false);
	
};

function showLoading() {
	$('#loadIndicator').show();
}

function hideLoading() {
	$('#loadIndicator').hide();
}

function activateMenu( id ) {
 	$('#RotBannerMenu li a').removeClass('activeMenu'); 
	$('#RotBannerMenu' + id).addClass('activeMenu');
}

function showBanner( id ) {
	if (active) {
		return;
	}	
	if (currentBanner == id) {
		return;
	}
	active = true;
	activateMenu( id );
	var bannerID = "#RotBanner" + id;
	assertImage(bannerID, "#RotBannerMenu" + id, true);
	fadeBanners(currentBanner, id);
	setLink( bannerID );
	currentBanner = id;
	active = false;
}

function fadeBanners( oldID, newID) {	

	var oldb = "#RotBanner" + oldID;
	var newb = "#RotBanner" + newID;
	if (simpleUI) {
		$(newb).fadeIn(800);
		$(oldb).fadeOut(800);
		return; 
	}
	if (newID > oldID) { /* New one on top, fade that in slower */
		$(newb).fadeIn(800, function() {
			$(newb + ' p').slideDown();
			$(oldb + ' p').hide();
			$(oldb).hide()
		});

	} else { /* Old one on top, fade that out slower */
		$(newb).fadeIn(400);
		$(oldb +' p').fadeOut(400, function() {
			$(oldb).fadeOut(600, function() {$(newb + ' p').slideDown();});			
		});	
	}
	
}

function setLink( bannerID ) {
	var lnk = $(bannerID + " .autolink");
	if (lnk.length > 0) {
		aLink = lnk.attr("href");
		$('#RotBannerMediaContainer').addClass("pointer");
	} else {
		$('#RotBannerMediaContainer').removeClass("pointer");
	}
}

function goLink() {
	if (aLink) {
		window.open(aLink, "_self");
	}
}

function assertImage( bannerID, menuID, showLoadIndicator) {
	if (( $(bannerID + " img").size()) > 0) {
		return; /* there is already an image */
	} else {
		if (showLoadIndicator) {showLoading();};
		var imgSrc = $(menuID).attr('href');
		var img = new Image();
        $(img).load(function () {
            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
            //$(this).hide();
			if (($(bannerID + " img").size()) <= 0) {
				$(bannerID).prepend(this);
			hideLoading();
			}
        }).error(function () {
			hideLoading();
            console.log("could not load the image");
        }).attr('src', imgSrc);
	}
}

function initializeImages() {
	$('.RotBanner').each( function() {
		var id = $(this).attr('id');
		assertImage("#"+id, "#RotBannerMenu" + (id.substring((id.length-1), id.length)), false);
	});
		
}

function showMenu() {
	$('ul#RotBannerMenu').hide();
	$('#RotBannerMenuContainer').css({width:"0"});
	$('#RotBannerMenuContainer').animate({width: menuWidth}, 200, function() {
 		$('ul#RotBannerMenu').fadeIn();
	});
	
}


function hideMenu() {
	$('ul#RotBannerMenu').fadeOut(100, function() {
		$('#RotBannerMenuContainer').animate({width: "0"}, 200);
	});
}

function showFlyout() {
	$('#RotBannerMenuFlyout').css({width:"0"});
	$('#RotBannerMenuFlyout').animate({width: flyoutWidth}, 500);
}

function hideFlyout() {
	$('#RotBannerMenuFlyout').animate({width: "0"}, 200);
	
}

