(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='iws-slider-container'>";
			var topBtn = "<div class='iws-slider-topbutton'></div>";
			var botBtn = "<div class='iws-slider-bottombutton'></div>";
			var thumb = "<div class='iws-slider-thumb'></div>";
			var track = "<div class='iws-slider-track'></div>";
			var containerEnd = "</div>";
			var str = container+topBtn+botBtn+track+thumb+containerEnd;
			
			obj.prepend(str);
			container = obj.children(".iws-slider-container");
			container.children(".iws-slider-track").css("height",obj.closest('div').height()-container.children(".iws-slider-topbutton").height()-container.children(".iws-slider-bottombutton").height()+1);
			obj.iwsSlider('positionElements');
			
			thumb = obj.children(".iws-slider-container").children(".iws-slider-thumb")
			thumb.mousedown(handleThumbDown);
			thumb.mouseup(handleThumbUp);
		});
	},
	
	
	
	update:function(){
		
	},
	
	positionElements:function() {
		var obj = $(this).children('.iws-slider-container');
		var thumb = obj.children('.iws-slider-thumb');
		var top = obj.children('.iws-slider-topbutton');
		var bot = obj.children('.iws-slider-bottombutton');
		var track = obj.children('.iws-slider-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) {
	  
	  var ref = $(this).closest('.iws-slider-container');
	  $(document).bind('mousemove.drag',function(e) {
		  handleThumbMoveManual(e,ref);
	  });
	  
	  $(document).bind('mouseup.drag',handleThumbUp);
	  $(this).closest('.iws-slider-container').bind('mouseleave.dragging',handleThumbOut);
	  e.preventDefault();
  }
  function handleThumbOut() {
	   $(this).closest('.iws-slider-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('.iws-slider-thumb');
	  var newTop = e.pageY-to-th.height()/2;
	  var minTop = b.children('.iws-slider-topbutton').height();
	  var maxTop = b.children('.iws-slider-track').height() + 
	  				b.children('.iws-slider-topbutton').height() - 
					b.children('.iws-slider-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('.iws-slider-thumb');
	  var newTop = e.pageY-to-th.height()/2;
	  var minTop = $(this).children('.iws-slider-topbutton').height();
	  var maxTop = $(this).children('.iws-slider-track').height() + 
	  				$(this).children('.iws-slider-topbutton').height() - 
					$(this).children('.iws-slider-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);
