/*  ---------------------------------------------------
	---------------------------------------------------
	Clase que muestra un popup de dialogo que es una capa como si fuera una
	ventana modal. (deshabilita el foco del resto de la ventana).
	Requiere CSS para posicionamiento y colores.
	---------------------------------------------------
	--------------------------------------------------- */
function WBEDialogPopup ()
{
	this.TabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");
	this.TabIndexes = new Array();
	
	this.url = "";					// URL que se mostrará en el iframe.
	this.width = 400;					// Anchura del popup
	this.height = 400;				// Altura del popup
	this.returnFunc = null;			// Función a la que retorna.
	this.IsDialogShowed = false; // Indica si el popup esta visible
	this.gi = 0;
	this.title = "Component";
	this.zIndex = 1000;
	
	// Indica si debe ocultar SELECTS
	this.HasToHideSelects = (parseInt(window.navigator.appVersion.charAt(0), 10) <= 6 &&
						window.navigator.userAgent.indexOf("MSIE") > -1);
	this.oldHeight = 0;
	this.maskLayer;
	this.titleLayer;
	this.containerLayer;
	this.iFrameLayer;
	this.shadowLayer;
	this.popupIsShown		= false;
	this.hideSelects		= false;
	this.isWizard			= false;

	this.wbe_JsHelper		= new WBEJsHelper();
	
	// Crea las capas que componen el cuadro de diálogo.
	this.wbe_createDialogLayers = function ()
	{
		if (!document.getElementById('wbe_DialogMask')) {
			document.write("<div id='wbe_edit_box_shadow'></div>");
			document.write("<div id='wbe_DialogMask' style=\"background-image: url('include/admin/img/maskBG.png')\">&nbsp;</div>");
			document.write("<div id='wbe_edit_box' style='display:none'>");
			document.write("<div id='wbe_edit_box_header'>");
			document.write("<div id='wbe_edit_box_title'></div>");
			document.write("<div id='buttons'>");
			document.write("<img src='include/admin/img/btn_min_edt_box.gif' id='wbe_bar_btn_min' hspace=3 border=0 onclick='javascript:___wbe_DialogMinimize()'>");
			document.write("<img src='include/admin/img/btn_rest_edt_box.gif' id='wbe_bar_btn_max' hspace=3 border=0 onclick='javascript:___wbe_DialogMaximize()' style='display:none' >");
			document.write("<img src='include/admin/img/btn_clo_edt_box.gif' hspace=3 border=0 onclick='javascript:___wbe_HideDialogHandler()'>");
			document.write("</div>");
			document.write("</div>");
			document.write("<div id='wbe_edit_box_showhelp'><a href='javascript:___showHelp(200);' onClick=''>ayuda &gt;&gt;</a></div>");
			document.write("<div id='wbe_edit_box_margin_top'> </div>");
			document.write("<div id='wbe_edit_box_help'>");
			document.write("<div id='wbe_edit_box_hidehelp'><a href='javascript:___hideHelp();'>&lt;&lt; ocultar ayuda</a></div>");
			document.write("<div id='wbe_edit_box_help_tit'></div>");
			document.write("<div id='wbe_edit_box_help_text'>");
			document.write("</div>");
			document.write("</div>");
			document.write("<iframe id=wbe_DialogIFrame name=wbe_editIFrame scrolling=auto frameborder=0 MARGINWIDTH=0 MARGINHEIGHT=0></iframe>");
			document.write("</div>");
		}
	};
	
	// Inicializa las variables globales del popup
	this.init = function () {

		// Creamos las capas.
		this.wbe_createDialogLayers();

		// Inicializamos el objeto
		this.maskLayer = document.getElementById("wbe_DialogMask");
		this.containerLayer = document.getElementById("wbe_edit_box");
		this.iFrameLayer = document.getElementById("wbe_DialogIFrame");
		this.titleLayer = document.getElementById("wbe_edit_box_title");
		this.shadowLayer = document.getElementById("wbe_edit_box_shadow");
		
		this.width = 800;					// Anchura inicial del popup
		this.height = 600;				// Altura inicial del popup
		this.returnFunc = null;			// Función a la que retorna.
		this.IsDialogShowed = false; // Inicalmente está oculto

		// If using Mozilla or Firefox, use Tab-key trap. KeyDown Handler
		if (!document.all) {
			document.onkeypress = wbe_keyDownHandler;
		}

		//this.wbe_JsHelper.handleEvent(window, "resize", ___wbe_CenterDialogHandler);
		//this.wbe_JsHelper.handleEvent(window, "scroll", ___wbe_CenterDialogHandler);

		window.onresize = ___wbe_CenterDialogHandler;
		window.onscroll = ___wbe_CenterDialogHandler;

	};
	
	// Muestra el pop up con las dimensiones que se le pasan.
	// El ultimo argumento es la funcion a la que hay que llamar
	this.show = function (bHideButtons)
	{
		if (bHideButtons)
			document.getElementById('buttons').style.display = "none";
		else
			document.getElementById('buttons').style.display = "";
			
		___restartHelpWindow();

		document.getElementById("wbe_edit_box").style.filter = 'alpha(opacity=100)';
		document.getElementById("wbe_edit_box").style.opacity = '1';

		this.isWizard = false;
		this.IsDialogShowed = true;
		this.disableTabIndexes();

		this.titleLayer.innerHTML = "Editor: " + this.title;
		
		this.maskLayer.style.display = "block";
		this.containerLayer.style.display = "block";
		this.shadowLayer.style.display = "block";

		// La ventana de edición siempre la mostramos en el top de la ventana.
		this.zIndex = this.wbe_JsHelper.getMaxZindex();	// Coge el maximo z-index
		this.maskLayer.style.zIndex = this.zIndex++;
		this.shadowLayer.style.zIndex = this.zIndex++
		this.containerLayer.style.zIndex = this.zIndex++;
		
		// centra la ventana dialogo en el centro.
		this.center(this.width, this.height);

		// Altura de la barra de título
		var titleBarHeight = parseInt(document.getElementById("wbe_edit_box_header").offsetHeight, 10);

		this.containerLayer.style.width = this.width + "px";
		this.containerLayer.style.height = (this.height+titleBarHeight) + "px";
		
		___resizeShadow();

		// need to set the width of the iframe to the title bar width because of the dropshadow
		// some oddness was occuring and causing the frame to poke outside the border in IE6
		this.iFrameLayer.style.width = this.width + "px";;
		this.iFrameLayer.style.height = (this.height) + "px";

		// set the url
		this.iFrameLayer.src = this.url;

		//gReturnFunc = this.returnFunc;
		// for IE
		if (this.IsDialogShowed == true) this.hideSelectBoxes();
	};

	// Muestra el pop up para el asistente.
	this.showWizard = function (bHideButtons)
	{
		if (bHideButtons)
			document.getElementById('buttons').style.display = "none";
		else
			document.getElementById('buttons').style.display = "";

		___restartHelpWindow();

		document.getElementById("wbe_edit_box").style.filter = 'alpha(opacity=100)';
		document.getElementById("wbe_edit_box").style.opacity = '1';
		
		this.isWizard = true;
		this.IsDialogShowed = true;
		//this.disableTabIndexes();

		this.titleLayer.innerHTML = "Asistente";
		
		this.maskLayer.style.display = "none";
		this.containerLayer.style.display = "block";
		this.shadowLayer.style.display = "block";

		// La ventana de edición siempre la mostramos en el top de la ventana.
		this.zIndex = this.wbe_JsHelper.getMaxZindex();	// Coge el maximo z-index
		this.maskLayer.style.zIndex = this.zIndex++;
		this.containerLayer.style.zIndex = this.zIndex++;
		//this.shadowLayer.style.display = this.zIndex - 1;
		
		// centra la ventana dialogo en el centro.
		this.center(this.width, this.height);

		// Altura de la barra de título
		var titleBarHeight = parseInt(document.getElementById("wbe_edit_box_header").offsetHeight, 10);

		this.containerLayer.style.width = this.width + "px";
		this.containerLayer.style.height = (this.height+titleBarHeight) + "px";

		// need to set the width of the iframe to the title bar width because of the dropshadow
		// some oddness was occuring and causing the frame to poke outside the border in IE6
		this.iFrameLayer.style.width = this.width + "px";;
		this.iFrameLayer.style.height = (this.height) + "px";

		// set the url
		this.iFrameLayer.src = this.url;

		//gReturnFunc = this.returnFunc;
		// for IE
		if (this.IsDialogShowed == true) this.hideSelectBoxes();
		
		/*this.containerLayer.style.display = '';
		this.containerLayer.style.filter = 'alpha(opacity=80)';
		this.containerLayer.style.opacity = '0.80';
		*/

		___resizeShadow();
	};
	
	// Muestra el pop up para el asistente con fading.
	this.showWizardWithFading = function (bHideButtons)
	{
		if (bHideButtons)
			document.getElementById('buttons').style.display = "none";
		else
			document.getElementById('buttons').style.display = "";
			
		//___hideHelp();
	
		this.containerLayer.style.display = '';
		this.containerLayer.style.filter = 'alpha(opacity=0)';
		this.containerLayer.style.opacity = '0.0';
		this.showWizard();
		setTimeout('___FadeWizard(0);', 10);
	};
	
	// centra esta ventana de diálogo			
	this.center = function () {
//alert('center - entra');		
		if (this.IsDialogShowed) {

			if (!this.isWizard) {
		
				if (this.width == null || isNaN(this.width)) {
					this.width = this.containerLayer.offsetWidth;
				}
				if (this.height == null) {
					this.height = this.containerLayer.offsetHeight;
				}
				
				var fullHeight = this.wbe_JsHelper.getViewHeight();
				var fullWidth = this.wbe_JsHelper.getViewWidth();
	
				var scTop = (document.documentElement.scrollTop) ? parseInt(document.documentElement.scrollTop,10) : parseInt(document.body.scrollTop,10);
				var scLeft = (document.documentElement.scrollLeft) ? parseInt(document.documentElement.scrollLeft,10) : parseInt(document.body.scrollLeft,10);
	
				this.maskLayer.style.height = fullHeight + "px";
				this.maskLayer.style.width = fullWidth + "px";
				this.maskLayer.style.top = scTop + "px";
				this.maskLayer.style.left = scLeft + "px";
	
				window.status = this.maskLayer.style.top + " " + this.maskLayer.style.left + " " + this.gi++;
	
				var titleBarHeight = parseInt(document.getElementById("wbe_edit_box_header").offsetHeight, 10);
	
				this.containerLayer.style.top = 150 + "px"; //(scTop + ((fullHeight - (this.height+titleBarHeight)) / 2)) + "px";
				this.containerLayer.style.left =  (scLeft + ((fullWidth - this.width) / 2)) + "px";
				
				this.shadowLayer.style.top = 160 + "px"; //((scTop + ((fullHeight - (this.height+titleBarHeight)) / 2)) +10) + "px";
				this.shadowLayer.style.left =  ((scLeft + ((fullWidth - this.width) / 2)) - 10) + "px";
				
				//alert(fullWidth + " " + width + " " + wbe_PopupEditContainer.style.left);
			
			} else {
//alert('center - 2');		

				if (this.width == null || isNaN(this.width)) {
					this.width = this.containerLayer.offsetWidth;
				}
				if (this.height == null) {
					this.height = this.containerLayer.offsetHeight;
				}
				
				// Posicionamos la ventana
				var fullWidth = this.wbe_JsHelper.getViewWidth();
				var titleBarHeight = parseInt(document.getElementById("wbe_edit_box_header").offsetHeight, 10);
				
				var scTop = (document.documentElement.scrollTop) ? parseInt(document.documentElement.scrollTop,10) : parseInt(document.body.scrollTop,10);
				var scLeft = (document.documentElement.scrollLeft) ? parseInt(document.documentElement.scrollLeft,10) : parseInt(document.body.scrollLeft,10);
	
				this.containerLayer.style.top = 150 + "px";
				this.containerLayer.style.left =  (scLeft + (fullWidth - (this.width + 30))) + "px";
		
				this.shadowLayer.style.top = 160 + "px";
				this.shadowLayer.style.left =  ((scLeft + (fullWidth - (this.width + 30))) - 10) + "px";
		
//alert('center - 3');		
			}
		}
//alert('center - sale');		
	};

	// Este es el evento de centrar la ventana.
	// Se lanza cuando se redimensiona el navegador y cuando se muestra el popup.
	this.hide = function () {
		this.IsDialogShowed = false;
		this.restoreTabIndexes();

		// Quitamos la capa de fondo (si existe)
		if (!this.maskLayer) return;
		this.maskLayer.style.display = "none";
		this.containerLayer.style.display = "none";
		this.shadowLayer.style.display = "none";

		if (this.returnFunc)
			this.returnFunc(this.iFrameLayer.returnVal);

		this.iFrameLayer.src = '';

		// Muestra todos los selects otra vez
		if (this.HasToHideSelects) this.displaySelectBoxes();
	}

	// Oculta todos los campos de formulario SELECT por el problema que tiene el IE. (como en el back azul)
	this.hideSelectBoxes = function () {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT")
					document.forms[i].elements[e].style.visibility="hidden";
			} // for e
		} // for i
	};
	
	// Muestra todos los campos de formulario SELECT por el problema que tiene el IE. (como en el back azul)
	this.displaySelectBoxes = function () {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT")
					document.forms[i].elements[e].style.visibility="visible";
			} // for e
		} // for i
	};

	// For IE. Almacena y elimina todos los TabIndex para que no respondan a los eventos.
	this.disableTabIndexes = function () {
		if (document.all) {
			var i = 0;
			for (var j = 0; j < this.TabbableTags.length; j++) {
				var tagElements = document.getElementsByTagName(this.TabbableTags[j]);
				for (var k = 0 ; k < tagElements.length; k++) {
					this.TabIndexes[i] = tagElements[k].tabIndex;
					tagElements[k].tabIndex="-1";
					i++;
				} // for k
			} // for j
		} // if
	};

	// For IE. Restaura todos los TabIndex de teclado para poder.
	this.restoreTabIndexes = function () {
		if (document.all) {
			var i = 0;
			for (var j = 0; j < this.TabbableTags.length; j++) {
				var tagElements = document.getElementsByTagName(this.TabbableTags[j]);
				for (var k = 0 ; k < tagElements.length; k++) {
					tagElements[k].tabIndex = this.TabIndexes[i];
					tagElements[k].tabEnabled = true;
					i++;
				} // for k
			} // for j
		} // if
	};
	
	// Minimizar la ventana
	this.minimize = function () {
		this.iFrameLayer.style.display = 'none';
		this.containerLayer.style.height = 16;
		this.shadowLayer.style.height = 0;
		document.getElementById("wbe_bar_btn_min").style.display = 'none';
		document.getElementById("wbe_bar_btn_max").style.display = '';
	};
	
	// Maximizar la ventana
	this.maximize = function() {
		this.iFrameLayer.style.display = '';
		this.containerLayer.style.height = this.height;
		var titleBarHeight = parseInt(document.getElementById("wbe_edit_box_header").offsetHeight, 10);
		
		___resizeShadow();
		
		document.getElementById("wbe_bar_btn_min").style.display = '';
		document.getElementById("wbe_bar_btn_max").style.display = 'none';
	};
	
	// Redimiensionar la ventana - AAAAAA
	this.resize = function(iWidth, iHeight) {
		
		this.width = iWidth;					// Anchura del popup
		this.height =iHeight;				// Altura del popup
		this.center();

		this.containerLayer.style.width = this.width + "px";
		var titleBarHeight = parseInt(document.getElementById("wbe_edit_box_header").offsetHeight, 10);
		this.containerLayer.style.height = (this.height+titleBarHeight) + "px";

		___resizeShadow();

		this.iFrameLayer.style.width = this.width + "px";;
		this.iFrameLayer.style.height = (this.height) + "px";
	};
	
};
/*  ---------------------------------------------------
	--------------------------------------------------- */

