// drag & drop Library
// Developed by Bernardo De Marco for Hyphen-Italia
function dnd_getLeft(obj, real) {

	if (real) {
		if (obj.offsetParent)
			res = parseInt(obj.offsetLeft) + dnd_getLeft(obj.offsetParent, real);
		else
			res = parseInt(obj.offsetLeft);
	} else {
		var res = (obj.style.position == 'relative') ? NaN : parseInt(obj.style.left);
		
		// controllo il tipo di errore
		if (isNaN(res)) {
			// calcolo il posizionamento nell'area di disegno in maniera recursiva
			if (obj.offsetParent)
				res = parseInt(obj.offsetLeft) + dnd_getLeft(obj.offsetParent);
			else
				res = parseInt(obj.offsetLeft);
		} 
	}
	return res;
}

function dnd_getTop(obj, real) {
	if (real) {
		// calcolo il posizionamento nell'area di disegno in maniera recursiva
		if (obj.offsetParent)
			res = parseInt(obj.offsetTop) + dnd_getTop(obj.offsetParent, real);	
		else 
			res = parseInt(obj.offsetTop);
	} else {
		var res = (obj.style.position == 'relative') ? NaN : parseInt(obj.style.top);
		// controllo il tipo di errore
		if (isNaN(res)) {
			// calcolo il posizionamento nell'area di disegno in maniera recursiva
			if (obj.offsetParent)
				res = parseInt(obj.offsetTop) + dnd_getTop(obj.offsetParent);	
			else 
				res = parseInt(obj.offsetTop);
			
		}
	}
	return res;
}
function dnd_getHeight(obj) {
	var res = parseInt(obj.style.height);
	// controllo il tipo di errore
	if (isNaN(res))
		res = parseInt(obj.offsetHeight);
	return res;

}
function dnd_getWidth(obj) {

	var res = parseInt(obj.style.width);
	// controllo il tipo di errore
	if (isNaN(res))
		res = parseInt(obj.offsetWidth);
	return res;

}

function dragBounds(xOrObj, y, w, h) {
	if (xOrObj.style) {
		this.x = dnd_getLeft(xOrObj);
		this.y = dnd_getTop(xOrObj);
		this.w = dnd_getWidth(xOrObj);
		this.h = dnd_getHeight(xOrObj);
	} else {
		this.x = xOrObj;
		this.y = y;
		this.w = w;
		this.h = h;
	}
}

function dragOverZone(id, x, y, w, h) {

	this.inited	= true;
	
	if (id.style) {
		this.obj = id;
		this.id = id.id;
		this.x = dnd_getLeft(id);
		this.y = dnd_getTop(id);
		this.w = dnd_getWidth(id);
		this.h = dnd_getHeight(id);
	} else {
		this.obj = null;
		this.id = id;
		
		if (x) {
			this.x = x;
			this.y = y;
			this.w = w;
			this.h = h;
		} else {
			this.x = -1;
			this.y = -1;
			this.w = -1;
			this.h = -1;
			this.inited = false;
		}
	}
	
	
	
	this.update = function() {
		if (!this.inited) {
			this.obj = document.getElementById(this.id);
			this.inited = true;
		}
		if (this.obj) {
			this.x = dnd_getLeft(id);
			this.y = dnd_getTop(id);
			this.w = dnd_getWidth(id);
			this.h = dnd_getHeight(id);
		}
	}
	
	this.relativeLeft	= 0;
	this.relativeTop	= 0;
	this.over = false;
	this.anchor = dragOverZone_anchor;
	
	// gestori proprieta estese
	this.setValue = object_setValue;
	this.getValue = object_getValue;
	this.container = null;
}

function object_setValue(name, value) {
	if (!this.properties) 
		this.properties = new Object();
	this.properties[name] = value;
}


function object_getValue(name) {
	try {
		return this.properties[name];
	} catch (e) {
		return;
	}
}


