/*
Code by Steven Cook, Voltage LTD
Last Modified 12/14/11
*/

function preload(arrayOfImages) { 
    $(arrayOfImages).each(function(){ 
        $('<img/>')[0].src = this; 
        // Alternatively you could use: 
        // (new Image()).src = this; 
    }); 
} 


var currentSlide = 0;
var currentSlideIndex = 0;
var numberSlides = 0;
var divs;
var buttons;
var accessSem = 1;


function switchToSlide(newSlideIndex) {
//	console.log(currentSlideIndex+" to "+newSlideIndex);
	
	// Save state of the old slide
	var oldSlide = currentSlide;
	var oldSlideIndex = currentSlideIndex;
	
	// Prepare new slide
	currentSlideIndex = newSlideIndex;
	currentSlide = divs[currentSlideIndex];
	
	// Highlight the right buttons
	$(buttons[oldSlideIndex]).css("background","url(img/circle_small.png)");
	$(buttons[currentSlideIndex]).css("background","url(img/circle_big.png)");

	// Show old slide in front
	$(oldSlide).css("z-index","10");
	// Show new slide behind
	$(currentSlide).css("z-index","1");
	// Fade out old slide
	$(currentSlide).show(1,function() {
		$(oldSlide).fadeOut(300, function() {
			accessSem++;
		});
	});
	
}


function advanceSlide() {
	var newSlideIndex = currentSlideIndex + 1;
	if (newSlideIndex >= numberSlides) {
		newSlideIndex = 0;
	}
	if (accessSem > 0 ) {
		accessSem--;
		switchToSlide(newSlideIndex);
	}
} 
function abortTimer() { // to be called when you want to stop the timer 
	clearInterval(tid); 
} 


$(document).ready(function() {
	divs = $('.voltageShow div').get();
	numberSlides = divs.length - 1;  // subtract out the voltageShowNav div

	buttons = $('#voltageShowNav li').get();

	// Start by showing the first slide
	currentSlide = divs[currentSlideIndex];
	$(currentSlide).show();
	$(buttons[currentSlideIndex]).css("background","url(img/circle_big.png)");

	$('#voltageShowNav li').click(function() {
		// stop the timer forever once the user clicks
		clearInterval(tid);
		
		var newSlideIndex = $(this).index();
		
		// Only allow one click at a time
		if (newSlideIndex == currentSlideIndex) {
			return;
		}
		if (accessSem > 0 ) {
			accessSem--;
			switchToSlide(newSlideIndex);
		}
	});
	
	$('#startVideo').click(function() {
		// stop the timer forever once the user clicks
		clearInterval(tid);

		$(this).hide();
		$("#youtube").append('<iframe width="824" height="500" src="http://www.youtube.com/embed/eVKZ9LWNWDw?autoplay=1&rel=0" frameborder="0" allowfullscreen></iframe>');
		
	});
	
	
	
	// set interval for the timer
	var tid = setInterval(advanceSlide, 7000); 

});