/*  ---------------------------------------------------
	---------------------------------------------------
	Eventos
	---------------------------------------------------
	--------------------------------------------------- */

function wbe_keyDownHandler(event) { if (___wbeADMIN.dialogPopUp.IsDialogShowed  && event.keyCode == 9)  return false; }
function ___wbe_CenterDialogHandler()	{	___wbeADMIN.dialogPopUp.center();	}
function ___wbe_DialogMaximize()		{	___wbeADMIN.dialogPopUp.maximize();	}
function ___wbe_DialogMinimize()		{	___wbeADMIN.dialogPopUp.minimize();	}

function ___wbe_HideDialogHandler() {	
	document.getElementById("wbe_DialogIFrame").style.display = '';
	___wbeADMIN.dialogPopUp.hide();	
}

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////






function ___FadeWizard(in_op) {
	in_op += 10;
	if (in_op < 100) {
		document.getElementById("wbe_edit_box").style.filter = 'alpha(opacity=' + in_op + ')';
		document.getElementById("wbe_edit_box").style.opacity = '0.' + in_op;
		setTimeout('___FadeWizard(' + in_op + ');', 10);
	} else {
		document.getElementById("wbe_edit_box").style.filter = 'alpha(opacity=100)';
		document.getElementById("wbe_edit_box").style.opacity = '1';
	}
}