function dragItemBehavior(ditem) {
	if (ditem) {
		this.onBegin	= ditem.onBegin;
		this.onEnd		= ditem.onEnd;
		this.onDrag		= ditem.onDrag;
		this.onDragOver	= ditem.onDragOver;
		this.onDragOut	= ditem.onDragOut;
		this.onDrop		= ditem.onDrop;
		this.onCancel	= ditem.onCancel;
		this.zones		= ditem.zones;
		this.bounds		= ditem.bounds;
	} else {
		this.onBegin		= null;
		this.onEnd			= null;
		this.onDrag			= null;
		this.onDragOver		= null;
		this.onDragOut		= null;
		this.onDrop			= null;
		this.onCancel		= null;
		this.onBeforeBegin	= null;
		this.onInit			= null;
		this.bounds			= null;
		this.zones			= new Array();
	}
	
	this.addOverObject	= draggableItem_addOverObject;
	this.addOverZone	= draggableItem_addOverZone;
	this.stepX			= 1;
	this.stepY			= 1;
	this.usePlaceholder	= false;
}

function dragOverZone_anchor(ditem) {
	ditem.obj.style.left = this.x;
	ditem.obj.style.top = this.y;
	ditem.obj.style.width = this.w;
	ditem.obj.style.height = this.h;
}

function draggableItem_addOverObject(id, x, y, w, h, container) {
	var newOver = new dragOverZone(id, x, y, w, h);
	newOver.container = container;
	// aggiunta zona
	this.zones[this.zones.length] = newOver;
	return newOver;
}

function draggableItem_addOverZone(zone) {
	if (zone) {
		// aggiunta zona
		zone.parent = this;
		this.zones[this.zones.length] = zone;
	}
}

// onBegin, onDrag, onEnd deprecati
function draggableItem(obj, objId, onBegin, onDrag, onEnd) {

	
	// evento
	this.ev				= null;
	
	// "delegati" eventi
	
	// inizio del drag & drop
	if (onBegin)
		this.onBegin	= onBegin;	// sara passato oggetto ed evento
	else
		this.onBegin	= null;
	
	// movimento
	if (onDrag)
		this.onDrag		= onDrag;	// sara passato oggetto ed evento
	else
		this.onDrag		= null;
	
	// fine del drag & drop
	if (onEnd)
		this.onEnd		= onEnd;	// sara passato oggetto ed evento
	else
		this.onEnd		= null;
	
	
	// drag over e out su zone
	this.onDragOver		= null;
	this.onDragOut		= null;
	this.onCancel		= null;
	
	// fine trascinamento su una zona
	this.onDrop		= null;
	
	if (obj.id) {
	
		this.obj		= obj;
		
		if (!objId) {
			this.objId = obj.id;
		} else {
			// id dell'oggetto che origina il trascinamento di un eventuale parent
			this.objId = objId;
		}
		this.inited		= true;
	} else {
		// obj come id dell'oggetto anzichč come div
		this.inited		= false;
		this.obj		= new Object();
		this.objId		= obj;
		this.obj.id		= obj;
	}
	
	
	// x e y: dimensioni rettangolo
	this.x			= -1;
	this.w			= -1;
	this.h			= -1;
	this.y			= -1;
	
	this.clickX		= -1;
	this.clickY		= -1;
	this.startX		= -1;
	this.startY		= -1;
	this.zones		= new Array();
	this.over		= false;

	this.behavior		= null;
	this.bounds			= null;
	
	// gestori proprieta estese
	this.setValue		= object_setValue;
	this.getValue		= object_getValue;
}

// prototipi oggetto draggableItem
draggableItem.prototype.setOffSet		= draggableItem_setOffSet;
draggableItem.prototype.setStart		= draggableItem_setStart;
draggableItem.prototype.addOverObject	= draggableItem_addOverObject;
draggableItem.prototype.addOverZone		= draggableItem_addOverZone;
draggableItem.prototype.restore			= draggableItem_restore;
draggableItem.prototype.init			= draggableItem_init;


