//nice way to use the same INFX namespace over multiple scripts.
if (typeof(_infx) == "undefined") var _infx = {};

// InFX Popup library
// Chris Merry 2008
_infx.popups = function (params){
	
	//DEFAULTS
	this.bgcolor = '#aaaaaa';
	
	for(var p in params){	//ADD ON PARAMS TO OBJECT
		this[p] = params[p];
	}
	
	this.maskerobj = null;
	this.frame = null;
	this.dialogue = null;
	
	//Page size properties
	this.y_with_scroll = null;
	this.x_with_scroll = null;
	
	this.rtnEl = function(id){
		return document.getElementById(id);
	}

	this.create_masker = function (){
		//alert('CREATE MASKER');
		this.maskerobj = this.rtnEl('masker');  
		if(!this.maskerobj){
			this.maskerobj = document.createElement("div");
			this.maskerobj.setAttribute("id", "masker");
			this.maskerobj.style.backgroundColor = this.bgcolor;
			if(this.frame){ this.frame.appendChild(this.maskerobj); }else{ document.body.appendChild(this.maskerobj); }
		}
	}
	
	this.create_popup = function (){
		this.frame = this.rtnEl('popup_frame');
		this.dialogue = this.rtnEl('popup_dialog');
		//alert("FRAME: "+this.frame+" DIALOG: "+this.dialogue);
		if(!this.frame){										//If there is no popup frame make one
			//alert("CREATE FRAME");
			this.frame = document.createElement("div");
			this.frame.setAttribute("id", "popup_frame");
			document.body.appendChild(this.frame);
		}
		if(!this.dialogue){									//If there is no popup dialogue make one							//If there is no popup frame make one
			//alert("CREATE DIALOG");
			this.dialogue = document.createElement("span");
			this.dialogue.setAttribute("id", "popup_dialog");
			this.frame.appendChild(this.dialogue);
			//document.body.appendChild(this.dialogue);
		}
	}
	
	this.switch_masker = function (onoff){
		if(this.maskerobj == null){
			this.create_masker();
		}
		if(this.maskerobj){
			this.setOpacity(this.maskerobj, 70);
			this.maskerobj.style.visibility = (onoff == 1)?'visible':'hidden';
			this.getPageSizeWithScroll();
			if (document.all){ // IE
				this.maskerobj.style.height = this.y_with_scroll; // IE
				this.maskerobj.style.width = this.x_with_scroll; // IE
			}else{
				this.maskerobj.style.height = (this.y_with_scroll)+'px'; // FF
				this.maskerobj.style.width = (this.x_with_scroll-((this.y_max_scroll > 0)?0:15))+'px'; // FF
			}
		}
	}
	
	this.switch_popup = function (onoff,inner_html){
		if(this.frame == null || this.dialogue == null){
			this.create_popup();
		}
		if(this.frame && this.dialogue){
			//alert("ONOFF: "+onoff+" SHOW POPUP FRAME");
			this.frame.style.visibility = (onoff == 1)?'visible':'hidden';
			this.dialogue.innerHTML = inner_html;
			this.popup_form = this.rtnEl('popup_form');
			//this.dialogue
			if(inner_html != '' && this.popup_form){
				if (document.all){ // IE
					this.page_offset = document.documentElement.scrollTop || (document.body.scrollTop || 0);
					this.window_height = document.documentElement.clientHeight; 
					this.window_width = document.documentElement.clientWidth;
				}else{ // FF
					this.page_offset = window.pageYOffset || (document.body.scrollTop || 0);
					this.window_height = window.innerHeight; 
					this.window_width = window.innerWidth; 
			    }
			    
				this.dialogue_height = this.popup_form.clientHeight;
				this.dialogue_width = this.popup_form.clientWidth;
			    
			    var top = parseInt((this.window_height/2) - (this.dialogue_height/2)) + this.page_offset;
		    	var left = parseInt((this.window_width/2) - (this.dialogue_width/2));

			    //alert("po: "+this.page_offset+" wh: "+this.window_height+" ww: "+this.window_width+" dh: "+this.dialogue_height+" dw: "+this.dialogue_width+" top: "+top+" left: "+left);		
				if (document.all){ // IE
					this.popup_form.style.pixelTop = top; // IE
					this.popup_form.style.pixelLeft = left; // IE
					//alert("IE: width=" + this.dialogue_width + " height=" + this.dialogue_height + " left=" + this.dialogue.style.pixelLeft + " top=" + this.dialogue.style.pixelTop);
				}else{
					this.popup_form.style.top = top + 'px'; // FF
					this.popup_form.style.left = left + 'px'; // FF
					//alert("FF: width=" + this.dialogue_width + " height=" + this.dialogue_height + " left=" + this.dialogue.style.left + " top=" + this.dialogue.style.top);
				}
			}
		}
	}
	
	this.getPageSizeWithScroll = function(){
		//if(this.y_with_scroll == null || this.x_with_scroll == null){
			this.x_max_scroll = 0;
			if (window.innerHeight) {							// Firefox
				this.y_with_scroll = window.innerHeight + window.scrollMaxY;
				this.x_with_scroll = window.innerWidth + window.scrollMaxX;
				this.x_max_scroll = window.scrollMaxX;
				//alert("window.innerHeight: "+window.innerHeight+" window.scrollMaxY: "+window.scrollMaxY);
			} else if (document.body.scrollHeight > document.body.offsetHeight){	// all but Explorer Mac
				this.y_with_scroll = document.body.scrollHeight;
				this.x_with_scroll = document.body.scrollWidth;
			} else { 																// works in Explorer 6 Strict, Mozilla (not FF) and Safari
				this.y_with_scroll = document.body.offsetHeight;
				this.x_with_scroll = document.body.offsetWidth;
			}
			//alert("window height with scroll: "+this.y_with_scroll+" window width with scroll: "+this.x_with_scroll);
		//}
	}

	this.setOpacity = function(obj, opacity) {
		opacity = (opacity == 100)?99.999:opacity;
		// IE/Win
		obj.style.filter = "alpha(opacity:"+opacity+")";
		// Safari<1.2, Konqueror
		obj.style.KHTMLOpacity = opacity/100;
		// Older Mozilla and Firefox
		obj.style.MozOpacity = opacity/100;
		// Safari 1.2, newer Firefox and Mozilla, CSS3
		obj.style.opacity = opacity/100;
	}
	
	this.fadeUp = function(obj_id,opacity,limit,fade_speed) {
		if(!limit) limit = 100;
		var obj = this.rtnEl(obj_id);
		if(obj){
			if (opacity <= limit) {
				this.setOpacity(obj, opacity);
				opacity += 2.5;
				window.setTimeout("_infx.popup.fadeUp('"+obj_id+"',"+opacity+","+limit+","+fade_speed+");", fade_speed);
			}
		}
	}
		
	this.fadeDown = function(obj_id,opacity,limit,fade_speed) {
		var obj = this.rtnEl(obj_id);
		if(obj){
			this.setOpacity(obj, opacity);
			if (opacity >= limit) {
				opacity -= 2.5;
				window.setTimeout("_infx.popup.fadeDown('"+obj_id+"',"+opacity+","+limit+","+fade_speed+");", fade_speed);
			}
		}
	}

	this.masker = function (onoff) {
		this.switch_masker(onoff);
    }
    
	this.layer = function (onoff,inner_html) {
		//alert("_infx.popup layer '"+inner_html+"'");
		this.switch_popup(onoff,inner_html);
		this.switch_masker(onoff);
    }
    
    this.loading = function (onoff){
		this.switch_masker(onoff);
		this.maskerobj.innerHTML = (onoff)?"<div id='loading_frame' style=''><img src='/images/ajax-loader.gif' style='visibility:visible;'>&nbsp;</div>":"";
    }
}

_infx.popup = new _infx.popups();