/**
 * jt_DialogBox.js - DHTML modal dialog box
 *
 * @version 23 Aug 2007
 * @author	Joseph Oster, wingo.com, Copyright (c) 2005-2007
 * @license http://www.wingo.com/dialogbox/license.html
 */
/************ REQUIRES: 'jt_BodyZ' and 'jt_Veil' from 'jt_.js' ************/

jt_DialogBox = function(isModal) {
	// CONSTRUCTOR for 'jt_DialogBox' object
	if (arguments.length == 0) return;
	this.isModal = isModal;
	this.container = document.createElement('div');
	this.container.className = jt_DialogBox.className;
	this.container.dialogBox = this;
	
	//BEGIN Top box
	var topDiv = document.createElement('div');
	
	var topLeft = document.createElement('div');
	topLeft.className = "jtDialogBoxLeft";
	topDiv.appendChild(topLeft);
	
	
	this.topCenter = document.createElement('div');
	this.topCenter.className = "jtDialogBoxCenter";
	this.topCenter.innerHTML="";
	topDiv.appendChild(this.topCenter);
	
	var topRight = document.createElement('div');
	topRight.className = "jtDialogBoxRight";
	topDiv.appendChild(topRight);
	topDiv.className = "TopDiv";
	this.container.appendChild(topDiv);
	
	//BEGIN Div Title
	this.titleDiv = document.createElement('div');
	this.titleDiv.className = "titleDiv";
	
	this.titleCell = document.createElement('div');
	this.titleCell.className = "jtDialogBoxTitle";
	this.titleDiv.appendChild(this.titleCell);
	
	var Button = document.createElement('img');
	Button.src="/admin/js/DialogBox/img/bot_cerrar.gif";
	Button.align="right";
	Button.alt="Cerrar";
	Button.title="Cerrar";
	Button.className = "imgHref";
	Button.onclick = jt_DialogBox.closeBox;
	
	var topClose = document.createElement('div');
	topClose.className = "jtDialogBoxClose";
	topClose.appendChild(Button);
	this.titleDiv.appendChild(topClose);
	
	this.container.appendChild(this.titleDiv);
	
	
	//BEGIN ContentArea
	this.contentDiv = document.createElement('div');
	this.contentArea = document.createElement('div');
	this.contentArea.className = "ContentArea";
	this.contentDiv.appendChild(this.contentArea);
	this.contentDiv.className = "ContentDiv";
	this.container.appendChild(this.contentDiv);
	
	//BEGIN Bottom box
	var bottomDiv = document.createElement('div');
	
	var bottomLeft = document.createElement('div');
	bottomLeft.className = "jtDialogBoxLeftB";
	bottomDiv.appendChild(bottomLeft);
	
	this.bottomCenter = document.createElement('div');
	this.bottomCenter.className = "jtDialogBoxCenter";
	this.bottomCenter.innerHTML="";
	bottomDiv.appendChild(this.bottomCenter);
	
	var bottomRight = document.createElement('div');
	bottomRight.className = "jtDialogBoxRightB";
	bottomDiv.appendChild(bottomRight);
	bottomDiv.className = "TopDiv";
	this.container.appendChild(bottomDiv);
	jt_BodyZ.toTop(this.container);
	Drag.init(this.titleCell, this.container, 0, null, 0);
}


//BEGIN: Public Methods
jt_DialogBox.imagePath = "../images/"; // set by application; directory path to 'window_close.gif' in titleBar
jt_DialogBox.FixNoViewElementNo=new Array();
jt_DialogBox.prototype.show = function() {
    jt_DialogBox.FixNoViewElement('none');
	this.container.style.display = "block";
	jt_BodyZ.toTop(this.container);
	//jt_TraceObj.show(this.container);
	if (this.isModal) jt_Veil.show(true);
	jt_divOnScrn(this.container);
	}

jt_DialogBox.prototype.hide = function(ok) {
	this.container.style.display = "none";
	if (this.isModal) jt_Veil.show(false);
	if (ok) {
		if (this.callOK)
			if (this.returnData) this.callOK(this.returnData);
			else this.callOK();
		}
	else if (this.callCancel) this.callCancel();
	}

jt_DialogBox.prototype.moveTo = function(x, y) {
	if (x == -1) x = Math.round((document.body.clientWidth - this.container.offsetWidth) / 2);
	if (y == -1) y = Math.round((document.body.clientHeight - this.container.offsetHeight) / 2) + document.body.scrollTop;
	this.container.style.left = x + "px";
	this.container.style.top = y + "px";
	}

