
/*

	News Rotator - jQuery plug-in
	
	

	-----------------------------------------------------------------------------------------------------------------------------

*/

(function($){



$.fn.NewsRotator = function(options)
{
	
	var o = jQuery.extend({//Default values
		interval: 3000,
		items_query: '.items',
		item_query: '.item',
		image_query: '.image',
		orientation: 'horizontal',
		current_class: 'current',
		duration: 1000,
		easing: "easeInOutExpo",
		beforeStart: function(){},
		afterSlide: function(){}
		
	},options);	
	
	function horizontal()//checks if the orientation of the slideshow is landscape/horizontal
	{
		if(o.orientation.toLowerCase().indexOf('horizontal') != -1)return true;
		return false;
	}
	
	
	
	function next(obj)
	{
		if(typeof $(obj).find('.' + o.current_class).next()[0] != 'undefined')
		{
			return 	$(obj).find('.' + o.current_class).next();
		} else {
			return 	$(obj).find(o.item_query + ':first');
		}
	}
		
	function previous(obj)
	{
		if(typeof $(obj).find('.' + o.current_class).previous()[0] != 'undefined')
		{
			return 	$(obj).find('.' + o.current_class).previous();
		} else {
			return 	$(obj).find(o.item_query + ':last');
		}	
	}
	
	

	return this.each(function(){
		
		//Set up the news rotator
		$(this).css({
			position: 'relative',
			'z-index': 1,
			overflow: 'hidden'
		});
		
		//Set up the items holder
		$(this).find(o.items_query).css({
			height: $(this).find(o.item_query).outerHeight(),
			width: ($(this).find(o.item_query).outerWidth() * $(this).find(o.item_query).length),
			overflow: 'hidden',
			position: 'relative'
		});
			
		if(horizontal())
		{
			$(this).find(o.item_query).css('float','left');				
		}
		
		$(this).find(o.item_query + ':first').addClass(o.current_class);
		
		var obj = this;
		
		o.beforeStart();
		
		setInterval(function(){
			var curPos = 0;
			$(obj).find(o.item_query).each(function($key,$value){
				if($(this).hasClass(o.current_class))curPos = $key + 1;
			});
			if(curPos == $(obj).find(o.item_query).length) curPos = 0;
			
			$(obj).find(o.items_query).animate({
				marginLeft: -(curPos * ($(obj).find('.' + o.current_class).width()))							
			},o.duration,o.easing,function(){
				$(obj).find('.' + o.current_class + ' .image').css({
					marginTop: 0
				});
				var nextItem = next(obj);
				$(obj).find(o.item_query).removeClass(o.current_class);
				$(nextItem).addClass(o.current_class);
				o.afterSlide();
			});					 
		},o.interval);
		
	});
	
	
};
		  
})(jQuery);































