// JavaScript Document
winCounter	= 0;
var padZIndex	= 0;
active_window = null;
moving_window = null;
scrollbar_weight = 13;

minimized_min	 = new Array;
total_min		 = 0;

function closeWindow(){
	obj_window = this.container;
	obj_window.style.display = "none";
	obj_window.active = false;
	obj_window.closed = true;
	shadow = document.getElementById("window_shadow");
	shadow.style.display = "none";
}

function openWindow(){
	this.container.closed = false;
	this.container.style.display = "";
}

function minimizeWindow(){
	obj_window = this.container;
	if(obj_window.maximized == true) obj_window.btn_2.src = "images/window/btn_max.gif";
	
	if(!obj_window.minimized){
		obj_window.resizer.style.display = "none";

		minimized_min.sort()
		obj_window.minimized_id = (minimized_min.length)? minimized_min.shift() : total_min;
		total_min++;

		obj_window.content_div.style.display = "none";
		resizeWindow(obj_window, 250, 23);
		moveWindow(obj_window, 10, 80 + (23 * obj_window.minimized_id))
		obj_window.minimized = true;
		obj_window.maximized = false;

	} else {
		obj_window.resizer.style.display = "";

		// Waarde uit minimized tabel halen
		minimized_min.push(obj_window.minimized_id)
		obj_window.minimized_id = null;
		total_min--;

		obj_window.content_div.style.display = "";
		resizeWindow(obj_window, obj_window.old_width, obj_window.old_height);
		moveWindow(obj_window, obj_window.old_left, obj_window.old_top)
		obj_window.minimized = false;
	}
}

maximized_max = new Array;
function maximizeMax(){
	for(counter = 0; counter < maximized_max.length; counter++){
		//alert(maximized_max[counter].id);
		obj_window = maximized_max[counter];
		maximized_max.splice(counter, 1);
		obj_window.maximized = false;
		obj_window.maximizeWindow();
	}
}

function maximizeWindow(){
	obj_window = this.container;

	if(!obj_window.maximized){
		obj_window.resizer.style.display = "none";
		moveWindow(obj_window, 0, 70);
		resizeWindow(obj_window, document.body.clientWidth, document.body.clientHeight - 70);
		obj_window.btn_2.src = "images/window/btn_max1.gif";

		if(obj_window.minimized){
			obj_window.content_div.style.display = "";
			// Waarde uit minimized tabel halen
			minimized_min.push(obj_window.minimized_id)
			obj_window.minimized_id = null;
			total_min--;
		}

		maximized_max.push(obj_window);
		obj_window.maximized_max_id = maximized_max.length - 1;

		obj_window.maximized = true;
		obj_window.minimized = false;
	}else{
		maximized_max.splice(obj_window.maximized_max_id, 1);

		obj_window.resizer.style.display = "";
		moveWindow(obj_window, obj_window.old_left, obj_window.old_top)
		resizeWindow(obj_window, obj_window.old_width, obj_window.old_height);
		obj_window.btn_2.src = "images/window/btn_max.gif";
		obj_window.maximized = false;
	}	
}

function resizeWindow(obj_window, width, height){
	width	= Math.min(parseFloat(width),  document.body.clientWidth - parseFloat(obj_window.style.left));
	height	= Math.min(parseFloat(height), document.body.clientHeight - parseFloat(obj_window.style.top));

	height	= parseFloat(height);
	width	= parseFloat(width);
	obj_window.style.height = height;
	obj_window.cover_div.style.height = height - 2;

	if(height > 24) obj_window.content_div.style.height = height - 24;
	h = (!document.all)? 36 : 26; // Mozilla/IE margin problem
	if(height > 26) obj_window.content2_div.style.height = height - h;

	obj_window.style.width = width;
	obj_window.cover_div.style.width = width - 2;
	obj_window.content_div.style.width = width - 6;
	w = (!document.all)? 18 : 8; // Mozilla/IE margin problem
	obj_window.content2_div.style.width = width - w;

	obj_window.title_div.style.width = width - 86;
	obj_window.btn_1.style.left = width - 60;
	obj_window.btn_2.style.left = width - 41;
	obj_window.btn_3.style.left = width - 22;

	obj_window.resizer.style.left	 = parseFloat(obj_window.style.width) - 11;
	obj_window.resizer.style.top	 = parseFloat(obj_window.style.height) - 11;
		
	if(obj_window.drag_shadow && (document.all)){ // Only use shadow in IE;
		shadow = document.getElementById("window_shadow");
		shadow.style.width	= obj_window.style.width;
		shadow.style.height	= obj_window.style.height;
		shadow.style.left	= parseFloat(obj_window.style.left) + 4;
		shadow.style.top	= parseFloat(obj_window.style.top)  + 4;
	}
}

function activateWindow(obj_window){
	obj_window = (this)? this : obj_window;
	if(obj_window.active == true || obj_window.closed == true) return;

	obj_window.style.display = "";
	obj_window.active = true;

	if(active_window) active_window.active = false;
	obj_window.style.zIndex = padZIndex++;
	obj_window.cover_div.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
	if(active_window && active_window != obj_window) active_window.cover_div.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";
	if(active_window != obj_window) active_window = obj_window;
	
	if(obj_window.drag_shadow && (document.all)){ // Only use shadow in IE;
		shadow = document.getElementById("window_shadow");
		shadow.style.width	= obj_window.style.width;
		shadow.style.height	= obj_window.style.height;
		shadow.style.left	= parseFloat(obj_window.style.left) + 4;
		shadow.style.top	= parseFloat(obj_window.style.top)  + 4;
		shadow.style.zIndex = padZIndex - 1;
		shadow.style.display = "";
	}
}

