function GDHTML()
{
	this.ChangeHTML = function(id,contenu)
	{
		document.getElementById(id).innerHTML=contenu;
	};

	this.CreateBox = function(titre,contenu)
	{
		rnd=Math.random()*10000;
		Idval=Math.round(rnd);
		Id = titre + Idval;
		objparent = document.body;
		var newBox = document.createElement('div');
		newBox.id = Id;
		newBox.className = 'window';

		var newHead= document.createElement('div');
		newHead.className='window_top';
		newHead.innerHTML = titre
		+ "<a href=\"#\" onclick=\"toggle(getEl('"+titre+'_body'+"'));\">[_]</a>"
		+ "<a href=\"#\" onclick=\"GDH.DeleteElement('"+Id+"');\">[X]</a>";
		newBox.appendChild(newHead);

		var newBody = document.createElement('div');
		newBody.innerHTML = contenu;
		newBody.className = 'window_body';
		newBody.id = titre + '_body';
		newBody.style.overflow='auto';
		newBox.appendChild(newBody);

		var newHead= document.createElement('div');
		newHead.className='window_bottom';
		newBox.appendChild(newHead);

		document.body.appendChild(newBox);
	};

	this.DeleteBox = function()
	{
		document.body.removeChild(document.body.lastChild);
	}

	this.showmenu = function()
	{
		var menu = document.getElementById('Menu');
		if(menu.style.display=='block')
		{
			document.getElementById('Menu').style.display='none';
		}else{
			document.getElementById('Menu').style.display='block';
		}
	};

	this.DeleteElement = function(id)
	{
		var element=document.getElementById(id);
		document.body.removeChild(element);
	}

	this.HideElement = function(id)
	{
		document.getElementById(id).style.display='none';
	}
	
	this.ShowElement = function(id)
	{
		document.getElementById(id).style.display='block';
	}

	this.ShowHideElement = function(id)
	{
		if(document.getElementById(id).style.display=='none')
		{
			this.ShowElement(id);
		}else{
			this.HideElement(id);
		}
	}

	this.refreshElement = function(id)
	{
		content=document.getElementById(id).innerHTML;
		document.getElementById(id).innerHTML='';
		document.getElementById(id).innerHTML=content;
	};

	this.countchar = function(id,idcontener,max)
	{
		string=document.getElementById(idcontener).value;
		var nb=max-string.length;
		document.getElementById(id).innerHTML=nb;
		if(nb<0)
		alert("Vous avez d&eacute;pass&eacute; la limite maximale autoris&eacute;e pour votre texte, il sera alors tronqu&eacute;.");
	};

	this.getPATime = function(lastpatime)
	{
		currenttime=new Date();
		currenttime=currenttime.getTime();
		lastpatime=lastpatime*1000;
		return lastpatime-currenttime;
	};
	
	this.timer = function(func,time)
	{
		timerObj=window.setTimeout(func,time*1000);
	}
	
	this.redirect = function(url)
	{
		window.location=url;
	}
}

var GDH = new GDHTML();

function ExtractNumber(value)
{
	var n = parseInt(value);
	return n == null || isNaN(n) ? 0 : n;
}

// this is simply a shortcut for the eyes and fingers
function $(id)
{
	return document.getElementById(id);
}

var _DragClass = 'window';
var _startX = 0; // Position de d�part de la souris.
var _startY = 0;
var _offsetX = 0; // offset de l'�l�ment courant.
var _offsetY = 0;
var _dragElement; // n�c�ssaire pour passer de OnMouseDown � OnMouseMove.
var _oldZIndex = 0; //on va temporairement incr�menter le z-index durant le d�placement.
var _debug = document.getElementById('Etat'); // simplifie la vie.
var _debugId = 'Etat';

InitDragDrop();

function InitDragDrop()
{
	document.onmousedown = OnMouseDown;
	document.onmouseup = OnMouseUp;
}

function OnMouseDown(e)
{
	// IE is retarded and doesn't pass the event object
	if (e == null)
	{
		e = window.event;
	}
	// IE uses srcElement, others use target
	var target = e.target != null ? e.target : e.srcElement;
	//GDH.ChangeHTML(_debugId,target.className == _DragClass ? 'draggable element clicked' : 'NON-draggable element clicked');
	// for IE, left click == 1
	// for Firefox, left click == 0
	if ((e.button == 1 && window.event != null || e.button == 0) && (target.className == _DragClass || target.parentNode.className == _DragClass))
	{
		if(target.className == _DragClass)
		{
			obj = target;
		}else if(target.parentNode.className == _DragClass)
		{
			obj = target.parentNode;
		}
		// grab the mouse position 
		_startX = e.clientX;
		_startY = e.clientY;
		// grab the clicked element's position 
		_offsetX = ExtractNumber(obj.style.left);
		_offsetY = ExtractNumber(obj.style.top);
		// bring the clicked element to the front while it is being dragged
		_oldZIndex = obj.style.zIndex;
		obj.style.zIndex = 10000;
		// we need to access the element in OnMouseMove 
		_dragElement = obj;
		// tell our code to start moving the element with the mouse 
		document.onmousemove = OnMouseMove;
		// cancel out any text selections
		document.body.focus();
		// prevent text selection in IE 
		document.onselectstart = function ()
		{
			return false;
		};
		// prevent text selection (except IE)
		return false;
	}
}

function OnMouseMove(e)
{
	if(e == null)
	{
		var e = window.event;
	}
	// this is the actual "drag code"
	varx=(e.clientX-ExtractNumber(ExtractNumber(_dragElement.style.width)/2));
	vary=(e.clientY);
	_dragElement.style.left =varx/*(_offsetX + e.clientX - _startX)*/ + 'px';//varx;
	_dragElement.style.top = vary/*(_offsetY + e.clientY - _startY)*/ + 'px';//vary;
	//GDH.ChangeHTML(_debugId,'(' + _dragElement.style.left + ', ' + _dragElement.style.top + ')');
}

function OnMouseUp(e)
{
	if(_dragElement != null)
	{
		_dragElement.style.zIndex = _oldZIndex;
		// we're done with these events until the next OnMouseDown
		document.onmousemove = null;
		document.onselectstart = null;
		// this is how we know we're not dragging
		_dragElement = null;
		//GDH.ChangeHTML(_debugId,'mouse up');
	}
}