/* 
* Create Inside Rotator
*
* Descripion:  	News rotator created for Eductor
* Author: 		Morten Najbjerg - Create Inside I/S
* License: 		Copyright 2009
*
*/

$.fn.ci_rotator = function(options){

    // Set defaults
    var defaults = {
    	speed : '10000'
    },
    settings = $.extend({}, defaults, options);
   
    this.each(function() {
      
    	var $this = $(this);
    	var elem = $(this).find('DIV.news:first');
    
    	// Save original width for calculating speed
        var originalWidth = parseInt(elem.css('width'));
		
		// Stop the animation when hovering
		$('DIV#news_rotator').hover(function() {
				elem.stop();
			}, function() {
				animateFirst();
		});
		
		function animateFirst()
		{
			// Find the first element inside the wrapper
      		elem = $this.find('DIV.news:first');
			
			// Speed is calculated so its always the same after mouse hovering
			var speed = settings.speed*parseInt(elem.css('width'))/originalWidth;
			
			// Animate first element
			elem.animate({
				width : '0px'
			}, speed, 'linear', function() {
				
				// Removes element and place it after the others
				var content = elem.html();
				elem.remove();
				$this.append("<div class='news'>"+content+"</div>");
				animateFirst();
				
			});
		}
		
		//Trigger animation
		animateFirst();
    
    });

    // Allow chainability
    return this;
}