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

(function($){
	$.fn.sliderHead = function(options) {
		
		// variabili di default
		var defaults = {
			items		: new Array(),
			cicle 		: true,
			cicleTime	: 5000
		}
		
		/*
		array[indice][0]: titolo
		array[indice][1]: sottotitolo
		array[indice][2]: testo
		array[indice][3]: link dettaglio
		array[indice][4]: link richiedi
		array[indice][5]: url foto
		*/
		
		var options = $.extend(defaults, options)

		_count	 = options.items.length
		_current = 0;
		_animation = true;
		
		// reset box
		this.each(function() {
			//alert(count)
			var obj = $(this)
			var _h3 			= obj.find('h3')
			var _h4 			= obj.find('h4')
			var _text 			= obj.find('.text')
			var _img	 		= obj.find('.thumb-diario')
			var _btLeggi 		= obj.find('.leggi-tutto')
			var _btRichiedi 	= obj.find('.richiedi')
			
						
			obj.attr('style','background:url(i/t/loader-head.gif) center no-repeat')
			if(options.items.length>1){
				obj.find('.item').append('<img src="i/t/head-diario-prev.png" class="btPrev" /><img src="i/t/head-diario-next.png" class="btNext" />')
				var _btNext			= obj.find('.btNext');
			}
			
			function init() {
				_h3.html('').css('opacity',0)
				_h4.html('').css('opacity',0)
				_text.html('').css('opacity',0)
				_img.css('top','-300px')
				_btLeggi.css('opacity',0)
				_btRichiedi.css('opacity',0)
				
			}

			function pulisci(indiceN){
				_img.stop().animate({'top':'-300'},700,'easeInQuint',function(){
					_h3.stop().animate({'opacity':0},'easeInQuint')
					_h4.stop().animate({'opacity':0},'easeInQuint')
					_text.stop().animate({'opacity':0},'easeInQuint')
					_btLeggi.stop().animate({'opacity':0},'easeInQuint')
					_btRichiedi.stop().animate({'opacity':0},'easeInQuint',function(){
						popola(indiceN)
					})
				})
			}

			function popola(indice){
				_h3.html(options.items[indice][0])
				_h4.html(options.items[indice][1])
				_text.html(options.items[indice][2])
				_btLeggi.attr('href',options.items[indice][3])
				_btRichiedi.attr('href',options.items[indice][4])
				_img.attr('src',options.items[indice][5])
				// una volta caricata l'immagine
				/*
				if ($.browser.msie){
					_img.ready(function(){
						obj.attr('style','none')										
						_img.attr('src','i/t/head-diario-mask.png')
						_img.attr('style',_img.attr('style')+';background:url('+options.items[indice][5]+') 33px 23px no-repeat')
						visualizza()
					})
				}else{
					_img.load(function(){
						obj.attr('style','none')										
						_img.attr('src','i/t/head-diario-mask.png')
						_img.attr('style',_img.attr('style')+';background:url('+options.items[indice][5]+') 33px 23px no-repeat')
						visualizza()
					})
				}
				*/
				obj.attr('style','none')										
				_img.attr('src','i/t/head-diario-mask.png')
				_img.attr('style',_img.attr('style')+';background:url('+options.items[indice][5]+') 33px 23px no-repeat')
				visualizza()
			}
			
			function visualizza(){
				_h3.stop().animate({'opacity':1},function(){ _h3.removeAttr('style') })
				_h4.stop().animate({'opacity':1},function(){
					_h4.removeAttr('style')
					_img.stop().animate({'top':'11px'},1000,'easeOutQuint',function(){
						_text.stop().animate({'opacity':1},function(){_text.removeAttr('style')})
						_btLeggi.stop().animate({'opacity':1},function(){_btLeggi.removeAttr('style')})
						_btRichiedi.stop().animate({'opacity':1},function(){_btRichiedi.removeAttr('style')})
						_animation = true;
					})
				})
			}
			
			
			$('.btNext').click(function(){
				if(_animation) {
					if(_current==(_count-1))
						_current=0;
					else
						_current++;
					pulisci(_current)
					_animation = false;
				}
			})

			$('.btPrev').click(function(){
				if(_animation) {
					if(_current==0)
						_current=_count-1;
					else
						_current--;
					pulisci(_current)
					_animation = false;
				}
			})

			// TEMPORIZZAZIONE
			if(options.cicle){
				interval_function = function () {
					_btNext.trigger('click')
				};
				
				interval = setInterval(interval_function, options.cicleTime);
			
				obj.hover(function () {
					clearInterval(interval);
				}, function () {
					interval = setInterval(interval_function, options.cicleTime);
				});			
				}
			// INIZIALIZZAZIONE
			init()
			popola(0)
			
			
		}) // fine funzione each
		return this
	}
})(jQuery)
