var __focus_obj = null;

ModalWindow = function() {
	var imagePath;
	var winObjId;
	var width;
	var height;
	var icon;
	var iconPath;
	var message;
	var button;
	var focusObj;
	var button1OnClick;
	var button2OnClick;
	var flashPath;
	var sound;
	
	this.imagePath = "";
	this.winObjId = jsGetRandom();
	this.width = 330;
	this.height = 80;
	this.icon = "alert.png";
	this.iconPath = "";
	this.message = "";
	this.button = 0;
	this.focusObj = null;
	this.button1OnClick = null;
	this.button2OnClick = null;
	this.flashPath = "";
	this.sound = "alert";
	
	this.onCompletion = function() { };
}

ModalWindow.prototype = {
	__createWindow : function() {
		try {
			var tags = '';
			tags += '<table id="WinContent_' + this.winObjId + '" width="' + this.width + '" height="' + 
			this.height + '" border="0" cellspacing="0" cellpadding="0">';
			tags += '<tr>';
			tags += '<td width="6" height="6">' + DisplayPicture(this.imagePath + "win01.png",6,6) + '</td>';
			tags += '<td style="border-top:1px solid #595757; background-color:#F6F6F6;"><img src="' + 
			this.imagePath + 'blank.gif" border="0"></td>';
			tags += '<td width="13">' + DisplayPicture(this.imagePath + "win02.png",13,6) + '</td>';
			tags += '</tr>';
			tags += '<tr>';
			tags += '<td style="border-left:1px solid #595757; background-color:#F6F6F6;"><img src="' + 
			this.imagePath + 'blank.gif" border="0"></td>';
			tags += '<td style="background-color:#F6F6F6;">';
				
				tags += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
				tags += '<tr>';
				if (this.icon) tags += '<td width="64" align="center">' + DisplayPicture(this.iconPath + this.icon,64,64) + '</td>';
				tags += '<td>';
				
					tags += '<table width="100%" height="35" border="0" cellspacing="1" cellpadding="1">';
					tags += '<tr>';
					tags += '<td align="center" style="font-size:11px; cursor:default;"><strong>' + this.message + '</strong></td>';
					tags += '<td>';
					tags += '</table>';
					
					tags += '<table align="center" border="0" cellspacing="1" cellpadding="1">';
					tags += '<tr>';
					if (this.button == 0 || this.button == 2) {
						tags += '<td>';
						tags += WinModalButton("B_Accept", this.imagePath, "Aceptar", this.iconPath + "accept.png", this.winObjId, 
						this.button1OnClick);
						tags += '</td>';
					}
					
					if (this.button == 1 || this.button == 2) {
						tags += '<td>';
						tags += WinModalButton("B_Cancel", this.imagePath, "Cancelar", this.iconPath + "cancel.png", this.winObjId, 
						this.button2OnClick);
						tags += '</td>';
					}
					
					if (this.button == 3) {
						tags += '<td>';
						tags += WinModalButton("B_Close", this.imagePath, "Cerrar", this.iconPath + "cancel.png", this.winObjId, 
						this.button2OnClick);
						tags += '</td>';
					}
					tags += '<td>';
					tags += '</table>';
				
				tags += '</td>';
				tags += '</tr>';
				tags += '</table>';
			
			tags += '</td>';
			tags += '<td style="' + SetBgPNG(this.imagePath + 'win03.png') + '"></td>';
			tags += '</tr>';
			tags += '<tr>';
			tags += '<td height="14">' + DisplayPicture(this.imagePath + "win06.png",6,14) + '</td>';
			tags += '<td style="' + SetBgPNG(this.imagePath + 'win05.png') + '"></td>';
			tags += '<td>' + DisplayPicture(this.imagePath + "win04.png",13,14) + '</td>';
			tags += '</tr>';
			tags += '</table>';
			
			return tags;
		} catch(err) {
			jsShowErrors("ModalWindow:__createWindow", err);
		}
	}
	,
	__createDivs : function() {
		try {
			this.divs_content = document.createElement('div');
			this.divs_content.id = "__WinMod__" + this.winObjId;
			this.divs_content.name = "__WinMod__" + this.winObjId;
			this.divs_content.style.left = '0px';
			this.divs_content.style.top = '0px';
			this.divs_content.style.width = '0px';
			this.divs_content.style.height = '0px';
			this.divs_content.style.zIndex = 500;
			this.divs_content.style.position = "absolute";
			this.divs_content.style.display = 'block';
			document.body.appendChild(this.divs_content);
			
			this.divs_shadow = document.createElement('div');
			this.divs_shadow.id = "__WinShadow__" + this.winObjId;
			this.divs_shadow.name = "__WinShadow__" + this.winObjId;
			this.divs_shadow.style.left = '0px';
			this.divs_shadow.style.top = '0px';
			this.divs_shadow.style.width = '100%';
			this.divs_shadow.style.height = '100%';
			this.divs_shadow.style.zIndex = this.divs_content.style.zIndex - 1;
			this.divs_shadow.style.opacity = 0;
			this.divs_shadow.style.MozOpacity = 0;
			this.divs_shadow.style.backgroundColor = "#000000";
			this.divs_shadow.style.KhtmlOpacity = 0;
			this.divs_shadow.style.filter = "alpha(opacity=0)";
			this.divs_shadow.style.position = "absolute";
			this.divs_shadow.style.display = 'block';
			document.body.appendChild(this.divs_shadow);
			
			if (_jslib_isIE) {
				this.iframe_content = document.createElement('iframe');
				this.iframe_content.style.left = '0px';
				this.iframe_content.style.top = '0px';
				this.iframe_content.style.width = '0px';
				this.iframe_content.style.height = '0px';
				this.iframe_content.style.filter = "alpha(opacity=0)";
				this.iframe_content.style.zIndex = this.divs_content.style.zIndex - 1;
				this.iframe_content.style.position = 'absolute';
				this.iframe_content.id = "__WinIframe__" + this.winObjId;
				this.iframe_content.name = "__WinIframe__" + this.winObjId;
				this.iframe_content.frameBorder = 0;
				this.iframe_content.marginHeight = 0;
				this.iframe_content.marginWidth = 0;
				this.iframe_content.scrolling = "no";
				this.iframe_content.vSpace = 0;
				this.iframe_content.hSpace = 0;
				document.body.insertBefore(this.iframe_content, this.divs_content);
				
				
				this.iframe_shadow = document.createElement('iframe');
				this.iframe_shadow.style.left = '0px';
				this.iframe_shadow.style.top = '0px';
				this.iframe_shadow.style.width = '100%';
				this.iframe_shadow.style.height = '100%';
				this.iframe_shadow.style.filter = "alpha(opacity=0)";
				this.iframe_shadow.style.zIndex = this.divs_shadow.style.zIndex - 1;
				this.iframe_shadow.style.position = 'absolute';
				this.iframe_shadow.id = "__WinIframeShadow__" + this.winObjId;
				this.iframe_shadow.name = "__WinIframeShadow__" + this.winObjId;
				this.iframe_shadow.frameBorder = 0;
				this.iframe_shadow.marginHeight = 0;
				this.iframe_shadow.marginWidth = 0;
				this.iframe_shadow.scrolling = "no";
				this.iframe_shadow.vSpace = 0;
				this.iframe_shadow.hSpace = 0;
				document.body.insertBefore(this.iframe_shadow, this.divs_shadow);
			}
			
			if (this.sound) {
				this.divs_flash = document.createElement('div');
				this.divs_flash.id = "__Sounds__" + this.winObjId;
				this.divs_flash.name = "__Sounds__" + this.winObjId;
				this.divs_flash.style.left = '0px';
				this.divs_flash.style.top = '0px';
				this.divs_flash.style.width = '1px';
				this.divs_flash.style.height = '1px';
				this.divs_flash.style.zIndex = this.divs_content.style.zIndex + 1;
				this.divs_flash.style.position = "absolute";
				this.divs_flash.style.visibility = 'hidden';
				document.body.appendChild(this.divs_flash);
			}
		} catch(err) {
			jsShowErrors("ModalWindow:__createDivs", err);
		}
	}
	,
	 __resizeDivs : function() {
		try {
			var win_content_w;
			var win_content_h;
			
			if (this.width) {
				jsSetStyle(this.divs_content, "width", this.width + 'px');
				win_content_w = this.width;
			} else {
				var content_w = jsGetObject("WinContent_" + this.winObjId).offsetWidth;
				win_content_w = this.divs_content.offsetWidth;
			}
			if (this.height) {
				jsSetStyle(this.divs_content, "height", this.height + 'px');
				win_content_h = this.height;
			} else {
				var content_h = jsGetObject("WinContent_" + this.winObjId).offsetHeight;
				win_content_h = this.divs_content.offsetHeight;
			}
			
			if (_jslib_isIE) {
				jsSetStyle(this.iframe_content, "width", (win_content_w-13) + 'px');
				jsSetStyle(this.iframe_content, "height", (win_content_h-13) + 'px');
			}
		} catch(err) {
			jsShowErrors("ModalWindow:__resizeDivs", err);
		}
	}
	,
	__init : function() {
		try {
			this.divs_content.innerHTML = this.__createWindow();
			
			this.__resizeDivs();
			
			if (this.focusObj) __focus_obj = this.focusObj;
			
			if (this.button == 0 || this.button == 2) {
				jsGetObject("B_Accept_" + this.winObjId).focus();
			}
			
			if (this.button == 1) {
				jsGetObject("B_Cancel_" + this.winObjId).focus();
			}
			
			jsSetCenterObj(this.divs_content);
			if (_jslib_isIE) jsSetCenterObj(this.iframe_content);
			
			if (this.sound) {
				var so = new SWFObject(this.flashPath + "sounds.swf", "_MsgSounds_" + this.winObjId, 1, 1, 7);
				so.addParam("menu", "false");
				so.addVariable("s", this.sound);
				so.write("__Sounds__" + this.winObjId);
			}
			
			btrans = new BlendTransition();
			btrans.ObjId = this.divs_shadow;
			btrans.opacStart = 1;
			btrans.opacEnd = 30;
			btrans.milliSec = 500;
			btrans.opacity();
			delete btrans;
		} catch(err) {
			jsShowErrors("ModalWindow:__init", err);
		}
	}
	,
	display : function() {
		try {
			this.__createDivs();
			this.__init();
		} catch(err) {
			jsShowErrors("ModalWindow:display", err);
		}
	}
}


