/**
 * jQuery Scroll Animate plugin
 * Estevão Lucas
 *
 * Copyright (c) 2006 Jan Sorgalla (http://sorgalla.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Built on top of the jQuery library
 *   http://jquery.com
 *
 * Library to scrool with a element, such as UL with LIs childrens, DIV with DIVs
 */
 
(function( $ )
{
	var defaults = {
		classInative: "disabled",
		parent: "ul",
		elements: "li",
		vertical: false,
		buttonLT: ".bt_voltar",
		buttonRB: ".bt_avancar",
		buttonClose: "button.bt_fechar:first",
		buttonsEvent: "click",
		itemSize: 160,
		velocity: 500,
		buttonsKeyUp: false,
		scrollStart: null,
		scrollStop: null,
		easing: null
	}
	
	$.fn.scrollAnimate = function( settings )
	{
		return this.each( function()
		{
			new $sA( this, settings );
		});
	}
	
	$.scrollAnimate = function( container, settings )
	{
		this.settings = $.extend( {}, defaults, settings || {});
		this.div = $( container );
		
		this.parent = this.div.children( this.settings.parent );
		this.parent = !this.parent.length ? $( "<" + this.settings.parent +"></" + this.settings.parent +">" ).appendTo( this.div ) : this.parent;
		this.element = this.parent.children( this.settings.elements );
		
		/*
		this.settings.initial != 0
			? this.settings.vertical
				? this.parent.css( "top", ( this.parent.height() * this.settings.initial / 100 ) - this.settings.itemSize * -1 )
				: this.parent.css( "left", ( ( this.parent.width() * this.settings.initial / 100 ) - this.settings.itemSize / 2 ) * -1 )
			: 0;
		*/
			
		// Right - Buttom
		this.buttonRB = null;
		// Left - Top
        this.buttonLT = null;
		
		this.init();
	}
	
	var self = $sA = $.scrollAnimate;
	
	$sA.fn = $sA.prototype = {
        scrollAnimate: '0.1'
    };
	
	$sA.fn.extend = $.extend;
	
	$sA.fn.extend( {
		
		init: function()
		{
			this.buttonRB = typeof this.settings.buttonRB == "string"
								? this.div.siblings( this.settings.buttonRB )
								: this.settings.buttonRB;
			this.buttonLT = typeof this.settings.buttonLT == "string"
								? this.div.siblings( this.settings.buttonLT )
								: this.settings.buttonLT;
			this.scrooling = false;
			
			this.buttons();
			this.bind();
		},
		
		buttons: function()
		{
			this.set();
			
			var self = this,
				lt = this.lt,
				next = this.buttonRB,
				prev = this.buttonLT,
				classe = this.settings.classInative,
				parent = this.settings.vertical ? self.parent.parent().height() : self.parent.parent().width(),
				length = parent == this.settings.itemSize,
				item = length ? 0 : this.settings.itemSize,
				wh = ( ( ( self.wh - parent ) - item  ) * -1 );
			
			lt <= wh
				? prev.addClass( classe )
				: prev.removeClass( classe );
				
			lt >= 0
				? next.addClass( classe )
				: next.removeClass( classe );
		},
		
		set: function()
		{
			var self = this,
				v = this.settings.vertical;
			
			this.lt = this.getNumber( this.parent.css( v ? "top" : "left" ) )
			this.wh = ( self.parent.children( self.settings.elements + ":visible" ).length ) * self.settings.itemSize ;
			v ? this.parent.height( this.wh + "px" ) : this.parent.width( this.wh + "px" );
		},
		
		bind: function()
		{
			var self = this,
				rb = this.settings.buttonRB,
				lt = this.settings.buttonLT,
				buttons = rb.add( lt );
			
			buttons = !buttons.length && this.div.siblings( rb + ", " + lt );
			buttons = typeof rb == "object"
						? $( [ rb[ 0 ], lt[ 0 ] ] )
						: buttons
			
			rb && lt && buttons.unbind().bind( this.settings.buttonsEvent, function()
			{
				var $this = $( this );
				
				if( !$this.is( "." + self.settings.classInative ) )
				{
					$this.is( typeof lt == "string" ? lt : "." + lt[ 0 ].className.replace( /\s/ig, "." )  )
						? self.scroll( "left" )
						: self.scroll( "right" );
				}

				return false;
			});
			
			if( rb && lt && self.div.is( ":visible" ) && self.settings.buttonsKeyUp )
			{
				$( document ).keyup( function( e )
				{
					var directions = { "left": 37, "right": 39 }
					for( direction in directions )
					{
						!self.buttonLT.is( "." + self.settings.classInative ) && e.keyCode == directions[ direction ] && self.scroll( direction );
					}
				});
			}
			
			if( this.settings.buttonClose )
			{
				var close = this.buttonClose = $( this.settings.buttonClose, this.element ),
					i = true,
					li, left;
				
				close.length && close.click( function()
				{
					if( !i ) return;
					
					var li = $( this ).parent(),
						id;
			
					li.length && li.fadeOut( "slow", function()
					{
						self.lt < 0
							? self.scroll( "right" ) | self.set()
							: self.buttons();
					});
					
					li.is( ":hidden" ) && li.remove();
					
					i = false;
				});
			}
		},
		
		scroll: function( direction )
		{
			var self = this,
				v = this.settings.vertical,
				value, lt, option;
			
			 lt = this.getNumber( !v
					? this.parent.css( "left" )
					: this.parent.css( "top" ) );
			
			
			if( this.scrooling == true ) return;
			
			self.settings.scrollStart && self.settings.scrollStart( direction );
			
			value = direction ==  "left"
							? lt - this.settings.itemSize
							: lt + this.settings.itemSize;
			
			option = !v ? { 'left': value } : { 'top': value };
			
			this.parent.animate(
				option, 
				self.settings.velocity,
				self.settings.easing,
				function()
				{
					self.buttons();	
					self.scrooling = false;
					
					self.settings.scrollStop && self.settings.scrollStop();
				}
			)
			
			this.scrooling = true;
		},
		
		getNumber: function( number )
		{
			return Number( number.replace( /[^0-9\-]/gi, "" ) );
		}
		
	})
})( jQuery );