// CLASS CONSTRUCTOR  :_pl_item

function _pl_item(_pop_obj) {
	this.obj_layer     = _pop_obj;			//popup object layer
	this.obj_iframe    = null;				//popup back iframe
	this.obj_screen    = null;				//popup modal object
	this.obj_screen_if = null;				//popup modal object
	this.left = 0;						//default show left
	this.top  = 0;						//default show top
	this.modalMode = false;
	this.screen = null;
	this.auto_created = 0;
	
	_pl_item.resizeObj(this.obj_layer, this.obj_iframe);

	return this;
}

_pl_item.prototype.screenOpacity=0;
_pl_item.prototype.screenColor="#333333";

_pl_item.prototype.moveTo = function(left, top) {
	this.obj_layer.style.left	= left+'px';
	this.obj_layer.style.top	= top+'px';
	if(this.obj_iframe) {
		this.obj_iframe.style.left	= left+'px';
		this.obj_iframe.style.top	= top+'px';
	}
}


_pl_item.resizeObj = function(_source, _target) {
	if (_target) {
		_target.style.left   = _source.style.left;
		_target.style.top    = _source.style.top;
		_target.style.width  = _source.style.width;
		if (_source.style.height=='')
			_target.style.height = _source.clientHeight;
		else
			_target.style.height = _source.style.height;
	}
}


_pl_item.prototype.hide = function(){
	with (this) {
		obj_layer.style.display="none";
		if(obj_iframe) obj_iframe.style.display="none";

		if (obj_screen!=null) {
			obj_screen.style.display = "none";
		}

		if (obj_screen_if!=null) {
			obj_screen_if.style.display = "none";
		}
		return obj_layer.style.zIndex;
	}
}

_pl_item.prototype.show = function(modal, center, zIndex){
	with (this) {
		var lastZindex = zIndex;
		if (modal) lastZindex=this.addScreen(lastZindex);

		/*
		if (center) {
			var pos = Position.getCenterObj(this.obj_layer);
			this.obj_layer.style.left = pos.left +'px';
			this.obj_layer.style.top = pos.top +'px';
		}
		*/

		if(this.obj_iframe) this.obj_iframe.style.zIndex=lastZindex++;
		this.obj_layer.style.zIndex=lastZindex++;

		obj_layer.style.display="block";
			
		if(this.obj_iframe) obj_iframe.style.display="block";

		if (center) {
			var pos = get_center_obj(this.obj_layer);
			this.obj_layer.style.left = pos.left +'px';
			this.obj_layer.style.top = pos.top +'px';
		}

		_pl_item.resizeObj(this.obj_layer, this.obj_iframe);

		return lastZindex;
	}
}

// Add an IFRAME shim for the DIV
// --------------------------------------------------------------------
_pl_item.prototype.addIframeShim = function() {
	if (this.iframe==null) {
		this.iframe = popupLayer.createIframe();
	}
	this.iframe.className = Popup.iframeClass;
	this.iframe.style.top = this.getTop()+"px";
	this.iframe.style.left = this.getLeft()+"px";
	this.iframe.style.width = this.div.offsetWidth + "px";
	this.iframe.style.height = this.div.offsetHeight + "px";
	this.iframe.style.zIndex = Popup.minZIndex++;
	set_style_opacity(this.iframe, 0);
	this.iframe.style.visibility = 'visible';
	this.iframe.style.display = 'block';
	this.iframe.style.border = '0';
};


