var toggleModal = function(objName,backgroundColour, options) {
    // modal view for the whole screen
	if($("modal")) {
        $("modal").dispose();
        return false;
    }
	
    var options = $merge({
        zIndex: 10000000,
        opacity: .8
    }, options);

    if(!$type(backgroundColour) && !$("modal"))
        return false;
    
    var contenedor = new Element("div", {
        id: "modal",
        styles: {
            position: "absolute",
            top: 0,
            left: 0,
            width: window.getScrollWidth(),
            height: window.getScrollHeight(),
            "z-index": options.zIndex
        },
        events: {
        	click: function() {
	            $("modal").fade('out');
	            (function() { toggleModal(); }).delay(500);
        	}
	    }
    }).inject(document.body);
    
    var contenido= new Element("div", {
        id: "modal_content",
        styles: {
            position: "absolute",
            top: 0,
            left: 0,
            width: window.getScrollWidth(),
            height: window.getScrollHeight(),
            "z-index": options.zIndex + 2,
            "text-align": "center"
        }
    }).inject(contenedor);
    
    var btn_cerrar = new Element("div", {
    	id: "modal_btn_cerrar",
    	text: "Cerrar [X]",
    	events: {
        	click: function() {
	            //$("modal").fade('out');
				$("modal").dispose();

	            (function() { toggleModal(); }).delay(500);
        	}
	    }
    });
    
    contenido.adopt(btn_cerrar);
    
    contenido.adopt($(objName));
    
    new Element("div", {
        id: "modal_overlay",
        styles: {
            position: "absolute",
            top: 0,
            left: 0,
            width: window.getScrollWidth(),
            height: window.getScrollHeight(),
            background: backgroundColour,
            "z-index": options.zIndex + 1
        },
        opacity: options.opacity
    }).inject(contenedor);

    return contenedor;
}
