var isdrag  = false;
var isresize = false;
var isClick = true;
var isClickFont = false;
var dobj;
var x_select,y_select;
var x,y;
var disable = 0;
var left_ancien;
var top_ancien;
var dZindex;
var obj     = new Array();
var func_ex;
var func_obj

function click_select(e) {
	if(disable == 0) {
		if(!e) {e = event;}
		if(isClickFont == true) {
			if(typeof(kill_menu) != 'undefined') {
				kill_menu();
			}
			var a,b,c,d;
			if(e.clientX>x_select) {
				a = e.clientX;
				b = x_select;
			} else {
				a = x_select;
				b = e.clientX;
			}
			
			if(e.clientY>y_select) {
				c = e.clientY;
				d = y_select;
			} else {
				c = y_select;
				d = e.clientY;
			}
			x_traie = a-b
			y_traie = c-d

			if(e.clientY>y_select) {
				document.getElementById('select').style.top = y_select
				document.getElementById('select').style.height = y_traie
				mode = 1
			} else {
				document.getElementById('select').style.top =  e.clientY
				document.getElementById('select').style.height = y_traie
				mode = 2
			}
			if(e.clientX>x_select) {
				document.getElementById('select').style.left = x_select
				document.getElementById('select').style.width = x_traie
				mode = mode+''+2;
			} else {
				document.getElementById('select').style.left = e.clientX
				document.getElementById('select').style.width = x_traie
				mode = mode+''+3;
			}
			document.getElementById('tabmenu').style.zIndex = 99
			mode = parseInt(mode, 10)
			//window.status = mode
			if(mode == 12) {
				//de haut en bas a droit	a
				for(p=0; p<arr_icon.length; p++) {
					var pTop = parseInt(document.getElementById(arr_icon[p]).style.top,10)
					var pLeft = parseInt(document.getElementById(arr_icon[p]).style.left,10)
					var pWidth = parseInt(document.getElementById(arr_icon[p]).style.width,10)
					var pHeight = parseInt(document.getElementById(arr_icon[p]).style.height,10)
					if(e.clientY > pTop && y_select < (pTop+pHeight) && e.clientX > pLeft && x_select < (pLeft+pWidth)) {
						document.getElementById(arr_icon[p]).getElementsByTagName('tr')[1].getElementsByTagName('td')[0].style.backgroundColor="#316ac5"
						select_icon[p] = 1;
					} else {
						document.getElementById(arr_icon[p]).getElementsByTagName('tr')[1].getElementsByTagName('td')[0].style.backgroundColor=""
						select_icon[p] = 0;
					}
				}
			} else if(mode == 22) {
				//de bas en haut a droit
				for(p=0; p<arr_icon.length; p++) {
					var pTop = parseInt(document.getElementById(arr_icon[p]).style.top,10)
					var pLeft = parseInt(document.getElementById(arr_icon[p]).style.left,10)
					var pWidth = parseInt(document.getElementById(arr_icon[p]).style.width,10)
					var pHeight = parseInt(document.getElementById(arr_icon[p]).style.height,10)
					if((pTop+pHeight)>e.clientY && pTop<y_select && pLeft<e.clientX && (pLeft+pWidth)>x_select) {
						document.getElementById(arr_icon[p]).getElementsByTagName('tr')[1].getElementsByTagName('td')[0].style.backgroundColor="#316ac5"
						select_icon[p] = 1;
					} else {
						document.getElementById(arr_icon[p]).getElementsByTagName('tr')[1].getElementsByTagName('td')[0].style.backgroundColor=""
						select_icon[p] = 0;
					}
				}
			} else if(mode == 13) {
				//de haut en bas a gauche
				for(p=0; p<arr_icon.length; p++) {
					var pTop = parseInt(document.getElementById(arr_icon[p]).style.top,10)
					var pLeft = parseInt(document.getElementById(arr_icon[p]).style.left,10)
					var pWidth = parseInt(document.getElementById(arr_icon[p]).style.width,10)
					var pHeight = parseInt(document.getElementById(arr_icon[p]).style.height,10)
					if(e.clientY>pTop && y_select<(pTop+pHeight) && x_select>pLeft && e.clientX<(pLeft+pWidth)) {
						document.getElementById(arr_icon[p]).getElementsByTagName('tr')[1].getElementsByTagName('td')[0].style.backgroundColor="#316ac5"
						select_icon[p] = 1;
					} else {
						document.getElementById(arr_icon[p]).getElementsByTagName('tr')[1].getElementsByTagName('td')[0].style.backgroundColor=""
						select_icon[p] = 0;
					}
				}
			} else if(mode == 23) {
				//de bas en haut a gauche
				for(p=0; p<arr_icon.length; p++) {
					var pTop = parseInt(document.getElementById(arr_icon[p]).style.top,10)
					var pLeft = parseInt(document.getElementById(arr_icon[p]).style.left,10)
					var pWidth = parseInt(document.getElementById(arr_icon[p]).style.width,10)
					var pHeight = parseInt(document.getElementById(arr_icon[p]).style.height,10)
					if(e.clientY < (pTop+pHeight) && y_select > pTop && e.clientX < (pLeft+pWidth) && x_select > pLeft) {
						document.getElementById(arr_icon[p]).getElementsByTagName('tr')[1].getElementsByTagName('td')[0].style.backgroundColor="#316ac5"
						select_icon[p] = 1;
					} else {
						document.getElementById(arr_icon[p]).getElementsByTagName('tr')[1].getElementsByTagName('td')[0].style.backgroundColor=""
						select_icon[p] = 0;
					}
				}
			}
			isClickFont = true;
			return false;
		}
	}
}


