$().ready(function(){
	var index = -2147483648;	//minimum integer
	$("#slider li").hover(function(){								   
		if($(this).hasClass("activeSlider")){
			return;
		}
		index++;
		var start = index;
		
		$(".activeSlider").removeClass("activeSlider");
		var value = getValue($(this).addClass("activeSlider"));
		
		var img = findImage(value);
		var newTitle = findTitle(value);
		if(index != start){
			return;
		}
		$(".f-content").fadeOut(250);
		$(".activeSliderImg").removeClass("activeSliderImg");
		$(".f-item img").fadeOut(250);
		setTimeout(function(){							
			if(index == start) {
				$(img).fadeIn(250).addClass("activeSliderImg");
				$(newTitle).fadeIn(250);
			}
		}, 300);
		
		setTimeout(function(){checkForMouse_slider()}, 1000);
	});
	
	$(".f-content .f-right").click(function(){
		$(".activeSlider").removeClass("activeSlider");
		var value = getValue($(this).parent()) * 1 + 1;
		
		if(value > 5){
			value=1;
		}
		
		var img = findImage(value);
		var title = findTitle(value);
		$(this).parent().fadeOut(250, function(){					  
			$(title).fadeIn(250);
		});
		$(".activeSliderImg").removeClass("activeSliderImg")
		$(".f-item img").fadeOut(250);
		
		setTimeout(function(){							
			$(img).fadeIn(250).addClass("activeSliderImg");		
		}, 300);
		
		setTimeout(function(){checkForMouse_slider()}, 1000);
	});
	
	$(".f-content .f-left").click(function(){
		$(".activeSlider").removeClass("activeSlider");
		var value = getValue($(this).parent()) * 1 - 1;
		
		if(value < 1){
			value=5;
		}
		
		var img = findImage(value);
		var title = findTitle(value);
		$(this).parent().fadeOut(250, function(){					  
			$(title).fadeIn(250);
		});
		$(".activeSliderImg").removeClass("activeSliderImg")
		$(".f-item img").fadeOut(250);
		
		setTimeout(function(){							
			$(img).fadeIn(250).addClass("activeSliderImg");		
		}, 300);
		
		setTimeout(function(){checkForMouse_slider()}, 1000);
	});
});

function findImage(value){
	var img;
	$(".sliderImg").each(function(){
								  
		if(getValue($(this)) == value){
			img = this;
		}
	});
	return img;
}
function findTitle(value){
	var title;
	$(".f-content").each(function(){
		if(getValue($(this)) == value){
			title = this;
		}
	});
	return title;
}

function checkForMouse_slider(){
	var slider = $("#featured");
	
	if(mouseX > (slider.offset().left + slider.width()) || mouseX < slider.offset().left ||
		mouseY > (slider.offset().top + slider.height()) || mouseY < slider.offset().top){
		
		resetSlider();
	}else{		
		setTimeout(function(){checkForMouse_slider();}, 750);
	}
}

function resetSlider(){
	var first = $("#slider li:first")
	if($(first).hasClass("activeSlider")){
		return;
	}
	
	$(".activeSlider").removeClass("activeSlider");
	var value = 1; //$(first).addClass("activeSlider").attr("value");
	
	var img = findImage(value);
	
	if($(img).css("display") == "block"){
		return;
	}
	
	var newTitle = findTitle(value);
	$(".f-content").fadeOut(250);
	setTimeout(function(){$(newTitle).fadeIn(250);}, 250);
	$(".activeSliderImg").removeClass("activeSliderImg").fadeOut(250);
	setTimeout(function(){							
		$(img).fadeIn(250).addClass("activeSliderImg");
	}, 300);	
}

function getValue(obj){
	var classes = jQuery(obj).attr('class').split(' ');
	var prefix = "sliderImgValue-";
	
	var value;
	for(i = 0; i < classes.length; i++){
		if(classes[i].indexOf(prefix) != -1){
			value = classes[i].substr(classes[i].indexOf(prefix) + prefix.length);
			break;
		}
	}
	return value;
}
