/*
 * 	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
 *
 *
 * Adapted for oliis - http://www.oliis.com 
 * By Phil Rennie 25th March 09
 *
 */
 
/*
 *	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 = {
			slideContainer:	'.downslider',
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			controlsAround:	false,
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	false,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false,
			width:			false,
			height:			false,
			slides:			false,
			visibleSlides:	false,
			startAt:		0,
			tabbed:			false,
			tabSelector:	'.tab'
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s;
			var w; 
			var h; 
			
			/*if(!options.slides){
				s = $("li", obj).length;
			}  else {
				s = options.slides;	
			}*/
			s = $("li", obj).length;
			
			if(!options.width){
				w = $("li", obj).width();
			}  else {
				w = options.width;
			}
			if(!options.height){
				h = $("li", obj).height();
			}  else {
				h = options.height;	
			}
			
			if(!options.vertical){
				if(!options.visibleSlides){
					$(options.slideContainer, obj).width(w);
				} else {
					$(options.slideContainer, obj).width(w*options.visibleSlides);
				} 
				$(options.slideContainer, obj).height(h); 
			} else {
				if(!options.visibleSlides){
					$(options.slideContainer, obj).height(h);
				} else {
					$(options.slideContainer, obj).height(h*options.visibleSlides);
				} 
				$(options.slideContainer, obj).width(w);
			}
			
			$(options.slideContainer, obj).css("overflow","hidden");
			
			
			if(!options.visibleSlides){
				var ts = s-1;
			} else {
				var ts = s-options.visibleSlides;
			}
			
			var t = options.startAt;
			
			$("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 += '<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>';
				if(options.controlsAround){
					html += options.controlsAfter;
					$(obj).before(html);	
					var html = options.controlsBefore;
				}
				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);										
			};
			
			$('.slidebutton',obj).show();
			
			if(options.tabbed){
					$(options.tabSelector).each(function(tabIndex){
						tabsel = options.tabSelector;
						
						$(this).click(function(){
							animateTo(tabIndex,true);
							$img = $('img', this);
							$(tabsel).each(function(){
								$('img',this).attr('src',$('img',this).attr('src').replace('active','off'));
							});
							
							$img.attr('src',$img.attr('src').replace('off','active'));
							
						});
					 });	
				//}
			} else {
				$('.nextBtn',obj).click(function(){		
					animate("next",true);
				});
				
				$('.prevBtn',obj).click(function(){		
					animate("prev",true);
				});
			}
			
			if(options.startAt > 0){
				var ot = t;
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;	
				
				if(!options.vertical) {
					p = (options.startAt*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						speed
					);				
				} else {
					p = (options.startAt*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						speed
					);					
				};
			}
	
			/*
			$("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 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 "last":
						t = ts;
						break; 
					default:
						break; 
				};	
				
				//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(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};
				
				if(!options.continuous && options.controlsFade){					
					if(t==ts){
						$('.nextBtn',obj).hide();
						//$("a","#"+options.lastId).hide();
					} else {
						$('.nextBtn',obj).show();
						//$("a","#"+options.lastId).show();					
					};
					if(t==0){
						$('.prevBtn',obj).hide();
						//$("a","#"+options.firstId).hide();
					} else {
						$('.prevBtn',obj).show();
						//$("a","#"+options.firstId).show();
					};					
				};
				
			};
			
			function animateTo(pos,clicked){
				
				// turn off display of video on current slide
				$toggle = (t - 1 >= 0) ? t-1 : 1000 ; 
				$('.productdemo:eq('+$toggle+')').toggle();
								
				t = pos;				
				$toggle = (t - 1 >= 0) ? t-1 : 1000 ;
				
				//var diff = Math.abs(ot-t);
				//var speed = diff*options.speed;						
				
				if(!options.vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						'normal',
						'swing',
						function postToggle($index){
							$('.productdemo:eq('+$toggle+')').toggle();
						}
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						'normal',
						'swing',
						function postToggle($index){
							$('.productdemo:eq('+$toggle+')').toggle();
						}
					);					
				};
				
				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};
				
				if(!options.continuous && options.controlsFade){					
					if(t==ts){
						$('.nextBtn',obj).hide();
						//$("a","#"+options.lastId).hide();
					} else {
						$('.nextBtn',obj).show();
						//$("a","#"+options.lastId).show();					
					};
					if(t==0){
						$('.prevBtn',obj).hide();
						//$("a","#"+options.firstId).hide();
					} else {
						$('.prevBtn',obj).show();
						//$("a","#"+options.firstId).show();
					};					
				};		
			};
			
			
			// 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);
