var windowHandle = new Array();
var objleft = true;
var objtop  = true;
var obj     = new Array();
var dobj;
var dZindex;
var disable = 0;
var ancienLeft;
var ancienTop;
var ancienHeight;
var ancienWidth;
var ancienzIndex;
var ancienId;
var izIndex = 2;
var isFocus = "";
var lastisFocus = "";
var subMenu =0;

function display_menu() {
	if(document.getElementById('tool_menu').style.visibility != 'hidden') {
		document.getElementById('tool_menu').style.visibility = 'hidden'
		document.getElementById('tabmenu').style.zIndex = 1;
	} else {
		document.getElementById('tool_menu').style.visibility = 'visible'
		document.getElementById('tabmenu').style.zIndex = 99;
	}
}

function kill_menu(){
	if(document.getElementById('tool_menu').style.visibility != 'hidden') {
		document.getElementById('tool_menu').style.visibility = 'hidden'
		document.getElementById('tabmenu').style.zIndex = 1;
	}
}

function date(){
	var today=new Date();
	var date_heure="";
	h = today.getHours();
	m = today.getMinutes();
	s = today.getSeconds();


	if(h<10)
		{ h = '0'+h; }
	if(m<10)
		{ m = '0'+m; }
	if(s<10)
		{ s = '0'+s; }
	date_heure = ''+h+':'+m+'';
	document.getElementById('d').innerHTML = date_heure;
	setTimeout("date()", 1000);
}

function cal_screen(){
	document.getElementById('screen').height = document.body.clientHeight-32
	document.getElementById('toolbar').style.top = document.body.clientHeight-32
	document.getElementById('tool_menu').style.top = document.body.clientHeight-(600-32)
	return (document.body.clientHeight-32)
}

function window_resize(root, e) {
	var root = document.getElementById(root);
	if(!e) {e = event;}
	if(isClick == true) {
		dobj   = root;
		x      = e.clientX;
		y      = e.clientY;
		tx     = parseInt(root.style.width,10);
		ty     = parseInt(root.style.height,10);
		isresize=true;
		return false;
	}
}

function color_menu(get, color) {
	get.style.background="url()";
	get.style.backgroundColor=color;
}

function over_menu(get) {
	get.style.background="url(./gif/menu-sur.gif)";
}

function kill_window(obj) {
	var tmp = Array();
	var obj = document.getElementById(obj);
	obj.style.display = 'none'
	for(b=0;b<windowHandle.length;b++) {
		if(obj.id.substr(7,obj.id.length) != windowHandle[b]){
			tmp.push(windowHandle[b]);
		}
	}
	document.body.removeChild(obj)
	document.getElementById('tool').getElementsByTagName('tbody')[0].getElementsByTagName('tr')[0].removeChild(document.getElementById('tab_'+obj.id.substr(7,obj.id.length)));
	windowHandle = tmp;
}

function window_up(obj) {
	var obj = document.getElementById(obj);
	if(obj.style.width=="100%") {
		obj.style.top = ancienLeft;
		obj.style.left = ancienTop;
		obj.style.width = ancienWidth;
		obj.style.height = ancienHeight;
		obj.style.zIndex = ancienzIndex;
	} else {
		ancienLeft = obj.style.top;
		ancienTop = obj.style.left;
		ancienWidth = obj.style.width;
		ancienHeight = obj.style.height;
		ancienzIndex = obj.style.zIndex;
		obj.style.top=0;
		obj.style.left=0;
		obj.style.width="100%";
		obj.style.height=document.body.clientHeight-32;
		obj.style.zIndex=99
	}
}

function window_down(obj) {
	var obj = document.getElementById(obj);
	obj.style.display = 'none'
}

