 /*
 ______       _          ______ _             _     
 (_) |        | |        (_) |  | |           | |    
    _|_  __,  | |   _       _|_ | |  __,   ,  | |    
   / | |/  |  |/_) |/      / | ||/  /  |  / \_|/ \   
  (_/   \_/|_/| \_/|__/   (_/   |__/\_/|_/ \/ |   |_/ v.1.1
*/
window.addEvent('domready', function() {
	// EDIT THIS
	var slideContainer = new Fx.Tween('slideContainer');
	var transitionDelay = 7000; //IN MILLISECONDS
	var totalSlides = 3; //TOTAL SLIDES
	var urlArray = [];
		urlArray[0] = '/media/safety/fireprep/rotatingHome/frame01.shtml'  //NEVER LOADING
		urlArray[1] = '/media/safety/fireprep/rotatingHome/frame01.shtml'
		urlArray[2] = '/media/safety/fireprep/rotatingHome/frame02.shtml'
		urlArray[3] = '/media/safety/fireprep/rotatingHome/frame03.shtml'
		//urlArray[4] = '/media/safety/fireprep/rotatingHome/frame04.shtml'
	var pagePath = '/media/safety/fireprep/rotatingHome/frame';
	
	//NOT THIS
	var arrayPosition = 1;
	var isPaused = false;
	var url;
	var initialLoad = true;
	var urlPage = '';
	var firstTime = true;
	var pageTracker = _gat._getTracker("UA-3913594-1");

// Play Pause In Slide Button
var startStop = function() {
	$('startStop').addEvent('click', function(){
		if( isPaused == false ) {
			isPaused = true;
			$clear(theTimer);
			this.set('html', 'play');
			this.setStyle('background-image', 'url(/includes/html/homePromo/control_play.png)');
		}
		else {
			isPaused = false;
			loadSlides();
			theTimer = loadSlides.periodical(transitionDelay);
			this.set('html', 'pause');
			this.setStyle('background-image', 'url(/includes/html/homePromo/control_pause.png)');
		}
	});
};

// Direct Movement Between Slides
function directTransition(url, pagePosition) {
	slideContainer.start('opacity', '1', '0');
	slideContainer.chain(function() {
		new Request.HTML({ 
		method: 'get',
		url: url,
		update: $('slideContainer'),
		onComplete: function() { 
			slideContainer.start('opacity', '0', '1');
			getSlideLinks();
			isPaused = true;
			startStop();
			$('startStop').set('html', 'play');
			$('startStop').setStyle('background-image', 'url(/includes/html/homePromo/control_play.png)');
			arrayPosition = (pagePosition*1) + 1;
			
			// Transition Border
			var temp = ((arrayPosition*1)-1);
			var activeSlide = new String(temp);
			var nexSteps = new Fx.Tween(activeSlide);
			nexSteps.set('border-color', '#000');
			}
		}).send();
	});
};

// Slide Link Scraper
var getSlideLinks = function() {
	$('slideContainer').getElements('a').addEvent('click', function(event){
		event.stop(); //Prevents the browser from following the link.
		var pageLocation = new String(this.href);
		var x = [];
		x = pageLocation.split('/');
		pagePosition = new String(x[x.length-1]);
		if ( (pagePosition == '01') || (pagePosition == '02') || (pagePosition == '03') || (pagePosition == '04') || (pagePosition == '05') ) {
			urlPage = pagePath + pagePosition + '.shtml';				
			$clear(theTimer);
			directTransition(urlPage, pagePosition)
		}
		else {
			pageTracker._trackPageview('/flashPromo/homepage/?='+pageLocation);
			window.location = pageLocation;
		}

	});
};
		
// Auto Get and Transition New Slides
function slidesTransition(currentSlideNumber) {
	slideContainer.start('opacity', '1', '0');
	slideContainer.chain(function() {
		new Request.HTML({ 
		method: 'get',
		url: currentSlideNumber,
		update: $('slideContainer'),
		onComplete: function() { 
			slideContainer.start('opacity', '0', '1');
			startStop();
			getSlideLinks();
			
			// Transition Border
			var temp = ((arrayPosition*1)-1);
			var activeSlide = new String(temp);
			var nexSteps = new Fx.Tween(activeSlide);
			nexSteps.set('border-color', '#000');
			}
		}).send();
	});
};

// Time Delay Function
function loadSlides() {			
	if (arrayPosition > totalSlides) { 
		arrayPosition = 1;  // Cycle back to first slide
	}
	slidesTransition(urlArray[arrayPosition]);
	arrayPosition = arrayPosition + 1;
}

if ( initialLoad == true ){
	//First page refresh, immediately load the first slide
	var theTimer = loadSlides.periodical(0, this);
	//Set flag to prevent this in future loads, if necessary
	initialLoad = false;
	//Now set normal load delay for future slides
	var theTimer = loadSlides.periodical(transitionDelay, this);
}
	else{
		//In case it's not the first page load, set normal slide transition delay
		var theTimer = loadSlides.periodical(transitionDelay, this);
	}




});