function point_select(e) {
	if(disable == 0) {
		if(!e) {e = event;}
		x_select = e.clientX;
		y_select = e.clientY;
		document.getElementById('select').style.display = 'block';
		isClickFont = true;
		return false;
	}
}

function resizeDrag(e, frm) {
	if(disable == 0) {
		if (isresize) {
			document.getElementById('background').style.height = document.body.clientHeight-32
			document.getElementById('background').style.display = 'inline';
			document.getElementById('background').style.zIndex = 98;
			if(!e) {e = window.event;}
			if(frm == "frame") {
				dobj.style.width = (tx + e.clientX - x)+parseInt(dobj.style.left,10);
				dobj.style.height  = (ty + e.clientY - y)+parseInt(dobj.style.top,10)+50;
			} else {
				dobj.style.width = tx + e.clientX - x;
				dobj.style.height  = ty + e.clientY - y;
			}
			return false;
		}
	}
}


function moveDrag(e, frm) {
	if(disable == 0) {
		if (isdrag) {
			if (document.getElementById('background')) {
				document.getElementById('background').style.height = document.body.clientHeight-32
				document.getElementById('background').style.display = 'inline';
				document.getElementById('background').style.zIndex = 98;
			}
			//document.getElementById('background').style.backgroundColor = "black"
			if(!e) {e = event;}
			if(frm == "frame") {
				dobj.style.left = (tx + e.clientX - x)+parseInt(dobj.style.left,10);
				dobj.style.top  = (ty + e.clientY - y)+parseInt(dobj.style.top,10)+25;
				
			} else if(dobj.getAttribute('type')=='icon') {
				var masX = parseInt(dobj.style.left,10);
				var masY = parseInt(dobj.style.top,10);
				var masH = parseInt(dobj.style.height,10);
				var masW = parseInt(dobj.style.width,10);
				for(l=0; l<select_icon.length; l++) {
					if((select_icon[l]==1||document.getElementById(arr_icon[l]).getElementsByTagName('tr')[1].style.backgroundColor != "") && document.getElementById(arr_icon[l]).getAttribute("action") == undefined) {
						var subX = parseInt(document.getElementById(arr_icon[l]).style.left,10)
						var subY = parseInt(document.getElementById(arr_icon[l]).style.top,10)
						var calcY = 0;
						var calcX = 0;
						calcX = (masX-subX);
						calcY = (masY-subY);
						//window.status = "caclY="+calcY+"      calcX="+calcX+"        subX="+subX+"        subY="+subY+"         masX="+masX+"        masY="+masY 
						//window.status = "(ty - calcY) + e.clientY - y = "+((ty - calcY) + e.clientY - y
						document.getElementById(arr_icon[l]).style.left = (tx - calcX)  + e.clientX - x;
						document.getElementById(arr_icon[l]).style.top = (ty - calcY) + e.clientY - y;
					}
				}
				for(i=0;i<action_icon.length; i++) {
					if((action_icon[i][2]-20) < masY && (action_icon[i][3]-20) < masX && ((action_icon[i][3]+action_icon[i][4])+20) > masX && ((action_icon[i][2]+action_icon[i][5])+20) > masY) {
						dobj.style.cursor = 'pointer';
						func_ex = document.getElementById(action_icon[i][0]).getAttribute("func");
						func_obj = dobj;
					} else {
						dobj.style.cursor = 'default';
						func_ex = undefined;
						func_obj = undefined;
					}
				}
			} else {
				dobj.style.left = tx + e.clientX - x;
				dobj.style.top  = ty + e.clientY - y;
				dobj.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('dragOn').style.display='inline'
			}
			return false;
		}
	}
}

function selectDrag(root, left, Top, e) {
	if(disable == 0) {
		if(!e) {e = event;}
		if(isClick == true) {	
			if(!left_ancien) {left_ancien = root.style.left;}
			if(!top_ancien) {top_ancien = root.style.top;}
			isdrag = true;
			if(dZindex == undefined) {
				dZindex = root.style.zIndex;
				switch(root.getAttribute("type")) {
					case 'icon':
						color_icon(root, '#316ac5');
					break;
				}
			}
			root.style.zIndex = 99;
			dobj   = root;
			tx     = parseInt(root.style.left+(document.body.clientWidth/100),10);
			ty     = parseInt(root.style.top+(document.body.clientHeight/100),10);
			x      = e.clientX;
			y      = e.clientY;
			obj['root'] = root;
			obj['left'] = tx;
			obj['top']  = ty;
			return false;	
		}
	}
}

function isDrag() {	
	if(disable == 0) {
		if(dobj) {
			isresize=false;
			if (document.getElementById('background')) {
				document.getElementById('background').style.display = 'none'
				document.getElementById('background').style.zIndex = 0
			}
			dobj.style.zIndex = dZindex
			if(dobj.getAttribute("type")!="icon"&&dobj.style.display!='none') {
				give_focus(dobj);
			}
			if(func_ex != undefined) {
				try {
					eval(func_ex);
				} catch(e) {}
			}
			color_icon(dobj, '');
			dZindex = undefined
			try {
			  dobj.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('dragOn').style.display='none'
			} catch(e) {}
		}
		isdrag = false
	}
}

function kill_select() {
	document.getElementById('select').style.display = 'none';
	document.getElementById('select').style.height = '0px';
	document.getElementById('select').style.width = '0px';
	document.getElementById('tabmenu').style.zIndex = 1
	isClickFont=false;
}