// Muestra la parte de la ayuda
function ___showHelp(width_help) {
	var edit_box = new WBEDivManager("wbe_edit_box"); //creo objeto para manejar propiedades de capa
	var new_width_edit_box;
	//alert("anchura de caja=" + edit_box.w);
	new_width_edit_box = edit_box.w + width_help + 5 - 2;
	//alert("la paso a:" + new_width_edit_box);
	document.getElementById('wbe_edit_box').style.width = '' + new_width_edit_box + 'px';
	document.getElementById('wbe_edit_box_help').style.display = 'block';//muestro la ventana de ayuda
	document.getElementById('wbe_edit_box_help').style.height = edit_box.h + 'px'; //ajusto la altura de la capa de ayuda
	document.getElementById('wbe_edit_box_margin_top').style.display = 'block';//muestro la capa que me dejará la edición en el mismo sitio
	document.getElementById('wbe_edit_box_showhelp').style.display = 'none';//oculto la ventana con el botón de mostrar ayuda		

	___wbeADMIN.dialogPopUp.width = new_width_edit_box;
	___wbeADMIN.dialogPopUp.center();

	___resizeShadow();
}


function ___hideHelp(){
	var edit_box = new WBEDivManager("wbe_edit_box"); //creo objeto para manejar propiedades de capa
	var help_box = new WBEDivManager("wbe_edit_box_help"); //creo objeto para manejar propiedades de capa
	var new_width_edit_box;
	//alert("anchura de caja=" + edit_box.w);
	if (help_box.w == 0) {
		new_width_edit_box = ___wbeADMIN.dialogPopUp.width
	} else {
		new_width_edit_box = edit_box.w - help_box.w - 5 - 2;
	}
	
	document.getElementById('wbe_edit_box').style.width = '' + new_width_edit_box + 'px';

	___hideHelpWindow();
	
	___wbeADMIN.dialogPopUp.width = new_width_edit_box;
	___wbeADMIN.dialogPopUp.center();	

	___resizeShadow();
}	

