// $Id: pop_layer.js,v 1.5 2006/11/14 15:43:47 andy Exp $
myEscape = function( str) {
  return escape( str).replace( /\//g, "%2F").replace( /\\/g, "%5C");
}

var cur_o;
var offsetx;
var offsety;
var operation_type = 0;
isIE=document.all;
isNN=!document.all&&document.getElementById;
ddEnabled=false;
min_y = 0;
__popLayers__ = {};


registerEvent = function( obj, event_, func) {
	document.all?obj.attachEvent( "on".concat( event_), func):obj.addEventListener( event_, func, false);
}

registerEvent( document, "mousedown",
	function(e) {
		topDog=isIE ? "BODY" : "HTML";
		hotDog=isIE ? event.srcElement : e.target;  
		while ( !( (hotDog.id || "").indexOf( "titleBar")==0 || (hotDog.id || "").indexOf( "whresize")==0) && hotDog.tagName!=topDog) {
			hotDog=isIE ? hotDog.parentElement : hotDog.parentNode;
		}
		if (hotDog.id.indexOf( "titleBar") == 0 ) {
			__popLayers__name = hotDog.id.substr( 8);
			offsetx=isIE ? event.clientX : e.clientX;
			offsety=isIE ? event.clientY : e.clientY;
			operation_type = 1;
			ddEnabled=true;
		} else if (hotDog.id.indexOf( "whresize") == 0 ) {
			__popLayers__name = hotDog.id.substr( 8);
			offsetx=isIE ? event.clientX : e.clientX;
			offsety=isIE ? event.clientY : e.clientY;
			operation_type = 2;
			ddEnabled=true;
		}
	});
registerEvent( document, "mousemove",
	function(e) {
		if (!ddEnabled) return;
		if ( operation_type == 1 ) {
			var cur_o = __popLayers__[__popLayers__name].Layer;
			now_offsetx=isIE ? event.clientX : e.clientX;
			now_offsety=isIE ? event.clientY : e.clientY;
			left_ = parseInt(cur_o.style.left)+(now_offsetx-offsetx);
			top_ = parseInt(cur_o.style.top)+(now_offsety-offsety);
			offsetx=now_offsetx;
			offsety=now_offsety;
			if ( top_ < min_y ) {
				top_ = min_y;
				ddEnabled = false;
			}
			cur_o.style.left=left_;
			cur_o.style.top=top_;
		} else if ( operation_type == 2 ) {
			var cur_o = __popLayers__[__popLayers__name];
			now_offsety=isIE ? event.clientY : e.clientY;
			now_offsetx=isIE ? event.clientX : e.clientX;
			cur_o.setHeight( parseInt( cur_o.getHeight())-(offsety-now_offsety));
			cur_o.setWidth( parseInt( cur_o.getWidth())-(offsetx-now_offsetx));
			offsety=now_offsety;
			offsetx=now_offsetx;
		} else {
			ddEnabled = false;
		}
	})
registerEvent( document, "mouseup", function() { ddEnabled=false})

function PopLayer( name, title, url, header) {
//-------------------------------------
//          Local Variables
//-------------------------------------
	this.whichDog=false;
	this.url = url;
	this.name = name;
	this.title = title;
	this.min_y = min_y;
	this.m_pos_y = 0;
	this.m_pos_x = 0;
	this.offsetx = 0;
	this.offsety = 0;
	this.width = 800;
	this.height = 700;
	this.open = false;
	this.iframe = null;
	this.iframe_width = "100%";
	this.iframe_name = this.name + ((new Date())-0);

	this.bgcolor = "#FFFFFF";
	if ( 	this.url.indexOf( "?") ) {
		this.url.concat( "&WINDOW_BODY_ONLOAD="+myEscape( "top."+this.name+"_.iframeOnload( window);"));
	} else {
		this.url.concat( "?WINDOW_BODY_ONLOAD="+myEscape( "top."+this.name+"_.iframeOnload( window);"));
	}
	this.scrollPosition=document.body.scrollTop;
	if ( !header ) {
		this.header = '\
<table width="1" BGCOLOR="$$bgcolor$$" height="$height" cellspacing="0" cellpadding="0" style="border:1px solid #007DBD;">\
	<tr>\
		<td width="100%">\
			<table border="0" width="100%" cellspacing="0" cellpadding="3" height="36">\
            <tr>\
              <td height=20 ID="titleBar$$name$$" style="cursor:move;background:#007DBD;" width="100%">\
                <table border="0" width="100%" cellspacing="0" cellpadding="0">\
                  <tr>\
                    <td valign="middle" align=left style="color:#FFFFFF;cursor:move"><b>'+title+'</b></td>\
                    <td valign="middle" align=right><a href="./" onClick="'+name+'_.hideMe();return false"><img src="./images/ico_close.gif" border=0></a></td>\
                  </tr>\
                </table>\
              </td>\
            </tr>\
				<tr>\
					<td style="padding:4px" colspan="2">\
						<table width="100%" border="0" cellpadding="1" cellspacing="1" valign=top bgcolor="$$bgcolor$$">\
							<tr>\
								<td align="center"><iframe TRUSTED="YES" APPLICATION="yes" id="$$iframe_name$$" name="$$iframe_name$$" width="$$iframe_width$$" src="$$url$$" FRAMEBORDER=0 scrolling="auto" onload="'+this.name+'_.iframeOnload( this);"></iframe></td>\
							</tr>\
						</table>\
					</td>\
				</tr>\
			</table>\
		</td>\
	</tr>\
	<tr>\
		<td align="right"></td>\
	</tr>\
</table>';
	} else {
		this.header = header;
	}
	this.Layer = document.getElementById( this.name);
	this.Layer.style.zIndex=10000;
	__popLayers__[this.name] = this;
//-------------------------------------
//              Methods
//-------------------------------------
	this.hideMe = function() {
		try {
			this.iframe.contentWindow.document.body.innerHTML = "!!!";
		} catch (e) {}
		document.getElementById( this.name).innerHTML = "";
		
		if (isIE||isNN) document.getElementById( this.name).style.visibility="hidden";
		ddEnabled = false;
		this.open = false;
	};
	this.showMe = function() {
		if (isIE||isNN){
			c = this.header.replace( /\$\$url\$\$/g, this.url);
			c = c.replace( /\$\$title\$\$/g, this.title);
			c = c.replace( /\$\$name\$\$/g, this.name);
			c = c.replace( /\$\$width\$\$/g, this.width);
			c = c.replace( /\$\$height\$\$/g, this.height);
			c = c.replace( /\$\$iframe_name\$\$/g, this.iframe_name);
			c = c.replace( /\$\$iframe_width\$\$/g, this.iframe_width);
			c = c.replace( /\$\$bgcolor\$\$/g, this.bgcolor);
			this.Layer.innerHTML = c;
			this.Layer.style.visibility="visible";
		}
		this.iframe = document.getElementById(this.iframe_name) 
	};
	this.centerWindow = function() {
		o = document.body;
		this.m_pos_y = isIE ? (o.scrollTop+o.clientHeight/2)-(this.Layer.clientHeight/2): (o.scrollTop+o.clientHeight/2)-(this.Layer.scrollHeight/2);
		this.m_pos_x = isIE ? (o.scrollLeft+o.clientWidth/2)-(this.Layer.clientWidth/2): (o.scrollLeft+o.clientWidth/2)-(this.Layer.scrollWidth/2);
		if ( this.m_pos_y < this.min_y ) this.m_pos_y = this.min_y;
		this.Layer.style.left = this.m_pos_x;
		this.Layer.style.top = this.m_pos_y;
		this.open = true;
	}
	this.setHeight = function(h) {
		this.iframe.style.height = h;
	};
	this.setWidth = function(w){
		this.iframe.style.width = w;
	};
	this.getHeight = function() {
		return this.iframe.style.height;
	};
	this.getWidth = function(){
		return this.iframe.style.width;
	};
	this.onScroll = function() {
		offset_y = document.body.scrollTop - this.scrollPosition;
		this.scrollPosition = document.body.scrollTop;
		var this_layer = document.getElementById( this.name);
		y = parseInt( this_layer.style.top);
		this_layer.style.top = (y+offset_y<this.min_y)?this.min_y:(y+offset_y);
	};
	this.iframeOnload = function(fr) {
		try {
			this.iframe = fr;
			var frw = fr.contentWindow;
			this.frw = frw;
			frw.popup_close_ = new Function( "top."+this.name+"_.hideMe();");
			frw.refreshSize = new Function( "top."+this.name+"_.refreshSize();");
			this.refreshSize();
		} catch(e) {}
	};
	this.refreshSize = function() {
		var body = this.frw.document.body;
		var w = (document.all?body.scrollWidth:body.offsetWidth) < 500 ? 500 : (document.all?body.scrollWidth:body.offsetWidth);
		this.setWidth( ( w < this.width )?w:this.width);
		var h = document.all?body.scrollHeight:body.offsetHeight;
		this.setHeight( (h < this.height)?h:this.height);
		if ( h < this.height && body.scrollHeight > body.offsetHeight ) {
			this.setHeight( ((h < this.height)?h:this.height) - (body.offsetHeight - body.scrollHeight));
		}
		this.centerWindow();
	}
}
