(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			300,
			speed_h: 			500	
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $("li.item", obj).length;
			var w = obj.width();
			var h = obj.height(); 	
			var ts = s-1;
			var t = 0;
			var th = 0;
			var anim_interval = 0;
			var anim_dir = "next";
			var vertical = (options.orientation == 'vertical');
			if(!vertical) { 
				$("li", obj).css('float','left');
				$("ul:first", obj).css('width',s*w);	
			}
			$(obj).parent().append('<span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span><span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>');		
			$('#' + options.prevId).hide();
			$('#' + options.nextId).hide();
			
			$('#' + options.nextId).click(function(){												   
				if(!$("ul:first",obj).is(":animated"))	{							   
					animate("next");
					if(!vertical && (t>=ts)) {					
						$(this).fadeOut();				
					}
					else if (vertical && (t>=(ts-3))) $(this).fadeOut();
					$('#' + options.prevId).fadeIn();
					clearInterval(anim_interval);		
				}
				return false;
			});
			$('#' + options.prevId).click(function(){	
				if(!$("ul:first",obj).is(":animated"))	{		
					animate("prev");
					if (t<=0) { 
						$(this).fadeOut();
						anim_dir = "next";
					}
					$('#' + options.nextId).fadeIn();
					clearInterval(anim_interval);
				}
				return false;
			});
			
			
			
			function animate_interval() {
				if(t>=ts) dir = "prev";
				else dir = "next";
				animate(dir);
			}
			
			//animation
			if((!vertical && (s > 1))) {
				anim_interval = setInterval(function(){if(anim_dir == "next") { animate("next"); if(t>=ts)  {$('#' + options.nextId).fadeOut(); anim_dir = "prev";}$('#' + options.prevId).fadeIn();} else {animate("prev"); if(t<=0)  {$('#' + options.prevId).fadeOut(); anim_dir = "next";}$('#' + options.nextId).fadeIn(); }}, 3500);
			}
			
			function animate(dir){
				
				if(dir == "next"){
					t = (t>=ts) ? ts : t+1;						
					if(vertical) {
						nt = t;
						el_h = $("li.item:eq(" + (nt-1) + ")", obj).children("a:first").height();	
						th = th+el_h+9;
						t_u = (current_item+1);						
					}
				} else {
					t = (t<=0) ? 0 : t-1;					
					if(vertical) {
						nt=current_item-1;
						el_h = $("li.item:eq(" + (t) + ")", obj).children("a:first").height();	
						th = th-(el_h+9);
						t_u =(current_item-4);						
					}
				};
				
				if(!vertical) {
					p = (t*w*-1);
					$("ul:first",obj).animate(
						{ marginLeft: p }, 
						options.speed_h
					);				
				} else {
					
					if(t == t_u) {
				
						var oldelem = $("#schedule").children('li').children('ul:visible').parent();						
						oldelem.children("a:first").removeClass("opened").addClass("closed");
						oldelem.children("ul").hide();
						
						var newelem = $("li.item:eq(" + (nt) + ")", obj);						
						newelem.children("a:first").removeClass("closed").addClass("opened");
						newelem.children("ul").show();
						newelem.children("ul").children().show();
						newelem.children("ul").height(newelem.children("ul").height());
						
						current_item = $("#schedule > li").index(newelem);					

					}
					
					$("ul:first",obj).animate(
						{ marginTop: th*-1 }, 
						options.speed
					);
				}
			};			
			if((vertical && (s > 4)) || (!vertical && (s > 1))) {
				$('#' + options.nextId).fadeIn();	
			}
		});
	  
	};

})(jQuery);
