/*
 * 	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: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			obj.width(w); 
			obj.height(h); 
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			if(!options.vertical) $("li", obj).css('float','left');
			
		    var opts = new Array();
		    opts[0] = "1"; opts[1]="2"; opts[2]="3"; opts[3]="4"; opts[4]="5";
		    opts[5] = "6"; opts[6]="7"; opts[7]="8"; opts[8]="9"; opts[9]="10";
		    var optnames = new Array();
		    optnames[0] = "s1"; optnames[1]="s2"; optnames[2]="s3"; optnames[3]="s4"; optnames[4]="s5";
		    optnames[5] = "s6"; optnames[6]="s7"; optnames[7]="s8"; optnames[8]="s9"; optnames[9]="s10";
		    
			if(options.controlsShow){

				var html = options.controlsBefore;
			    
				if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
				html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				for(ii=0; ii<=ts; ii++) {
					html += ' <span id="'+optnames[ii]+'"><a id="a'+optnames[ii]+'" href=\"javascript:void(0);\">'+opts[ii]+'</a></span>';					
				}
				html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
				if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
				html += options.controlsAfter;						
				$(obj).after(html);										
			};

			// Az utolsóra ugró button poziciója a hírek számától függően
		    var nextBtn = document.getElementById("nextBtn");
		    //alert("EasySlider:"+ts+"--"+nextBtn); 		    
		    switch(ts) {
		        case 2: {  // 3 hír
		       	  nextBtn.style.left = "454px";
		    	  break;
		    	}
		    	case 3: { // 4 hír
		    	  nextBtn.style.left = "482px";
  		    	  break;
		    	}
		    	case 4: { // 5 hír
			      nextBtn.style.left = "510px";
	  		      break;
			    }
		    	case 5: { // 6 hír
			      nextBtn.style.left = "538px";
	  		      break;
			    }
		    	case 6: { // 7 hír
			      nextBtn.style.left = "566px";
	  		      break;
			    }
		    	case 7: { // 8 hír
			      nextBtn.style.left = "594px";
	  		      break;
			    }
		    	case 8: { // 9 hír
			      nextBtn.style.left = "622px";
	  		      break;
			    }
		    	case 9: { // 10 hír
			      nextBtn.style.left = "650px";
	  		      break;
			    }
		    }
		    		    
			
			$("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);				
			});
		
			$("a","#"+optnames[0]).click(function(){		
				animate(optnames[0],true);				
			});			
			$("a","#"+optnames[1]).click(function(){		
				animate(optnames[1],true);				
			});
			$("a","#"+optnames[2]).click(function(){		
				animate(optnames[2],true);				
			});
			$("a","#"+optnames[3]).click(function(){		
				animate(optnames[3],true);				
			});			
			$("a","#"+optnames[4]).click(function(){		
				animate(optnames[4],true);				
			});			
			$("a","#"+optnames[5]).click(function(){		
				animate(optnames[5],true);				
			});			
			$("a","#"+optnames[6]).click(function(){		
				animate(optnames[6],true);				
			});
			$("a","#"+optnames[7]).click(function(){		
				animate(optnames[7],true);				
			});
			$("a","#"+optnames[8]).click(function(){		
				animate(optnames[8],true);				
			});			
			$("a","#"+optnames[9]).click(function(){		
				animate(optnames[9],true);				
			});			
			
			function animate(dir,clicked){
				var ot = t;			
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;						
						break; 
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						break; 
					case "first":
						t = 0;
						break; 
					case optnames[0]:
						t = 0;
						break; 
					case optnames[1]:
						//t = (ot>=2) ? (options.continuous ? 0 : ts) : 1;
					    t = 1;
						break; 
					case optnames[2]:
						t = (ot>=3) ? (options.continuous ? 0 : ts) : 2;
						break; 
					case optnames[3]:
						t = (ot>=4) ? (options.continuous ? 0 : ts) : 3;
						break; 
					case optnames[4]:
						t = (ot>=5) ? (options.continuous ? 0 : ts) : 4;
						break; 
					case optnames[5]:
						t = (ot>=6) ? (options.continuous ? 0 : ts) : 5;
						break; 
					case optnames[6]:
						t = (ot>=7) ? (options.continuous ? 0 : ts) : 6;
						break; 
					case optnames[7]:
						t = (ot>=8) ? (options.continuous ? 0 : ts) : 7;
						break; 
					case optnames[8]:
						t = (ot>=9) ? (options.continuous ? 0 : ts) : 8;
						break; 
					case optnames[9]:
						t = (ot>=10) ? (options.continuous ? 0 : ts) : 9;
						break;						
					default:
						break;
				};
				// Mindegyiket halvánnyá teszem
				// A t-edik legyen select
				if(document.getElementById("as1"))  {document.getElementById("as1").style.color="#B19E7D";}
				if(document.getElementById("as2"))  {document.getElementById("as2").style.color="#B19E7D";}
				if(document.getElementById("as3"))  {document.getElementById("as3").style.color="#B19E7D";}
				if(document.getElementById("as4"))  {document.getElementById("as4").style.color="#B19E7D";}
				if(document.getElementById("as5"))  {document.getElementById("as5").style.color="#B19E7D";}
				if(document.getElementById("as6"))  {document.getElementById("as6").style.color="#B19E7D";}
				if(document.getElementById("as7"))  {document.getElementById("as7").style.color="#B19E7D";}
				if(document.getElementById("as8"))  {document.getElementById("as8").style.color="#B19E7D";}
				if(document.getElementById("as9"))  {document.getElementById("as9").style.color="#B19E7D";}
				if(document.getElementById("as10")) {document.getElementById("as10").style.color="#B19E7D";}
				if(t==0) { document.getElementById("as1").style.color="#3F201B"; }
				if(t==1) { document.getElementById("as2").style.color="#3F201B";}				
				if(t==2) { document.getElementById("as3").style.color="#3F201B"; }				
				if(t==3) { document.getElementById("as4").style.color="#3F201B"; }
				if(t==4) { document.getElementById("as5").style.color="#3F201B"; }
				if(t==5) { document.getElementById("as6").style.color="#3F201B"; }
				if(t==6) { document.getElementById("as7").style.color="#3F201B"; }
				if(t==7) { document.getElementById("as8").style.color="#3F201B"; }
				if(t==8) { document.getElementById("as9").style.color="#3F201B"; }
				if(t==9) { document.getElementById("as10").style.color="#3F201B";}
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(!options.vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						speed
					);				
				} 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==0){
						$("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(){
					animate("next",false);
				},options.pause);
			};		
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};

})(jQuery);