/*
Copyright (C) 2010 Damir Ribaric

This file is part of nTicker.

nTicker is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation;
either version 2, or (at your option) any later version.

nTicker is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU General Public License for more
details.

You should have received a copy of the GNU General Public
License along with nTicker; see the file COPYING.  If not,
write to the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA 02110-1301, USA.
*/
;(function(jQuery)
{
	var NTicker=function(options, obj)
	{
		var attributes=
		{
			position: 0,
			
			originalChildrenNum: obj.children().length,
			originalChildrenWidth: 0,
			
			childrenNum: 0,
			childrenWidth: 0,
			
			tickerTimeoutHandle: null,
			tickerFunction: function() {},
			
			isHoverStop: false
		};
		
		attributes.childrenNum=2*attributes.originalChildrenNum;
			
		// pokupim djecu
		var newClonedChildren =  obj.children().clone();
		
		// dodam klasu da znam koji su kopije (iako mi to za sada nije bitno)
		//newClonedChildren.each( function(ix)
		//					{
		//						var o=jQuery(this);
		//						o.addClass('nTicker_copy');
		//					}
		//				);
		
		// dodam na kraj
		obj.append( newClonedChildren );
		
		// racunam ukupnu sirinu tickera
		obj.children().each( function(ix)
							{
								attributes.childrenWidth += jQuery(this).outerWidth();
							}
						);
		// polovicna sirina tickera
		attributes.originalChildrenWidth = attributes.childrenWidth / 2;
		
		// postavim ukupnu sirinu
		obj.css( 'width' , attributes.childrenWidth);
		
		// DOM element tickera
		var domObj=obj.get(0);
		
		// fja za horiz ticker scroll
		function startTickerHoriz()
		{
			if(options.speed < 0 )
			{
				// s lijeva na desno
				if(attributes.position <= 0)
				{
					obj.prepend( obj.children().slice(-attributes.originalChildrenNum) );
					attributes.position = attributes.originalChildrenWidth;
				}
				
				attributes.position += options.speed;
				
				domObj.style.left = (-1*attributes.position) + 'px';	
			}
			else if(options.speed > 0 )
			{
				// s desna na lijevo
				
				if( attributes.position >= attributes.originalChildrenWidth)
				{
					obj.append( obj.children().slice(0, attributes.originalChildrenNum) );
					attributes.position = 0;
				}
				
				attributes.position += options.speed;
				
				domObj.style.left = (-1*attributes.position) + 'px';
			}

			attributes.tickerTimeoutHandle = setTimeout( attributes.tickerFunction, options.interval );
		}
		
		// ovu fju koristim za ticker - za sada imam smo jednu :(
		attributes.tickerFunction=startTickerHoriz;
		
		function hoverStop()
		{
			if( attributes.tickerTimeoutHandle )
			{
				attributes.isHoverStop=true;
				clearTimeout( attributes.tickerTimeoutHandle );
				attributes.tickerTimeoutHandle=null;
			}
		};
		
		function hoverStart()
		{
			if(attributes.isHoverStop && !attributes.tickerTimeoutHandle)
			{
				attributes.isHoverStop=false;
				attributes.tickerFunction();
			}
		};
		
		
		// da li se ticker zaustavlja na hover?
		if(options.stopOnHover)
		{
			obj.hover(hoverStop, hoverStart);
		}
		
		////////////////////////////////////////////////////////////
		// pokreni ticker
		attributes.tickerFunction();
		////////////////////////////////////////////////////////////
		
		
		////////////////////////// public functions
		
		this.setSpeed=function(speed)
		{
			options.speed=speed;
			return obj;
		};
		
		this.stop=function()
		{
			if( attributes.tickerTimeoutHandle )
			{
				clearTimeout( attributes.tickerTimeoutHandle );
				attributes.tickerTimeoutHandle=null;
			}
		};
		
		this.start=function()
		{
			if(!attributes.tickerTimeoutHandle)
			{
				attributes.tickerFunction();
			}	
		};
		
		this.increaseSpeed=function()
		{
			options.speed++;
		};
		
		this.decreaseSpeed=function()
		{
			options.speed--;
		};
		
		this.toggleDirection=function()
		{
			options.speed*=-1;
		};
		
		this.getSpeed=function()
		{
			return options.speed;
		};
	};
	
	jQuery.fn.nTicker=function(opt, arg1, arg2)
	{
		return this.each(
						function() 
						{
							var obj = jQuery(this);
							var options=jQuery.extend( {}, jQuery.fn.nTicker.defaults, opt );
		
							// creating a new AutoCompleteEx object and attach to the element's data, if not already attached
							var data=obj.data('nTicker_object');
							if (!data)
							{
								obj.data('nTicker_object', new NTicker(options, obj));
							}
							else if(typeof(data[opt])=='function')
							{
								data[opt](arg1, arg2);
							}
						}
					);   //End Each JQ Element
	};
	
	jQuery.fn.nTicker.defaults =
	{
		speed: 1,
		interval: 30,
		stopOnHover: true
	};
})(jQuery);