function DeleteWinModal(obj, _obj_focus) {
	try {
		var id = obj;
		var obj_win = jsGetObject("__WinMod__" + id);
		obj = jsGetObject("__WinShadow__" + id);
		btrans = new BlendTransition();
		btrans.ObjId = obj;
		btrans.opacStart = 30;
		btrans.opacEnd = 0;
		btrans.milliSec = 500;
		btrans.onCompletion = function() {
			try {
				if (jsGetParent(obj)) {
					if (_jslib_isIE) {
						if (jsGetObject("__WinIframe__" + id)) {
							if (jsGetParent(obj)) {
								jsGetParent(obj).removeChild(jsGetObject(obj.id.replace("__WinShadow__", "__WinIframe__")));
							}
						}
						if (jsGetObject("__WinIframeShadow__" + id)) {
							if (jsGetParent(obj)) {
								jsGetParent(obj).removeChild(jsGetObject(obj.id.replace("__WinShadow__", "__WinIframeShadow__")));
							}
						}
					}
					
					if (jsGetObject("__Sounds__" + id)) {
						if (jsGetParent(obj)) {
							jsGetParent(obj).removeChild(jsGetObject(obj.id.replace("__WinShadow__", "__Sounds__")));
						}
					}
					if (obj_win) {
						if (jsGetParent(obj_win)) jsGetParent(obj_win).removeChild(obj_win);
					}
					if (obj) {
						if (jsGetParent(obj)) jsGetParent(obj).removeChild(obj);
					}
				}
				if (__focus_obj) {
					if (__focus_obj.type != "hidden") __focus_obj.focus();
				}
			} catch(err) {
				//jsShowErrors("ModalWindow:DeleteWinModal:onCompletion", err);
				return false;
			}
		}
		btrans.opacity();
		delete btrans;
	} catch(err) {
		jsShowErrors("ModalWindow:DeleteWinModal", err);
	}
}


