// JavaScript Document

var picInterval = 3;	//seconds
var fadeDuration = 1000; //milliseconds
var spaceOrder = [1, 2, 3, 4];
var currentSpace = 0;
var visibleGroup = 0;
var flag = false;

var groups = []
groups[0] = group1 = new Array();
group1[1] = '/images/banner_slides/slideshow1_1.jpg';
group1[2] = '/images/banner_slides/slideshow2_1.jpg';
group1[3] = '/images/banner_slides/slideshow3_1.jpg';
group1[4] = '/images/banner_slides/slideshow4_1.jpg';

groups[1] = group2 = new Array();
group2[1] = '/images/banner_slides/slideshow1_2.jpg';
group2[2] = '/images/banner_slides/slideshow2_2.jpg';
group2[3] = '/images/banner_slides/slideshow3_2.jpg';
group2[4] = '/images/banner_slides/slideshow4_2.jpg';

$(document).ready(function(){
	$.noConflict();
	
	jQuery('#flash_txt1').fadeTo('fast', 0, function(){});
	jQuery('#flash_txt2').fadeTo('fast', 0, function(){});
	
	textTransition();
	
	picTimer = setInterval(function() { picTransition(); }, picInterval*1000);
	
	jQuery(window).blur(function(){flag=true;});
	jQuery(window).focus(function(){flag=false;});
});

function picTransition() {
	if(!flag){
		var nextGroup = groups[visibleGroup+1] ? visibleGroup+1 : 0;
		var newGroup = groups[nextGroup];
		
		jQuery('#next'+spaceOrder[currentSpace]).attr( 'src', newGroup[spaceOrder[currentSpace]] );
		jQuery('#current'+spaceOrder[currentSpace]).fadeTo( fadeDuration, 0, function(){
			jQuery('#current'+spaceOrder[currentSpace]).attr( 'src', newGroup[spaceOrder[currentSpace]] ).fadeTo(fadeDuration, 1);
			if(currentSpace >= (spaceOrder.length - 1)){
				currentSpace = 0;
				visibleGroup = nextGroup;
			} else {
				currentSpace++;
			}
		});	
	}
}

function textTransition(){
	jQuery('#flash_txt1').animate({
		opacity: 1.0,
		right: '+=440'
	  }, 3000, function() {
		  delay = setTimeout(function() { 
		  		jQuery('#flash_txt1').fadeTo('slow', 0, function(){
					jQuery('#flash_txt2').animate({
						opacity: 1.0,
						right: '+=270'
					  }, 2000, function() {
						  	secondDelay = setTimeout(function(){
								jQuery('#flash_txt2').fadeTo('slow', 0, function(){ 
									jQuery('#flash_txt1').css('right', '0');
									jQuery('#flash_txt2').css('right', '0');
									jQuery('#flash_txt2').css('font-size', '26px');
									textTransition();
								});
							}, 3000);
					  });
				});
		   }, 3000);
	  });	
}
