var JBSPopup = {
	DIV_POPUP: 1,
	IFRAME_POPUP: 2,
	
	POSITION_CENTER: 1,
	POSITION_RELATIVE: 2,
	
	IFRAME_POPUP_ID: "_jbs_iframe_popup_",
	DIV_POPUP_ID: "_jbs_div_popup_",
	displayType: 2/*DIV_POPUP*/,
	parseIFrameScript: true,
	iFrameScrollbars: false,
	addMouseOut: true,
	useIFrameBelow: false,	/* If displayType=DIV_POPUP - set this to true to paint an iframe below - necessary if you need to overlay window'd controls */
	positionType: 2, /*POSITION_RELATIVE*/
	top:-1,
	left:-1,
	frameSource: "javascript:void(0)",
	frameborder:1,
	pendingMessage: "Fetching...",
	useAutoHeight: false,
	useAutoWidth: false,
	startHidden: false,
	
	/* call from initial call -*/
	initialize: function(EventElement,Width,Height,Top,Left) {
		this.eventElement = EventElement;
		this.width = Width;
		this.height = Height;
		if (Top)
			this.top = Top;
		else 
			this.top = -1;
		if (Left)
			this.left = Left;
		else 
			this.left = -1;
		this.showPending(this.pendingMessage);
	},
	showPending: function(msg) {
	    
	    
		if (this.top == -1)
			if (this.positionType == JBSPopup.POSITION_RELATIVE) {
			    var ct = document.all ? this.eventElement.clientTop : 0;
				this.top = ct + this.eventElement.clientHeight + this.eventElement.offsetHeight + getElementOffsetY(this.eventElement);
			} else {
				this.top = (getBrowserHeight() / 2) - (this.height/2);
			}
		
		if (this.left == -1)
			if (this.positionType == JBSPopup.POSITION_RELATIVE) {
			    var cl = document.all ? this.eventElement.clientLeft : 0;
				this.left = cl + getElementOffsetX(this.eventElement);
			} else {
				this.left = (getBrowserWidth() / 2) - (this.width/2);	
			}
				
		
		var sWidth = this.width + "px";
		var sHeight = this.height + "px";
		var sDisplay = "";
		if(this.useAutoHeight)
		    sHeight = "auto";
		if(this.useAutoWidth)
		    sWidth = "auto";
		if(this.startHidden)
		    sDisplay="none";
		if (JBSPopup.displayType == JBSPopup.IFRAME_POPUP || this.useIFrameBelow == true) {
			// create an iframe if iframe or using as backdrop
			var scroll = JBSPopup.iFrameScrollbars ? "yes" : "no";
			var iframe = document.getElementById(JBSPopup.IFRAME_POPUP_ID); 
			if (iframe == null){
				iframe = document.createElement("iframe");
				iframe.id = JBSPopup.IFRAME_POPUP_ID;
				iframe.style.display = "none";
				iframe.style.position = "absolute";
				iframe.style.left = "0px";
				iframe.style.top = "0px";
				iframe.setAttribute("src", this.frameSource);
				iframe.setAttribute("scrolling", scroll);
				iframe.setAttribute("frameBorder", this.frameBorder);
				document.body.insertBefore(iframe, null);
			}
			iframe.style.zIndex = 1;
			
			// need to show regardless
			iframe.style.top = this.top;
			iframe.style.left = this.left;
			iframe.style.width = sWidth;
			iframe.style.height = sHeight;
			iframe.style.display=sDisplay;
		}
		
		if(JBSPopup.displayType == JBSPopup.DIV_POPUP) {
			var div = document.getElementById(JBSPopup.DIV_POPUP_ID); 
			if (div == null){
				div = document.createElement("div");
				div.id = JBSPopup.DIV_POPUP_ID;
				div.style.display = "none";
				div.style.position = "absolute";
				div.style.left = "0px";
				div.style.top = "0px";
				
				document.body.insertBefore(div, null);
			}

			div.style.zIndex = 1;
			div.style.top = this.top;
			div.style.left = this.left;
			div.style.height = sHeight;
			div.style.width = sWidth;
			div.innerHTML = msg;
			div.style.display=sDisplay;
			

			if (this.addMouseOut)
				div.onmouseout = function(){ JBSPopup.hide(); };
		} else {
			//iframe.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
			var iframeDiv = "<div id='" + JBSPopup.DIV_POPUP_ID + "'>" + msg + "</div>";
			document.frames[iframe.id].document.write( iframeDiv );

			if (this.addMouseOut)
				iframe.onmouseout = function(){ JBSPopup.hide(); };
		}
	},
	display: function(DisplayHtml){
		
		if(JBSPopup.displayType == JBSPopup.DIV_POPUP) {
			var div = document.getElementById(JBSPopup.DIV_POPUP_ID);
			div.innerHTML = DisplayHtml;
		} else {
			var sOutput = this.parseIFrameScript ? JBSPopup.parseResponse(DisplayHtml) : DisplayHtml;
	    	document.frames[JBSPopup.IFRAME_POPUP_ID].document.body.removeChild(document.frames[JBSPopup.IFRAME_POPUP_ID].document.getElementById(JBSPopup.DIV_POPUP_ID));
	    	document.frames[JBSPopup.IFRAME_POPUP_ID].document.write( sOutput );
		}
	}, // Alternative to Hide - keeps control alive
	setVisibility: function(bVisible) {
	    if (JBSPopup.displayType == JBSPopup.IFRAME_POPUP || this.useIFrameBelow == true) {
	        var iframe = document.getElementById(JBSPopup.IFRAME_POPUP_ID); 
	        bVisible ? iframe.style.display='' : iframe.style.display='none';
	    }
	    if(JBSPopup.displayType == JBSPopup.DIV_POPUP) {
	        var div = document.getElementById(JBSPopup.DIV_POPUP_ID);
	        bVisible ? div.style.display='' : div.style.display='none';
	    }
	},
	move: function(x,y) {
	    this.top = y;
	    this.left = x;
	    if (JBSPopup.displayType == JBSPopup.IFRAME_POPUP || this.useIFrameBelow == true) {
	        var iframe = document.getElementById(JBSPopup.IFRAME_POPUP_ID); 
	        iframe.style.top = this.top;
	        iframe.style.left = this.left;
	    }
	    if(JBSPopup.displayType == JBSPopup.DIV_POPUP) {
	        var div = document.getElementById(JBSPopup.DIV_POPUP_ID);
	        div.style.top = this.top;
			div.style.left = this.left;
	    } 
	},
	moveWithIFrameUnderBuffer: function(x,y,w,h,leftBuffer,rightBuffer,topBuffer,bottomBuffer) { /* when using iframebelow - keeps a specified buffer border*/
	    JBSPopup.move(x,y);
	    if (this.useIFrameBelow == true) {
	        var iframe = document.getElementById(JBSPopup.IFRAME_POPUP_ID); 
	        iframe.style.width=(w-leftBuffer-rightBuffer);
	        iframe.style.height=(h-topBuffer-bottomBuffer);
	        iframe.style.top = y+topBuffer;
	        iframe.style.left=x+leftBuffer;
	    }
	},
	resize: function(width,height) {
	    if (JBSPopup.displayType == JBSPopup.IFRAME_POPUP || this.useIFrameBelow == true) {
	        var iframe = document.getElementById(JBSPopup.IFRAME_POPUP_ID); 
	        iframe.style.width=width;
	        iframe.style.height=height;
	    }
	    if(JBSPopup.displayType == JBSPopup.DIV_POPUP) {
	        var div = document.getElementById(JBSPopup.DIV_POPUP_ID);
	        div.style.width = width;
			div.style.height = height;
	    }
	},
	parseResponse: function(DisplayHtml) {
		// cheaped out here, but easier
		var s = new String(DisplayHtml);
		var re = /javascript:parent./gi;
		var sClean = s.replace(re, "javascript:");
		// then one more
		re = /javascript:/gi;
		return sClean.replace(re, "javascript:parent.");
	},
	hide: function() {
		var element = document.getElementById(JBSPopup.IFRAME_POPUP_ID);
		if (element)
			document.body.removeChild( document.getElementById(JBSPopup.IFRAME_POPUP_ID) );
		element = document.getElementById(JBSPopup.DIV_POPUP_ID);
		if (element)
			document.body.removeChild( document.getElementById(JBSPopup.DIV_POPUP_ID) );
	}
	
}
function getElementOffsetY(element) {
	var totalOffset = 0;
	if (element.offsetTop != null) {
        totalOffset += element.offsetTop;
		while (element.offsetParent) {
			totalOffset += element.offsetParent.offsetTop;
			element = element.offsetParent;
		}
	}
	return totalOffset;
}
function getElementOffsetX(element) {
	var totalOffset = 0;
	if (element.offsetLeft != null) {
        totalOffset += element.offsetLeft;
		while (element.offsetParent) {
			totalOffset += element.offsetParent.offsetLeft;
			element = element.offsetParent;
		}
	}
	return totalOffset;
}
function getBrowserHeight(){

  if( typeof( window.innerWidth ) == 'number' ) {
    return window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    return document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    return document.body.clientHeight;
  }

}

function getBrowserWidth(){
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    return window.innerWidth;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    return document.body.clientWidth;
  }
}
