(function($){  
  $.fn.wbtslider = function(options) {  
   	
		var defaults = {
		 width: 540,
		 slideshow: false,
		 sleep: 5000
		};

		var options = $.extend(defaults, options); 
				
		return this.each(function() {
			var sliderObj = $(this);
			var obj = $(this).find('.tab');
			var numObj = 0;
			var timer = 0;	
			var curObj = 0;
			
			
			// Count elements and set to hide
			obj.each(function(){
				if(numObj == 0) curObj = numObj;
				$(this).css('left', numObj*options.width).css('width', '100%');
				numObj++;
			});
			if(numObj <= 1) return;
			
			// build nav
			obj.parent().append('<div class="nav"></div>');
			obj.parent().find(".nav").each(function(){
				$(this).append('<div class="play"><a href="#play"></a></div>');
				$(this).append('<ul></ul>');															 
			});
			
			obj.parent().find(".nav ul").each(function(){
				for(var i=0;i <numObj;i++){
					var page = i +1;
					$(this).append('<li><a href="#'+i+'">'+page+'</a></li>');
				}
			});
			
			
			// Add Click Function
			
			// Change tab
			sliderObj.parent().parent().find("h3").click(function(){
				if(!$(this).hasClass('ui-state-active')){
					sliderObj.find(".nav .stop a").attr('href', '#play');
					sliderObj.find(".nav .stop").removeClass('stop').addClass('play');
					clearInterval(timer);
					}
			});
			
			// add menu link
			
			obj.parent().find(".nav li a").click(function(){
				var objNum = $(this).attr('href').split('#')[1];
				objNum = parseInt(objNum);
				curObj = slide_to(obj,objNum,curObj);
				return false;
			});
			
			
			// add slideshow link
			obj.parent().find(".nav .stop a, .nav .play a").click(function(){
					var action = $(this).attr('href').split('#')[1];
					// delete slideshow
					if(action == 'stop'){
						clearInterval(timer);
						$(this).attr('href', '#play');
						$(this).parent().removeClass('stop').addClass('play');
					}else{
					// add slideshow
						timer = setInterval(function(){
							if(curObj >= (numObj-2)){
								sliderObj.find(".nav .stop a").attr('href', '#play');
								sliderObj.find(".nav .stop").removeClass('stop').addClass('play');
								curObj = slide_to(obj,curObj+1,curObj);
								clearInterval(timer);
								return;
							}else{
								curObj = slide_to(obj,curObj+1,curObj);
							}
						}, options.sleep);
						$(this).attr('href', '#stop');
						$(this).parent().removeClass('play').addClass('stop');
					}
			
				return false;
			});
			
    });
		
		function slide_to(obj,to_obj,curObj){
			var dif = curObj - to_obj;
			var newObjPos = 0;
			var curPos = 0;
			var i=0;
			obj.each(function(){
				curPos = (i - curObj)*options.width;
				newObjPos = curPos+(options.width*dif);
				$(this).animate( { left: newObjPos+"px"} );
				i++;
			});
			return to_obj;
		}
				
  };
	
})(jQuery);


jQuery.fn.extend({
  slideRight: function() {
    return this.each(function() {
      jQuery(this).animate({width: 'show'});
    });
  },
  slideLeft: function() {
    return this.each(function() {
      jQuery(this).animate({width: 'hide'});
    });
  },
  slideToggleWidth: function() {
    return this.each(function() {
      var el = jQuery(this);
      if (el.css('display') == 'none') {
        el.slideRight();
      } else {
        el.slideLeft();
      }
    });
  }
});