var KB_instance_arr = new Array();
var KB_DONE = false;
var KB_OVERLAY_DONE = false;
var KB_ZINDEX = 160;
var initEventX = 0;
var initEventY = 0;

/** *********** Creation classe Overlay ************* */
var KB_OVERLAY = function() {
	new Insertion.Top('body', "<div id='KB_overlay'></div>");
	this.setOverlayHeight();
	KB_OVERLAY_DONE = true;
}

KB_OVERLAY.prototype.setOverlayHeight = function() {
	myDocSize = documentSize();
	myWindowSize = windowSize();
	height = myDocSize[1] > myWindowSize[1] ? myDocSize[1] : myWindowSize[1];
	$('KB_overlay').style.height = height + "px";
}

/** Creation de la classe KB_WINDOW ************** */
var KB_WINDOW = function(instanceName, type, caption, requestParams, width,
		height, backgroundColor, padding, noScroll) {
	var exist = false;
	for (i = 0; i < KB_instance_arr.length; i++) {
		if (KB_instance_arr[i].instanceName == instanceName) {
			exist = true;
			putKB_ontop(KB_instance_arr[i]);
			return;
		}
	}
	if (!exist) {
		this.instanceName = instanceName;
		this.width = width || 780;
		this.height = height || 520;
		this.requestParams = requestParams;
		this.caption = caption;
		this.type = type;
		this.backgroundColor = '#ffffff';
		this.padding = padding ? 0 : padding;
		this.noScroll = noScroll;
		this.zindex = KB_ZINDEX++;
		this.resizeInProgress = false;
		this.KB_DONE = false;
		if (!KB_OVERLAY_DONE)
			Zoverlay = new KB_OVERLAY();
		KB_instance_arr.push(this);
		if (type == 'image') {
			this.KB_showImage();
		} else if (type == 'flash') {
			this.KB_showFlash();
		} else if (type == 'file') {
			this.KB_showFile();
		} else {
			this.KB_show();
		}
	}
}

putKB_window = function(ZwindowObj) {
	if (ZwindowObj.KB_DONE) {
		myWindowSize = windowSize();
		Zoverlay.setOverlayHeight();
		windowXmiddle = myWindowSize[0] / 2;
		windowYmiddle = myWindowSize[1] / 2;
		ZdivName = $('KB_window_' + ZwindowObj.instanceName);
		ZdivName.style.left = windowXmiddle - (ZwindowObj.width / 2)
				+ ((KB_instance_arr.length - 1) * 20) + "px";
		ZdivName.style.top = windowYmiddle - (ZwindowObj.height / 2)
				+ ((KB_instance_arr.length - 1) * 20) + "px";
		// new Effect.Appear(ZdivName);
		// new Effect.SlideDown(ZdivName, {duration:0.6});

		ZdivName.style.display = 'block';
	}
}

putKB_ontop = function(ZwindowObj) {
	for (i = 120; i < KB_instance_arr.length; i++) {
		if (KB_instance_arr[i].instanceName != ZwindowObj.instanceName) {
			$('KB_window_' + KB_instance_arr[i].instanceName).style.zIndex = i + 1;
		}
	}
	$('KB_window_' + ZwindowObj.instanceName).style.zIndex = KB_ZINDEX++;
}

KB_kill = function(ZwindowObj) {
	Effect.Fade('KB_window_' + ZwindowObj.instanceName);
	newArr = new Array;
	for (i = 0; i < KB_instance_arr.length; i++)
		if (ZwindowObj.instanceName != KB_instance_arr[i].instanceName)
			newArr.push(KB_instance_arr[i]);
	KB_instance_arr = newArr;
	if (KB_instance_arr.length > 0) {
		string = "Element.remove('KB_window_" + ZwindowObj.instanceName + "')";
	} else {
		Event.stopObserving(window, 'resize', function(e) {
			putKB_window(windowObj);
		});
		Event.stopObserving(window, 'scroll', function(e) {
			Zoverlay.setOverlayHeight();
		});
		string = "Element.remove('KB_window_"
				+ ZwindowObj.instanceName
				+ "');if(typeof helpPanelPosition!='undefined'){if(helpPanelPosition=='in') showHelpPanel()};Element.remove('KB_overlay')";
		Zoverlay = null;
		KB_OVERLAY_DONE = false;
	}
	setTimeout(string, 1000);
	ZwindowObj.KB_DONE = false;
}

