// JavaScript Document
var _MOVE_ 		= 0;
var _RESIZE_N_ 	= 1;
var _RESIZE_S_ 	= 2;
var _RESIZE_W_ 	= 3;
var _RESIZE_E_ 	= 4;
var _RESIZE_NW_ = 5;
var _RESIZE_NE_ = 6;
var _RESIZE_SW_ = 7;
var _RESIZE_SE_ = 8;

var thePadding=5;

var objectDragendrop=null;
//px,py position du div conteneur par rapport a la page...
//pxmax,pymax limite de deplacement dans la page...
function clDragendrop(pdivname,purlimage,pwidth,pheight,px,py,pxmax,pymax,bgcolor){

	//PS: pdivname doit avoir le meme nom que l'object

    //variable

	this.debscript="Image/resize.php?image=";
	this.finscript="&h=300&w=300";
	this.urlimage =purlimage;
	this.divname =pdivname;
	this.oldpoint;
	this.point = new Array(px,py);
	
	this.xparent=px;
	this.yparent=py;
	this.xparentmax=pxmax;
	this.yparentmax=pymax;
	
	this.last_width;
	this.last_height;
	
	this.last_left=0;
	this.last_top=0;
	
	if(LireCookie('debscript')!=null){
		this.debscript=LireCookie('debscript')
	}
	if(LireCookie(this.divname + '.styleobject.height')!=null){
		pheight=parseInt(LireCookie(this.divname + '.styleobject.height'));
	}
	if(LireCookie(this.divname + '.styleobject.width')!=null){
		pwidth=parseInt(LireCookie(this.divname + '.styleobject.width'));
	}
	if(LireCookie(this.divname + '.styleobject.top')!=null){
		this.last_top=parseInt(LireCookie(this.divname + '.styleobject.top'));
	}
	if(LireCookie(this.divname + '.styleobject.left')!=null){
		this.last_left=parseInt(LireCookie(this.divname + '.styleobject.left'));
	}
	
	
	this.pclicked =false;
	this.numcursor=0;
	
	this.paddingtop = 0;
	this.paddingleft = 0;
	this.paddingright = 0;
	this.paddingbottom = 0;
	
	//function
	
	function create(width,height,bgcolor){
		document.write("<div id='" + this.divname + "' style='{background: #" + bgcolor + ";border: 0px;cursor: move;position: absolute; top: " + this.last_top + "px; left: " + this.last_left + "px; width: " + width + "px; height: " + height + "px}' ");
		document.write("onmousedown='" + this.divname + ".onmousedown(event);' ");
		document.write("onmouseup='" + this.divname + ".onmouseup(event);' ");
		document.write("onmouseover='" + this.divname + ".move(event);' ");
		document.write("onmousemove='" + this.divname + ".move(event);' ");
		document.write("onmouseout='" + this.divname + ".move(event);' ");
		document.write("></div>");
		document.onmousemove=this.moveout;
		document.onmouseup=this.onmouseup;
		this.styleobject=getelement(this.divname).style;
		return true;	
	}
	this.create=create;

	function onmousedown(e) {
		if(this.pclicked!=true){
			objectDragendrop=this;
			this.pclicked=true;
			this.oldpoint=getPositionCurseur(e);
			this.numcursor=this.curseur(e);
			this.visible(false);		
			this.last_left=parseInt(this.styleobject.left);
			this.last_top=parseInt(this.styleobject.top);
			this.last_width=parseInt(this.styleobject.width);
			this.last_height=parseInt(this.styleobject.height);
		}
	}
	this.onmousedown = onmousedown;
		
	function onmouseup(e) {
		if(objectDragendrop!=null){
			if(objectDragendrop.pclicked!=false){
				objectDragendrop.pclicked=false;
				objectDragendrop.visible(true);
				objectDragendrop.last_top=Math.abs(objectDragendrop.last_top);
				objectDragendrop.last_left=Math.abs(objectDragendrop.last_left);
				objectDragendrop.last_width=Math.abs(objectDragendrop.last_width);
				objectDragendrop.last_height=Math.abs(objectDragendrop.last_height);					
				objectDragendrop=null;
			}
		}
	}
	this.onmouseup = onmouseup;
		
	function move(e) {
		if(!this.moveout(e)){
			this.numcursor=this.curseur(e);
		}
	}
	this.move = move;
		
	function moveout(e) {
		if(objectDragendrop!=null){
			if(objectDragendrop.pclicked==true){
			
				objectDragendrop.point=getPositionCurseur(e);
				deltax=objectDragendrop.point[0]-objectDragendrop.oldpoint[0];
				deltay=objectDragendrop.point[1]-objectDragendrop.oldpoint[1];
				
				if(objectDragendrop.numcursor==_MOVE_){
					objectDragendrop.top(objectDragendrop.last_top + deltay);				
					objectDragendrop.left(objectDragendrop.last_left + deltax);
				}
				//W
				if(objectDragendrop.numcursor==_RESIZE_NW_ || objectDragendrop.numcursor==_RESIZE_SW_ || objectDragendrop.numcursor==_RESIZE_W_){
					objectDragendrop.width(objectDragendrop.last_width - deltax);
				}
				//E
				if(objectDragendrop.numcursor==_RESIZE_NE_ || objectDragendrop.numcursor==_RESIZE_SE_ || objectDragendrop.numcursor==_RESIZE_E_){
					objectDragendrop.width(objectDragendrop.last_width + deltax);				
				}
				//S
				if(objectDragendrop.numcursor==_RESIZE_SE_ || objectDragendrop.numcursor==_RESIZE_SW_ || objectDragendrop.numcursor==_RESIZE_S_){
					objectDragendrop.height(objectDragendrop.last_height + deltay);				
				}
				//N
				if(objectDragendrop.numcursor==_RESIZE_NE_ || objectDragendrop.numcursor==_RESIZE_NW_ || objectDragendrop.numcursor==_RESIZE_N_){
					objectDragendrop.height(objectDragendrop.last_height - deltay);				
				}
				
				objectDragendrop.oldpoint=objectDragendrop.point;				
				return true;
			}
		}
		return false;
	}
	this.moveout = moveout;
				
	function visible(pvisible) {
		if(pvisible){
			getelement(this.divname).innerHTML="<img height='100%' width='100%' src='" + this.debscript + this.urlimage + this.finscript + "'/>"
		}else{
			getelement(this.divname).innerHTML=""
		}
	}
	this.visible = visible;
		
	function left(pleft) {
		this.last_left=pleft;
		if(pleft <= 0){
			this.styleobject.left = "0px";
		}else if((pleft + parseInt(this.styleobject.width)) >= (this.xparentmax - this.xparent)){
			this.styleobject.left = (this.xparentmax - this.xparent) - parseInt(this.styleobject.width) + "px";
		}else{
			this.styleobject.left=pleft + "px";
		}
		EcrireCookie(this.divname + '.styleobject.left',this.styleobject.left);
	}
	this.left = left;
		
	function top(ptop) {
		this.last_top=ptop;
		if( ptop <= 0 ){
			this.styleobject.top = "0px";
		}else if((ptop + parseInt(this.styleobject.height)) >= (this.yparentmax - this.yparent)){
			this.styleobject.top = (this.yparentmax - this.yparent) - parseInt(this.styleobject.height) + "px";
		}else{
			this.styleobject.top=ptop + "px";
		}
		EcrireCookie(this.divname + '.styleobject.top',this.styleobject.top);
	}
	this.top = top;
		
	function width(pwidth) {
		//W
		if(this.numcursor==_RESIZE_SW_ || this.numcursor==_RESIZE_W_ || this.numcursor==_RESIZE_NW_){
			if(pwidth<0){
				this.paddingright=this.paddingleft;
				this.left(parseInt(this.styleobject.left)+parseInt(this.styleobject.width));
				pwidth=((this.point[0]+ this.paddingright) - this.xparent) - parseInt(this.styleobject.left);
				
				this.styleobject.width=pwidth + "px";				
				this.numcursor=this.numcursor+1;	
				this.allocatcurseur(this.numcursor);
				if(this.debscript=="Image/resizef.php?image="){
					this.debscript="Image/resize.php?image=";
				}else{
					this.debscript="Image/resizef.php?image=";
				}
				this.width(pwidth);
				return;
			}else if(pwidth<15){
				var bkwidth=parseInt(this.styleobject.width);
				this.styleobject.width=15+ "px";
				this.left(parseInt(this.styleobject.left)-(parseInt(this.styleobject.width)-bkwidth));
			}else if(pwidth >= parseInt(this.styleobject.left)+parseInt(this.styleobject.width)){
				this.styleobject.width = parseInt(this.styleobject.left)+parseInt(this.styleobject.width) + "px";
				this.left(0);
			}else{
				pwidth=(parseInt(this.styleobject.left)+parseInt(this.styleobject.width)) - (this.point[0] - this.xparent - this.paddingleft) ;
				this.left((this.point[0] - this.xparent) - this.paddingleft);
				this.styleobject.width=pwidth + "px";
			}
		}
		//E
		else if(this.numcursor==_RESIZE_SE_ || this.numcursor==_RESIZE_E_ || this.numcursor==_RESIZE_NE_){
			if(pwidth<0){				
				this.paddingleft=this.paddingright;
				pwidth=parseInt(this.styleobject.left) - (this.point[0] - this.xparent) + this.paddingleft ;
				this.left((this.point[0] - this.xparent) - this.paddingleft);
				this.styleobject.width=pwidth + "px";				
				this.numcursor=this.numcursor-1;
				this.allocatcurseur(this.numcursor);					
				if(this.debscript=="Image/resizef.php?image="){
					this.debscript="Image/resize.php?image=";
				}else{
					this.debscript="Image/resizef.php?image=";
				}
				this.width(pwidth);
				return;
			}else if(pwidth<15){
				this.styleobject.width=15+ "px";
			}else if((parseInt(this.styleobject.left) + pwidth) >= (this.xparentmax - this.xparent)){
				this.styleobject.width = (this.xparentmax - this.xparent) - parseInt(this.styleobject.left) + "px";
			}else{
				pwidth=((this.point[0]+ this.paddingright) - this.xparent) - parseInt(this.styleobject.left);
				this.styleobject.width=pwidth + "px";
			}
		}
		this.last_width=pwidth;
		
		EcrireCookie("debscript",this.debscript);
		EcrireCookie(this.divname + '.styleobject.width',this.styleobject.width);
	}
	this.width = width;
		
	function height(pheight) {
		//S
		if(this.numcursor==_RESIZE_SE_ || this.numcursor==_RESIZE_SW_ || this.numcursor==_RESIZE_S_){
			if(pheight<15){
				this.styleobject.height=15+ "px";
			}else if((parseInt(this.styleobject.top) + pheight) >= (this.yparentmax - this.yparent)){
				this.styleobject.height = (this.yparentmax - this.yparent) - parseInt(this.styleobject.top) + "px";
			}else{
				this.styleobject.height=pheight + "px";
			}
		}
		//N
		if(objectDragendrop.numcursor==_RESIZE_NE_ || objectDragendrop.numcursor==_RESIZE_NW_ || objectDragendrop.numcursor==_RESIZE_N_){			
			if(pheight<15){
				var bkheight=parseInt(this.styleobject.height);
				this.styleobject.height=15+ "px";
				this.top(parseInt(this.styleobject.top)-(parseInt(this.styleobject.height)-bkheight));
			}else if(pheight >= parseInt(this.styleobject.top)+parseInt(this.styleobject.height)){
				this.styleobject.height = parseInt(this.styleobject.top)+parseInt(this.styleobject.height) + "px";
				this.top(0);
			}else{
				var bkheight=parseInt(this.styleobject.height);
				this.styleobject.height=pheight + "px";
				this.top(parseInt(this.styleobject.top)-(pheight-bkheight));
			}
		}
		this.last_height=pheight;
		EcrireCookie(this.divname + '.styleobject.height',this.styleobject.height);
	}
	this.height = height;

	function allocatcurseur(idcurseur) {
		switch(idcurseur){
			case _RESIZE_NW_:
				this.styleobject.cursor = 'nw-resize';
				break;
			case _RESIZE_NE_:
				this.styleobject.cursor = 'ne-resize';
				break;
			case _RESIZE_SW_:
				this.styleobject.cursor = 'sw-resize';
				break;
			case _RESIZE_SE_:
				this.styleobject.cursor = 'se-resize';
				break;
			case _RESIZE_W_:
				this.styleobject.cursor = 'w-resize';
				break;
			case _RESIZE_E_:
				this.styleobject.cursor = 'e-resize';
				break;
			case _RESIZE_N_:
				this.styleobject.cursor = 'n-resize';
				break;
			case _RESIZE_S_:
				this.styleobject.cursor = 's-resize';
				break;
		}
	}
	this.allocatcurseur = allocatcurseur;

	function curseur(e){
	
		this.point=getPositionCurseur(e);

		this.paddingleft = (this.point[0]) - (parseInt(this.styleobject.left)+this.xparent);
		this.paddingtop = (this.point[1]) - (parseInt(this.styleobject.top)+this.yparent);
		this.paddingright = (parseInt(this.styleobject.left) + this.xparent +  parseInt(this.styleobject.width)) - this.point[0];
		this.paddingbottom = (parseInt(this.styleobject.top) + this.yparent +  parseInt(this.styleobject.height)) - this.point[1];

		// haut gauche
		if(this.paddingtop < thePadding && this.paddingleft < thePadding){
				this.styleobject.cursor = 'nw-resize';				
				return _RESIZE_NW_;
			}
		
		// haut droit
		if(this.paddingtop < thePadding && this.paddingright < thePadding){
				this.styleobject.cursor = 'ne-resize';
				return _RESIZE_NE_;
			}
			
		// bas gauche
		if(this.paddingbottom < thePadding && this.paddingleft < thePadding){
				this.styleobject.cursor = 'sw-resize';
				return _RESIZE_SW_;
			}
			
		// bas droit
		if(this.paddingbottom < thePadding && this.paddingright < thePadding){
				this.styleobject.cursor = 'se-resize';
				return _RESIZE_SE_;
			}
			
		// cote gauche
		if(this.paddingleft < thePadding){
				this.styleobject.cursor = 'w-resize';
				return _RESIZE_W_;
			}
			
		// cote droit
		if(this.paddingright < thePadding){
				this.styleobject.cursor = 'e-resize';
				return _RESIZE_E_;
			}
			
		// haut
		if(this.paddingtop < thePadding){
				this.styleobject.cursor = 'n-resize';
				return _RESIZE_N_;
			}
			
		// bas
		if(this.paddingbottom < thePadding){
				this.styleobject.cursor = 's-resize';
				return _RESIZE_S_;
			}
			
		this.styleobject.cursor = 'move';
		return _MOVE_;
	}
	this.curseur = curseur;
	
	//construteur
	
	this.create(pwidth,pheight,bgcolor);
	this.last_width=pwidth;
	this.last_height=pheight;
	this.visible(true);	
}

function getelement(id){
	if (document.all) {
	  return document.all[id];
	}
	else {
	  return document.getElementById(id) ;
	}
}

function getPositionCurseur(e){

	var point = new Array(0,0);
	
	var ie = (document.all)? true:false;
	var ns4 = (document.layers)? true:false;
	var ns6 = (document.getElementById)? true:false;

	if (ie){
	 point[0] = event.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
	 point[1] = event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	}else if (ns4){
	 point[0] = e.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
	 point[1] = e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	}else if (ns6){
	 point[0] = e.pageX;
	 point[1] = e.pageY;
	}else{
	 point[0] = 0;
	 point[1] = 0;
	}
	return point;
}
function getCookieVal(offset){
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
	}
function LireCookie(nom){
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen)
	{
	var j=i+alen;
	if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
	i=document.cookie.indexOf(" ",i)+1;
	if (i==0) break;
	
	}
	return null;
}
function EcrireCookie(nom, valeur){
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}