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: 1, /*POSITION_RELATIVE*/
	top:-1,
	left:-1,
	frameSource: "javascript:void(0)",
	frameborder:1,
	divToUse:null,
	handleScript:false,
	/* call from initial call -*/
	initialize: function(EventElement,Width,Height,Top,Left) {
	    try{
	        this.hide();
	    }
	    catch(e) {}
		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("");
		
	},
	showPending: function(msg) {
		if (this.top == -1)
			if (this.positionType == JBSPopup.POSITION_RELATIVE)
				this.top = this.eventElement.clientTop + this.eventElement.clientHeight + this.eventElement.offsetHeight + getElementOffsetY(this.eventElement);
			else{
				this.top = ((getBrowserHeight() / 2) - (this.height/2)) + getYOffset();
			}
		
		if (this.left == -1)
			if (this.positionType == JBSPopup.POSITION_RELATIVE)
				this.left = this.eventElement.clientLeft + getElementOffsetX(this.eventElement);
			else
				this.left = (getBrowserWidth() / 2) - (this.width/2);		
		
		var sWidth = this.width + "px";
		var sHeight = this.height + "px";
		
		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 id='" + JBSPopup.IFRAME_POPUP_ID + "' style='DISPLAY: None; LEFT: 0px; POSITION: absolute; TOP: 0px' src='" + this.frameSource + "' frameBorder='" + this.frameBorder + "' scrolling='" + scroll + "'></IFRAME>");
				document.body.insertBefore(iframe);
			}
			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='';
		}
		
		if(JBSPopup.displayType == JBSPopup.DIV_POPUP) {
		    
			var div = document.getElementById(JBSPopup.DIV_POPUP_ID); 
			if (div == null){
			    if (isIE()) {
    				div = document.createElement("<div id=\"" + JBSPopup.DIV_POPUP_ID + "\" style=\"DISPLAY: None; LEFT: 0px; POSITION: absolute; TOP: 0px\"></div>");
    				document.body.insertBefore(div);
    			}
    			else{
    			    div = document.createElement("div");
    			    div.setAttribute("id", JBSPopup.DIV_POPUP_ID);
    			    document.body.insertBefore(div, null);
    			}

			}
			    if (isIE()) {
			        div.style.top = this.top;
			        div.style.left = this.left;
			        div.style.height = sHeight;
			        div.style.width = sWidth;

			        div.style.display='';
			        //div.style.border = '10px';

    			}
    			else{
    			    var sStyle = "position:absolute;top:" + this.top  +"px;left:" + this.left + "px;height:" + sHeight + "px;width:" + sWidth + "px";  //;border:10px
    			    div.setAttribute("style", sStyle);
    			}

			div.innerHTML = msg;

			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(); };
		}
	},
	setMouseOver: function(funct){
	    var div = document.getElementById(JBSPopup.DIV_POPUP_ID); 
	    div.onmouseover = funct;
	},
	setMouseOut: function(funct){
	    var div = document.getElementById(JBSPopup.DIV_POPUP_ID); 
	    div.onmouseout = funct;
	},

	display: function(DisplayHtml){
		
		if(JBSPopup.displayType == JBSPopup.DIV_POPUP) {
		    
			var div = document.getElementById(JBSPopup.DIV_POPUP_ID);
			if (this.divToUse == null){
			    if (this.handleScript){
			        div.innerHTML = null;
			        while(div.firstChild)
                        div.removeChild(div.firstChild);

			        var re = /<script\b[\s\S]*?>([\s\S]*?)<\//ig; 
                    var match; 
                    while (match = re.exec(DisplayHtml)) { 
                       var newScript = document.createElement('script'); 
                       newScript.type = "text/javascript"; 
                       newScript.text = match[1]; 
                       div.appendChild(newScript); 
                    } 
                }

			    div.innerHTML = DisplayHtml;
			}    
			else
			    div.insertBefore(this.divToUse, null);
		} 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 );
		}
	},
	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.getElementById(JBSPopup.DIV_POPUP_ID).innerHTML = "";
			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;
  }
}
function getYOffset(){
    if (self.pageYOffset)
        return self.pageYOffset;
    else
        return document.documentElement.scrollTop;
}
function isIE(){
	try{
		if(navigator.userAgent.indexOf("MSIE 6") >-1 || navigator.userAgent.indexOf("MSIE 7") >-1)
			return true;
		return false;
   }
   catch(e){
		return false;
   }

}