function draggableItem_setOffSet(ev, relLeft, relTop) {
	var newX = (this.startX - this.clickX) + parseInt(ev.clientX) + parseInt(document.body.scrollLeft) - (relLeft ? relLeft : 0);
	var newY = (this.startY - this.clickY) + parseInt(ev.clientY) + parseInt(document.body.scrollTop) - (relTop ? relTop : 0);


	var bounds = this.behavior.bounds;

	// controllo se l'oggetto ha dei limiti di movimento ("contenitore")
	if (bounds) {
		if (((bounds.x < newX)  && ((bounds.x + bounds.w) > (newX + this.w))) 
			&& ((bounds.y < newY) && ((bounds.y + bounds.h) > (newY + this.h)))) {
		} else {
			// posiziono la x e la y hai margini del "contenitore"
			// x e y
			if (bounds.x > newX) {
				newX = bounds.x;
			} else {
				if ((bounds.x + bounds.w) < (newX + this.w)) {
					newX = (bounds.x + bounds.w) - this.w;
				}
			}
			
			if (bounds.y > newY) {
				newY = bounds.y;
			} else {
				if ((bounds.y + bounds.h) < (newY + this.h)) {
					newY = (bounds.y + bounds.h) - this.h;
				}
			}
		}
	}
	
	this.x = newX;
	if (this.behavior.stepX > 1) 
		this.x += (newX % this.behavior.stepX ? this.behavior.stepX - (newX % this.behavior.stepX): 0);
		
	this.y = newY;
	
	if (this.behavior.usePlaceholder) {
		this.phDiv.style.left	= this.x;
		this.phDiv.style.top	= this.y;
		this.opacity += 0.05;
		
		// aumento l'opacitą del div
		if (this.opacity < 0.90) {
			if (!document.all) 
				this.phDiv.style.MozOpacity = this.opacity;
		}
	
		
	} else {
		this.obj.style.left	= (this.x);
		this.obj.style.top	= (this.y);
	}
	return true;
	
}

function draggableItem_restore(ev) {
	this.obj.style.left = this.startX;
	this.obj.style.top = this.startY;
}


function draggableItem_init() {
	var res = false;
	if (!this.inited) {	// assegno il div all'elemento se non ancora inizializzato
		this.obj = document.getElementById(this.obj.id);
		this.inited = true;
		res = true;
	}
	
	return res;
	
}

function draggableItem_setStart(ev) {

	
	if (ev) {
		this.clickX	= parseInt(ev.clientX) + parseInt(document.body.scrollLeft);
		this.clickY	= parseInt(ev.clientY) + parseInt(document.body.scrollTop);
	} else {
		this.clickX = dnd_getLeft(this.obj, this.behavior.usePlaceholder) + 10;
		this.clickY = dnd_getTop(this.obj, this.behavior.usePlaceholder) + 10;
	}
	this.startX = dnd_getLeft(this.obj, this.behavior.usePlaceholder);
	this.startY = dnd_getTop(this.obj, this.behavior.usePlaceholder);
	this.w = dnd_getWidth(this.obj);
	this.h = dnd_getHeight(this.obj);
	
}				


