// global vars
var feature_nav = "ul#feature-nav li a";
var feature_class = '.feature';
var count_rotates = 1; // for the rotateImages function
var interval_speed = 5000;
var feature_loader = "#feature-loader";
var feature_loader_inner = "#feature-loader-inner";
var width_start = 1; // feature loader width to start at
var width_total = 150; // total width of the div
var enable_loader = false;

// no touch!!
var rotation_interval;
var loader_interval;
var rotation_index;


var hasBeenRotated = false;

// preload images
promo1 = new Image();
promo1.src = '../images/feed_the_soul-ENG.jpg';

promo2 = new Image();
promo2.src = '../images/summer-menu2011.jpg';

promo3 = new Image();
promo3.src = '../images/feature-franchise.jpg';

promo4 = new Image();
promo4.src = '../images/feature-onlinecommunity.jpg';

promo5 = new Image();
promo5.src = '../images/promo-nutrition.jpg';


window.onload = function(){
	
    // cycle through feature images
	//setTimeout("initRotation();");
	
	//uncomment out code and get rid of the next lines to stop forcing slider to go to the next frame.
	interval_speed = 1;
	rotateImages();
	
	
	/*
	if(enable_loader){
		startLoader(count_rotates);
	}
	else {
		removeLoader();
	}
	*/

	// takes care of clicking the links...
	$(feature_nav).hover(function(){
		
		// remove loader if they click
		//if(enable_loader) removeLoader();
	
		// remove all classes on anchors.. resetting all
		$(feature_nav).removeClass('selected');
		$(feature_class).hide();
		
		// adding the new image
		var attribute = $(this).attr('id'); // feature-default... we need to get default
		var the_class = attribute.split('-')[1]; // we've got "default"
		
		// add the selected class then show it
		$(this).addClass('selected');
		$("."+the_class).show();
		
		// clear the rotation interval
		rotation_interval = clearInterval(rotation_interval);
		return false;
		
	});

};

function initRotation(){
	
	rotation_interval = setInterval('rotateImages(null);',interval_speed);
	
}

/**** LOADER ***/
/*
function removeLoader(){
	
	$(feature_loader).hide();
	resetLoader();
	
}

function resetLoader(){

	loader_interval = clearInterval(loader_interval);
	$(feature_loader_inner).css('width','1px');
	width_start = 0;
	
}

function startLoader(index){
	
	// reset it
	resetLoader();
	loader_interval = setInterval('moveLoader();',30);
	
}

function moveLoader(){
	
	if(width_start <= width_total){
		width_start++;
	}
	
	$(feature_loader_inner).css('width',width_start+'px');
	
}
*/

/**** IMAGE ROTATION *****/

function resetRotate(){
	
	rotation_interval = clearInterval(rotation_interval);
	rotation_interval = setInterval('rotateImages(null);',interval_speed);
	
}

function rotateImages(set_count){
	
	//get rid of this if statement to return the slider to normal
	if(!hasBeenRotated){
	
		// if set count from when they moused off
		if(set_count != null){
			count_rotates = set_count;
			resetRotate();
		}
	
		// amount of slides
		var amount = $(feature_class).length; // 4
		var slide = $(feature_class+':eq('+count_rotates+')');
		
		// loader
		if(enable_loader) startLoader(count_rotates);
		
		// show the loader above the link
		if(count_rotates < (amount - 1)){
			count_rotates++;
		}
		else {
			count_rotates = 0;
		}
		
		$(feature_class).hide(); // hide all of them
		slide.fadeIn("fast"); // uses an index starting at 0 of the elemtn
	
		// stop it on mouseover
		slide.mouseover(function(){
			rotation_interval = clearInterval(rotation_interval);
			rotation_index = count_rotates;
		});
		
		slide.mouseout(function(){
			rotation_interval = clearInterval(rotation_interval);
			rotation_interval = setInterval('rotateImages('+rotation_index+');',interval_speed);
		});
		
		// get the nav for the appropriate slider
		var class_name = slide.attr('class'); // feature default
		var class_to_use = class_name.split(' ')[1];
		
		// remove all anchor selected classes
		$(feature_nav).removeClass('selected');
		
		// add the new class
		$('#feature-'+class_to_use).addClass('selected');
		
	}
	
}
