// 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	: 7,						// 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,
			_isWork		: false,
			_mobile		: false
		}
		
		var options = $.extend(defaults, options)

		this.each(function() {
			var obj = $(this)
			var current = 0
			var numElements
			var selected = 0
			
			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()

			if(options._isWork) {
				/*
				$('#bt-cliente').html(obj.find('#sliderPanel3').children('img').attr('alt'))
				var active = obj.find('#sliderPanel3').attr('rel')
				var nactive= obj.find('#sliderPanel3').children('img').attr('src')
				obj.find('#sliderPanel3').attr('rel',nactive)
				obj.find('#sliderPanel3').children('img').attr('src',active)
				*/
			}

			// --------------
			// controlli vari
			// --------------
			if(options._isWork){
				//$('#bt-cliente').html($(_panels[3]).find('img').attr('alt'))
				var voxActive = $('#viewer .active,#people .active').attr('id')
				if(voxActive) { 
					_mask.scrollTo('#'+voxActive,1)
					ccurrent = parseInt(voxActive.substring(11))
					if(ccurrent > (numElements - options._stepView - 1))
						current = numElements - options._stepView
					else 
						current = ccurrent
							
					controllaPulsanti()
				}
			}else{
				var voxActive = $('#viewer .active').attr('id')
				_mask.scrollTo('#'+voxActive,1)
				ccurrent = parseInt(voxActive.substring(11))
				if(ccurrent > (numElements - options._stepView - 1))
					current = numElements - options._stepView
				else 
					current = ccurrent
						
				controllaPulsanti()
			}

			
			function scrollImg(step,stepTo) {
				// se prossimo ...
				if(!stepTo){
					if(step) {
						// if push next
						// if(current == (numElements - (numElements % options._stepView))) {
						if((options._isWork && current == (numElements-3)) || (!options._isWork && current == numElements)) {
							if(options._circle) {
								current=0
							}else {
								current = current + 1
							}
						}else {
							current = current + options._stepScroll
							$('#log').html(current)
							if((current+options._stepScroll > numElements))
								current = numElements - options._stepScroll
							$('#log').html(current)
								
						}
					} else {
						// if push prev
						if(current == 0) {
							if(options._circle) {
								current = numElements - (numElements % options._stepView)
							}
						}else{
							current = current - options._stepScroll
							if((current < 0))
								current = 0
							$('#log').html(current)
						}
					}
				}else{
						//current = stepTo-3
				}
				

				if(options._isWork) {
				}else{
					//var active = obj.find('#sliderPanel'+(current)).attr('rel')
					//var nactive= obj.find('#sliderPanel'+(current)).children('img').attr('src')
				}
				if(stepTo){
						// disattivo voce attivata
						if(!options._isWork) { 
							
							obj.find('#sliderPanel'+(current+3)).attr('rel',nactive)
							obj.find('#sliderPanel'+(current+3)).children('img').attr('src',active)
	
							//current+3 //item corrente
							
							var pactive = obj.find('#sliderPanel'+(stepTo)).attr('rel')
							var pnactive= obj.find('#sliderPanel'+(stepTo)).children('img').attr('src')
							obj.find('#sliderPanel'+(stepTo)).attr('rel',pnactive)
							obj.find('#sliderPanel'+(stepTo)).children('img').attr('src',pactive)
							//alert(stepTo-3)
							
						current=stepTo-3
						}
				}else{
					if(step) {
						if(options._isWork) {
						}
					}else{			
						if(options._isWork) {
						}
					}
				}
				

				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))))) {
						
							_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)})
			if(options._isWork) {
				_panels.click(function(){
					
					if(!$(this).hasClass('thumb-space')) {
						/*
						var sstep = $(this).attr('id').substring(11)
						var hhref = $(this).attr('href')
						$('#bt-cliente').attr('href',hhref)
						DISATTIVATO per inviare direttamente al lavoro
						scrollImg(true,sstep)
						return false
						*/
					}
					
				})
			}
			
			

			//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)
