/*global $ */

function INDEXPAGE() {
	
	var cbBanners = ["madden", "lrg"], pauseBanner = false, timeoutId, curBanner = 0, rotateBanner, player, videoLinkImage;
		
	function showBanner(bannerToShow) {
		var lastBanner, nextBannerName;							 				 
		clearTimeout(timeoutId);
		lastBanner = curBanner;
		curBanner = parseFloat(bannerToShow);
		$('.bannerRotator').css("backgroundImage", 'url(../images/index/banner_rotator/' + $('.bannerRotator a.mainBannerHolder:eq(' + lastBanner + ')').attr("name") + '.jpg)');
		$('.bannerRotator a').css('display', 'none');
		nextBannerName = $('.bannerRotator a.mainBannerHolder:eq(' + curBanner + ')').attr("name");
		$('.bannerRotator [name=' + nextBannerName + ']').fadeIn(1000);		
		$('#bannerRotatorNav').attr('class', 'show' + curBanner);	
		timeoutId = setTimeout(function () {
			rotateBanner();
		}, 7000);
	}
	
	rotateBanner = function () {
		if (!pauseBanner) {
			var bannerRotation = curBanner;
			if (bannerRotation >= 4) {
				bannerRotation = 0;
			} else {
				bannerRotation = bannerRotation + 1;
			}
			showBanner(bannerRotation);
		} else {
			timeoutId = setTimeout(function () {
				rotateBanner();
			}, 7000);
		}
	};
	
	function loadBannerRotator() {
		var  nextBannerName;	
		$('#bannerRotatorNav').attr('class', 'show0');
		$('.bannerRotator a').each(function () {
			$(this).css("backgroundImage", 'url(/images/index/banner_rotator/' + this.name + '.jpg)');
		});
		nextBannerName = $('.bannerRotator a:eq(' + curBanner + ')').attr("name");
		$('.bannerRotator [name=' + nextBannerName + ']').fadeIn(1000, function () {
			$('.bannerRotator img.bgHolder').css('display', 'none');													 
		});
		timeoutId = setTimeout(function () {
			rotateBanner();
		}, 7000);
	}
	loadBannerRotator();
	
	function loadCorporateBanner() {
		var newRandomBannerNumber = Math.round((cbBanners.length - 1) * Math.random());
		$('#corporateBanner img').attr('src', '/images/index/medium_banners/' + cbBanners[newRandomBannerNumber] + '.png');
		$('.mediumSubBanners a').fadeIn(500);
		$('.smallSubBanners a').fadeIn(500);
	}
	loadCorporateBanner();
	
	function attachEvents() {
		
		$('#bannerRotatorNav a').click(function () {					 
			showBanner(this.id.slice(-1));	
			return false;
		});
		
		$('.bannerRotator').hover(function () {
			pauseBanner = true;							   
		}, function () {
			pauseBanner = false;
		});
				
		$('#stealthVideo ').click(function () {		
			if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))  {
				document.location.href = "flowplayer/movies/colorweek.m4v";
			}
			return false;
		});
	}
	attachEvents();
	/*	
	var myCWCOMMON = new CWCOMMON();
	myCWCOMMON.setupProductVideo();
*/
}

$(document).ready(function () {
	INDEXPAGE();
});

