/*
 * 	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:	false,
			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');
			
			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>';
				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);										
			};
	
			$("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(!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();				
			};				
			
		});
	  
	};
	
	$.fn.PlayScroller = function(){
		this.children().stop();
		this.children().trigger('marquee',['resume']);
	};
	$.fn.PauseScroller = function(){
		this.children().stop();
	};
	$.fn.ResetScroller = function(options){
		this.children().stop();
		this.children().unbind('marquee');
		options = $.extend(this.data(), options);
		this.SetScroller(options);
	};
	$.fn.RemoveScroller = function(){
		this.children().stop();
		this.children().css({ top:0, left:0 });
		this.children().unbind('marquee');
	};
	$.fn.SetScroller = function (options) {
		var _ = { me: this };
		options = $.extend({ 
								velocity: 50,
								direction: 'horizontal',
								startfrom: 'right',
								loop: 'infinite',
								movetype: 'linear',
								onmouseover: 'pause',
								onmouseout: 'play',
								onstartup: 'play',
								cursor: 'pointer'
							}, 
							options);
		var velocity = typeof options.velocity == 'number' ? parseInt(options.velocity) : 50;
		var direction = options.direction.toLowerCase();
		var startfrom = options.startfrom.toLowerCase();
		var loop = typeof options.loop == 'number' && options.loop > 0 ? parseInt(options.loop) : -1; 
		var movetype = options.movetype.toLowerCase(); //TODO: 'linear' or 'pingpong'
		var onmouseover = options.onmouseover.toLowerCase();
		var onmouseout = options.onmouseout.toLowerCase();
		var onstartup = options.onstartup.toLowerCase();
		var cursor = options.cursor.toLowerCase();
		_.me.data(options);
		main();
		function main() {
			_.me.css('cursor',cursor);
			if(direction=='horizontal'){
				if(startfrom!='right' && startfrom!='left'){ startfrom='right'; };
				scrollHorizontal();
			}else{
				if(startfrom!='bottom' && startfrom!='top'){ startfrom='bottom'; };
				scrollVertical();
			};
			if(onmouseover=='play'){
				_.me.mouseover(function(){
					$(this).children().stop();
					$(this).children().trigger('marquee',['resume']);
				});
			}else{
				_.me.mouseover(function(){
					$(this).children().stop();
				});
			};
			if(onmouseout=='play'){
				_.me.mouseout(function(){
					$(this).children().stop();
					$(this).children().trigger('marquee',['resume']);
				});
			}else{
				_.me.mouseout(function(){
					$(this).children().stop();
				});
			};
			if(onstartup!='play'){
				_.me.children().stop();
			};
		};

		function scrollVertical(){
			_.me.children().bind('marquee', function(event,c) {
				var ob = $(this);
				var sh = parseInt(ob.parent().height());
				var th = parseInt(ob.height());
				var tt = parseInt(ob.position().top);
				var v  = velocity>0 && velocity<100 ? (100-velocity)*100 : 5000;
				var dr = parseInt(v*th/100)+v;
				if(movetype=='pingpong'){
					switch(startfrom){
					case 'bottom':
						if(typeof c == 'undefined'){ob.css({ top: sh-th });};
						sh = tt - (th + sh);
						break;
					default:
						if(typeof c == 'undefined'){ob.css({ top: 0 });};
						sh += tt + th;
					};
				}else{
					switch(startfrom){
					case 'bottom':
						if(typeof c == 'undefined'){
							ob.css({ top: sh });
							sh = -th;
						}else{
							sh = tt - (th + sh);
						};
						break;
					default:
						if(typeof c == 'undefined'){
							ob.css({ top: -th });
						}else{
							sh += tt + th;
						};
					};
				};
				if(loop<0 || loop>0){
					if(loop>0){loop--;};
					ob.animate(	{top:sh},
							{	duration:dr,
								easing:'linear',
								complete:function(){ob.trigger('marquee');},
								step:function(){
									switch(movetype){
									case 'pingpong':
										if(startfrom == 'bottom'){
											if(parseInt(ob.position().top) <= 0){
												startfrom='top';
												ob.stop();
												ob.trigger('marquee');
											};
										}else{
											if(parseInt(ob.position().top)+parseInt(ob.height()) >= parseInt(ob.parent().height())){
												startfrom='bottom';
												ob.stop();
												ob.trigger('marquee');
											};
										};
										break;
									default:
										if(startfrom == 'bottom'){
											if(parseInt(ob.position().top) < -parseInt(ob.height())){
												ob.stop();
												ob.trigger('marquee');
											};
										}else{
											if(parseInt(ob.position().top) > parseInt(ob.parent().height())){
												ob.stop();
												ob.trigger('marquee');
											};
										};
									};
								}
							});
				};
			}).trigger('marquee');
		};
		
		function scrollHorizontal(){
			_.me.children().bind('marquee', function(event,c) {
				var ob = $(this);
				var sw = parseInt(ob.parent().width());
				var tw = parseInt(ob.width());
				var tl = parseInt(ob.position().left);
				var v  = velocity>0 && velocity<100 ? (100-velocity)*100 : 5000;
				var dr = parseInt(v*tw/100)+v;				
				if(movetype=='pingpong'){
					switch(startfrom){
					case 'right':
						if(typeof c == 'undefined'){ob.css({ left: sw-tw });};
						sw = tl - (tw + sw);
						break;
					default:
						if(typeof c == 'undefined'){ob.css({ left: 0 });};
						sw += tl + tw;
					};
				}else{
					switch(startfrom){
					case 'right':
						if(typeof c == 'undefined'){	
							ob.css({ left: sw });
							sw = -tw;
						}else{
							sw = tl - (tw + sw);
						};
						break;
					default:
						if(typeof c == 'undefined'){						
							ob.css({ left: -tw });
						}else{
							sw += tl + tw;
						};
					};
				};
				if(loop<0 || loop>0){
					if(loop>0){loop--;};
					ob.animate(	{left:sw},
							{	duration:dr,
								easing:'linear',
								complete:function(){ob.trigger('marquee');},
								step:function(){
									switch(movetype){
									case 'pingpong':
										if(startfrom == 'right'){
											if(parseInt(ob.position().left) <= 0){
												startfrom='left';
												ob.stop();
												ob.trigger('marquee');
											};
										}else{
											if(parseInt(ob.position().left)+parseInt(ob.width()) >= parseInt(ob.parent().width())){
												startfrom='right';
												ob.stop();
												ob.trigger('marquee');
											};
										};
										break;
									default:
										if(startfrom == 'right'){
											if(parseInt(ob.position().left) < -parseInt(ob.width())){
												ob.stop();
												ob.trigger('marquee');
											};
										}else{
											if(parseInt(ob.position().left) > parseInt(ob.parent().width())){
												ob.stop();
												ob.trigger('marquee');
											};
										};
									};
								}
							});					
				};
			}).trigger('marquee');
		};

		return this;
	};

})(jQuery);
