// JavaScript Document

/*
Code by: Marc Louie Mancilla
For: A-Plus English Online
Date: 11/09/2010
*/

var duration = 10000,
	timer = null,
	index = 0;

var childTimer = null,
	childIndex = 0,
	childDuration = 1000;

function displayBannerDescription(){
	
	var _bannerCollection = $("div.wImg"), 
		description = null;
	
	if(_bannerCollection.eq(index).is(":visible")){
		
		description = _bannerCollection.eq(index).find('div.collection div');
		description.eq(childIndex).hide();
		
		if($.browser.msie)
			description.css('background-color','white');
		
		childDuration = 1000; 
		
		if(childIndex < 2){
			description.eq(childIndex).fadeIn(500);
		} else {
			description.fadeOut(500);
			description.eq(childIndex).fadeIn(500);
		}
	}
	
	childIndex ++;
	
	if(childIndex == 2)
		childDuration = 3000;
	
	// description.eq(childIndex).hide();
	
	if(childIndex == description.length && description != null){ 
		childIndex = 0;

		// increment index (for fading the next banner):
		index++;
	}
	
	// if index is at equal number of _bannerCollections, 
	// equate to zero to start over the beginning banner:
	if(index == _bannerCollection.length) 
		index = 0;
	
	clearInterval(childTimer);
	
	if(childIndex != 0)
		childTimer = setInterval("displayBannerDescription()", childDuration);

}

// function rotate banners:
function displayBanner(){
	
	// specify the class of banners:
	var bannerCollection = $("div.wImg");
	
	// check if the corresponding banner is NOT visible
	if(!bannerCollection.eq(index).is(":visible")){
		
		// hide all banners:
		bannerCollection.hide();
		
		bannerCollection.eq(index).find('div.collection div').hide();
		
		// alert(bannerCollection.eq(index).find('div.collection div').length);
		
		// fade in the corresponding banner:
		bannerCollection.eq(index).fadeIn(1000, function(){
			displayBannerDescription();
		});
		
	}
	// alert('calling..');
	// reset the timer:
	clearInterval(timer);
	timer = setInterval("displayBanner()", duration);
	
}

$(document).ready(function(){
	// start displaying banner:
	displayBanner();
});
