(function($){
	
  var settings = {
	onMouseWheel: null,
	onDrag: null,
	onUpButton:null,
	onDownButton:null
  };

  var methods = {
	config: function(options){
		$.extend(settings, options);
	},
	
    init:function(options){
		$.extend(settings,options);
						
		$(this).each(function() {
			var obj = $(this);
			var container = "<div class='iwsSlider_Container'>";
			var topBtn = "<div class='iwsSlider_TopButton'></div>";
			var botBtn = "<div class='iwsSlider_BottomButton'></div>";
			var thumb = "<div class='iwsSlider_Thumb'></div>";
			var track = "<div class='iwsSlider_Track'></div>";
			var containerEnd = "</div>";
			var str = container + topBtn + botBtn + track + thumb + containerEnd;
			
			obj.prepend(str);
			container = obj.children(".iwsSlider_Container");
			container.children(".iwsSlider_Track").height(1 + obj.closest('div').height() - 
							 							 container.children(".iwsSlider_TopButton").height() - 
							 							 container.children(".iwsSlider_BottomButton").height());
			
			thumb = container.children(".iwsSlider_Thumb")
			thumb.mousedown(handleThumbDown);
			thumb.mouseup(handleThumbUp);
			
			obj.iwsSlider('positionElements');
		});
	},
	
	positionElements:function() {
		var obj = $(this).children('.iwsSlider_Container');
		var thumb = obj.children('.iwsSlider_Thumb');
		var top = obj.children('.iwsSlider_TopButton');
		var bot = obj.children('.iwsSlider_BottomButton');
		var track = obj.children('.iwsSlider_Track');
		
		thumb.css("top",top.height());
		track.css("top",top.height());
		
		bot.css("top",track.height() + top.height());
		var w = $(this).width();
		var nw = top.width();
		var tw = w-nw;
		thumb.css("left",tw+"px");
		top.css("left",tw+"px");
		bot.css("left",tw+"px");
		track.css("left",tw+"px");
	}
  
  };
  
  function handleThumbDown(e) {
	  e.preventDefault();
	  var ref = $(this).closest('.iwsSlider_Container');
	  $(document).bind('mousemove.drag',function(e) {
		  handleThumbMoveManual(e,ref);
	  });
	  
	  $(document).bind('mouseup.drag',handleThumbUp);
	  $(this).closest('.iwsSlider_Container').bind('mouseleave.dragging',handleThumbOut);
  }
  
  function handleThumbOut() {
	   $(this).closest('.iwsSlider_Container').unbind('mousemove.dragging');
  }
  
  function handleThumbUp() {
	  $(document).unbind('mousemove.drag');
	  $(document).unbind('mouseup.drag');
  }
  
  function handleThumbMoveManual(e, b) {
	  var lo = b.offset().left;
	  var to = b.offset().top;
	  var th = b.children('.iwsSlider_Thumb');
	  var newTop = e.pageY - to - th.height() / 2;
	  var minTop = b.children('.iwsSlider_TopButton').height();
	  var maxTop = b.children('.iwsSlider_Track').height() + 
	  				b.children('.iwsSlider_TopButton').height() - 
					b.children('.iwsSlider_Thumb').height();
					
	  if(newTop < minTop)
	  	newTop = minTop
	  if(newTop > maxTop)
	  	newTop = maxTop;
		
	  th.css("top",newTop);
	  
	  var perc = (newTop - minTop) / (maxTop - minTop);
	  if(settings.onDrag)
		  settings.onDrag(b.closest("div"), perc);
  }
  
  function handleThumbMove(e) {
	  var lo = $(this).offset().left;
	  var to = $(this).offset().top;
	  var th = $(this).children('.iwsSlider_Thumb');
	  var newTop = e.pageY-to-th.height()/2;
	  var minTop = $(this).children('.iwsSlider_TopButton').height();
	  var maxTop = $(this).children('.iwsSlider_Track').height() + 
	  				$(this).children('.iwsSlider_TopButton').height() - 
					$(this).children('.iwsSlider_Thumb').height();
					
	  if(newTop < minTop)
	  	newTop = minTop
	  if(newTop > maxTop)
	  	newTop = maxTop;
		
	  th.css("top",newTop);
	  
	  var perc = (newTop - minTop) / (maxTop - minTop);
	  if(settings.onDrag)
		  settings.onDrag($(this).closest("div"), perc);
  }

  $.fn.iwsSlider = function(method){
    if (methods[method]){
      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === 'object' || !method) {
      return methods.init.apply(this, arguments);
    } else {
      $.error('Method ' +  method + ' does not exist on iwsSlider');
    }    
  };
  
})(jQuery);
