(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,				
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {

			var obj      = $(this);
            var cont     = $(this).parent();
            var child    = $(this).children();
            var ws       = 0;
            var t        = 0;
            var active   = 0;
            var elWidth  = new Array();
            var sumWidth = 0;
            var visWidth = 0;
            var sliWidth = $(obj).width();

            $("a", obj).each(function(i) {
                elWidth[i] = $(this).outerWidth(true);
                sumWidth  += $(this).outerWidth(true);
                if(sumWidth < sliWidth) {
                    ws       = i+1;
                    visWidth = sumWidth;
                }
                if($(this).hasClass('active')) {
                    active = i+1;
                }
            });

            if($(child).is('table')) {
                var s  = $("td[class*='sideMiddle']", obj).length;
                var w  = $("td.sideMiddle", obj).width()+36;w=89;
                var h  = $("td", obj).height();
                var hs = s-11;
                $("table", obj).css('width',s*w-15);
            } else {
                var s  = $("a", obj).length;
                var w  = $("a", obj).width();
                var h  = $("a", obj).height();
                var hs = s-ws;
                $("div", obj).css('width',sumWidth);
                //alert('sumWidth='+sumWidth+' visibleWidth='+visWidth+' spaceWidth='+$(obj).width()+' s='+s+' hs='+hs+' ws='+ws+' active:'+active);
            }
			
			if(options.controlsShow){
				var html = options.controlsBefore;
				if(options.firstShow) html += '<span class="'+ options.firstId +'"><a href=\"javascript:void(0);\">&nbsp;</a></span>';
				html += ' <span class="'+ options.prevId +'"><a href=\"javascript:void(0);\">&nbsp;</a></span>';
				html += ' <span class="'+ options.nextId +'"><a href=\"javascript:void(0);\">&nbsp;</a></span>';
				if(options.lastShow) html += ' <span class="'+ options.lastId +'"><a href=\"javascript:void(0);\">&nbsp;</a></span>';
				html += options.controlsAfter;
				$(obj).after(html);
			}
	
			$('.'+options.nextId+' a', cont).click(function(){
				animate("next",true);
			});
			$('.'+options.prevId+' a', cont).click(function(){
				animate("prev",true);				
			});
			$('.'+options.firstId+' a', cont).click(function(){
				animate("first",true);
			});				
			$('.'+options.lastId+' a', cont).click(function(){
				animate("last",true);				
			});		
			
			function animate(dir,clicked){
				var ot = t;				

                if(dir == 'next') {
                    t = (ot>=hs) ? (options.continuous ? 0 : hs) : t+1;
                } else if(dir == 'prev') {
                    t = (t<=0) ? (options.continuous ? hs : 0) : t-1;
                } else if(dir == 'first') {
                    t = 0;
                } else if(dir == 'last') {
                    t = hs;
                }
				
				var diff  = Math.abs(ot-t);
				var speed = diff*options.speed;
                
				if($(child).is('table')) {
					p = (t*w*-1);
                    $("table",obj).animate({ marginLeft: p }, speed);
				} else {
                    if(t==0) {
                        p = 0;
                    } else {
                        p = sliWidth-sumWidth;
                        for(var i=s-1;i>s-1-(hs-t);i--) {
                            p += elWidth[i];
                        }
                    }
                    $("div",obj).animate({ marginLeft: p }, speed);
                }

				if(!options.continuous && options.controlsFade){
					if(t==hs){
						$('.'+options.nextId+' a',cont).hide();
						$('.'+options.lastId+' a',cont).hide();
					} else {
						$('.'+options.nextId+' a',cont).show();
						$('.'+options.lastId+' a',cont).show();
					}
					if(t==0){
						$('.'+options.prevId+' a',cont).hide();
						$('.'+options.firstId+' a',cont).hide();
					} else {
						$('.'+options.prevId+' a',cont).show();
						$('.'+options.firstId+' a',cont).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){
				$('.'+options.prevId+' a',cont).hide();
				$('.'+options.firstId+' a',cont).hide();
                if(t==hs){
                    $('.'+options.nextId+' a',cont).hide();
					$('.'+options.lastId+' a',cont).hide();
                }
			}

            //rewind to active
            if(active > ws) {
                for(var i=0;i<=(active-ws);i++) {
                    animate("next",false);
                }
            }
		});
	};
})(jQuery);