function give_focus(obj, up) {
	kill_menu();
	if(typeof(obj)=='string') {
		var obj = document.getElementById("window_"+obj);
	}
	lastisFocus = isFocus;
	isFocus = obj.id.substr(7,obj.id.length);
	window.status = up 
	izIndex++
	if(up==true) {
		obj.style.display = 'inline'
	}
	
	if(document.getElementById('tab_'+isFocus)) {
		document.getElementById('tab_'+isFocus).innerHTML = '<span style="background: url(gif/statusleftactive.gif); float: left; height: 30px">&nbsp;</span><table class="statusWindowActive"><td><nobr>'+isFocus+'</nobr></td></table><span style="background: url(gif/statusrightactive.gif); float: left; height: 30px">&nbsp;</span>';
	}
	if(lastisFocus != "" && lastisFocus != isFocus && document.getElementById('tab_'+lastisFocus)) {
		document.getElementById('tab_'+lastisFocus).innerHTML = '<span style="background: url(gif/statusleft.gif); float: left; height: 30px">&nbsp;</span><table class="statusWindow"><td><nobr>'+lastisFocus+'</nobr></td></table><span style="background: url(gif/statusright.gif); float: left; height: 30px">&nbsp;</span>';
	}
	obj.style.zIndex = izIndex;
}

function creatWindow(handle, width, height, ico, page) {
	if(width==''||width==undefined) {width='500';}
	if(height==''||height==undefined) {height='500';}
	if(page==undefined) {return;}
	for(key in const_arr_dir) {
		var reg=new RegExp(key, "ig");
		page = page.replace(key, const_arr_dir[key]);
	}

	if(in_array(handle, windowHandle) == false) {
		windowHandle.push(handle);
		new_tab = document.createElement("td");
		new_tab.setAttribute("valign", "top");
		new_tab.setAttribute("id", "tab_"+handle);
		new_tab.setAttribute("width", "158");
		new_tab.setAttribute("onclick", "give_focus('"+handle+"', true)");
		new_tab.innerHTML = '<span style="background: url(gif/statusleft.gif); float: left; height: 30px">&nbsp;</span><table class="statusWindow"><td><nobr>'+handle+'</nobr></td></table><span style="background: url(gif/statusright.gif); float: left; height: 30px">&nbsp;</span>';
		obj = document.getElementById('tool').getElementsByTagName('tbody')[0].getElementsByTagName('tr')[0];
		obj.insertBefore(new_tab, obj.getElementsByTagName('td')[obj.getElementsByTagName('td').length-2]);
		var html = new Array();
		html.push('<span id="window_'+handle+'" onclick="kill_menu()" style="height: '+height+'; width: '+width+'; position: absolute; top: 50; left: 350;z-index:2">');
		html.push('<table width="100%" height="100%" cellpadding=0 cellspacing=0 style="margin: 0px 0px Opx 0px; padding: 0px 0px 0px 0px">');
		html.push('<tr onmousedown=\'selectDrag(document.getElementById("window_'+handle+'"), 350, 50, event);\'>');
		html.push('<td style="background: url(gif/window-up-right.gif) no-repeat; width:5px">&nbsp;</td>');
		html.push('<td style="background: url(gif/window-up.gif); width:100%;height:26px">');
		html.push('<table width="100%" height="100%">');
		html.push('<td width="100%" style="font-family: Arial; font-size: 14px"><b><nobr>'+(ico!=''&&ico!=undefined?'<img src="'+ico+'" style="width:15px;height:15px" align=absmiddle>':'')+handle+'</nobr></b></td>');
		html.push('<td width="28" height="100%" onclick="window_down(\'window_'+handle+'\')" style="background: url(gif/traieActive.gif) no-repeat; background-position: center center; width: 28px; height: 15px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>');
		html.push('<td width="28" height="100%" onclick="window_up(\'window_'+handle+'\')" style="background: url(gif/carrerActive.gif) no-repeat; background-position: center center; width: 28px; height: 15px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>');
		html.push('<td width="28" height="100%" onclick="kill_window(\'window_'+handle+'\')" style="background: url(gif/crossActive.gif) no-repeat; background-position: center center; width: 28px; height: 15px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>');
		html.push('</table>');
		html.push('</td>');
		html.push('<td style="background: url(gif/window-up-left.gif) no-repeat; width:5px; background-position: top right"></td>');
		html.push('</tr>');
		html.push('<tr>');
		html.push('<td style="background: url(gif/window-right.gif);background-repeat: repeat-y;width:5px; height:100%">&nbsp;</td>');
		html.push('<td width="100%" style="background-color: #f4f4f4" onclick="give_focus(\''+handle+'\');" id="html_'+handle+'" valign=top><iframe src="'+page+'" height=99% frameborder=0 width=99%></iframe></td>');
		html.push('<td colspan=2 style="background: url(gif/window-left.gif);background-repeat: repeat-y;background-position: top right;width:5px;height:100%;">&nbsp;</td>');
		html.push('</tr>');
		html.push('<tr>');
		html.push('<td style="background: url(gif/window-down-left.gif) no-repeat;width:4px;height:3px"></td>');
		html.push('<td style="background: url(gif/window-down.gif);width:100%;height:3px;"></td>');
		html.push('<td style="background: url(gif/window-down-right.gif);background-repeat: repeat-y;background-position: top right;width:4px;height:3px;cursor:nw-resize" onmousedown="window_resize(\'window_'+handle+'\', event)"></td>');
		html.push('</tr>');
		html.push('</table>');
		html.push('</span>');
		document.body.innerHTML += html.join('');
		give_focus(handle);
	}
	else {
		give_focus(handle);
	}
}

