speedValue = 500;

$().ready(function() {
	$('.speed-link').hover(
		function(){
			if (IntS) clearTimeout(IntS);
			$("#speed-text").html($(this).attr('title'));
			finishPos = $(this).attr('pos'); sInt = setInterval(Gas, speedValue);
		},
		function(){ }
	);
	finishPos = 7; currentPos = 0;
	IntS = setInterval( Gas_, speedValue*2);


	$("#nav li").hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");});
});

var Gas = function() {
	if(currentPos == finishPos){ clearInterval(sInt); sInt = false; return; }
	_setData(); if (!sInt) sInt = setInterval(Gas, speedValue);
	return;
};

var Gas_ = function() {
	if(currentPos == finishPos && finishPos!=0) { finishPos = 0; }
	if(currentPos == finishPos && finishPos==0){ clearInterval(IntS); IntS = false; return; }
	_setData(); $("#speed-text").html($("#speed-link"+currentPos).attr('title'));
	if (!IntS) IntS = setInterval( Gas_, speedValue*2);
	return;
};

var _setData = function(){
	currentPos = (currentPos < finishPos) ? currentPos+1 : currentPos-1;
	$("#speed-arrow-img").css('background-position',"0px -" + (currentPos-1)*101 + "px");
}

//Rotator begin

function theRotator() {
	var RandomImage=Math.floor(Math.random() * (4 - 0 + 1)) + 0;
	//Set the opacity of all images to 0
	$('div.rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div.rotator ul li:eq('+RandomImage+')').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',10000);
	
}

function rotate() {	
	//Get the first image
	var current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));

    if ( current.length == 0 ) current = $('div.rotator ul li:first');

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') :current.next()) : $('div.rotator ul li:first'));
	
	//Un-comment the 3 lines below to get the images in random order
	
	var sibs = current.siblings();
        var rndNum = Math.floor(Math.random() * sibs.length );
        var next = $( sibs[ rndNum ] );
			
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};


$(document).ready(function() {		
	//Load the slideshow
	theRotator();
	$('div.rotator').fadeIn(1000);
    $('div.rotator ul li').fadeIn(1000); // tweek for IE
});

//Rotator end
