/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.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.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		
		var targ;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3) // defeat Safari bug
			targ = targ.parentNode;
		if (targ!=o) return;
		
		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);
		y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );

		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	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.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)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.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;
	}
};


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

// Iguana

function dragInit(id,name) {
	var iefix = (BrowserDetect.browser=="Explorer" && parseInt(BrowserDetect.version)<8)?true:false;
	var handle = document.getElementById("bih_"+id);
	var root   = document.getElementById(name);
	if(!handle || !root) return;
	if(typeof(root.onDrag) == "function") return;
	handle.className+=" move";
	var rw = root.offsetWidth;
	var rh = root.offsetHeight;
	var rt = root.offsetTop;
	var rl = root.offsetLeft;

	var prt = 0;
	var prl = 0;
	
	if(iefix) {
		var pos = findPos(root);
		rt=pos[1];
		rl=pos[0];
		root.iefixot=rt;
		//root.parentNode.style.position='relative';
	}
	
	
	Drag.init(handle, root);
	//Drag.init(root,null);

	root.onDragStart = function (x,y) {
		root.ozIndex=root.style.zIndex;
		root.style.zIndex=10000;
		
		rt = root.offsetTop;
		rl = root.offsetLeft;
		
		
		if(iefix) {
			var pos = findPos(root);
			//document.getElementById('debug').innerHTML = root.offsetParent.offsetParent.id;
			rt=pos[1];
			rl=pos[0];
			root.iefixot=rt;
			root.iefixol=rl;
			//root.parentNode.style.position='relative';
		}

		//alert(rt + ' ' + rl);
		root.style.position='absolute';
		root.style.top=((rt-5))+'px';
		root.style.left=(rl)+'px';
		
		//Iguana.placeholder.style.width=rw+'px';
		Iguana.placeholder.style.height=(root.offsetHeight-12)+'px';
		root.parentNode.insertBefore(Iguana.placeholder,root.nextSibling);
		//root.className="boxd";
				
	}
	
	root.onDragEnd = function (x,y) {
		var p=document.getElementById('placeholder');
		root.postAction = function() {
			//root.className='box';
			root.style.zIndex=root.ozIndex;
			root.style.position='relative';
			root.style.top='0px';
			root.style.left='0px';
			child=p.parentNode.replaceChild(root,p);
			Iguana.rebuildBoxdata();
		};
		var ppos = findPos(p);
		var atop=(iefix)?ppos[1]:p.offsetTop;
		var aleft=(iefix)?ppos[0]:p.offsetLeft;
		//DDSanim.animate(root.id,{"top":[p.offsetTop+p.parentNode.offsetTop,"easeOutSine"],"left":[p.offsetLeft+p.parentNode.offsetLeft,"easeOutSine"]},300);	
		DDSanim.animate(root.id,{"top":[(atop-5),"easeOutSine"],"left":[aleft-5,"easeOutSine"]},300);
	}
	
	root.onDrag = function (x,y) {
		var center=[x+(root.offsetWidth/2),y];
		var wn=0;
		var p=document.getElementById('placeholder');
		//if(center[0]>p.offsetLeft && center[0]<p.offsetLeft+p.offsetWidth && center[1]>p.offsetTop && center[1]<p.offsetTop+p.offsetHeight-20)
			//alert('haho');
		var areas=Iguana.getAreas();
		var boxes=Iguana.getBoxes();
		for(var i=0;i<areas.length;i++) {
			var abox = document.getElementById(areas[i]);
			if(!abox) continue;
			if(iefix) abox.iefixol = findPos(abox)[0];
				
			var aol=(iefix && abox.iefixol)?abox.iefixol:abox.offsetLeft;
			
			if(center[0]>aol && center[0]<aol+abox.offsetWidth) {
				var child=p.parentNode.removeChild(p);
				for(var n=0; n<abox.childNodes.length;n++) {
					var cnode=abox.childNodes[n];
					if(iefix) cnode.iefixot = findPos(cnode)[1];
					if(cnode.nodeType!=1) {
						abox.removeChild(cnode);
					}
					if(cnode.id!=root.id && cnode.nodeType==1 && in_array(cnode.id,boxes)) {
						var cot=(iefix && cnode.iefixot)?cnode.iefixot:cnode.offsetTop;
						if(center[1]<cot+cnode.offsetHeight-20) {
							abox.insertBefore(child,cnode);
							return;
						} 
						wn=n;
					}
				}
				abox.insertBefore(child,abox.childNodes[wn].nextSibling);
			}
		}		
	}
}
function in_array(needle, haystack) {
	var key;
	for (key in haystack) {
		if (haystack[key] == needle) {
			return true;
		}
	}
	return false;
}

// font size changer

function initFontSizeChanger(w,hw,is) {
	var minsize = 11;
	var range = 6;
	
	var handle = document.getElementById("fcslider");
	if(is) {
		handle.style.left = ((parseInt(is)-minsize)*((w-hw)/range))+'px';
	}

	Drag.init(handle, null,0,(w-hw),0,0);

	handle.onDragStart = function (x,y) {

	}
	
	handle.onDragEnd = function (x,y) {
		DOsaveSettings("fontSize",Math.round(x/((w-hw)/range))+minsize);
	}
	
	handle.onDrag = function (x,y) {
		var val=Math.round(x/((w-hw)/range));
		document.getElementById("contentbody").style.fontSize = document.getElementById("fcvalue").innerHTML = (minsize + val) + 'px'; 
		//document.getElementById("fsvalue").innerHTML = (minsize + val) + 'px';
	}
}