function dragAndDrop(name) {

	this.name		= name ? name: 'dnd';

	this._WAITING	= 0;
	this._STARTING	= 1;
	this._MOVING	= 2;
	this._PROXIMITY = 5;
	this._ZONE_BOUND = 10;
	this._MOUSE_OVER_ZONE = true; 
	
		
	this.objects		= new Object();
	this.addObject		= dnd_addObject;
	this.add			= dnd_add;
	this.removeObject	= dnd_removeObject;
	this.clear			= dnd_clear;

	// "delegati" eventi globali di drag & drop
	this.onBegin		= null;	// inizio dd
	this.onDrag			= null; // dd in corso
	this.onEnd			= null; // fine dd
	this.onCancel		= null; // se inizio drag senza movimento mouse effettivo

	// "delegati" eventi rilanciati se l'evento del mouse non causa dd
	this.onMouseDown	= null;	// rilanciata se non inizia un dd (click su oggetto non trascinabile)
	this.onMouseMove	= null; // rilanciata durante movimenti del mouse non di trascinamento
	this.onMouseUp		= null; // rilanciata al rilascio del pulsante del mouse se non dd
	
	// gestori mouse
	this.mouseMove		= dnd_mouseMove;
	this.mouseUp		= dnd_mouseUp;
	this.mouseDown		= dnd_mouseDown;
	
	// gestori di cattura degli eventi di drag & drop
	this.attachEvents	= dnd_attachEvents;
	this.detachEvents	= dnd_detachEvents;
	
	// inizia il trascinamento dell'oggetto anche se non viene cliccato 
	// sull'oggetto corrispondente
	this.startDrag		= dnd_startDrag;
	
	// funzione per placeholder di trascinamento
	this.getPlaceholder	= dnd_getPlaceholder;
	
	// valore booleano che indica se č in corso un'azione di trascinamento
	this.dragging		= false;
	
	this.status			= this._WAITING;
	this.currentObject	= null;
	
	this.previousX		= -1;
	this.previousY		= -1;
	
	this.overZone		= dnd_overZone;
	
	try {
		this.resizer		= new dragResizer(this);
	} catch (e) {
		this.resizer		= null;
	}
	
}

function dnd_attachEvents() {

	// creo degli alias alla funzione
	eval('function _dnd_mm_'+this.name+'(ev) { '+this.name+ '.mouseMove(ev); }');
	eval('function _dnd_md_'+this.name+'(ev) { '+this.name+ '.mouseDown(ev); }');
	eval('function _dnd_mu_'+this.name+'(ev) { '+this.name+ '.mouseUp(ev); }');
		

	// attach degli eventi del mouse
	if (document.all) {
		document.attachEvent('onmousemove', eval('_dnd_mm_'+this.name));
		document.attachEvent('onmousedown', eval('_dnd_md_'+this.name));
		document.attachEvent('onmouseup', eval('_dnd_mu_'+this.name));
	} else {
		document.addEventListener('mousemove', eval('_dnd_mm_'+this.name), true);
		document.addEventListener('mousedown', eval('_dnd_md_'+this.name), true);
		document.addEventListener('mouseup', eval('_dnd_mu_'+this.name), true);
	}
	
}

function dnd_detachEvents() {
	// attach degli eventi del mouse
	if (document.all) {
		document.detachEvent('onmousemove', eval('_dnd_mm_'+this.name));
		document.detachEvent('onmousedown', eval('_dnd_md_'+this.name));
		document.detachEvent('onmouseup', eval('_dnd_mu_'+this.name));
	} else {
		document.removeEventListener('mousemove', eval('_dnd_mm_'+this.name), true);
		document.removeEventListener('mousedown', eval('_dnd_md_'+this.name), true);
		document.removeEventListener('mouseup', eval('_dnd_mu_'+this.name), true);
	}
}


function dnd_overZone(zone, x, y, w, h) {
	if (this._MOUSE_OVER_ZONE) {
	
		return (zone.x <= (x + parseInt(document.body.scrollLeft) + zone.relativeLeft)) && (zone.x + zone.w >= (x + parseInt(document.body.scrollLeft) + zone.relativeLeft))
		 && (zone.y <= (y + parseInt(document.body.scrollTop) + zone.relativeTop)) && (zone.y + zone.h >= (y + parseInt(document.body.scrollTop) + zone.relativeTop))
		
		//return ((zone.x - this._ZONE_BOUND) <= x) && (zone.x + zone.w >= x) && ((zone.y - h) < y) && (zone.y + zone.h > y);
	} else  {
		return ((zone.x - w) <= x) && (zone.x + zone.w >= x) && ((zone.y - h) < y) && (zone.y + zone.h > y);
	}
}