// Create a "screen" to make a popup modal
// --------------------------------------------------------------------
_pl_item.prototype.addScreen = function(zIndex) {
	var lastZindex = zIndex;
	
	if (this.obj_screen==null) {
		this.obj_screen=popupLayer.createDiv();
		this.obj_screen.style.top="0px";
		this.obj_screen.style.left="0px";
		this.obj_screen.style.backgroundColor = this.screenColor;
		//this.obj_screen.className=Popup.screenClass;;
		set_style_opacity(this.obj_screen, this.screenOpacity);
		this.obj_screen.onclick = function(e) { stopEvent(e); }
	}
	if (this.obj_screen_if==null) {
		this.obj_screen_if = popupLayer.createIframe();
		this.obj_screen_if.style.top="0px";
		this.obj_screen_if.style.left="0px";
		//this.obj_screen_if.className=Popup.screenIframeClass;
		set_style_opacity(this.obj_screen_if, 0);
	}
	this.obj_screen.style.width = get_doc_width()+"px";
	this.obj_screen.style.height = get_doc_height()+"px";
	this.obj_screen_if.style.width = get_doc_width()+"px";
	this.obj_screen_if.style.height = get_doc_height()+"px";
	this.obj_screen_if.style.zIndex = lastZindex++;
	this.obj_screen_if.style.visibility="visible";
	this.obj_screen_if.style.display="block";
	this.obj_screen.style.zIndex = lastZindex++;
	//this.obj_screen.style.zIndex = lastZindex;
	this.obj_screen.style.visibility="visible";
	this.obj_screen.style.display="block";
	
	return lastZindex;
};



var popupLayer = function() {
	return this;
};

popupLayer.prototype.lstPopup 		= null;
popupLayer.prototype.activated_laye	= -1;
popupLayer.prototype.offsetX		= 0;
popupLayer.prototype.offsetY		= 0; 
popupLayer.prototype.nowX 			= 0;      
popupLayer.prototype.nowY 			= 0;      
popupLayer.prototype.bInitialized	= 0;
popupLayer.lastZindex     = 0;

popupLayer.prototype.GetItem = function(sLayerID) {
	with(popupLayer.prototype) {
		if (lstPopup!=null) {
			var i;
			for (i=0; i<lstPopup.length; i++) {
				if(sLayerID == lstPopup[i].obj_layer.id) return(lstPopup[i]);
			}
		}
	}
}

// popupLayer_init
popupLayer.prototype.init = function()
{
	with (popupLayer.prototype) {
		lstPopup		= new Array();
		activated_layer	= -1;
		
		document.onmousedown = mousedown;
		document.onmouseup   = mouseup;
		
		bInitialized = true;
	}
}
popupLayer.create_popup = function(sLayerID, sOptions)
{
	var obj_layer = popupLayer.createDiv();
	obj_layer.id = sLayerID;
	obj_layer.style.zIndex = popupLayer.lastZindex;

	if (sOptions['left']) obj_layer.style.left = sOptions['left'] + 'px';
	if (sOptions['top']) obj_layer.style.top = sOptions['top'] + 'px';
	if (sOptions['width']) obj_layer.style.width = sOptions['width'] + 'px';
	if (sOptions['height']) obj_layer.style.height = sOptions['height'] + 'px';

	obj_layer.innerHTML = popupLayer.GetPopupHTML(sLayerID, sOptions);

	if (sOptions['height']) document.getElementById(sLayerID + '_body_wrapper').height = (sOptions['height'] - 25) + 'px';

	return obj_layer;
}

