var el_rotator = {
	init:function(){
	
			
		// give each image ID corresponding to its order
		var num = 1;
		$('#rotator img').each(function(){
			$(this).attr('id', 'image-'+num);
			num++;
		});
		
		// make first image visible
		$('#image-1').css('display', 'block').addClass('current');
		el_rotator.current = 1;
		
		// store value of last image
		el_rotator.lastImage = $('#rotator img:last').attr('id').split('-')[1];		
		
		// set the order for the next slide to appear
		el_rotator.prepNextSlide();
		
	},
	
	prepNextSlide:function(doFade){
		// set var img depending on whether last image is selected or not
		if(el_rotator.current == el_rotator.lastImage){
			el_rotator.nextImg = '#image-1';
			el_rotator.next 	= 1;
		} else {
			el_rotator.next 	= parseInt(el_rotator.current)+1;			
			el_rotator.nextImg = '#image-'+el_rotator.next;
		}		
		el_rotator.get_rotatorSpeed();
	},
	
	get_rotatorSpeed:function(){
		if($('#image-'+el_rotator.current).attr('class'))
			el_rotator.speed = parseInt($('#image-'+el_rotator.current).attr('class').split('-')[1] );	
		
		if(el_rotator.speed)
			el_rotator.el_rotatorSpeed = el_rotator.speed*1000;
		else
			el_rotator.el_rotatorSpeed = 5000;		
			
		// trigger the fading function
		el_rotator.timer = setTimeout(function(){ el_rotator.fadeIt(); }, el_rotator.el_rotatorSpeed);
	},
	
	fadeIt:function(){	
		
		// fade out current image
		$('#image-'+el_rotator.current).fadeOut('slow').removeClass('current');
		
		// set next image to be new image
		$(el_rotator.nextImg).addClass('current');
		$(el_rotator.nextImg).fadeIn('slow');
		
		el_rotator.current = el_rotator.next;
		
		el_rotator.prepNextSlide();
		

	}
	
}

$(document).ready(function(){	
	el_rotator.init();
});

