// SLIDER v.1.0 :: 14.06.2010
// cody by Luca Garbin - www.ritzwebmaster.com

(function($){
	$.fn.slider = function(options) {
		
		// variabili di default
		var defaults = {
			_panels 	: 'img',					// elementi da scrollare (di default sono img)
			_container 	: '.scrollContainer',		// il contenitore delle immagini
			_mask 		: '.scroll',				// la maschera
			_prev 		: '.left',					// bottone per il precedente elemento
			_next 		: '.right',					// bottone per il successivo elemento
			_horizontal	: true,						// slide orizzontale
			_padding	: 14,						// padding/margin destro degli elementi 
			_stepById	: true,					// scroll indicizzato dagli id, se false scrolla per la larghezza dell'elemento
			_stepView	: 3,						// elementi visualizzati nella maschera
			_stepScroll	: 1,
			_easing		: 'easeInOutQuint',
			_durata		: 430,						// durata animazione
			_circle		: false,						// animazione ciclica
			_tPlay 		: 1000,						// delay animazione autamtica
			_isPlay		: false,						// player immagini
			_useFirst	: false
		}
		
		var options = $.extend(defaults, options)

		this.each(function() {
			var obj = $(this)
			var current = 0
			var numElements
			
			var _panels = obj.find(options._container+' > '+options._panels)
			var _container = obj.find(options._container)
			var _mask = obj.find(options._mask)
			var _prev = obj.find(options._prev)
			var _next = obj.find(options._next)
			var isPlay = options._isPlay
						
			numElements = _panels.length
			
			
			
			if (options._horizontal) {
				_panels.css({
					'float' : 'left',
					'position' : 'relative' // IE fix to ensure overflow is hidden
				})
				
				elemMore = numElements
				if(numElements % options._stepView != 0 && options._stepScroll > 1) 
					elemMore = elemMore + (numElements % options._stepView)
								
				_container.css('width', (_panels[0].offsetWidth + options._padding)*elemMore )
			}
			
		   	_mask.css('overflow', 'hidden')
			_mask.scrollTo(0)
			controllaPulsanti()
			assignId()

			// --------------
			// controlli vari
			// --------------


			function scrollImg(step) {
				// se prossimo ...
				if(step) {
					// if push next
					// if(current == (numElements - (numElements % options._stepView))) {
					if(current == (numElements)) {
						alert('fine')
						if(options._circle) {
							current=0
						}else {
							current = current + 1
						}
					}else {
						current = current + options._stepScroll
					}
				} else {
					// if push prev
					if(current == 0) {
						if(options._circle) {
							current = numElements - (numElements % options._stepView)
						}
					}else{
						current = current - options._stepScroll
					}
				}
				
				if(options._stepById){
					if(options._easing!='')
						_mask.stop().scrollTo('#sliderPanel'+current,options._durata,{easing:options._easing})
					else
						_mask.stop().scrollTo('#sliderPanel'+current,options._durata)
				} else {
					var toX = _panels[current].offsetLeft+'px'
					if(options._easing!='')
						_mask.stop().scrollTo(toX, options._durata,{easing:options._easing})
					else
						_mask.stop().scrollTo(toX, options._durata)
				}
				controllaPulsanti()
			}
			
			
			

			function controllaPulsanti() {
				if(numElements > options._stepView) {
					// se primo...
					if(current==0) {
						if(options._circle) _prev.show()
						else _prev.hide()
						_next.show()
					}else {
						if((current==(numElements - options._stepView)) || ((current == (numElements - (numElements % options._stepView))))) {
						//if(current==numElements - options._stepView) {
							_prev.show()
							if(options._circle) _next.show()
							else _next.hide()
						}else{
							_prev.show()
							_next.show()
						}
					}
				}else{
					_prev.hide()
					_next.hide()
				}
			}


			function assignId() {
				var ii = 0
				_panels.each(function(){
					$(this).attr('id','sliderPanel'+ii)
					ii++
				})
			}

			_prev.click(function() {scrollImg(false)})
			_next.click(function() {scrollImg(true)})
			
			
			//temporizzatore 
			if(isPlay) {slidePlay()}
			
			function slidePlay() {
				
				var ttime
				pplay()
			
				obj.hover(sstop, function() {
					isPlay = true
					pplay()	
				})
	
				function pplay() {
					if(isPlay) {
						ttime = setTimeout(function() {
							scrollImg(true)
							pplay()
						}, options._tPlay)
					}
				}
				
				function sstop() {
					window.clearTimeout(ttime)
					isPlay = false
				}
			}
			
			if(options._useFirst) {
				//alert('ciao')
				//current = 2
			}

		}) // fine funzione
		return this
	}
})(jQuery)