KB_startRedim = function(e, windowObj) {
	windowObj.resizeInProgress = true;
	initEventX = Event.pointerX(e);
	initEventY = Event.pointerY(e);
	Event.observe(document, 'mousemove', function(e) {
		if (windowObj.resizeInProgress)
			KB_newDim(e, windowObj);
	});
	Event.observe(document, 'mouseup', function(e) {
		if (windowObj.resizeInProgress)
			KB_stopRedim(windowObj);
	});

}

KB_newDim = function(e, windowObj) {
	// windowObj.resizeInProgress=false;
	ZWindowDivName = $('KB_window_' + windowObj.instanceName);
	ZContentDivName = $('KB_content_' + windowObj.instanceName);
	deltaX = initEventX - Event.pointerX(e);
	deltaY = initEventY - Event.pointerY(e);
	ZContentDivName.style.height = (windowObj.height - deltaY) + 'px';
	ZContentDivName.style.width = (windowObj.width - deltaX) + 'px';
	ZWindowDivName.style.width = (windowObj.width - deltaX) + 'px';
	// windowObj.resizeInProgress=true;
}

KB_stopRedim = function(windowObj) {
	windowObj.resizeInProgress = false;
	initEventX = 0;
	initEventY = 0;
	windowObj.width = windowObj.width - deltaX;
	windowObj.height = windowObj.height - deltaY;
	Event.stopObserving(document, 'mousemove', function(e) {
		if (windowObj.resizeInProgress)
			KB_newDim(e, windowObj);
	});
}

KB_WINDOW.prototype.KB_show = function() {
	new Insertion.Top(
			'adminSpacer',
			"<div id='KB_window_"
					+ this.instanceName
					+ "' class='KB_window'><div id='KB_topbar_"
					+ this.instanceName
					+ "' title='Déplacer la fenêtre "
					+ this.instanceName
					+ "' class='KB_topbar'><div class='KB_caption'>"
					+ this.caption
					+ "</div><div id='KB_close_"
					+ this.instanceName
					+ "' class='KB_close'><img id='KB_imgclose_"
					+ this.instanceName
					+ "' src='/js/kmtBox/close.gif' class='KB_imgclose' Title='Fermer la fenêtre' alt='Fermer la fenêtre'/></div></div><div id='KB_content_"
					+ this.instanceName
					+ "' class='KB_content'></div><div id='KB_status_"
					+ this.instanceName + "' class='KB_status'></div></div>");
	$('KB_window_' + this.instanceName).style.display = "none";
	var windowObj = this;

	Event.observe('KB_status_' + this.instanceName, 'mousedown', function(e) {
		KB_startRedim(e, windowObj);
	});
	Event.observe('KB_close_' + this.instanceName, 'click', function(e) {
		KB_kill(windowObj);
	});
	Event.observe(window, 'resize', function(e) {
		putKB_window(windowObj);
	});
	Event.observe(window, 'scroll', function(e) {
		Zoverlay.setOverlayHeight();
	});
	Event.observe('KB_topbar_' + this.instanceName, 'click', function(e) {
		putKB_ontop(windowObj);
	});
	new Draggable('KB_window_' + this.instanceName, {
		scroll : window,
		handle : 'KB_topbar_' + this.instanceName
	});
	putKB_ontop(this);
	this.KB_DONE = true;
	ajaxUpdaterRequest('KB_content_' + this.instanceName, 'get',this.requestParams,'',true);
	this.KB_resize();

	$('KB_window_' + this.instanceName).style.backgroundImage = "url(/icones/admin/bg_1.jpg)";

}

KB_WINDOW.prototype.KB_showImage = function() {
	new Insertion.Top(
			'adminSpacer',
			"<div id='KB_window_"
					+ this.instanceName
					+ "' class='KB_window_image' ><div id='KB_content_"
					+ this.instanceName
					+ "' class='KB_content'></div><div id='KB_close_"
					+ this.instanceName
					+ "' class='KB_close_image'><img src='/js/kmtBox/closebox.png' class='KB_imgclose' Title='Fermer la fenêtre' alt='Fermer la fenêtre'/></div></div>");
	windowObj = this;
	// Event.observe(window,'resize',function(e){putKB_window(windowObj);});
	Event.observe("KB_close_" + this.instanceName, 'click', function(e) {
		KB_kill(windowObj);
	});
	putKB_ontop(this);
	this.KB_DONE = true;
	$('KB_content_' + this.instanceName).style.backgroundColor = this.backgroundColor;
	$('KB_content_' + this.instanceName).style.padding = this.padding + "px";
	$('KB_content_' + this.instanceName).innerHTML = "<div id='KB_image'></div>";
	myImgWidth = this.width;
	myImgHeight = this.height;
	putKB_window(this);
	ajaxUpdaterRequest('KB_image', 'get', 'ajax=1&kmt_multimedias='
			+ this.requestParams + '&view=afficher_media&largeur=' + myImgWidth
			+ '&hauteur=' + myImgHeight, '', false);
	KB_image_resize();
	$('KB_content_' + this.instanceName).style.overflow = 'hidden';

}