popupLayer.GetPopupHTML = function(sLayerID, sOptions) { 
	var btnclose = (typeof(sOptions['btnclose']) != 'undefined') ? sOptions['btnclose'] : true;
	var sIframeSrc = (typeof(sOptions['iframe']) != 'undefined') ? sOptions['iframe'] : null;
	var sInner_obj = (typeof(sOptions['inner_obj']) != 'undefined') ? sOptions['inner_obj'] : null;
	var sInner_html = (typeof(sOptions['inner_html']) != 'undefined') ? sOptions['inner_html'] : null;

	var sHTML = '';
	var sHTML_button = '';
	var sHTML_inner = '';
	var sHTML_splash = '';
	if (btnclose) {
		sHTML_button = '<a class="popuplayer_button" href="Javascript:popupLayer.hide(\'' 
			+ sLayerID + '\');"></a>';
	}

	if (sIframeSrc != null) {
		if (sInner_html != null) {
			sHTML_inner = '';	
			sHTML_splash = '';
		}
		sHTML_inner += '<iframe ' + sHTML_splash + 'name="' + sLayerID + '_iframe" id="' + sLayerID + '_iframe" src="' 
			+ sIframeSrc + '" frameborder="0" width="100%" height="100%"></iframe>';
	} else if (sInner_obj != null) {
		sHTML_inner = document.getElementById(sInner_obj).innerHTML;
		document.getElementById(sInner_obj).innerHTML = '';
	} else if (sInner_html != null) {
		sHTML_inner = sInner_html;
	}

	sHTML = '<div style="position:relative; width:100%; height:100%;">'
		+ '<div class="popuplayer plc_' + sLayerID + '" style="position:absolute; width:100%; height:100%;">'
		+ '<table class="popuplayer_frame" style="width:100%; height:100%;" >'
		+ '<thead><tr><td class="left"></td><td class="center"><div class="popuplayer_title" id="' + sLayerID + '_ti">' + sHTML_button 
			+ '<div id="' + sLayerID + '_title">' + sOptions['title'] + '</div></div></td><td class="right"></td></tr></thead>'
		+ '<tfoot><tr><td class="left"></td><td class="center"></td><td class="right"></td></tr></tfoot>'
		+ '<tbody><tr><td class="left"></td><td class="center" id="' + sLayerID + '_body_wrapper"><div style="height:100%; width:100%; overflow:auto;">'
			+ '<div id="' + sLayerID + '_body" class="popuplayer_body">' + sHTML_inner + '</div></div></td><td class="right"></td></tr></tbody><div'
		+ '</table>'
		+ '</div>'
		+ '<img src="/image/common/shadow.png" alt="" class="popup_shadow" '
			+'style="position:absolute; display:block; width:104%; height:105%; left:-2%; top:-2%; z-Index:-1;" />'
		+ '</div>';

	return sHTML;
}

// options info
// title, iframe, classname, btnclose, left, top, width, height, inner_obj
popupLayer.AddPopup = function(sLayerID, sOptions)
{
	var _pop, obj_layer;
	var sOptions = (typeof(sOptions) != 'undefined') ? sOptions: new Array();

	if (!popupLayer.prototype.bInitialized) this.prototype.init();

	if (obj_layer = document.getElementById(sLayerID)){
		_pop = obj_layer; 
	} else {
		_pop = popupLayer.create_popup(sLayerID, sOptions);
		_pop.auto_created = 1;
	}
	var _pop_title  = document.getElementById(sLayerID+'_ti');

	if (_pop!='undefined' && _pop_title!='undefined') {
		_pls = new _pl_item(_pop, _pop_title);

		popupLayer.prototype.lstPopup[popupLayer.prototype.lstPopup.length] = _pls;
	}
	
	return _pls;
}


popupLayer.prototype.mouseup = function(e){
	popupLayer.prototype.activated_layer = -1;
}

popupLayer.prototype.mousedown = function(e){
	with (popupLayer.prototype) {
		if(!lstPopup) return;
		if(lstPopup.length==0) return;

		var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;
		
		var topDog    = isIE ? "BODY" : "HTML";
		var hotDog    = isIE ? event.srcElement : e.target;  
		
		var i = lstPopup.length;
		
		while (hotDog && hotDog.tagName!=topDog && i==lstPopup.length){
			for (i=0; i<lstPopup.length; i++) {
				if(hotDog==lstPopup[i].obj_title) break;
			}
			hotDog = isIE ? hotDog.parentElement : hotDog.parentNode;
		}  
		if (i < lstPopup.length){
			activated_layer = i;
			var whichDog  = lstPopup[activated_layer].obj_layer;
			
			offsetX   = isIE ? event.clientX : e.clientX;
			offsetY   = isIE ? event.clientY : e.clientY;
			nowX      = parseInt(whichDog.style.left);
			nowY      = parseInt(whichDog.style.top);

			document.onmousemove = mousemove;
		}
	}
}