function dnd_startDrag(objectId, ev, relLeft, relTop) {
	// richiamo la funzione come se il mouse fosse down
	this.mouseDown(ev, objectId, relLeft, relTop);	
	return this.currentObject;
}

function dnd_addObject(htmlObject, behavior) {
	var ditem = new draggableItem(htmlObject);
	if (behavior) {
		ditem.behavior = behavior;
	}
	this.objects[ditem.objId] = ditem;
	
}

function dnd_add(ditem, behavior) {
	this.objects[ditem.objId] = ditem;
	if (behavior)
		ditem.behavior = behavior;
}



function dnd_removeObject(ditem) {
	this.objects[ditem.objId] = null;
}

function dnd_clear() {
	// azzero tutti gli oggetti del drag&drop
	this.objects = null;
	this.objects = new Object();
}

function dnd_mouseMove(ev) {
	
	if (this.dragging) {
		

		
		var obj = this.currentObject;
		
		if (this.status = this._STARTING) {
			if (this.onBegin) {
				// evento 
				this.onBegin(obj, ev);
			}
			
			if (obj.behavior.onBegin) {
				// richiamo gestore del singolo oggetto
				obj.behavior.onBegin(obj, ev);
				
				
			} 
			
			if (this.resizer && this.resizer.available && !this.resizer.resizing)
				this.resizer.hide();
				
			
			this.status = this._MOVING
		}
		
		// posizionamento oggetto
		var doMovement = obj.setOffSet(ev, this.currentObject.relativeLeft, this.currentObject.relativeTop);
		
		
		ev.cancelBubble = true;
		ev.returnValue = false;
		if (ev.preventDefault)
			ev.preventDefault();
		if (doMovement) {
			
			
			
			if (obj.behavior.zones.length) {
				
				this.over = false;
				
				var e = new Object();
				e.x = obj.x;
				e.y = obj.y;
				
				
				
				// verifico se si trova su qualche zona
				if ((e.x > (this.previousX + this._PROXIMITY)) || (e.x < (this.previousX - this._PROXIMITY)) 
				 || (e.y > (this.previousY + this._PROXIMITY)) || (e.y < (this.previousY - this._PROXIMITY))) {
					
					var overZones = new Array();
					
					// controllo la zona
					for (var i = 0; i < obj.behavior.zones.length; i++) {
						var thisZone = obj.behavior.zones[i]; 
						
						if (this.overZone(thisZone, e.x, e.y, obj.w, obj.h)) {
							if (!thisZone.over) {
								overZones[overZones.length] = thisZone;
								thisZone.over = true;
							}
						} else {
							if (thisZone.over) {
								if (obj.behavior.onDragOut) {
									obj.behavior.onDragOut(obj, thisZone, ev);
								}
								thisZone.over = false;
							}
						}
						
					}
					
					// imposto lo stato della zona
					this.over = thisZone.over || this.over;
					
					
					for (var i = 0; i < overZones.length; i++) {
						if (obj.behavior.onDragOver) {
							obj.behavior.onDragOver(obj, overZones[i], ev);
						}
					}
								
					overZones.length = 0;
					
					this.previousX = e.x;
					this.previousY = e.y;
				}
			}
			
			if (this.onDrag) {
				// richiamo la funzione definita in javascript
				this.onDrag(obj, ev);
			}
			
			// oggetto corrente
			if (obj.behavior.onDrag) {
				obj.behavior.onDrag(obj, ev);
			}
		}
	} else {
		// rilancio l'eventuale delegato
		if (this.onMouseMove) {
		
			var srcElement = null;
			if (document.all) {
			// elemento da cui si č originato l'evento
				srcElement = ev.srcElement;
			} else {
				srcElement = ev.target;
			}
		
			this.onMouseMove(srcElement, ev);
		}
	}
	return true;
}

