// JavaScript Document

var slides, rotator_btns, slide_bgs = ['slide1','slide2','slide3','slide4','slide5'];
var imageRotatorBox;
var homeImageRotatorBox;
var imageURLforBanner;
var intervalTime = 6000;

function initializeRotator() {
	slides = $("div#rotator div.bannerInfo");
	rotator_btns = $("#rotator_btns > div");
	slides.css({opacity: 0.0}).removeClass('hidden'); //Set the opacity of all images to 0
	slides.eq(0).css({opacity: 1.0}); //Get the first image and display it	
	
	//Set banner background for SubSite Home Page
	imageRotatorBox = $('#how_we_help_box');
	SetBackgroundforSubsiteBanner(0);
	
	//Set Banner background for Home Page
	homeImageRotatorBox = $('#feature_main');
	SetBackgroundforHomeBanner(0);
}

function SetBackgroundforHomeBanner(currentBanner)
{
	if(homeImageRotatorBox!= null)
	{
		imageURLforBanner = slides.eq(currentBanner).find(".hideBackGroundImage img").attr("src");	//Find Image conatrol and src of it
		if(imageURLforBanner == null)
		{
			imageURLforBanner = "/Style Library/images/background-feature-main-1.jpg"
		}
		imageURLforBanner  = "url('" + imageURLforBanner  + "') no-repeat 0 0";	//generate CSS for banner background
		homeImageRotatorBox.css("background", imageURLforBanner);	//Set background for div
	}
}

function SetBackgroundforSubsiteBanner(currentBanner)
{
	if(imageRotatorBox != null)
	{
		imageURLforBanner = slides.eq(currentBanner).find(".hideBackGroundImage img").attr("src");	//Find Image conatrol and src of it
		if(imageURLforBanner == null)
		{
			imageURLforBanner = "/Style Library/images/how_we_help_bg-1.jpg"
		}
		imageURLforBanner  = "url('" + imageURLforBanner  + "') top center no-repeat";	//generate CSS for banner background
		imageRotatorBox.css("background", imageURLforBanner);	//Set background for div
	}
}


function rotate() {
	var current, next, last = slides.length - 1;
	current = slides.index($("#rotator .show"));
	next = (current < last) ? (current + 1) : 0;
	slides.removeClass('show');
	slides.eq(next).addClass('show').css({opacity: 0.0}).animate({opacity: 1.0}, 1000);

	//$("#feature_main").attr('class', slide_bgs[next]);
	//$("#how_we_help_box").attr('class', slide_bgs[next]);
	SetBackgroundforHomeBanner(next);
	SetBackgroundforSubsiteBanner(next);

	slides.eq(current).animate({opacity: 0.0}, 1000);
	rotator_btns.eq(next).addClass('current');
	rotator_btns.eq(current).removeClass('current');
}


$(document).ready(function() {
	//Set default value for Image rotation
	initializeRotator();
	//If slides are more than 1 than set timer, otherwise no need of timer
	if(slides.length > 1)
	{
		var intervalImg = setInterval('rotate()', intervalTime);
	}
	
	$('#main_navigation ul li a,#search button').hover(function(){
		$(this).addClass('on');
	}, function(){
		$(this).removeClass('on');
	});
	
	$('#pausePlayBtn').toggle(function() {
		clearInterval(intervalImg);
		$(this).attr('src','/Style Library/images/feature_button_play.gif');
	}, function() {
		intervalImg = setInterval('rotate()', intervalTime);
		rotate();
		$(this).attr('src','/Style Library/images/feature_button_pause.gif');
	});

	$('#rotator_btns div').bind('click', function() {
		clearInterval(intervalImg);
		//Set Text of banner
		$("div#rotator div.bannerInfo").removeClass('show').animate({opacity: 0.0}, 250);
		$("div#rotator div.bannerInfo").eq($(this).index()).css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 500);
		
		//Set Background of Image
		//alert($(this).index());
		SetBackgroundforHomeBanner($(this).index());
		SetBackgroundforSubsiteBanner($(this).index());
		
		//Set button Image
		$('#rotator_controls #rotator_btns .current').removeClass('current');
		$(this).addClass('current');
		
		//Change play pause button to click
		$('#pausePlayBtn').attr('src','/Style Library/images/feature_button_play.gif');
		//intervalImg = setInterval('rotate()', 4000);
	});
	
	$('#who_we_help ul li').hover(function(){
			var w = $('#who_we_help ul li').index(this);
			$('#who_people div:eq('+w+')').css('background-position','center bottom');
		},function(){
			var w = $('#who_we_help ul li').index(this);
			$('#who_people div:eq('+w+')').css('background-position','center top');
		});
	$('#who_people div').hover(function(){
			var w = $('#who_people div').index(this);
			$('#who_we_help ul li:eq('+w+')').addClass('on')
			$(this).css('background-position','center bottom');
		},function(){
			var w = $('#who_people div').index(this);
			$('#who_we_help ul li:eq('+w+')').removeClass('on')
			$(this).css('background-position','center top');
		});
});
