/*
 * 	Easy Slider 1.5 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#slider").easySlider();
 *	
 * 	<div id="slider">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {		
	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'&nbsp;',
			nextId: 		'nextBtn',	
			nextText: 		'&nbsp;',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			1200,
			auto:			false,
			pause:			2000,
			continuous:		false
		}; 
		
		var options = $.extend(defaults, options);  
		
		$("ul").css('marginLeft',(($(window).width()-956)/2)-7+(956*-1));		
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = 956; 
			var h = $("li", obj).height(); 
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
            obj.css("display","block");
			var ts = s-2;
			var t = 1; // incrementale della posizione
			
			$("ul", obj).css('width',s*w);
			if(!options.vertical) $("li", obj).css('float','left');
			
			if(options.controlsShow){
				var html = options.controlsBefore;
				if(options.firstShow) html += '<div><span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
				html += ' <div class="comandi"><span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span></div>';
				if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span></div>';
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
	
			$("a","#"+options.nextId).click(function(){		
				animate("next",true);
			});
			$("a","#"+options.prevId).click(function(){		
				animate("prev",true);				
			});	
			$("a","#"+options.firstId).click(function(){		
				animate("first",true);
			});				
			$("a","#"+options.lastId).click(function(){		
				animate("last",true);				
			});	
			
			function muovi_standard(){		
				animate("standard",true);				
			}
			function muovi_classic(){		
				animate("classic",true);				
			}
			function muovi_superior(){		
				animate("superior",true);				
			}
			function muovi_marite(){		
				animate("marite",true);				
			}
			
			$('.categorieHotel ._1 h1 a').bind('click', muovi_standard);
			$('.categorieHotel ._2 h1 a').bind('click', muovi_classic);
			$('.categorieHotel ._3 h1 a').bind('click', muovi_superior);
			$('.categorieHotel ._4 h1 a').bind('click', muovi_marite);
			
			
			$(window).resize(function() {
				$("ul").css('marginLeft',(($(window).width()-956)/2)-7+(956*-t));	
				
			});
			
			var diff;
			function animate(dir,clicked){
				var ot = t;	
		
			// gestione apertura camere //
			
				switch(dir){
					case "same":
						t = t;
					break;
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;	
						diff = Math.abs(ot-t)
						break; 
					case "prev":
						t = (t<=1) ? (options.continuous ? ts : 1) : t-1;
						diff = Math.abs(ot-t)
						break; 
					case "first":
						t = 1;
						break; 
					case "last":
						t = ts;
						break; 
					case "marite":
						t = 1;
						diff = 2;
						break;
					case "superior":
						t = 8;
						diff = 2;
						break;
					
					case "classic":
						t = 13;
						diff = 2;
						break;
					case "standard":
						t = 17;
						diff = 2;
						break;
					default:
						break; 
				};
				
				if (t == 8) {
					if($('.categorieHotel div._3').hasClass('_sf1') == false && $('.categorieHotel div._3').hasClass('_sf1bis') == false){
						
					$('.categorieHotel ._3 h1 a').unbind('click', muovi_superior);
					$('.categorieHotel ._3 h1 a').trigger('click');
					$('.categorieHotel ._3 h1 a').bind('click', muovi_superior);
					}
					
				}
				if (t == 1) {
					if($('.categorieHotel div._4').hasClass('_sf1') == false && $('.categorieHotel div._4').hasClass('_sf1bis') == false){
					$('.categorieHotel ._4 h1 a').unbind('click', muovi_marite);
					$('.categorieHotel ._4 h1 a').trigger('click');
					$('.categorieHotel ._4 h1 a').bind('click', muovi_marite);
					}	
				}
				if (t == 13) {
					if($('.categorieHotel div._2').hasClass('_sf1') == false && $('.categorieHotel div._2').hasClass('_sf1bis') == false){
					$('.categorieHotel ._2 h1 a').unbind('click', muovi_classic);
					$('.categorieHotel ._2 h1 a').trigger('click');
					$('.categorieHotel ._2 h1 a').bind('click', muovi_classic);
					}	
				}
				if (t == 17) {
					if($('.categorieHotel div._1').hasClass('_sf1') == false && $('.categorieHotel div._1').hasClass('_sf1bis') == false){
					$('.categorieHotel ._1 h1 a').unbind('click', muovi_standard);
					$('.categorieHotel ._1 h1 a').trigger('click');
					$('.categorieHotel ._1 h1 a').bind('click', muovi_standard);
					}	
				}
				if (t != 1) {
					$('.video').fadeOut('500','easeOutQuint');	
				}
				if (t == 1) {
					$('.video').fadeIn('500','easeInOutQuint');	
				}
				
				var speed = diff*options.speed;						
				if(!options.vertical) {
					p = (($(window).width()-956)/2)-7+(t*w*-1); // posizione finale dello spostamento
					$("ul",obj).animate(
						{ marginLeft: p }, 
						speed,'easeInOutQuint'
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						speed
					);					
				};
				
				if(!options.continuous && options.controlsFade){					
					if(t==ts){
						$("a","#"+options.nextId).hide();
						$("a","#"+options.lastId).hide();
					} else {
						$("a","#"+options.nextId).show();
						$("a","#"+options.lastId).show();					
					};
					if(t==1){
						$("a","#"+options.prevId).hide();
						$("a","#"+options.firstId).hide();
					} else {
						$("a","#"+options.prevId).show();
						$("a","#"+options.firstId).show();
					};					
				};				
				
				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					stop(false,false).animate("next",false);
				},options.pause);
			};		
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);




