// ==UserScript==
// @name showModalDialog
// @author Leonardo Javier Alassia
// @version 1.1
// @description  Emulate a modal window or just open a new page.
// @ujs:download http://www.leoalassia.com.ar/userjs/showModal.js
// ==/UserScript==

/***************************************************
* Version 1.1 - August 12, 2008
*	- Fix some problems with Flash objects
*
* Version 1.0 - August 07, 2008
*	- First release
****************************************************/

//defino la funcion ya que no existe
function showModalDialog() {;}

//la redefino como magica para override todas
window.opera.defineMagicFunction("showModalDialog",
	function (oReal, oThis, oParam1, oParam2, oParam3) {
		//paso los parametros a otra forma
		newParams = oParam3.replace(/dialog/gi, " ");
		newParams = newParams.replace(/:/g, "=");
		newParams = newParams.replace(/;/g, ",");
		
		//ventana de confirmacion
		sMD_prompt = confirm("Opera is not compible with Modal Windows.\nclic 'OK' to emulate it or 'Cancel' for new window");
		if (sMD_prompt) {
			//el tamaņo de la ventana modal
			sMD_width = screen.availWidth-100;
			sMD_height = screen.availHeight-200;
			
/*			list = newParams.split(",");
			for ( var i in list ) {
				list[i] = list[i].replace(/px/, "");
				if(list[i].toLowerCase().indexOf("width")>=0) sMD_width = list[i].substr(7);
				if(list[i].toLowerCase().indexOf("height")>=0) sMD_height = list[i].substr(8);
			}*/
			
			//si ya estoy, solo cambio el src del iframe y muestro la ventana
			if(typeof sMD_element != 'undefined') {
				sMD_frame.setAttribute("src", oParam1);
				sMD_element.style.display = 'block';
				return true;
			}
			
			//recorro todos los tags <object>
			sMD_objectTags = document.getElementsByTagName("object");
			for(j=0;j<sMD_objectTags.length;j++) {
				//si tienen un tag <embed> le agrego el wmode=transparent
				sMD_flashEmbed = sMD_objectTags[j].getElementsByTagName("embed");
				sMD_flashEmbed[0].setAttribute("wmode","transparent");
	
				//ahora recorro los tag <param>
				var sMD_tieneWmode = false;
				sMD_paramTags = sMD_objectTags[j].getElementsByTagName('param');
				for (i=0;i<sMD_paramTags.length;i++) {
					//si ya tiene un name=wmode lo seteo o cambio a transparent
					if (sMD_paramTags[i].getAttribute("name").toLowerCase()=="wmode") {
						sMD_paramTags[i].setAttribute("name","wmode");
						sMD_paramTags[i].setAttribute("value","transparent");
						sMD_tieneWmode = true;
					}
				}
				//si no tiene name=wmode lo agrego como transparent
				if (!sMD_tieneWmode) {
					sMD_flashParam = document.createElement('param');
					sMD_flashParam.setAttribute("name", "wmode");
					sMD_flashParam.setAttribute("value", "transparent");		
					sMD_objectTags[j].insertBefore(sMD_flashParam,sMD_flashEmbed[0]);
				}
			}
			//esto sirve para refrescar el flash, es como borrarlo y volverlo a poner
			if (sMD_objectTags.length>0) {
				anterior = sMD_objectTags[0].parentNode;
				anterior.innerHTML = anterior.innerHTML;
			}
			
			//el codigo que le agrego
			//este es contenedor global que ocupa toda la pantalla con fondo negro
			sMD_element = document.createElement('div');
			sMD_element.style = "position: fixed; top:0; left: 0; padding:0; margin:0; width: 100%; height: 100%; min-height:100%; background:#333333; z-index:2147483647";
			sMD_element.setAttribute("align", "center");
			sMD_element.setAttribute("id", "showModalDialogV1");
			
			//el contenedor que tiene las medidas especificadas
			sMD_container = document.createElement('div');
			sMD_container.style = "padding:0;margin:0;margin-top:10px;width:"+sMD_width+"px;height:"+sMD_height+"px;background:#FFFFFF;";
			sMD_element.appendChild(sMD_container);
	
			//el titulo de la ventana
			sMD_top = document.createElement('div');
			sMD_top.style = "padding:2;margin:0;width:100%;height:14px;background:#34586E;color:#FFFFFF;font-family:Arial;font-size:11px;border-top: 1px solid #5E8399;border-left:1px solid #668BA1;border-right:1px solid #162E3D;border-bottom:1px solid #162E3D;cursor:pointer;";
			sMD_top.setAttribute("align", "center");
			sMD_top.setAttribute("onclick", "javascript:document.getElementById('showModalDialogV1').style.display='none';");
			sMD_top.innerHTML = "Opera Modal Window v1.1 - Click here to close it";
			
			//el iframe
			sMD_frame = document.createElement('iframe');
			sMD_frame.setAttribute("src", oParam1);
			sMD_frame.setAttribute("width", sMD_width);
			sMD_frame.setAttribute("height", sMD_height);
			sMD_frame.setAttribute("scrolling", "auto");

			sMD_container.appendChild(sMD_top);
			sMD_container.appendChild(sMD_frame);
			
			document.body.appendChild(sMD_element);
			return false;
		} else {
			return window.open(oParam1,oParam2,newParams);
		}
	}
);
