/**
* EasyDrag 1.5 - Drag & Drop jQuery Plug-in
*
* Thanks for the community that is helping the improvement
* of this little piece of code.
*
* For usage instructions please visit http://fromvega.com/scripts
*/

(function($){
	var isMouseDown    = false;
	var currentElement = null;
	var dropCallbacks = {};
	var dragCallbacks = {};
	var bubblings = {};
	var lastMouseY;
	var lastElemTop;
	var lastElemLeft;
	var dragStatus = {};	
	var holdingHandler = false;

	$.getMousePosition = function(e){
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageY) { posy = e.pageY; }
		else if (e.clientY) { posy = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop; }
		return { 'y': posy };
	};

	$.updatePosition = function(e) {
		var pos = $.getMousePosition(e);
		if(lastElemTop+(pos.y - lastMouseY)>500 && lastElemTop + (pos.y - lastMouseY)<949) {
/*
		if(lastElemTop+(pos.y - lastMouseY)>480 && lastElemTop + (pos.y - lastMouseY)<920) {
*/
			var spanY = (pos.y - lastMouseY);
			$(currentElement).css("top",  (lastElemTop + spanY));
		};
	};

	$(document).mousemove(function(e){
		if(isMouseDown && dragStatus[currentElement.id] != 'false'){
			if(parseInt($('#steps').height())>190) $('#stepsCont').css({ overflow:'hidden' });
			$.updatePosition(e);
			if(dragCallbacks[currentElement.id] != undefined){
				dragCallbacks[currentElement.id](e, currentElement);
			}

			return false;
		}
	});

	$(document).mouseup(function(e){
		if(isMouseDown && dragStatus[currentElement.id] != 'false'){
			if(parseInt($('#steps').height())>190) $('#stepsCont').css({ overflowX:'hidden', overflowY:'scroll' });
			$('#directions input:first').focus();
			isMouseDown = false;
			if(dropCallbacks[currentElement.id] != undefined){
				dropCallbacks[currentElement.id](e, currentElement);
			}
			return false;
		}
	});

	$.fn.ondrag = function(callback){
		return this.each(function(){
			dragCallbacks[this.id] = callback;
		});
	};

	$.fn.ondrop = function(callback){
		return this.each(function(){
			dropCallbacks[this.id] = callback;
		});
	};
	
	$.fn.dragOff = function(){
		return this.each(function(){
			dragStatus[this.id] = 'off';
		});
	};
	
	$.fn.dragOn = function(){
		return this.each(function(){
			dragStatus[this.id] = 'on';
		});
	};
	
	$.fn.setHandler = function(handlerId){
		return this.each(function(){
			var draggable = this;
			bubblings[this.id] = true;
			$(draggable).css("cursor", "");
			dragStatus[draggable.id] = "handler";
			$("#"+handlerId).css("cursor", "n-resize");	
			$("#"+handlerId).mousedown(function(e){
				holdingHandler = true;
				$(draggable).trigger('mousedown', e);
			});
			$("#"+handlerId).mouseup(function(e){
				holdingHandler = false;
			});
		});
	}

	$.fn.easydrag = function(allowBubbling){
		return this.each(function(){
			if(undefined == this.id || !this.id.length) this.id = "easydrag"+(new Date().getTime());
			bubblings[this.id] = allowBubbling ? true : false;
			dragStatus[this.id] = "on";
			$(this).css("cursor", "n-resize");
			$(this).mousedown(function(e){
				if((dragStatus[this.id] == "off") || (dragStatus[this.id] == "handler" && !holdingHandler))
					return bubblings[this.id];
				$(this).css("position", "absolute");
				$(this).css("z-index", parseInt( new Date().getTime()/1000 ));
				isMouseDown    = true;
				currentElement = this;
				var pos    = $.getMousePosition(e);
				lastMouseY = pos.y;
				lastElemTop  = this.offsetTop;
				lastElemLeft = this.offsetLeft;
				$.updatePosition(e);
				return bubblings[this.id];
			});
		});
	};

})(jQuery);
