/*
 * Jarquee jQuery Plug-in
 * http://fazchanj.com/jarquee
 * Simple Marquee using .animate()
 *
 * Copyright (c) 2010 Samuel Rouse
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 * Requires jQuery 1.4+, tested with 1.4.2
 *
 * Date: 2010-05-13
 * Revision: 1
 * Options: 
 *	- speed (rough characters per second)
 *	- baseWidth (integer width to briefly set the inner to make sure the content is on one line)
 *	- pause (true offers pause on mouseenter, resume on mouseleave)
 *	- center (true centers content if narrower than marquee container
 */
(function($)
{
	$.fn.jarquee = function(options)
	{
		// Set the options.
		options = $.extend({}, $.fn.jarquee.defaults, options);

		// Go through the matched elements and return the jQuery object.
		return this.each(function()
		{
			var obj = $(this)
			
			// Check for missing descendants DIV and Span. Create if needed.
			if( ! obj.find('div span').size()){
		    	// Pack everything inside.
		    	obj.wrapInner('<span class="jarquee_content_wrapper" />').wrapInner('<div class="jarquee_inner" />');
		    }
			
			// Important variables.
			var marqueeWidth = obj.width();								// Inner width of the banner.
			obj.children('.jarquee_inner').width(options.baseWidth);	//Widen to make sure span is on one line;
	    	var contentWidth = obj.find('div span').outerWidth();		// Width of content, including padding & border.
		    
		    //Check for start centered
		    if(options.center && marqueeWidth > contentWidth){
		    	var offset = parseInt(marqueeWidth - contentWidth) >> 1;
		    	obj.children('.jarquee_inner').css('left',offset);
		    }
		    
		   	obj.css({'position':'relative','overflow':'hidden'});
		    obj.find('.jarquee_inner').css({'width':('' + (contentWidth + 20)),'position':'absolute'});

			$.fn.jarquee.object = obj;
			$.fn.jarquee.options = options;
			$.fn.jarquee.settings = {
				mW : marqueeWidth,
				cW : contentWidth
			};
			
			if (options.pause){
				$(this).bind({
					'mouseenter' : function(){ $.fn.jarquee.action.stop(); },
					'mouseleave' : function(){ $.fn.jarquee.move(); }
				});			
			}
			
			$.fn.jarquee.move();
		});
	};

	
	// Public Values
	$.fn.jarquee.defaults = {
		speed: 5,
		baseWidth: 5000
	};
	$.fn.jarquee.action = "";
	$.fn.jarquee.object = "";
	$.fn.jarquee.options = "";
	$.fn.jarquee.settings = "";
	// Public Functions	
	$.fn.jarquee.move = function() {
		var obj = $.fn.jarquee.object;
		var options = $.fn.jarquee.options;
		var settings = $.fn.jarquee.settings;
		
	    var innerLeft = obj.find('div.jarquee_inner').css('left');
	    innerLeft = isNaN(innerLeft.replace('px','')) ? 0 : innerLeft.replace('px','');
	    var endGoal = (-1 *(settings.cW * -1 - innerLeft));
		var duration = ((settings.cW - (innerLeft * -1)) / (options.speed * 20)) * 1000;

		// Test adjustment to possibly help smoothness
	    var dur_temp = parseInt(duration / endGoal) * endGoal;
        if (duration % endGoal > endGoal / 2) {
            duration += parseInt(endGoal >> 1);
        }
		
		$.fn.jarquee.action = $.fn.jarquee.object.children('.jarquee_inner').animate({left: '-=' + endGoal},duration,'linear',function(){
            $.fn.jarquee.object.find('.jarquee_inner').css('left',$.fn.jarquee.settings.mW);
            $.fn.jarquee.move();
        });
	}; 
})(jQuery);