function buildWindow(Title, win_id, Left, Top, Width, Height, Content, Resize, Drag, ScrollBars,SysScroll, Minimize, Maximize, Kill){
	scrollbarWidth = (ScrollBars)? 13 : 0;
	// Window 'container'
	var window_ctr = document.createElement("DIV");
	window_ctr.appendChild(window_ctr2 = document.createElement("DIV"));		// Window
	window_ctr2.appendChild(window_ico = document.createElement("IMG"));		// Icon
	window_ctr2.appendChild(window_btn1 = document.createElement("IMG"));		// Minimize
	window_ctr2.appendChild(window_btn2 = document.createElement("IMG"));		// Maximaize
	window_ctr2.appendChild(window_btn3 = document.createElement("IMG"));		// Close
	window_ctr2.appendChild(window_ttl = document.createElement("SPAN"));		// Title span
	window_ttl.appendChild(document.createTextNode(Title));						// Title test
	window_ctr2.appendChild(window_cnt = document.createElement("DIV"));		// Content border
	window_cnt.appendChild(window_cnt2 = document.createElement("DIV"));		// Content

	window_ctr.id = win_id;
	window_cnt2.innerHTML = Content;

	window_ico.src = "images/icons/"+ win_id +".gif";
	window_btn1.src = "images/window/btn_min.gif";
	window_btn2.src = "images/window/btn_max.gif";
	window_btn3.src = "images/window/btn_close.gif";

	window_ico.style.left	= 2;
	window_ctr.drag_shadow	= true;
	window_ctr.drag_block	= true;

	window_ttl.container	= window_ctr;
	window_btn1.container	= window_ctr;
	window_btn2.container	= window_ctr;
	window_btn3.container	= window_ctr;
	window_ctr.container	= window_ctr;

	window_ctr.cover_div	= window_ctr2;
	window_ctr.title_div	= window_ttl;
	window_ctr.content_div	= window_cnt;
	window_ctr.content2_div	= window_cnt2;
	window_ctr.btn_1		= window_btn1;
	window_ctr.btn_2		= window_btn2;
	window_ctr.btn_3		= window_btn3;
	window_ctr.icon_div		= window_ico;
	window_ctr.minimized	= false;
	window_ctr.maximized	= false;
	window_ctr.active		= false;
	window_ctr.closed		= false;
	window_ctr.old_height	= Height;
	window_ctr.old_width	= Width;
	window_ctr.old_left		= Left;
	window_ctr.old_top		= Top;

	window_btn1.onmousedown = minimizeWindow;
	window_btn2.onmousedown = maximizeWindow;
	window_btn3.onmousedown = closeWindow;

	window_ctr.onmousedown	= activateWindow;
	window_ttl.onmousedown	= dragStart;
	window_ttl.ondblclick   = maximizeWindow;

	window_ctr.minimizeWindow	= minimizeWindow;
	window_ctr.maximizeWindow	= maximizeWindow;
	window_ctr.activateWindow	= activateWindow;
	window_ctr.closeWindow		= closeWindow;
	window_ctr.openWindow		= openWindow;

	window_ctr.className	= "window_ctr";
	window_ctr2.className	= "window_ctr2";
	window_ico.className	= "window_ico";
	window_btn1.className	= "window_ico";
	window_btn2.className	= "window_ico";
	window_btn3.className	= "window_ico";
	window_ttl.className	= "window_ttl";
	window_cnt.className	= "window_cnt";
	window_cnt2.className	= "window_cnt2";

	// Resizer
		window_ctr2.appendChild(resizer = document.createElement("IMG"));
		resizer.style.position = "absolute";
		resizer.style.cursor = "se-resize";
		resizer.src = "images/window/wdwResize.gif";
		resizer.container = window_ctr;
		resizer.ondragstart = returnFalse;
		resizer.onmousedown = resizeStart;
		window_ctr.resizer = resizer;
	// Resizer

	moveWindow(window_ctr, Left, Top);
	resizeWindow(window_ctr, Width, Height);

	window_ctr.activateWindow();

	document.body.appendChild(window_ctr);	// Create the container, containing a few other HTML elements
}

function returnFalse(){ return(false) }

// Resize window
function resizeStart(e){
	resizing_window = this.container;
	resizing_window.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=33)";
	
	border = document.getElementById("resize_border");
	border.style.left = resizing_window.style.left;
	border.style.top = resizing_window.style.top;
	border.style.width = resizing_window.style.width;
	border.style.height = resizing_window.style.height;
	border.style.display = "";

	if(!document.all) event = e;
	resizing_window.int_x = (parseFloat(resizing_window.style.width)) - event.clientX;
	resizing_window.int_y = (parseFloat(resizing_window.style.height)) - event.clientY;
	document.body.onmousemove	= resizeMove;
	document.body.onmouseup		= resizeStop;
}

function resizeMove(e){
	border = document.getElementById("resize_border");

	if(!document.all) event = e;
	mouse_x = Math.min(event.clientX, document.body.clientWidth);
	mouse_y = Math.min(event.clientY, document.body.clientHeight);
	x = Math.max(resizing_window.int_x + mouse_x, 100);
	y = Math.max(resizing_window.int_y + mouse_y, 27);

	border.style.width	= x;
	border.style.height	= y;
}

function resizeStop(){
	border = document.getElementById("resize_border");
	resizeWindow(resizing_window, border.style.width, border.style.height)
	resizing_window.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
	border.style.display = "none";
	document.body.onmousemove	= null;
	document.body.onmouseup		= null;
}
// Resize window