popupLayer.prototype.mousemove = function(e){
	with (popupLayer.prototype) {
		if (activated_layer == -1) return;

		var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;

		var nleft = isIE ? nowX+event.clientX-offsetX : nowX+e.clientX-offsetX; 
		var nTop  = isIE ? nowY+event.clientY-offsetY : nowY+e.clientY-offsetY;

		lstPopup[activated_layer].moveTo(nleft, nTop);
		
		return false;  
	}
}

// Append an object to the body
// --------------------------------------------------------------------
popupLayer.appendToBody = function(o) {
	var body = document.body;
	if (body && body.appendChild) {
		body.appendChild(o);
	}
};


// Create a new DIV object to be used for a popup
// --------------------------------------------------------------------
popupLayer.createDiv = function() {
	if (document.createElement) {
		var d = document.createElement("DIV");
		d.style.position="absolute";
		d.style.display="none";
		//d.style.visibility="hidden";
		popupLayer.appendToBody(d);
		return d;
	}
	alert("ERROR: Couldn't create DIV element in Popup.prototype.createDiv()");
	return null;
};

// Create a new IFRAME object to be used behind the popup
// --------------------------------------------------------------------
popupLayer.createIframe = function() {
	if (document.createElement) {
		var i= document.createElement("IFRAME");
		i.style.border="0";
		i.style.position="absolute";
		i.style.display="none";
		//i.style.visibility="hidden";
		i.style.background="none";
		popupLayer.appendToBody(i);
		return i;
	}
	else {
		alert("ERROR: Couldn't create IFRAME object in Popup.prototype.createIframe()");
	}
};


popupLayer.hide = function(sLayerID){
	var _popItem = popupLayer.prototype.GetItem(sLayerID);
	
	if (_popItem!=null) {
		var close_zIndex = _popItem.hide();
		if(close_zIndex<popupLayer.lastZindex)
			popupLayer.lastZindex = close_zIndex;
		var splash = document.getElementById(sLayerID + '_splash');
		if (splash) { 
			var iframe = document.getElementById(sLayerID + '_iframe');
			if(iframe) iframe.src = '';
		}
	}
}

// options info
// title, iframe, classname, btnclose, left, top, width, height, inner_obj
popupLayer.show = function(sLayerID, modal, center, sOptions){
	var _popItem = popupLayer.load(sLayerID, sOptions);
	if (_popItem!=null) {
		var lastZindex = _popItem.show(modal, center, popupLayer.lastZindex);
		popupLayer.lastZindex = lastZindex++;
	}
}


popupLayer.load = function(sLayerID, sOptions){
	var _popItem = popupLayer.prototype.GetItem(sLayerID);
	if(_popItem) {
		if (sOptions) {
			if(document.getElementById(sLayerID + '_title') !=null && typeof(sOptions['title']) != 'undefined')
				document.getElementById(sLayerID + '_title').innerHTML = sOptions['title'];
			if(sOptions['refresh']) {
				var iframe = document.getElementById(sLayerID + '_iframe');
				if(typeof(sOptions['inner_html']) != 'undefined') {
					if (iframe && document.getElementById(sLayerID + '_splash')) {
						document.getElementById(sLayerID + '_splash').innerHTML = sOptions['inner_html'];
					} else {
						document.getElementById(sLayerID + '_body').innerHTML = sOptions['inner_html'];
					}
				}
				else if(iframe && typeof(sOptions['iframe']) != 'undefined') {
					iframe.src = sOptions['iframe'];
				}
			}
		}
	} else { 
		_popItem = popupLayer.AddPopup(sLayerID, sOptions);
	}
	if (_popItem!=null) {
		var splash = document.getElementById(sLayerID + '_splash');
		if(splash) splash.style.display='block';
	}
	return _popItem;
}