function dnd_mouseUp(ev) {
	if (this.dragging) {
		this.dragging = false;
		var obj = this.currentObject;
		
		
		if (this.status == this._MOVING) {
	
	
			if (this.onEnd) {
				// richiamo la funzione definita in javascript
				this.onEnd(obj, ev);
			}
			
			var onDropFired = false;
			
			// controllo la zona
			for (var i = 0; i < obj.behavior.zones.length; i++) {
				var thisZone = obj.behavior.zones[i]; 
				if (thisZone.over) {
					if (obj.behavior.onDrop) {
						obj.behavior.onDrop(obj, thisZone, ev);
						onDropFired = true;
					}
				}
				thisZone.over = false;
			}
			
			// oggetto corrente
			if (!onDropFired && obj.behavior.onEnd) {
				obj.behavior.onEnd(obj, ev);
			}
		} else {
			if (this.onCancel) {
				// richiamo la funzione definita in javascript
				this.onCancel(obj, ev);
			}
			
			if (obj.behavior.onCancel) {
				obj.behavior.onCancel(obj, ev);
			}
		}
		
		this.status = this._WAITING;
		
		if (obj.behavior.usePlaceholder)
			obj.phDiv.style.visibility = 'hidden';
		
	} else {
		// rilancio l'eventuale delegato
		if (this.onMouseUp) {
			var srcElement = null;
			if (document.all) {
			// elemento da cui si č originato l'evento
				srcElement = ev.srcElement;
			} else {
				srcElement = ev.target;
			}
		
			this.onMouseUp(srcElement, ev);
		}
	}
	
	
	return true;
}




function dnd_getPlaceholder(ditem) {
	
	var placeHolderName = '__hy_dnd_ph';

	// creo il placeholder
	var phDiv = document.getElementById(placeHolderName);
	if (!phDiv) {
		
		phDiv = document.createElement('div');
		// aggiunta nuovo div
		phDiv.id = placeHolderName;
		
		phDiv.style.position		= 'absolute';
		phDiv.style.zIndex			= 999;
		
		document.body.appendChild(phDiv);
	}

	// clono il div
	for (prop in ditem.obj.style) {
		if (ditem.obj.style[prop]) {
			try {
				if (isNaN(prop)) {
					switch (prop) {
					case 'position': case 'width': case 'height': 
					case 'top': case 'left': case 'cssText': case 'zIndex': 
					case 'visibility':
						break;
					default:
						phDiv.style[prop] = ditem.obj.style[prop];
					}
				}
			} catch (e) {
			}
		}
	}

	// proprietą dello stile
	phDiv.className				= ditem.obj.className;
	phDiv.style.visibility		= 'visible';
	phDiv.style.top				= ditem.y;
	phDiv.style.left			= ditem.x;
	phDiv.style.width			= ditem.w;
	phDiv.style.height			= ditem.h;
	
	if (ditem.obj.src) {
		phDiv.innerHTML = '<img src="'+ditem.obj.src+'" width="'+ditem.w+'" height="'+ditem.h+'" />';
	} else {
		phDiv.innerHTML = ditem.obj.innerHTML;
	}
	
	// placeholder div
	ditem.phDiv = phDiv;
	ditem.opacity = 0.05;
	
	if (!document.all)
		phDiv.style.MozOpacity = ditem.opacity;
	

}

