﻿(function($){
	$.fn.dragable=function(options) {
		var def={
			windowhandle: null
		};
		var o=$.extend(def, options);
		
		return this.each(function() { 
			var it=$(this);
			var wh=it;
			if (o.windowhandle!=null) {wh=$(o.windowhandle)}
			var x=null;
			var y=null;
			var shiftX=null;
			var shiftY=null
			it.bind("mousedown", startDrag);
			it.css("position", "absolute");
			
			function startDrag(event) {
				x=event.pageX;
				y=event.pageY;
				var pos=it.position();
				shiftY=y-pos.top;
				shiftX=x-pos.left;
				it.unbind("mousedown", startDrag);
				$(document).bind("mousemove", onMouseMove);
				$(document).bind("mouseup", stopDrag);
				//event.preventDefault(); 
				return false;
			}
			
			function onMouseMove(event) {
				  x=event.pageX;
				  y=event.pageY;
				  it.css({position: "absolute", top: y-shiftY, left: x-shiftX});
				  return false;
				  
			}
			
			function stopDrag(event) {
				$(document).unbind("mousemove", onMouseMove);
				$(document).unbind("mouseup", stopDrag);
				it.bind("mousedown", startDrag);
			}
		});		
	};
	function debug(message) {
		if (window.console && window.console.log) {
			window.console.log(message); 
		} 		
	};
})(jQuery);