function color_item(obj, color) {
	switch(color) {
		case 1:
			obj.getElementsByTagName('div')[0].style.backgroundColor = '#e1ecf0';
			obj.getElementsByTagName('div')[0].style.border = '1px solid #c5d0d4';
			//obj.getElementsByTagName('div')[0].style.marginLeft = '5px';
			obj.getElementsByTagName('div')[0].style.width = "142px"
			obj.getElementsByTagName('div')[0].style.height = "18"
		break;
		case 2:
			obj.getElementsByTagName('div')[0].style.backgroundColor = '#f0f0f0';
			obj.getElementsByTagName('div')[0].style.border = '1px solid #f0f0f0';
			//obj.getElementsByTagName('div')[0].style.borderBottom = '1px solid #e0e0e0';
		break;
	}
}

function kill_subcontxtmenu(sub_id) {
	for(l=sub_id;l<7;l++) {
		if(document.getElementById('contextmenu'+l) && document.getElementById('contextmenu'+l).style.display != 'none') {
			document.getElementById('contextmenu'+l).style.display='none';
		}
	}
}

function createMenu(e, arr, force, master) {
	if (!e) {e = window.event;}
	if(master==true) {
		kill_subcontxtmenu(0);
		document.getElementById('contextmenu').style.display = "none";
		subMenu=0;
	}
	if (e.button == 2 || force == true)
	{
		var total_height = 0;
		var html=new Array();
		var html_libre=new Array();
		if(document.getElementById('contextmenu').style.display != 'none') {
			for(o=0;o<7;o++) {
				if(document.getElementById('contextmenu'+o) && document.getElementById('contextmenu'+o).style.display == 'none') {
					subMenu = o;
					break;
				}
			}
		}
		html.push('<table cellpadding=0 cellspacing=0 style="border-collapse:collapse">');
		for(i=0;i<arr.length;i++) {
			total_height = total_height+20;
			if(arr[i][4]==true) {
			html_libre.push('<span style="position: absolute;top:'+(total_height-15)+'px;left:140px"><img src="./gif/fleche.gif"  align=absmiddle></span>');
			}
			html.push('<tr><td onmouseover="color_item(this, 1);'+(arr[i][5]!=""?arr[i][5]:'')+(arr[i][4]==false?'kill_subcontxtmenu('+(subMenu+1)+');':'')+'" onmouseout="color_item(this, 2)"><div style="border:1px solid #f0f0f0;height:18"><div style="backgroung-color:#e6f1f7;width:18;float:left;border-right:1px solid #dce2de">'+(arr[i][1]==''?'&nbsp;':'<img src="'+arr[i][1]+'" align=absmiddle>')+'</div><div style="float:left;white-space: nowrap;'+(arr[i][2]==true?'border-bottom:1px solid #e0e0e0;':'')+'width:123;height:100%"><nobr>'+(arr[i][3]==true?'<font color=gray>'+arr[i][0]+'</font>':arr[i][0])+'</nobr></div></div></td></tr>');
		}
		html.push('</table>');
		if(document.getElementById('contextmenu').style.display == 'none') {
			document.getElementById('contextmenu').style.display = 'inline';
			document.getElementById("contextmenu").style.top = e.clientY + 'px';
			document.getElementById("contextmenu").style.left = e.clientX + 'px';
			document.getElementById("contextmenu").style.height = total_height + 'px';
			document.getElementById("contextmenu").innerHTML = html.join('')+html_libre.join('');
		}
		else {
			//subMenu++;
			document.getElementById('contextmenu'+subMenu).style.display = 'inline';
			document.getElementById("contextmenu"+subMenu).style.top = e.clientY + 'px';
			document.getElementById("contextmenu"+subMenu).style.left = (parseInt(document.getElementById("contextmenu"+((subMenu-1)==0?'':(subMenu-1))).style.left,10)+150) + 'px';
			document.getElementById("contextmenu"+subMenu).style.height = total_height + 'px';
			document.getElementById("contextmenu"+subMenu).innerHTML = html.join('')+html_libre.join('');
		}
	}
	return false;
}

