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() {
	µ("body").after("<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').css("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.css("left", windowXmiddle - (ZwindowObj.width / 2)	+ ((KB_instance_arr.length - 1) * 20) + "px");
		ZdivName.css("top", windowYmiddle - (ZwindowObj.height / 2)	+ µ(document).scrollTop() + ((KB_instance_arr.length - 1) * 20) + "px");
		ZdivName.fadeIn(600);
	}
}

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).css("zIndex", i + 1);
		}
	}
	µ('#KB_window_' + ZwindowObj.instanceName).css("zIndex", KB_ZINDEX++);
}

KB_kill = function(ZwindowObj) {
	µ('#KB_window_' + ZwindowObj.instanceName).fadeOut(1000);
	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 {
		
		string = "µ('#KB_window_"+ ZwindowObj.instanceName+ "').remove();if(typeof helpPanelPosition!='undefined'){if(helpPanelPosition=='in') showHelpPanel()};µ('#KB_overlay').remove()";
		Zoverlay = null;
		KB_OVERLAY_DONE = false;
	}
	setTimeout(string, 1000);
	ZwindowObj.KB_DONE = false;
}


KB_startRedim = function(e, windowObj) {
	windowObj.resizeInProgress = true;
	initEventX = e.pageX;
	initEventY = e.pageY;
	
	µ(document).bind('mousemove',function(e){
		if (windowObj.resizeInProgress)
		KB_newDim(e, windowObj);
	});
	µ(document).bind('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 -e.pageX;
	deltaY = initEventY -e.pageY;
	ZContentDivName.css('height', (windowObj.height - deltaY) + 'px');
	ZContentDivName.css('width', (windowObj.width - deltaX) + 'px');
	ZWindowDivName.css('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;
	µ(document).unbind('mousemove');
}

KB_WINDOW.prototype.KB_show = function() {
	µ("#adminSpacer").after("<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).css("display","none");
	var windowObj = this;

	µ('#KB_status_' + this.instanceName).mousedown(function(e) {
		KB_startRedim(e, windowObj);
	});
	µ('#KB_close_' + this.instanceName).click( function(e) {
		KB_kill(windowObj);
	});
	µ(window).resize(function(e) {putKB_window(windowObj);});
	µ(window).scroll(function(e) {Zoverlay.setOverlayHeight();});
	µ('#KB_topbar_' + this.instanceName).click(function(e) {putKB_ontop(windowObj);});
	µ('#KB_window_' + this.instanceName).draggable({scroll : true,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).css("backgroundImage","url(/icones/admin/bg_1.jpg)");

}

KB_WINDOW.prototype.KB_showImage = function() {
	µ('#adminSpacer').after("<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);});
	
	µ("#KB_close_" + this.instanceName).click(function(e) {	KB_kill(windowObj);	});
	µ(window).resize(function(e) {putKB_window(windowObj);});
	µ(window).scroll(function(e) {Zoverlay.setOverlayHeight();});
	putKB_ontop(this);
	this.KB_DONE = true;
	µ('#KB_content_' + this.instanceName).css("backgroundColor",this.backgroundColor);
	µ('#KB_content_' + this.instanceName).css("padding",this.padding + "px");
	µ('#KB_content_' + this.instanceName).html("<div id='KB_image'><div style='text-align:center;margin:20px'><img src='/icones/common/wait.gif' style='margin-bottom:15px;' alt='Chargement...'/><br/>Chargement...</div></div>");
	myImgWidth = this.width;
	myImgHeight = this.height;
	
	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).css("overflow",'hidden');
	putKB_window(this);

}

KB_WINDOW.prototype.KB_showFlash = function() {
	µ('#adminSpacer').after("<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;
	µ('#KB_close_' + this.instanceName).click(function(e) {KB_kill(windowObj);});
	putKB_ontop(this);
	this.KB_DONE = true;
	µ('#KB_content_' + this.instanceName).css("backgroundColor",this.backgroundColor);
	µ('#KB_content_' + this.instanceName).css("padding", this.padding + "px");
	µ('#KB_content_' + this.instanceName).html("<div id='KB_image' style='text-align:center;'></div>");
	µ('#KB_topbar_' + this.instanceName).css("width", this.width + 'px');
	putKB_window(this);
	ajaxUpdaterRequest('KB_image', 'get', this.requestParams, '', false);
	this.KB_resize();
	µ('#KB_content_' + this.instanceName).css("overflow","hidden");
}
KB_WINDOW.prototype.KB_showFile = function() {
	µ('#adminSpacer').after("<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;
	µ("#KB_close_" + this.instanceName).click(function(e) {KB_kill(windowObj);});
	putKB_ontop(this);
	this.KB_DONE = true;
	µ('#KB_content_' + this.instanceName).css("backgroundColor",this.backgroundColor);
	µ('#KB_content_' + this.instanceName).css("padding",this.padding + "px");
	µ('#KB_content_' + this.instanceName).html("<div id='KB_image'></div>");
	myImgWidth = this.width;
	myImgHeight = this.height;
	µ('#KB_image').html("<img src=" + this.requestParams + ">");
	//KB_image_resize();
	µ('#KB_content_' + this.instanceName).css("overflow","hidden");
	putKB_window(this);
}

KB_WINDOW.prototype.KB_resize = function() {
	µ('#KB_content_' + this.instanceName).css("height",this.height + 'px');
	µ('#KB_content_' + this.instanceName).css("width",this.width + 'px');
	µ('#KB_window_' + this.instanceName).css("width",this.width + 'px');
	putKB_window(this);
}

KB_image_resize = function() {
	//alert('resize');
	if (µ('#media_' + myImgWidth) !=undefined) {
		imgElement = µ('#media_' + myImgWidth);
		//alert('resize');
		µ('#KB_content_' + windowObj.instanceName).css('height',imgElement.height() + 'px');
		µ('#KB_content_' + windowObj.instanceName).css('width',imgElement.width() + 'px');
		µ('#KB_window_' + windowObj.instanceName).css('width',imgElement.width() + 'px');
		windowObj.width = imgElement.width();
		windowObj.height = imgElement.height();
		putKB_window(windowObj);
	}
}