function WinModalButton(_bid, _path, _label, _icon, _codigo, _func_name) {
	try {
		if (!_label) _label = "Aceptar";
		if (!_func_name) _func_name = "";
		_bid += "_" + _codigo;
		
		/*var tags = '';
		tags += '<table border="0" cellspacing="0" cellpadding="0" style="background-image:url(' + _path 
		+ 'bgGray.gif);" class="AdminButtonOut" onMouseOver="this.className=\'AdminButtonOver\'"';
		tags += 'onMouseOut="this.className=\'AdminButtonOut\'" onClick="DeleteWinModal(\'' + _codigo + '\');' + _func_name + '">';
		tags += '<tr style="padding-top:2px;">';
		if (_icon) tags += '<td style="padding-left:5px;">' + DisplayPicture(_icon, 16,16) + '</td>';
		tags += '<td style="padding-left:5px; padding-right:5px;" align="center">' + _label + '</td>';
		tags += '</tr>';
		tags += '</table>';*/
		
		var tags = '';
		tags += '<table border="0" cellspacing="0" cellpadding="0" style="background-image:url(' + _path 
		+ 'bgGray.gif);" class="AdminButtonOut" onMouseOver="this.className=\'AdminButtonOver\'"';
		tags += 'onMouseOut="this.className=\'AdminButtonOut\'">';
		tags += '<tr style="padding-top:2px;">';
		if (_icon) tags += '<td style="padding-left:5px;" onClick="DeleteWinModal(\'' + _codigo + '\');' 
		+ _func_name + '">' + DisplayPicture(_icon, 16,16) + '</td>';
		tags += '<td align="center">';
		tags += '<input type="button" name="' + _bid + '" id="' + _bid + '" value="' + _label 
		+ '" style="background-color:transparent; ';
		tags += 'border:0px; cursor:pointer; width:auto;" onClick="DeleteWinModal(\'' + _codigo + '\');' 
		+ _func_name + '" tabindex="0">';
		tags += '</td>';
		tags += '</tr>';
		tags += '</table>';
		
		return tags;
	} catch(err) {
		jsShowErrors("ModalWindow:WinModalButton", err);
	}
}
