//	PRAGMA S.A.
// Por José Manuel Conde Campo (jconde@pragma.com.co)
// 
// .transparent {
//	  background-color: #000000;
//	  filter:Alpha(Opacity=65);
//	  -moz-opacity:0.65;
//  } 
// <div id="blackout" class="transparent" style="display:none;z-index:999"></div>
// <div id="elQueVaEncima" class=""cualquiera" style="display:none;z-index:1000">
//    ...
// </div>
// 
// Uso:
// <script>
//    switchModal('elQueVaEncima');
// </script>



function modalPopup(col,opac){
	var that = this;
	this.blackoutDiv;
	this.overDiv;
	this.height;
	this.width;
	this.totalHeight;
	this.totalWidth;
	this.h;
	this.w;
	this.msgDiv;
	this.waitDiv;
	this.divTexto;
	this.divBoton;
	this.waitItem;
	this.position;
	this.showingBlackout;
	/*if(opac){this.opacity=opac;}else{this.opacity=50;}
	if(col){this.color=col;}else{this.color="#000";}*/
		
	this.opacity = opac?opac:50;
	this.color = col?col:"#000";
	document.write("<style>.jc_modal_transparent {background-color: "+this.color+";filter:Alpha(Opacity="+this.opacity+");-moz-opacity:0."+this.opacity+";-khtml-opacity: 0."+this.opacity+";opacity: 0."+this.opacity+";}");
	document.write(".debug{background-color: #FFF;border:3px solid #333}</style>");
	
	this.crearBlackoutDiv = function(){
		this.blackoutDiv = document.createElement("DIV");
		this.blackoutDiv.id="blackoutDiv";
		this.blackoutDiv.className = "jc_modal_transparent";
		this.blackoutDiv.style.display="none";
		this.blackoutDiv.style.zIndex=1000;
		this.blackoutDiv.style.position="absolute";
		this.blackoutDiv.style.top=0+'px';
		this.blackoutDiv.style.left=0+'px';
		this.waitDiv= document.createElement("DIV");
		this.waitDiv.id="waitDiv";
		this.waitDiv.style.display="none";
		this.waitDiv.style.zIndex=1001;
		this.waitDiv.style.position="absolute";
		this.waitDiv.style.height = "2px";
		this.waitDiv.style.width = "2px";
		this.blackoutDiv.style.left=0+'px';
		this.msgDiv= document.createElement("DIV");
		
		document.body.appendChild(this.blackoutDiv);
		document.body.appendChild(this.waitDiv);
		document.body.appendChild(this.msgDiv);
		if(window.addEventListener){
			window.addEventListener('resize',this.resize,false);
			window.addEventListener('scroll',this.scroll,false);
		}else{
			window.attachEvent('onresize',this.resize);
			window.attachEvent('onscroll',this.scroll);
		}
		
		//MsgDiv
		this.msgDiv.id="msgDiv";
		this.msgDiv.style.display="none";
		this.msgDiv.style.zIndex=1001;
		this.msgDiv.style.position="absolute";
		this.msgDiv.style.width="350px";
		this.msgDiv.style.height="110px";
		this.msgDiv.style.textAlign = "center";
		this.divTexto = document.createElement("DIV");
		this.divTexto.id="txtMsgDiv";
		this.divTexto.style.height="80px";		
		this.divTexto.style.textAlign = "left";
		this.msgDiv.appendChild(this.divTexto);
		this.divBoton = document.createElement("DIV");
		this.msgDiv.appendChild(this.divBoton);
		this.divBoton.id="divBoton";
		document.body.appendChild(this.msgDiv);
	}
	
	
	this.calcular = function(){
		this.height = Screen.getViewportHeight();
		this.width = Screen.getViewportWidth();
		this.totalHeight = Screen.getDocumentHeight();
		this.totalWidth = Screen.getDocumentWidth();
		// Posicion de la ventana a mostrar
		this.h = parseInt(((this.height-parseInt(this.overDiv.style.height,10))/2)+parseInt(Screen.getScrollTop(),10),10);
		this.w = parseInt((parseInt(this.width,10)-parseInt(this.overDiv.style.width,10))/2,10)+Screen.getScrollLeft();
	}
	
	this.resize = function(){
		if(that.overDiv!=null){
			that.initDivs();
		}
	}
	
	this.scroll = function(){
		if(that.overDiv!=null){
			that.initDivs();
		}
	}
	
	
	
	
	this.initDivs = function(){
		this.calcular();
		//Blackout
		if(this.showingBlackout){
			this.blackoutDiv.style.width=this.totalWidth+'px';
			this.blackoutDiv.style.height=this.totalHeight+'px';
		}
		
		//OverDiv
		this.overDiv.style.position="absolute";
		this.overDiv.style.zIndex=1001;
		
		if(!this.position || this.position=="centered"){
			this.overDiv.style.top=this.h+'px';
			this.overDiv.style.left=this.w+'px';
		}else if(this.position=="top-left"){
			this.overDiv.style.top=parseInt(Screen.getScrollTop()+5,10)+'px';
			this.overDiv.style.left=parseInt(Screen.getScrollLeft()+5,10)+'px';
		}else if(this.position=="top-right"){
			this.overDiv.style.top=parseInt(Screen.getScrollTop()+5,10)+'px';
			this.overDiv.style.left=parseInt(Screen.getScrollLeft()+this.width-parseInt(this.waitDiv.style.width,10)-5,10)+'px';
		}else if(this.position=="top-center"){
			this.overDiv.style.top=parseInt(Screen.getScrollTop()+5,10)+'px';
			this.overDiv.style.left=this.w+'px';
		}else if(this.position=="bottom-left"){
			this.overDiv.style.top=parseInt(Screen.getScrollTop()+this.height-5-parseInt(this.waitDiv.style.height,10),10)+'px';
			this.overDiv.style.left=parseInt(Screen.getScrollLeft()+5,10)+'px';
		}else if(this.position=="bottom-center"){
			this.overDiv.style.top=parseInt(Screen.getScrollTop()+this.height-5-parseInt(this.waitDiv.style.height,10),10)+'px';
			this.overDiv.style.left=this.w+'px';
		}else if(this.position=="bottom-right"){
			this.overDiv.style.top=parseInt(Screen.getScrollTop()+this.height-5-parseInt(this.waitDiv.style.height,10),10)+'px';
			this.overDiv.style.left=parseInt(Screen.getScrollLeft()+this.width-parseInt(this.waitDiv.style.width,10)-5,10)+'px';
		}else if(this.position=="middle-left"){
			this.overDiv.style.top=this.h+'px';
			this.overDiv.style.left=parseInt(Screen.getScrollLeft()+5,10)+'px';
		}else if(this.position=="middle-right"){
			this.overDiv.style.top=this.h+'px';
			this.overDiv.style.left=parseInt(Screen.getScrollLeft()+this.width-parseInt(this.waitDiv.style.width,10)-5,10)+'px';
		}else if(this.position.indexOf("custom:")!=-1){
			var w = this.position.substring(this.position.indexOf(":")+1,this.position.indexOf(","));
			var h = this.position.substring(this.position.indexOf(",")+1);
			this.overDiv.style.top=h+'px';
			this.overDiv.style.left=w+'px';
		}
	}
	
	this.switchModal = function(id){
		this.switchControls();
		if(this.blackoutDiv.style.display=="none"){
			this.showModal(id);
		}else{
			this.hideModal(id)
		}
	}
	
	this.hideModal = function(id){
		this.showControls();
		this.overDiv = document.getElementById(id);
		this.blackoutDiv.style.display="none";
		this.overDiv.style.display="none";
		this.overDiv=null;
		this.position = null;
	}
	
	this.showModal = function(id,blackIt){
		this.hideControls();
		this.showingBlackout = true;
		if(this.overDiv!=null){
			this.overDiv.style.display="none";
			this.position = null;
		}else{
			if(!blackIt){
				this.showingBlackout = false;
				this.blackoutDiv.style.display="block";
			}
		}
		this.overDiv = document.getElementById(id);
		this.initDivs();
		this.overDiv.style.display="block";
	}
	
	this.switchControls = function (){
		var iframes = document.getElementsByTagName("iframe");
		for(var i=0;i<iframes.length;i++){
			if(iframes[i].style.display=="none"){
				iframes[i].style.display="block";
			}else{
				iframes[i].style.zIndex = this.blackoutDiv.style.zIndex-1;
				iframes[i].style.display="none";
			}
		}
		var obj = document.getElementsByTagName("object");
		for(var i=0;i<obj.length;i++){
			if(obj[i].style.display=="none"){
				obj[i].style.display = "block";
			}else{
				obj[i].style.display = "none";
			}
		}
		var selects = document.getElementsByTagName("select");
		for(var i=0;i<selects.length;i++){
			if(selects[i].style.display=="none"){
				selects[i].style.display="block";
			}else{
				selects[i].style.display="none";
			}
		}
	}
	
	this.showControls = function(){
		/*var iframes = document.getElementsByTagName("iframe");
		for(var i=0;i<iframes.length;i++){
			iframes[i].style.display="block";
		}
		var obj = document.getElementsByTagName("object");
		for(var i=0;i<obj.length;i++){
			obj[i].style.display = "block";
		}*/
		var selects = document.getElementsByTagName("select");
		for(var i=0;i<selects.length;i++){
			selects[i].style.display="block";
		}
	}
	this.hideControls = function(){
		var iframes = document.getElementsByTagName("iframe");
		/*for(var i=0;i<iframes.length;i++){
			iframes[i].style.zIndex = this.blackoutDiv.style.zIndex-1;
			iframes[i].style.display="none";
		}
		var obj = document.getElementsByTagName("object");
		for(var i=0;i<obj.length;i++){
			obj[i].style.display = "none";
		}*/
		var selects = document.getElementsByTagName("select");
		for(var i=0;i<selects.length;i++){
			selects[i].style.display="none";
		}
	}
	
	this.crearMsgDiv = function(claseDiv,claseTexto,claseBoton,valorBoton){
		if(claseDiv) this.msgDiv.className = claseDiv;
		if(claseTexto) this.divTexto.className = claseTexto;
		if(!valorBoton){
			valorBoton = "Aceptar";
		}
		this.divBoton.innerHTML = "<input type='button' class='"+claseBoton+"' value='"+valorBoton+"' onclick='modal.hideMsg()' />";
	}
	
	this.showMsg = function(msg){
		if(this.msgDiv!=null){
			document.getElementById("txtMsgDiv").innerHTML=msg;
			this.showModal("msgDiv");
		}
	}
	
	this.hideMsg = function(){
		if(this.msgDiv!=null){
			document.getElementById("txtMsgDiv").innerHTML="";
			this.hideModal("msgDiv");
		}
	}
	
	this.wait = function(){
		if(this.waitItem){
			this.waitDiv.appendChild(this.waitItem);
		}
		this.position = this.waitDiv.posicion;
		this.showModal("waitDiv");
	}
	
	this.hideWait = function(){
		if(this.waitItem){
			this.waitDiv.removeChild(this.waitItem);
		}
		this.hideModal("waitDiv");
	}
	
	this.addWaitImage = function (url,color){
		if(color)
			this.waitDiv.style.backgroundColor = color;
		this.waitItem = new Image();
		this.waitItem.src = url;
		this.waitItem.border=0;
		this.waitItem.style.visibility = "hidden";
		this.waitItem.style.display= "none";
		this.waitDiv.style.width = (this.waitItem.width+20)+"px";
		this.waitDiv.style.height = (this.waitItem.height+20)+"px";
		this.waitDiv.style.textAlign= "center";
		this.waitDiv.style.verticalAlign = "middle";
		this.waitDiv.appendChild(this.waitItem);
	}
	
	this.addWaitText = function (text,color,background,border,width,height,font,pos){
		if(color)
			this.waitDiv.style.color = color;
		if(background)
			this.waitDiv.style.backgroundColor = background;
		if(!pos)
			pos = "centered";
		this.waitDiv.posicion = pos;
		this.waitDiv.style.border = border;
		this.waitDiv.style.font = font;
		this.waitDiv.style.width = width+"px";
		this.waitDiv.style.height = height+"px";
		this.waitItem = document.createElement("SPAN");
		this.waitItem.innerHTML=text;
	}
	
	this.crearBlackoutDiv();
}