function defaultMenu(e) {
	if (!e) {e = window.event;}
	var arr = new Array();
	arr[0] = ["Copier", "", false, false, false, ""];
	arr[1] = ["Coller", "", false, false, false, ""];
	arr[2] = ["Couper", "", true, false, false, ""];
	arr[3] = ["Nouveau", "", false, true, false, ""];
	arr[4] = ["Propriétés", "", false, false, true, "neaveauMenu(event)"];
	createMenu(e, arr, false, true);
}

function neaveauMenu(e) {
	if(subMenu ==0 || document.getElementById('contextmenu'+subMenu).style.display == 'none') {
		if (!e) {e = window.event;}
		var arr = new Array();
		arr[0] = ["aaaaaaa", "", false, false, false, ""];
		arr[1] = ["bbbbbbb", "", false, false, false, ""];
		arr[2] = ["ccccccc", "", false, false, false, ""];
		arr[3] = ["ddddddd", "", false, false, false, ""];
		arr[4] = ["eeeeeee", "", false, false, false, ""];
		arr[5] = ["fffffff", "", false, false, true, "neaveau2Menu(event)"];
		arr[6] = ["ggggggg", "", false, false, false, ""];
		createMenu(e, arr, true, false);
	}
}
function neaveau2Menu(e) {
	if(subMenu ==1 || document.getElementById('contextmenu'+subMenu).style.display == 'none') {
		if (!e) {e = window.event;}
		var arr = new Array();
		arr[0] = ["aaaaaaa", "", false, false, false, ""];
		arr[1] = ["bbbbbbb", "./gif/icon_biggrin.gif", false, false, false, ""];
		createMenu(e, arr, true, false);
	}
}

function dir_system(name, type, obj) {
	var html = new Array();
	html.push("<table ondblclick=\"goto('"+name+"', '"+obj.id+"', '"+type+"')\" cellpadding=0 cellspacing=0 id=\"icon_3\" type=\"icon\" onmousedown='selectDrag(this, 5, 5, event);' style=\"color:white;z-index:1;font-family: Arial; font-size: 14px;height:40;width:80;table-layout:fixed;\">");
	html.push("<tr>");
	html.push("<td style=\"background: url(gif/dir.gif) no-repeat;background-position: center center;width:28; height:48;\">&nbsp;</td>");
	html.push("</tr>");
	html.push("<tr align=center>");
	html.push("<td style='color:black'>"+name+"</td>");
	html.push("</tr>");
	html.push("</table>");
	obj.innerHTML += html.join('');
}

function goto_back(str, id, type) {
	document.getElementById(id).innerHTML = '';
	if(type == "hdd") {
		str = "%ROOT_"+str+"%/";
	} else {
		str += "/";
	}
	if(document.getElementById(id).action == undefined) {
		document.getElementById(id).action = "";
	}
	document.getElementById(id).action += str;
	var xhr = Ajax.requete("./xhr/dir_system.php?id="+id+"&dir="+document.getElementById(id).action);
	xhr.onreadystatechange = function() {  
		if(xhr.readyState == 4 && xhr.status == 200) {
			try {
				eval(xhr.responseText)
			} catch(e) {alert('Error !!\n faut pas déconner c\'est un windows quand meme')}
		}
	}
}