var SLIDER_STATUS = false;
var CURRENT_SLIDE = 1;
var TOTAL_SLIDES = 3;

$(function() {	

	//BEGIN CREATE CURRENT IMAGE
	var img = new Image();
	img.src = $('#slides > li.active > img').attr('src');
	$(img).load(function() {
		$(this)
			.hide()
			.attr('id','currentSlide')
			.prependTo($('#slideshow'))
			.fadeIn('slow');
	});
	//END CREATE CURRENT IMAGE
	$('#slider-menu > li').click(function() {
		do_slide(this);
		return false;
	});	
	$('#slider-menu').everyTime(7500,'slider',function() {
		CURRENT_SLIDE = (CURRENT_SLIDE==TOTAL_SLIDES) ? 1 : CURRENT_SLIDE+1;
		var nextChild = $('li:nth-child('+CURRENT_SLIDE+')',this);
		do_slide(nextChild);
	});

});


function do_slide(obj) {
	if(!SLIDER_STATUS) {
		SLIDER_STATUS = true;
		var thisObject = $(obj);
		var title = thisObject.attr('title');
		$('#currentSlide').animate({opacity:0},'slow');
		$('#slides > li.active').fadeOut('slow',function() {
			var thisId = $(this).attr('id');
			$(this).removeClass('active');
			$('#slider-menu > li[title='+thisId+']').removeClass('active');
			thisObject.addClass('active');
			var newImage = $('#slides > li#'+title+' > img').attr('src');
			$('#currentSlide').attr('src',newImage)
				.animate({opacity:1},'slow');
			$('#slides > li#'+title).fadeIn('slow',function() {
				var newObject = $(this);
				$(this).addClass('active');
				SLIDER_STATUS = false;
			});
		});
	}
}
