// Array of banner objects
var banners = new Array (
	{ 
		message: 'Still Confused about your Major Project !!', 
		images: new Array
					(
						"./common/images/home/banners/degrees.jpg",
						"./common/images/home/banners/ggurug9_frsaurabh copy.jpg"
					), 
		textColor: '#FFFFFF',
		backgroundColor: '#426c9f',
		backgroundOpacity: '0.8'
	},
	{ 
		message: 'Unable to fit the Company Criteria',
		images: new Array
					(
						"./common/images/home/banners/faculty.jpg", 
						"./common/images/home/banners/ggurug6_frsaurabh copy.jpg"
					),
		textColor: '#FFFFFF',
		backgroundColor: '#272928',
		backgroundOpacity: '0.8'
	},
	{
		message: 'Weak or not Confident with your technical skills',
		images: new Array
					(
						"./common/images/home/banners/championships.jpg",
						"./common/images/home/banners/ggurug7_frsaurabh copy.jpg"
					),
		textColor: '#FFFFFF',
		backgroundColor: '#940000',
		backgroundOpacity: '0.8'
	},
	{ 
		message: 'Find a way through Java,[.]NET and Oracle', 
		images: new Array
					(
						"./common/images/home/banners/ggurug9_frsaurabh copy.jpg",
						"./common/images/home/banners/studyabroad.jpg"
					), 

		textColor: '#FFFFFF',
		backgroundColor: '#4964B1',
		backgroundOpacity: '0.8'
	},

	{ 
		message: 'Still Confused about your Major Project !!', 
		images: new Array
					(
						"./common/images/home/banners/degrees.jpg",
						"./common/images/home/banners/ggurug8_frsaurabh copy.jpg"
					), 

		textColor: '#FFFFFF',
		backgroundColor: '#4964B1',
		backgroundOpacity: '0.8'
	},

	{
		message: 'Weak or not Confident with your technical skills',
		images: new Array
					(
						"./common/images/home/banners/faculty.jpg",
						"./common/images/home/banners/ggurug7_frsaurabh copy.jpg"
					),
		textColor: '#FFFFFF',
		backgroundColor: '#940000',
		backgroundOpacity: '0.8'
	});

var delayImage = 3;
var delayMessage = 1;

/////////////////////////////////////////////////////////////////

function decideNext(){
	if(!messageLoaded){
		if(currentImage == 1){
			timer = setTimeout('loadMessage()', delayMessage*1000);
		}else{
			//timer = setTimeout('loadImage()', 2*1000);
			loadImage();
		}
	}else if(currentImage == banners[currentBanner].images.length){
		currentBanner++;
		if(currentBanner >= banners.length){
			currentBanner = 0;
		}
		if(currentBanner != randomStart){
			timer = setTimeout('unload()',delayImage*1000);
		}
	}else{
		timer = setTimeout('loadImage()',delayImage*1000);
	}
	
	/*
	if(currentImage == 1  && !messageLoaded)
	{
		timer = setTimeout('loadMessage()', delayMessage*1000);
		//loadMessage();
	}else if(currentImage == banners[currentBanner].images.length){
		currentBanner++;
		if(currentBanner != randomStart){
		timer = setTimeout('unload()',delayImage*1000);
		}//unload();
	}else{
		timer = setTimeout('loadImage()',delayImage*1000);
		//loadImage();
	}*/
}

function loadMessage(){
	clearTimeout(timer);
	$('bannerTextInner').innerHTML = banners[currentBanner].message;
	$('bannerText').style.color = banners[currentBanner].textColor;
	$('bannerTextBack').style.backgroundColor = banners[currentBanner].backgroundColor;
	//Element.setOpacity('bannerTextBack', banners[currentBanner].backgroundOpacity);
	messageLoaded = true;
	new Effect.Parallel(
		[
			new Effect.Appear('bannerText', {sync:true}),
			new Effect.Appear('bannerTextBack', {sync:true, to: banners[currentBanner].backgroundOpacity})
		],
		{
			duration: 1.5,
			transition: Effect.Transitions.linear,
			fps: 10,
			afterFinish: decideNext,
			queue:{position:'end', scope: 'banner'}
		}
	);
	
}

function unload(){
	clearTimeout(timer);
	messageLoaded = false;
	currentImage = 0;
	//currentBanner++;
	new Effect.Parallel(
		[
			new Effect.Fade('bannerText', {sync:true}),
			new Effect.Fade('bannerTextBack', {sync:true})
		],
		{
			duration: 1.0,
			transition: Effect.Transitions.linear,
			fps: 10,
			afterFinish: decideNext,
			queue:{position:'end', scope: 'banner'}
		}
	);
	//new Effect.Opacity('bannerMessage', {duration: 3.0, from:1.0, to: 0.0, afterFinish: decideNext, queue:{position:'end', scope: 'banner'}});
}

function loadImage(){
	clearTimeout(timer);
	if(firstDiv){
		activeBanner = $('banner1');
		nextBanner = $('banner2');
	}else{
		activeBanner = $('banner2');
		nextBanner = $('banner1');
	}
	nextBanner.firstChild.src = banners[currentBanner].images[currentImage];
	firstDiv = !firstDiv;
	currentImage++;
	new Effect.Parallel(
		[	new Effect.Opacity(activeBanner, {to: 0.0, sync: true}),
			new Effect.Opacity(nextBanner, {to: 1.0, sync: true})],
		{ 
			duration: 1.0,
			transition: Effect.Transitions.linear,
			fps:10,
			afterFinish: decideNext, 
			queue:
			{
				position:'end', 
				scope: 'banner'
			}
		});
}

/*function setTimer(){
	timer = setTimeout('next_banner()',time*1000);
}*/

function initBanner(){
	firstBanner = Builder.node
					('div',
						{
							id:'banner1',
							style:'display:none'
						},
						[
							Builder.node
							('img',
								{
									src: '',
									className:'bannerImg',
									alt: 'Welcome to GguruG!'
								})
						]
					);
	secondBanner = Builder.node
					('div',
						{
							id:'banner2',
							style:'display:none'
						},
						[
							Builder.node
							('img',
								{
									src: '',
									className:'bannerImg',
									alt: 'Welcome to GguruG!'
								})
						]
					);
	bannerMessage = Builder.node
					('div',
						{
							id:'bannerMessage'
						},
						[
							Builder.node
							('div',
								{
									id:'bannerText',
									style:'display:none'
								},
								[
									Builder.node
									('div',
										{
											id:'bannerTextInner'
										}
									)
								]
							),
							Builder.node
							('div',
								{
									id:'bannerTextBack',
									style:'display:none'
								}
							)
						]
					);
					
	$('banner').appendChild(firstBanner);
	$('banner').appendChild(secondBanner); 				
	$('banner').appendChild(bannerMessage);
	
	Element.setOpacity(firstBanner, 0.0);
	Element.setOpacity(secondBanner, 0.0);
					
	Element.show(firstBanner);
	Element.show(secondBanner);
 
	
	loadImage();
	
}

function get_random(){
	var current = Math.round((banners.length-1)*Math.random());
	return current;
}

firstDiv = true;
messageLoaded = false;
timer = 0;
randomStart = get_random();
currentBanner = randomStart;
currentImage = 0;
Event.observe(window, 'load', initBanner, false);