function dnd_mouseDown(ev, objectId, relLeft, relTop) {
	

	var objId = null;
	var srcElement = null;
	var objSpecified = false;
	var res = false;
	
	
	
	if (objectId) {
		objId = objectId;
		objSpecified = true;
	} else {
		if (document.all) {
			// elemento da cui si č originato l'evento
			srcElement = ev.srcElement;
		} else {
			srcElement = ev.target;
		}
		
		// id dell'elemento
		if (srcElement.id) {
			objId = srcElement.id;
		}
		
	}
	
	
	// controllo se č specificato l'oggetto
	if (objId) {
		
		if (this.objects[objId]) {
			
			
			this.dragging = true;
			this.currentObject = this.objects[objId];
			
			if (!this.currentObject.behavior) {
				this.currentObject.behavior = new dragItemBehavior(this.currentObject);
			}
			
			if (this.currentObject.init()) {
				// richiamo la routine di inizializzazione se presente
				if (this.currentObject.behavior.onInit)
					this.currentObject.behavior.onInit(this.currentObject, ev);
			}
			
			
			
			if (objSpecified) {
				this.currentObject.setStart();
			} else {
				this.currentObject.setStart(ev);
			}
			
			
			
			// controllo se devo usare un placeholder per il drag&drop
			if (this.currentObject.behavior.usePlaceholder)
				this.getPlaceholder(this.currentObject);
			
			if (relLeft) {
				this.currentObject.relativeLeft = relLeft;
				this.currentObject.relativeTop = relTop;
			} else {
				this.currentObject.relativeLeft = null;
				this.currentObject.relativeTop = null;
			}
			
			if (this.currentObject.behavior.onBeforeBegin)
				res = this.currentObject.behavior.onBeforeBegin(this.currentObject, ev);
			
			// aggiornamento zone
			for (var i = 0; i < this.currentObject.behavior.zones.length; i++) {
				this.currentObject.behavior.zones[i].update();
			}
			
			
			
			
			
			
			this.currentObject.setOffSet(ev, relLeft, relTop);
			
			
			
			this.status = this._STARTING;
			
			if (document.all) {
				ev.returnValue = false;
				ev.cancelBubble = true;
			} else {
				ev.preventDefault();
			}
		} else {
			// rilancio l'eventuale delegato
			if (this.onMouseDown) {
				res = this.onMouseDown(srcElement, ev);
			}
		}
	} else {
		// rilancio l'eventuale delegato
		if (this.onMouseDown) {
			res = this.onMouseDown(srcElement, ev);
		}
	}
	return res;
}



function Div(div) {
	
	
	
	this.div = div;
	this.x = dnd_getLeft(div);
	this.y = dnd_getTop(div);
	this.w = dnd_getWidth(div);
	this.h = dnd_getHeight(div);
	
	this.startX = this.x;
	this.startY = this.y;
	this.startW = this.w;
	this.startH = this.h;
}


Div.prototype.show			= div_show;
Div.prototype.hide			= div_hide;
Div.prototype.move			= div_move;
Div.prototype.resize		= div_resize;
Div.prototype.updateOffset	= div_updateOffset;

function div_show() {
	this.div.style.visibility = 'visible';
}
function div_hide() {
	this.div.style.visibility = 'hidden';
}

function div_move(x, y) {
	if (y) {
		this.div.style.top = parseInt(y) + 'px';
		this.y = parseInt(y);
	}
	if (x) {
		this.div.style.left = parseInt(x) + 'px';
		this.x = parseInt(x);
	}
}

function div_resize(w, h) {
	var bw = parseInt(this.div.style.borderWidth);

	bw = isNaN(bw) ? 0 : bw;

	if (w != null) {
		this.div.style.width = (parseInt(w) - (bw * 2)) + 'px';
		this.w = w;
	}
	
	if (h != null) {
		this.div.style.height = (parseInt(h) - (bw * 2)) + 'px';
		this.h = h;
	}
	
}

function div_updateOffset() {
	this.x = dnd_getLeft(this.div);
	this.y = dnd_getTop(this.div);
	this.w = dnd_getWidth(this.div);
	this.h = dnd_getHeight(this.div);
	
	this.startX = this.x;
	this.startY = this.y;
	this.startW = this.w;
	this.startH = this.h;
}