jt_DialogBox.prototype.setTitle = function(title) {
	this.titleCell.innerHTML = title;
}

jt_DialogBox.prototype.setUrl = function(url, height) {
	// creates one IFRAME above 'setContent()' area, updates 'url'
	if (!this._jtDialogBIF) {
		this._jtDialogBIF = document.createElement('IFRAME');
		this._jtDialogBIF.setAttribute('frameBorder', 'no');
		this._jtDialogBIF.style.width = "100%";
		if (height) this._jtDialogBIF.style.height = height;
		this.contentArea.parentNode.insertBefore(this._jtDialogBIF, this.contentArea);
		}
	this._jtDialogBIF.src = url;
	}

jt_DialogBox.prototype.getUrl = function() {
	if (this._jtDialogBIF) {
		jt_TraceObj.show(this._jtDialogBIF);
		var url = this._jtDialogBIF.src;
		if (this._jtDialogBIF.contentWindow) {
			try {url = this._jtDialogBIF.contentWindow.location.href;}
			catch(e) {}
			}
		return url;
		}
	}

jt_DialogBox.prototype.setContent = function(htmlContent) {
	this.contentArea.innerHTML = htmlContent;
	}

jt_DialogBox.prototype.setWidth = function(width) {
	this.contentDiv.style.width = width + "px";
	this.container.style.width = width + "px";
	this.titleCell.style.width= (width - 120 ) + "px";
	this.topCenter.style.width= (width - 24 ) + "px";
	this.bottomCenter.style.width= (width - 24 ) + "px";
	}
jt_DialogBox.prototype.setHeightTitle = function(height) {
	this.titleDiv.style.height= height + "px";
}

jt_DialogBox.prototype.setHeightTitle = function(height) {
	this.titleDiv.style.height=height+"px";
	}

jt_DialogBox.prototype.setCallOK = function(callOK) {
	// set by application as needed
	this.callOK = callOK;
	}

jt_DialogBox.prototype.setCallCancel = function(callCancel) {
	// set by application as needed
	this.callCancel = callCancel;
	}

jt_DialogBox.prototype.getContentNode = function() {
	// expose 'contentArea' DOM node for direct manipulation
	return this.contentArea;
	}
//END: Public Methods


//BEGIN: Private Methods/
jt_DialogBox.className = "jtDialogBox"; // CSS className
jt_DialogBox.closeIcon = null;
jt_DialogBox.maxDepth = 5; // optimize search of parent nodes

jt_DialogBox.initCloseIcon = function() {
	// pre-fetch this icon so it doesn't distort dialog box size
	if (jt_DialogBox.closeIcon == null) {
		jt_DialogBox.closeIcon = new Image();
		jt_DialogBox.closeIcon.src = jt_DialogBox.imagePath + "window_close.gif";
		}
	}
jt_DialogBox.FixNoViewElement=function(displayValue){
    if (/MSIE (5\.5|6\.)/.test(navigator.userAgent)){
	    if(document.getElementById('BannerLeft')){
	        document.getElementById('BannerLeft').style.display=displayValue;
	    }
	    if(document.getElementById('BannerContent')){
	        document.getElementById('BannerContent').style.display=displayValue;
	    }
	    for(numForm=0;numForm<document.forms.length;numForm++) {
	        for(elementForm=0;elementForm<document.forms[numForm].elements.length;elementForm++) {
	            if(document.forms[numForm].elements[elementForm].type=="select-one"){
	                changeView=true;
	                if(document.forms[numForm].elements[elementForm].style.display=='none' && displayValue=='none'){
	                    this.FixNoViewElementNo.push(document.forms[numForm].elements[elementForm].name);
	                } else if (displayValue=='') {
	                    for(vi=0; vi<this.FixNoViewElementNo.length; vi++){
	                        if(document.forms[numForm].elements[elementForm].name==this.FixNoViewElementNo[vi]){
	                            changeView=false;
	                            break;
	                        }
	                    }
	                }
	                if(changeView){
	                    document.forms[numForm].elements[elementForm].style.display=displayValue;
	                }
	            }
	        }
	    }
	 }
}
jt_DialogBox.closeBox = function(e) {
    jt_DialogBox.FixNoViewElement('');
	if (!e) e = window.event;
	var node = e.target ? e.target : e.srcElement;
	var count = 0;
	while ((node != null) && (count < jt_DialogBox.maxDepth)) {
		if (node.dialogBox) {
			node.dialogBox.hide();
			return false;
			}
		node = node.parentNode;
		count++;
		}
	return false;
	}
