(new Image()).src = e104.IMG_DIR+'ajax-loading.gif';
(new Image()).src = e104.IMG_DIR+'min.gif';
(new Image()).src = e104.IMG_DIR+'close.gif';
(new Image()).src = e104.IMG_DIR+'restore.gif';
(new Image()).src = e104.IMG_DIR+'resize.gif';

var DhtmlWindow = {
	imagefiles: [e104.IMG_DIR + 'min.gif', e104.IMG_DIR + 'close.gif', e104.IMG_DIR + 'restore.gif', e104.IMG_DIR + 'resize.gif'], 
	tobjects: [], 
	
	bindPoint: $.LEFT_TOP,
	bindPos: $.RIGHT_TOP,
	x: 5,
	y: 0,
	
	init: function(t) {		
		
		var domwindow = $(document.createElement("div")); 
		domwindow.id = t;
		domwindow.className = "DhtmlWindow";
		domwindow.setAttribute('class', 'DhtmlWindow');
		var domwindowdata='<div class="drag-handle">DHTML Window <div class="drag-controls"><img style="display:none" src="'+this.imagefiles[0]+'" title="Minimize" /><img align="absmiddle" src="'+this.imagefiles[1]+'" title="Close" /></div></div><div class="drag-contentarea"></div><div class="drag-statusarea"><div class="drag-resizearea" style="background: transparent url('+this.imagefiles[3]+') bottom right no-repeat;"></div></div></div>';
		domwindow.innerHTML = domwindowdata;
		
		$id("DhtmlWindowholder").appendChild(domwindow);
		this.zIndexvalue= (this.zIndexvalue) ? this.zIndexvalue + 1 : 10; 
		var t= $id(t);
		
		var divs = t.getElementsByTagName("div");
		for (var i=0; i<divs.length; i++) { 
			if (/drag-/.test(divs[i].className))
				t[divs[i].className.replace(/drag-/, "")] = divs[i]; 
		}
		t.style.zIndex = this.zIndexvalue; 
		if(t.__underwear) {
			$(t.__underwear).style.zIndex = t.style.zIndex-1;
		}
		t.handle._parent=t; 
		t.resizearea._parent = t; 
		t.controls._parent = t;  
		t.onclose = function(){return true} 
		
		if(t.id.split('_').indexOf('sunsHade')<0){
			t.handle.onmousedown = DhtmlWindow.setupdrag;
			t.resizearea.onmousedown = DhtmlWindow.setupdrag;
		}	
		t.controls.onclick = DhtmlWindow.enablecontrols;
		t.show = function(){DhtmlWindow.show(this)}
		t.hide = function(){DhtmlWindow.close(this)}
		t.setSize = function(w, h){DhtmlWindow.setSize(this, w, h)} 
		t.moveTo = function(x, y){DhtmlWindow.moveTo(this, x, y)} 
		t.isResize = function(bol){DhtmlWindow.isResize(this, bol)}
		t.isScrolling = function(bol){DhtmlWindow.isScrolling(this, bol)}
		t.load = function(contenttype, contentsource, title){DhtmlWindow.load(this, contenttype, contentsource, title)} 
		
		this.tobjects[this.tobjects.length] = t;
		return t 
	},
	
	cancelEvent: function () {
		return false;
	},
	
	open: function(t, contenttype, contentsource, title, attr, recalonload) {
		var d = DhtmlWindow; 
		
		function getValue(Name){
			var config = new RegExp(Name+"=([^,]+)", "i"); 
			return (config.test(attr))? parseInt(RegExp.$1) : 0; 
		}
		
		if ($id(t)==null) 
			t = this.init(t); 
		else
			t = $id(t);
		
		t.contenttype = contenttype;
		t.setSize(getValue(("width")), (getValue("height"))); 
		var xpos = getValue("center")? "middle" : getValue("left") 
		var ypos = getValue("center")? "middle" : getValue("top") 
		
		if (typeof recalonload!="undefined" && recalonload=="recal" && this.scroll_top==0) { 
			if (window.attachEvent && !window.opera) {
				addOnloadListener(function() {
					setTimeout(function(){
						t.moveTo(xpos, ypos);
					}, 400);
				});
			}
			else {
				addOnloadListener(function() {
					setTimeout(function(){
						t.moveTo(xpos, ypos);
					}, 400);
				});
			}
		}
		
		t.isResize(getValue("resize")); 
		t.isScrolling(getValue("scrolling")); 
		t.style.visibility="visible";
		t.style.display="none";
		t.contentarea.style.display="block";
		t.moveTo(xpos, ypos); 
		return t;
	},

	setSize: function(t, w, h) { 
	
		t.style.width=Math.max(parseInt(w), 150)+"px";
		if(t.contenttype == 'iframe')
			t.contentarea.style.height=Math.max(parseInt(h), 100)+"px";
		
	},

	moveTo: function(t, x, y){ 
		this.getviewpoint(); 
		t.style.left=(x=="middle")? this.scroll_left+(this.docwidth-t.offsetWidth)/2+'px' : this.scroll_left+parseInt(x)+"px";
		t.style.top=(y=="middle")? this.scroll_top+(this.docheight-t.offsetHeight)/2+'px' : this.scroll_top+parseInt(y)+"px";
		t.takeUnderwear();
	},

	isResize: function(t, bol){ 
		t.statusarea.style.display = (bol == 1) ? "block" : "none";
		t.resizeBool=(bol)? 1 : 0;
	},

	isScrolling: function(t, bol){ 
		t.contentarea.style.overflow = (bol)? "auto" : "hidden";
	},

	load: function(t, contenttype, contentsource, title) { 
			var contenttype = contenttype.toLowerCase(); 
		if (typeof title!="undefined")
			t.handle.firstChild.nodeValue=title;
		if (contenttype=="iframe") {
			t.contentarea.style.padding = '0px';
			t.contentarea.style.overflow = "hidden"; 
			if (!t.contentarea.firstChild || t.contentarea.firstChild.tagName!="IFRAME") { 
				t.contentarea.innerHTML='<iframe frameborder="0" scrolling="auto" style="margin:0; padding:0; width:100%; height: 100%" name="_iframe-'+t.id+'"></iframe>';
			}
			window.frames["_iframe-"+t.id].location.href = contentsource; 
		} 
		else {
			t.contentarea.innerHTML = '<div style="text-align:left"><img src="'+e104.IMG_DIR+'ajax-loading.gif" align="absmiddle" />&nbsp;&nbsp;<span class="loading-text">loading...</span></div>';
			var myAjax = new Ajax.Request(
				contentsource,{	
					parameters: 'contenttype=ajax&_parentId='+t.id,
					onSuccess: function (originalRequest) {
						var data = originalRequest.responseText;
						t.contentarea.innerHTML = data;
						data.evalScripts();
						t.takeUnderwear();
					}
				}
			);
		}
		
		t.contentarea.datatype = contenttype; 
	},

	setupdrag: function(e) {
		
		var d = DhtmlWindow; 
		var t = this._parent;
		d.etarget = this; 
		var e = window.event || e;
		d.initmousex = e.clientX; 
		d.initmousey = e.clientY;
		d.initx = parseInt(t.offsetLeft); 
		d.inity = parseInt(t.offsetTop);
		d.width = parseInt(t.offsetWidth); 
		d.contentheight = parseInt(t.contentarea.offsetHeight); 
		if (t.contentarea.datatype=="iframe") { 
			t.style.backgroundColor="#F8F8F8"; 
			t.contentarea.style.visibility = "hidden";
		}
		Event.observe(document, 'mousemove', d.getdistance);
		Event.observe(document, 'mouseup', d.stopupdrag);
		Event.observe(document, 'selectstart', DhtmlWindow.cancelEvent);
		return false;
	},
	
	stopupdrag: function() {
		var etarget, t;
		if(DhtmlWindow.etarget && DhtmlWindow.etarget._parent) {
			t = DhtmlWindow.etarget._parent;
			if (t.contentarea.datatype=='iframe') { 
				t.contentarea.style.backgroundColor="white";
				t.contentarea.style.visibility="visible";
			}
			DhtmlWindow.stop();
		}
	},
	
	getdistance: function(e){
		var d = DhtmlWindow;
		var etarget = d.etarget;
		var e = window.event || e;
		if(etarget) {
			d.distancex = e.clientX-d.initmousex; 
			d.distancey = e.clientY-d.initmousey;
			if (etarget.className=="drag-handle") 
				d.move(etarget._parent, e);
			else if (etarget.className=="drag-resizearea") 
				d.resize(etarget._parent, e);
		}
		return false;
	},
	
	getviewpoint: function(){ 
		var ie=document.all && !window.opera;
		var domclientWidth = document.documentElement && parseInt(document.documentElement.clientWidth) || 100000; 
		this.standardbody = (document.compatMode=="CSS1Compat")? document.documentElement : document.body;
		this.scroll_top = (ie)? this.standardbody.scrollTop : window.pageYOffset;
		this.scroll_left = (ie)? this.standardbody.scrollLeft : window.pageXOffset;
		this.docwidth = (ie)? this.standardbody.clientWidth : (/Safari/i.test(navigator.userAgent))? window.innerWidth : Math.min(domclientWidth, window.innerWidth-16);
		this.docheight = (ie)? this.standardbody.clientHeight: window.innerHeight;
	},

	move: function(t, e){
		t.style.left = DhtmlWindow.distancex+DhtmlWindow.initx+'px';
		t.style.top = DhtmlWindow.distancey+DhtmlWindow.inity+'px';
		t.takeUnderwear();
	},

	resize: function(t, e) {
		t.style.width = Math.max(DhtmlWindow.width + DhtmlWindow.distancex, 80) + 'px';
		t.contentarea.style.height = Math.max(DhtmlWindow.contentheight+DhtmlWindow.distancey, 50)+'px';
		t.takeUnderwear();
	},

	enablecontrols: function(e) {
		var d = DhtmlWindow;
		var sourceobj = window.event? window.event.srcElement : e.target; 
		if (/Close/i.test(sourceobj.getAttribute("title"))) 
			d.close(this._parent);
		return false
	},

	close: function(t) {
		var closewinbol;
		try {
			t.contentarea.innerHTML='';
			closewinbol = t.onclose();
		}catch(e) { 
			closewinbol = true;
	 	}finally{ 
			if (typeof closewinbol == 'undefined') {
				alert("An error has occured somwhere inside your \"onclose\" event handler");
				closewinbol = true;
			}
		}
		
		if (closewinbol){
			t.disappear();
			if(dSH=$('DhtmlSunsHade'))
				dSH.parentNode.removeChild(dSH)
				
		}	

		return closewinbol;
	},

	show: function(t) {
		t.appear();
	},

	stop: function() {
		Event.stopObserving(document, 'mousemove', DhtmlWindow.getdistance);
		Event.stopObserving(document, 'mouseup', DhtmlWindow.stopupdrag);
		Event.stopObserving(document, 'selectstart', DhtmlWindow.cancelEvent);
		DhtmlWindow.etarget = null; 
	},

	cleanup: function() {
		for (var i=0; i<DhtmlWindow.tobjects.length; i++){
			DhtmlWindow.tobjects[i].handle._parent = DhtmlWindow.tobjects[i].resizearea._parent = DhtmlWindow.tobjects[i].controls._parent = null;
		}
		window.onload=null;
	},
	
	plugin: function(el, obj, ev) {
		ev.contenttype = ev.contenttype || 'iframe';
		ev.title = ev.title || '';
		ev.width = ev.width || 400;
		ev.height = ev.height || 200;
		ev.resize = ev.resize || 1;
		ev.center = ev.center || 0;
		ev.scrolling = ev.scrolling || 1;
		ev.reload = (ev.reload) ? true : false;
		ev.sunshade = (ev.sunshade)?true:false;
		
		if(ev.sunshade){
			hade = $(document.createElement("div")); 
			tb=document.getElementsByTagName('body')[0];
			hade.id="DhtmlSunsHade";
			hade.style.height=eval(tb.scrollHeight+50)+'px';
			hade.style.zIndex=5;
			tb.appendChild(hade);
		}		
		
		if(ev.contenttype != 'iframe') {
			ev.resize = 0;
			ev.scrolling = 0;
		}
		
		var el = $(el);	
		el.style.cursor = 'pointer';
		var dwin = DhtmlWindow.open('DhtmlWindow_'+el.id+'_'+(ev.sunshade?"sunsHade":"noSunsHade"), ev.contenttype, obj, ev.title, 'width='+ ev.width +'px, height='+ ev.height +'px, resize='+ev.resize+', scrolling='+ev.scrolling +', center='+ev.center);
		el.onclick = function() {
			dwin.load(ev.contenttype, obj, ev.title);
			dwin.bindPoint = $.LEFT_TOP;
			this.bind(dwin, $.RIGHT_TOP, 5, 0, false);
			if(ev.reload)
				dwin.load(ev.contenttype, obj, ev.title);
			dwin = $(dwin);
			dwin.setZIndex(6);				
			dwin.show();
		}
		el = null;
		return dwin;
	},
	
	plugin2: function(el, obj, ev) {
		ev.contenttype = ev.contenttype || 'iframe';
		ev.title = ev.title || '';
		ev.width = ev.width || 400;
		ev.height = ev.height || 200;
		ev.resize = ev.resize || 1;
		ev.scrolling = ev.scrolling || 1;
		ev.center = ev.center || 0;
		ev.reload = (ev.reload)?true:false;
		ev.sunshade = (ev.sunshade)?true:false;
		
		if(ev.sunshade){
			hade = $(document.createElement("div")); 
			tb=document.getElementsByTagName('body')[0];
			hade.id="DhtmlSunsHade";
			hade.style.height=eval(tb.scrollHeight+50)+'px';
			hade.style.zIndex=5;
			tb.appendChild(hade);
		}
		
		
		
		
		if(ev.contenttype != 'iframe') {
			ev.resize = 0;
			ev.scrolling = 0;
		}
		
		var el = $(el);	
		el.style.cursor = 'pointer';
		var dwin = DhtmlWindow.open('DhtmlWindow_'+el.id+'_'+(ev.sunshade?"sunsHade":"noSunsHade"), ev.contenttype, obj, ev.title, 'width='+ ev.width +'px, height='+ ev.height +'px, resize=0, scrolling='+ev.scrolling);
		dwin.load(ev.contenttype, obj, ev.title);
		dwin.bindPoint = $.LEFT_TOP;
		el.bind(dwin, $.RIGHT_TOP, 5, 0, false);
		dwin = $(dwin);
		dwin.setZIndex(6);
		dwin.show();
		el = null;
		return dwin;
	}
	,
	plugin3: function(el, obj, ev,algin) {
		ev.contenttype = ev.contenttype || 'iframe';
		ev.title = ev.title || '';
		ev.width = ev.width || 400;
		ev.height = ev.height || 200;
		ev.resize = ev.resize || 1;
		ev.scrolling = ev.scrolling || 1;
		ev.center = ev.center || 0;
		ev.reload = (ev.reload)?true:false;
		ev.sunshade = (ev.sunshade)?true:false;
		
		if(ev.sunshade){
			hade = $(document.createElement("div")); 
			tb=document.getElementsByTagName('body')[0];
			hade.id="DhtmlSunsHade";
			hade.style.height=eval(tb.scrollHeight+50)+'px';
			hade.style.zIndex=5;
			tb.appendChild(hade);
		}
		
		if(ev.contenttype != 'iframe') {
			ev.resize = 0;
			ev.scrolling = 0;
		}
		
		var el = $(el);	
		el.style.cursor = 'pointer';
		var dwin = DhtmlWindow.open('DhtmlWindow_'+el.id+'_'+(ev.sunshade?"sunsHade":"noSunsHade"), ev.contenttype, obj, ev.title, 'width='+ ev.width +'px, height='+ ev.height +'px, resize=0, scrolling='+ev.scrolling);
		dwin.load(ev.contenttype, obj, ev.title);
		dwin.bindPoint = $.LEFT_TOP;
		el.bind(dwin, algin, 5, 0, false);
		dwin = $(dwin);
		dwin.setZIndex(6);
		dwin.show();
		el = null;
		return dwin;
	}
	
} 

document.write('<div id="DhtmlWindowholder"><span style="display:none">.</span></div>') 
Event.observe(window, 'unload', DhtmlWindow.cleanup);
e104.widget.DhtmlWindow = DhtmlWindow;