/* 20090121-1000 */
/******************************************************************************
 * jqDnR - Minimalistic Drag'n'Resize for jQuery.
 *
 * Copyright (c) 2007 Brice Burgess <bhb@iceburg.net>, http://www.iceburg.net
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * $Version: 2007.08.19 +r2
 * Adaptado por Terra Networks - Equipe Webdev
 *
 ******************************************************************************/

(function($){
	$.fn.jqDrag=function(h,fDrag,fStop){return i(this,h,'d',fDrag,fStop);};
	$.fn.jqResize=function(h){return i(this,h,'r');};
	$.jqDnR={dnr:{},e:0,
		drag:function(v,fDrag){
			if(M.k == 'd') E.css({left:Math.max(Math.min(M.X+v.pageX-M.pX,E.parent().width() - E.width()),0),top:Math.max(Math.min(M.Y+v.pageY-M.pY,E.parent().height() - E.height()),0)});
			else E.css({width:Math.max(v.pageX-M.pX+M.W,0),height:Math.max(v.pageY-M.pY+M.H,0)});
			if (fDrag) fDrag();
			return false;
		},
		stop:function(fStop){$().unbind('mousemove').unbind('mouseup');if(fStop)fStop();}
	};
	var J=$.jqDnR,M=J.dnr,E=J.e,
	i=function(e,h,k,fDrag,fStop){
		return e.each(
			function(){h=(h)?$(h,e):e;
				h.bind('mousedown',{e:e,k:k},function(v){
					var d=v.data,p={};E=d.e;
					$("html").bind('dragstart', function(){return false;}).bind('selectstart', function(){return false;});

					// attempt utilization of dimensions plugin to fix IE issues
					if(E.css("position") != "absolute"){try{E.css("position","absolute");}catch(e){}
				}
				M={X:p.left||f('left')||0,Y:p.top||f('top')||0,W:f('width')||E[0].scrollWidth||0,H:f('height')||E[0].scrollHeight||0,pX:v.pageX,pY:v.pageY,k:d.k};
				$().mousemove(function(v) { $.jqDnR.drag(v,fDrag) }).mouseup(function() { $.jqDnR.stop(fStop);$("html").unbind("dragstart").unbind("selectstart"); });
				return false;
			});
		});
	},
	f=function(k){return parseInt(E.css(k))||false;};
})(jQuery);