KB_WINDOW.prototype.KB_showFlash = function() {
	new Insertion.Top(
			'adminSpacer',
			"<div id='KB_window_"
					+ this.instanceName
					+ "' class='KB_window_flash' ><div id='KB_topbar_"
					+ this.instanceName
					+ "' title='Déplacer la fenêtre "
					+ this.instanceName
					+ "' class='KB_topbar_flash'><div class='KB_caption'></div><div id='KB_close_"
					+ this.instanceName
					+ "' class='KB_close'><img src='/js/kmtBox/closebox.png' class='KB_imgclose' Title='Fermer la fenêtre' alt='Fermer la fenêtre'/></div></div><div id='KB_content_"
					+ this.instanceName + "' class='KB_content'></div></div>");
	windowObj = this;
	Event.observe('KB_close_' + this.instanceName, 'click', function(e) {
		KB_kill(windowObj);
	});
	putKB_ontop(this);
	this.KB_DONE = true;
	$('KB_content_' + this.instanceName).style.backgroundColor = this.backgroundColor;
	$('KB_content_' + this.instanceName).style.padding = this.padding + "px";
	$('KB_content_' + this.instanceName).innerHTML = "<div id='KB_image' style='text-align:center;'></div>";
	$('KB_topbar_' + this.instanceName).style.width = this.width + 'px'
	putKB_window(this);
	ajaxUpdaterRequest('KB_image', 'get', this.requestParams, '', false);
	this.KB_resize();
	$('KB_content_' + this.instanceName).style.overflow = 'hidden';
}
KB_WINDOW.prototype.KB_showFile = function() {
	new Insertion.Top(
			'adminSpacer',
			"<div id='KB_window_"
					+ this.instanceName
					+ "' class='KB_window_image' ><div id='KB_content_"
					+ this.instanceName
					+ "' class='KB_content'></div><div id='KB_close_"
					+ this.instanceName
					+ "' class='KB_close_image'><img src='/js/kmtBox/closebox.png' class='KB_imgclose' Title='Fermer la fenêtre' alt='Fermer la fenêtre'/></div></div>");
	windowObj = this;
	// Event.observe(window,'resize',function(e){putKB_window(windowObj);});
	Event.observe("KB_close_" + this.instanceName, 'click', function(e) {
		KB_kill(windowObj);
	});
	putKB_ontop(this);
	this.KB_DONE = true;
	$('KB_content_' + this.instanceName).style.backgroundColor = this.backgroundColor;
	$('KB_content_' + this.instanceName).style.padding = this.padding + "px";
	$('KB_content_' + this.instanceName).innerHTML = "<div id='KB_image'></div>";
	myImgWidth = this.width;
	myImgHeight = this.height;
	putKB_window(this);
	$('KB_image').innerHTML = "<img src=" + this.requestParams + ">";
	KB_image_resize();
	$('KB_content_' + this.instanceName).style.overflow = 'hidden';
}

KB_WINDOW.prototype.KB_resize = function() {
	$('KB_content_' + this.instanceName).style.height = this.height + 'px';
	$('KB_content_' + this.instanceName).style.width = this.width + 'px';
	$('KB_window_' + this.instanceName).style.width = this.width + 'px';
	putKB_window(this);
}

KB_image_resize = function() {
	
	if ($('media_' + myImgWidth) !=undefined) {
		imgElement = $('media_' + myImgWidth);
		if (imgElement.complete()) {
			ZWindowDivName = $('KB_window_' + windowObj.instanceName);
			ZContentDivName = $('KB_content_' + windowObj.instanceName);
			ZContentDivName.style.height = imgElement.height + 'px';
			ZContentDivName.style.width = imgElement.width + 'px';
			ZWindowDivName.style.width = imgElement.width + 'px';
			windowObj.width = imgElement.width;
			windowObj.height = imgElement.height;
			putKB_window(windowObj);
		} else {
			setTimeout("KB_image_resize();", 250);
		}
	}
}