function defined(o) {
	return (typeof(o)!="undefined");
}

function zero(n) {
	return (!defined(n) || isNaN(n))?0:n;
}

/* ******************************************************************* */
/*   CSS FUNCTIONS                                                     */
/* ******************************************************************* */
var CSS = (function(){
	var css = {};

	// Convert an RGB string in the form "rgb (255, 255, 255)" to "#ffffff"
	css.rgb2hex = function(rgbString) {
		if (typeof(rgbString)!="string" || !defined(rgbString.match)) { return null; }
		var result = rgbString.match(/^\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*/);
		if (result==null) { return rgbString; }
		var rgb = +result[1] << 16 | +result[2] << 8 | +result[3];
		var hex = "";
		var digits = "0123456789abcdef";
		while(rgb!=0) { 
			hex = digits.charAt(rgb&0xf)+hex; 
			rgb>>>=4; 
		} 
		while(hex.length<6) { hex='0'+hex; }
		return "#" + hex;
	};

	// Convert hyphen style names like border-width to camel case like borderWidth
	css.hyphen2camel = function(property) {
		if (!defined(property) || property==null) { return null; }
		if (property.indexOf("-")<0) { return property; }
		var str = "";
		var c = null;
		var l = property.length;
		for (var i=0; i<l; i++) {
			c = property.charAt(i);
			str += (c!="-")?c:property.charAt(++i).toUpperCase();
		}
		return str;
	};
	
	// Determine if an object or class string contains a given class.
	css.hasClass = function(obj,className) {
		if (!defined(obj) || obj==null || !RegExp) { return false; }
		var re = new RegExp("(^|\\s)" + className + "(\\s|$)");
		if (typeof(obj)=="string") {
			return re.test(obj);
		}
		else if (typeof(obj)=="object" && obj.className) {
			return re.test(obj.className);
		}
		return false;
	};
	
	// Add a class to an object
	css.addClass = function(obj,className) {
		if (typeof(obj)!="object" || obj==null || !defined(obj.className)) { return false; }
		if (obj.className==null || obj.className=='') { 
			obj.className = className; 
			return true; 
		}
		if (css.hasClass(obj,className)) { return true; }
		obj.className = obj.className + " " + className;
		return true;
	};
	
	// Remove a class from an object
	css.removeClass = function(obj,className) {
		if (typeof(obj)!="object" || obj==null || !defined(obj.className) || obj.className==null) { return false; }
		if (!css.hasClass(obj,className)) { return false; }
		var re = new RegExp("(^|\\s+)" + className + "(\\s+|$)");
		obj.className = obj.className.replace(re,' ');
		return true;
	};
	
	// Fully replace a class with a new one
	css.replaceClass = function(obj,className,newClassName) {
		if (typeof(obj)!="object" || obj==null || !defined(obj.className) || obj.className==null) { return false; }
		css.removeClass(obj,className);
		css.addClass(obj,newClassName);
		return true;
	};
	
	// Get the currently-applied style of an object
	css.getStyle = function(o, property) {
		if (o==null) { return null; }
		var val = null;
		var camelProperty = css.hyphen2camel(property);
		// Handle "float" property as a special case
		if (property=="float") {
			val = css.getStyle(o,"cssFloat");
			if (val==null) { 
				val = css.getStyle(o,"styleFloat"); 
			}
		}
		else if (o.currentStyle && defined(o.currentStyle[camelProperty])) {
			val = o.currentStyle[camelProperty];
		}
		else if (window.getComputedStyle) {
			val = window.getComputedStyle(o,null).getPropertyValue(property);
		}
		else if (o.style && defined(o.style[camelProperty])) {
			val = o.style[camelProperty];
		}
		// For color values, make the value consistent across browsers
		// Convert rgb() colors back to hex for consistency
		if (/^\s*rgb\s*\(/.test(val)) {
			val = css.rgb2hex(val);
		}
		// Lowercase all #hex values
		if (/^#/.test(val)) {
			val = val.toLowerCase();
		}
		return val;
	};
	css.get = css.getStyle;

	// Set a style on an object
	css.setStyle = function(o, property, value) {
		if (o==null || !defined(o.style) || !defined(property) || property==null || !defined(value)) { return false; }
		if (property=="float") {
			o.style["cssFloat"] = value;
			o.style["styleFloat"] = value;
		}
		else if (property=="opacity") {
			o.style['-moz-opacity'] = value;
			o.style['-khtml-opacity'] = value;
			o.style.opacity = value;
			if (defined(o.style.filter)) {
				o.style.filter = "alpha(opacity=" + value*100 + ")";
			}
		}
		else {
			o.style[css.hyphen2camel(property)] = value;
		}
		return true;
	};
	css.set = css.setStyle;
	
	// Get a unique ID which doesn't already exist on the page
	css.uniqueIdNumber=1000;
	css.createId = function(o) {
		if (defined(o) && o!=null && defined(o.id) && o.id!=null && o.id!="") { 
			return o.id;
		}
		var id = null;
		while (id==null || document.getElementById(id)!=null) {
			id = "ID_"+(css.uniqueIdNumber++);
		}
		if (defined(o) && o!=null && (!defined(o.id)||o.id=="")) {
			o.id = id;
		}
		return id;
	};
	
	return css;
})();

/* ******************************************************************* */
/*   SCREEN FUNCTIONS                                                  */
/* ******************************************************************* */
var Screen = (function() {
	var screen = {};

	// Get a reference to the body
	// --------------------------------------------------------------------
	screen.getBody = function() {
		if (document.body) {
			return document.body;
		}
		if (document.getElementsByTagName) {
			var bodies = document.getElementsByTagName("BODY");
			if (bodies!=null && bodies.length>0) {
				return bodies[0];
			}
		}
		return null;
	};

	// Get the amount that the main document has scrolled from top
	// --------------------------------------------------------------------
	screen.getScrollTop = function() {
		if (document.documentElement && defined(document.documentElement.scrollTop) && document.documentElement.scrollTop>0) {
			return document.documentElement.scrollTop;
		}
		if (document.body && defined(document.body.scrollTop)) {
			return document.body.scrollTop;
		}
		return null;
	};
	
	// Get the amount that the main document has scrolled from left
	// --------------------------------------------------------------------
	screen.getScrollLeft = function() {
		if (document.documentElement && defined(document.documentElement.scrollLeft) && document.documentElement.scrollLeft>0) {
			return document.documentElement.scrollLeft;
		}
		if (document.body && defined(document.body.scrollLeft)) {
			return document.body.scrollLeft;
		}
		return null;
	};
	
	// Util function to default a bad number to 0
	// --------------------------------------------------------------------
	screen.zero = function(n) {
		return (!defined(n) || isNaN(n))?0:n;
	};

	// Get the width of the entire document
	// --------------------------------------------------------------------
	screen.getDocumentWidth = function() {
		var width = 0;
		var body = screen.getBody();
		if (document.documentElement && (!document.compatMode || document.compatMode=="CSS1Compat")) {
		    var rightMargin = parseInt(CSS.get(body,'marginRight'),10) || 0;
		    var leftMargin = parseInt(CSS.get(body,'marginLeft'), 10) || 0;
			width = Math.max(body.offsetWidth + leftMargin + rightMargin, document.documentElement.clientWidth);
		}
		else {
			width =  Math.max(body.clientWidth, body.scrollWidth);
		}
		if (isNaN(width) || width==0) {
			width = screen.zero(self.innerWidth);
		}
		return width;
	};
	
	// Get the height of the entire document
	// --------------------------------------------------------------------
	screen.getDocumentHeight = function() {
		var body = screen.getBody();
		var innerHeight = (defined(self.innerHeight)&&!isNaN(self.innerHeight))?self.innerHeight:0;
		if (document.documentElement && (!document.compatMode || document.compatMode=="CSS1Compat")) {
		    var topMargin = parseInt(CSS.get(body,'marginTop'),10) || 0;
		    var bottomMargin = parseInt(CSS.get(body,'marginBottom'), 10) || 0;
			return Math.max(body.offsetHeight + topMargin + bottomMargin, document.documentElement.clientHeight, document.documentElement.scrollHeight, screen.zero(self.innerHeight));
		}
		return Math.max(body.scrollHeight, body.clientHeight, screen.zero(self.innerHeight));
	};
	
	// Get the width of the viewport (viewable area) in the browser window
	// --------------------------------------------------------------------
	screen.getViewportWidth = function() {
		if (document.documentElement && (!document.compatMode || document.compatMode=="CSS1Compat")) {
			return document.documentElement.clientWidth;
		}
		else if (document.compatMode && document.body) {
			return document.body.clientWidth;
		}
		return screen.zero(self.innerWidth);
	};
	
	// Get the height of the viewport (viewable area) in the browser window
	// --------------------------------------------------------------------
	screen.getViewportHeight = function() {
		if (!window.opera && document.documentElement && (!document.compatMode || document.compatMode=="CSS1Compat")) {
			return document.documentElement.clientHeight;
		}
		else if (document.compatMode && !window.opera && document.body) {
			return document.body.clientHeight;
		}
		return screen.zero(self.innerHeight);
	};

	return screen;
})();