var Goodle = {

/** VARIAVEIS E INICIALIZACOES **/
	obj : null,nivel: 100000,

/** DRAGS DROPS**/
	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper){

			o.onmousedown	= Goodle.start;

			o.hmode	= bSwapHorzRef ? false : true ;
			o.vmode	= bSwapVertRef ? false : true ;

			o.root = oRoot && oRoot != null ? oRoot : o ;

			if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
			if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
			if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
			if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

			o.minX	= typeof minX != 'undefined' ? minX : null;
			o.minY	= typeof minY != 'undefined' ? minY : null;
			o.maxX	= typeof maxX != 'undefined' ? maxX : null;
			o.maxY	= typeof maxY != 'undefined' ? maxY : null;

			o.xMapper = fXMapper ? fXMapper : null;
			o.yMapper = fYMapper ? fYMapper : null;

			o.root.ondblClick 	= new Function();
			o.root.onDragStart	= new Function();
			o.root.onDragEnd	= new Function();
			o.root.onDrag		= new Function();

			o.root.style.zIndex = Goodle.nivel++;
			
		}
	,
	start : function(e){
			var o = Goodle.obj = this;
			e = Goodle.fixE(e);
			var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
			var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );

			o.root.onDragStart(x, y);

			o.lastMouseX	= e.clientX;
			o.lastMouseY	= e.clientY;

			if (o.hmode) {
				if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
				if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
			} else {
				if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
				if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
			}

			if (o.vmode) {
				if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
				if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
			} else {
				if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
				if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
			}

			document.onmousemove	= Goodle.drag;
			document.onmouseup		= Goodle.end;

			document.onselectstart = new Function("return false");

			return false;
		}
	,
	resize: function(e, x, y){

		var o = Goodle.obj;
			
		if(o.lastMouseY!=y){
			Y = (parseInt(o.root.offsetHeight) + (y-o.lastMouseY))-2;
			o.root.style.height = Goodle.n2px(Y);
		}
		
		if(o.lastMouseX!=x){
			X = (parseInt(o.root.offsetWidth) + (x-o.lastMouseX))-2;
			o.root.style.width = Goodle.n2px(X);
		}
		
		return false;

	}
	,
	drag : function(e){

			e = Goodle.fixE(e);
			var o = Goodle.obj;

			var ey	= e.clientY;
			var ex	= e.clientX;
			var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
			var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
			var nx, ny;
	
			if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
			if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
			if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
			if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

			nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
			ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
			
			if (o.xMapper)		nx = o.xMapper(y);
			else if (o.yMapper)	ny = o.yMapper(x);

			//if(!e){
				/*Para IE e OP*/
			//	var e = window.event;
			//}
			
			if(e){
				if(e.shiftKey){
					//o.root.style.cursor = 'resize';
					Goodle.resize(e, ex, ey);
				}else{
					o.root.style[o.hmode ? "left" : "right"] = Goodle.n2px(nx);
					o.root.style[o.vmode ? "top" : "bottom"] = Goodle.n2px(ny);
				}
			}
		
			o.lastMouseX	= ex;
			o.lastMouseY	= ey;

			o.root.style.zIndex = Goodle.nivel++;

			//o.root.onDrag(nx, ny);

			return false;
		}
	,
	end : function(){
			document.onmousemove = null;
			document.onmouseup   = null;
			Goodle.obj.root.onDragEnd(	parseInt(Goodle.obj.root.style[Goodle.obj.hmode ? "left" : "right"]),
										parseInt(Goodle.obj.root.style[Goodle.obj.vmode ? "top" : "bottom"]));
			Goodle.obj = null;
		}
	,
	fixE : function(e){
			if (typeof e == 'undefined') e = window.event;
			if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
			if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
			return e;
		}
	,
	aux : function(w_obj,w_url,w_larg,w_alt){

			v_l = document.getElementById(w_obj)
			v_td = document.getElementById("td_"+w_obj)
			v_n = document.getElementById("navegador_"+w_obj)

			v_td.style.width = Goodle.n2px(parseInt(w_larg)+20);
			v_td.style.height = Goodle.n2px(parseInt(w_alt)+20);
			v_l.style.top =  Goodle.n2px(((screen.height-w_alt) / 2)-100);
			v_l.style.left = Goodle.n2px(((screen.width-w_larg) / 2)-100);

			//alert(w_obj + '= '+ (parseInt(w_larg)+20))
			v_n.src = "/includes/vazio.asp";
			v_n.style.height = Goodle.n2px(w_alt);
			v_n.style.width = Goodle.n2px(w_larg);
			v_n.src = w_url ? w_url : "/includes/vazio.asp";

			//ARRANJAR POIS NO IE invalida o select do campo à poosteriori
			//document.onselectstart = new Function("return false");

			Goodle.desactivaTags("none");

			v_l.style.display = "block";

			}
	,
	desactivaTags : function(w_modo){
			var tags = new Array("applet", "select");
			for (n=0;n<tags.length;n++){
				v_ele=document.getElementsByTagName(tags[n]);
				for(z=0;z<v_ele.length;z++){
					v_ele.item(z).style.display=w_modo;
				}
			}
		}
	,

	n2px : function (wN){return parseInt(wN) + "px";}

};