function ___hideHelpWindow(){
	document.getElementById('wbe_edit_box_help').style.display = 'none';
	document.getElementById('wbe_edit_box_margin_top').style.display = 'none';
	document.getElementById('wbe_edit_box_showhelp').style.display = 'block';
}	

function ___restartHelpWindow(){
	document.getElementById('wbe_edit_box_help').style.display = 'none';
	document.getElementById('wbe_edit_box_margin_top').style.display = 'block';
	document.getElementById('wbe_edit_box_showhelp').style.display = 'none';
	document.getElementById('wbe_edit_box_help_tit').innerHTML = '';
	document.getElementById('wbe_edit_box_help_text').innerHTML = '';
}	

function ___setHelp(helpTile, helpText) {
	if ('' + helpText == '') {
		document.getElementById('wbe_edit_box_margin_top').style.display = 'none';
		document.getElementById('wbe_edit_box_showhelp').style.display = 'none';
	} else {
		document.getElementById('wbe_edit_box_help_tit').innerHTML = unescape(helpTile);
		document.getElementById('wbe_edit_box_help_text').innerHTML = '<p>' + unescape(helpText) + '</p>';
		document.getElementById('wbe_edit_box_showhelp').style.display = 'block';
		document.getElementById('wbe_edit_box_margin_top').style.display = 'none';
	}
	
	___resizeShadow();
}

function ___resizeShadow() {
	var oDivPopUp = new WBEDivManager('wbe_edit_box');
	document.getElementById('wbe_edit_box_shadow').style.height = oDivPopUp.h + 'px';
	document.getElementById('wbe_edit_box_shadow').style.width = oDivPopUp.w + 'px';
}
