function jpCookies() {
	this.cooks = {};

	this.getValue = function(key) {
		return this.cooks[key];
	}

	this.setValue = function(key,value,age) {
		var ckv = key + "=" + value.toString();
		if( typeof(age)!="undefined" ) {
			var dti = new Date();
			var dts = new Date(new Number(dti)+age*1000);
		  ckv += ("; expires=" + dts.toUTCString()); 
	  }
		document.cookie = ckv;
		this.cooks[key] = (age==0)?undefined:value.toString();
	}

	this.remove = function(key) {
		this.setValue(key,"",0);
	}

	var items = document.cookie.split("; ");
	for(var i=0;i<items.length;i++ ) {
		var item = items[i];
		if( !item ) continue;
		item = item.split("=",2);
		if( !item[1] ) continue;
		this.cooks[item[0]] = item[1];
	}
}
jpCookie = new jpCookies();

function jpObject() {
	this._super = this.superClass;
	this.superClass = this;

	this.isIE = (navigator.appVersion.indexOf("MSIE")!=-1)?true:false;
	this.ie6 = (navigator.appVersion.indexOf("MSIE 6")!=-1)?true:false;
	this.events = {};

	this.dispatchEvent = function(e) {
		var fn = this.events[e.type];
		if( typeof(fn)=="function" ) return fn(e);
		fn = this[e.type];
		if( typeof(fn)=="function" ) return fn(e);
	}
	
	this.addEventListener = function(et,efn) {
		this.events[et] = efn;
	}

	this.fireEvent = function(eventType,targetElem) {
		this.dispatchEvent( { type:eventType, src:this, target:targetElem } );
	}

	this.add = function() {
		if( !window.jpObject_insts ) jpObject_insts = [];
		this.id = jpObject_insts.length;
		for(var i=0;i<this.id; i++) {
			if( !jpObject_insts[i] ) {
				this.id = i; break;
			}
		}
		jpObject_insts[this.id] = this;
		return this;
	}
	
	this.release = function() {
		jpObject_insts[this.id] = null;
	}

	this.qName = function() {
		return "jpObject_insts["+this.id+"]";
	}
	
	this.applyThis = function(tx) {
		try {
			tx = tx.replace( new RegExp("<this>","g"),this.qName() );
			tx = tx.replace( new RegExp("<site>","g"),siteID() );
			for(var s in this) {
				if( typeof(this[s])=="function" ) continue;
				tx = tx.replace( new RegExp("<this."+s+">","g"),this[s] ? this[s] : "" );
			}
			return tx;
		} catch(e) {
			alert( [e,e.message].join("\n") );
		}
	}
}

function jpRenderer() {
	this._super = this.superClass;
	this.superClass = this;

	this.crts = [];
	this.afters = null;
	this.disp = null;

	this.getHTML = function() { return ""; }

	this.refresh = function() {
		this.render();
	}
	
	this.initRender = function() {
		return this;
	}

	this.render = function() {
		this.initRender();
		this.resetAfters();
		var htmls = [];
		htmls[htmls.length] = this.getHTML();
		this.disp.innerHTML = htmls.join("\n");
		return this.onAfter();
	}
	
	this.resetAfters = function() {
		this.afters = [];
		return this;
	}
	
	this.addCreate = function(fn,args) {
		if( !this.crts ) return this;
		this.crts[this.crts.length] = { "fn":fn,"args":args };
		return this;
	}

	this.addAfter = function(fn,args) {
		this.afters[this.afters.length] = { "fn":fn,"args":args };
		return this;
	}

	this.onAfter = function() {
		if( this.crts ) {
			for(var s in this.crts) {
				var obj = this.crts[s];
				if( typeof(obj.fn)!="function" ) continue;
				obj.fn.apply( this,obj.args );
			}
			this.crts = null;
		}
		for(var s in this.afters) {
			var obj = this.afters[s];
			if( typeof(obj.fn)!="function" ) continue;
			obj.fn.apply( this,obj.args );
		}
		return this;
	}
}
jpRenderer.prototype = new jpObject;

function jpOpener() {
	this._super = this.superClass;
	this.superClass = this;

	this.type = null;
	this.tout = 2500;
	this.timeout = 0;
	this.listener = { };

	this.setTimeout = function(tox) {
		this.tout = tox;
		return this;
	}

	this.setListener = function(ee) {
		this.listener = ee;
		return this;
	}

	this.pos = function(ee) {
		var p = { x:0, y:0 };
		if( ee.offsetParent ) {
			var po = this.pos(ee.offsetParent);
			p.x += po.x;
			p.y += po.y;
		}
		p.x += ee.offsetLeft?ee.offsetLeft:0;
		p.y += ee.offsetTop?ee.offsetTop:0;
		return p;
	}

	this.overer = function() {
		var ee = (typeof(window.event)=="object" && typeof(event.srcElement)=="object") ? event.srcElement : (typeof(event.target)=="object"?event.target:null);
		if( !ee ) return;
		this.loc = this.pos(ee);
		var x = ee.className.split(" ");
		for(var s in x) {
			var xx = this.listener[x[s]];
			if( typeof(xx)=="function" )
				xx.apply(this,[x[s]]);
		}
	}

	this.setTime = function(nanos) {
		this.timeout = (new Date()).getTime()+nanos;
	}
	
	this.initRender = function() {
		if( !this.disp ) {
			this.disp = document.createElement("DIV");
			this.disp.style.position = "absolute";
			this.disp.style.display = "none";
			document.body.appendChild( this.disp );
		}
		if( !this.lock ) this.lock = this.loc;
		this.disp.style.left = this.lock.x;
		this.disp.style.top = this.lock.y;
		this.disp.style.display = "block";
	}

	this.open = function() {
		this.args = [];
		for(var i=0;i<arguments.length;i++)
			this.args[i] = arguments[i];
		this.type = this.args[0];
		this.setTime(this.tout);
		this.lock = null;
		this.render();
	}

	this.close = function() {
		this.disp.style.display = "none";
		this.timeout = 0;
	}

	this.closer = function() {
		var ts = (new Date()).getTime();
		if( !this.timeout || ts<this.timeout ) return;
		this.close();
	}

	this.start = function() {
		var fnOver = new Function("e",this.qName()+".overer(e)");
		if( window.attachEvent ) {
			document.attachEvent("onmouseover",fnOver);
		} else {
			window.addEventListener("mouseover",fnOver,false);
		}
		this.tobj = setInterval( new Function("e",this.qName()+".closer(e)"),100 );
		return this;
	}
}
jpOpener.prototype = new jpRenderer;

function makeButton(elem,oi,di) {
	var src = elem.src.substr(0,elem.src.lastIndexOf("/")+1);
	elem.oi = src+oi;
	elem.di = src+di;
	elem.ui = elem.src;
	elem.onmouseover = function(evt) { this.src = (window.globalDf)?this.di:this.oi; }
	elem.onmouseout = function(evt) { this.src = this.ui; }
	elem.onmousedown = function(evt) {
		document.omu = document.onmouseup;
		document.onmouseup = function(evt) {
			document.onmouseup = document.omu;
			window.globalDf = false;
			if( document.releaseCapture )
				document.releaseCapture();
		}
		window.globalDf = true;
		this.src = this.di;
		if( this.setCapture )
			this.setCapture();
	}
	elem.onmouseup = function(evt) { this.src = this.oi; }
	elem.src = src+oi;
}

function jpLayer() {
	this._super = this.superClass;
	this.superClass = this;

	this.useTheme = true;
	this._x = 0;
	this._y = 0;
	this.crts = [];
	this.hideObjs = [];
	this.afters = null;
	this.opacity = 35;
	this.popover = null;
	this.overlay = null;
	this.display = null;
	this.clickDisplay = false;
	this.enableOuterClose = true;
	this.backgroundWhite = true;
	
	this.superAdd = this.add;
	this.superRelease = this.release;

	this.add = function() {
		this.superAdd();
		if( !jpLayer.prototype.refCnt ) {
			jpLayer.prototype.oss = document.onselectstart;
			jpLayer.prototype.ods = document.ondragstart;
			document.onselectstart = function() {
				var ct = event.currentTarget || event.srcElement;
				if(ct.tagName=="INPUT" || ct.tagName=="TEXTAREA") return true;
				return false;
			}
			document.ondragstart = function() { return false; }
			jpLayer.prototype.refCnt = 0;
		}
		jpLayer.prototype.refCnt ++;
		return this;
	}
	
	this.release = function() {
		jpLayer.prototype.refCnt --;
		if( !jpLayer.prototype.refCnt ) {
			document.onselectstart = jpLayer.prototype.oss;
			document.ondragstart = jpLayer.prototype.ods;
		}
		this.superRelease();
	}

	this.attachEvents = function() {
		if( window.attachEvent )
			window.attachEvent("onscroll",this.scrfn = new Function( this.qName()+".onscroll(event)"));
		else
			document.addEventListener("scroll",this.scrfn = new Function("e",this.qName()+".onscroll(e)"),false);

		return this;
	}

	this.detachEvents = function() {
		if( !this.scrfn ) return this;
		if( window.detachEvent ) 
			window.detachEvent("onscroll",this.scrfn );
		else
			document.removeEventListener("scroll",this.scrfn,false);
		return this;
	}

	this.onscroll = function(w) {
		this.scrollTop = new Number( "0" + (document.documentElement ? document.documentElement.scrollTop : 0) );
		this.scrollTop += document.body.scrollTop;
		this.popover.style.top = this.overlay.style.top = this.scrollTop + "px";
	}

	this.getHTML = function() { return ""; }

	this.close = function() {
		this.detachEvents();
		document.body.removeChild( this.overlay );
		document.body.removeChild( this.popover );
		if( this.ie6 ) this.showCombo();
		this.shower("OBJECT,EMBED");
		this.release();
	}

	this.cancel = function() {
		if( this.clickDisplay ) { this.clickDisplay = false; return; }
		this.close();
	}

	this.click = function() {
		this.clickDisplay = true;
	}

	this.transparent = function(r) {
		this.opacity = r;
		return this;
	}

	this.refresh = function() {
		this.render(this._x,this._y);
	}
	
	this.getTitleIcon = function() {
		return "<img style='position:absolute; left:7px; top: 7px;' src='/staticApp/images/common/cafe/icn/rank_6.gif'>";
	}
	
	this.getTitleText = function() {
		return "Theme Title";
	}

	this.getThemeTitle = function() {
		var htmls = [];
		htmls[htmls.length] = this.getTitleIcon();
		htmls[htmls.length] = "<span style='white-space:nowrap; position:absolute; font-size:9pt; font-weight:bold; left:27px; top:6px; color:black;'>"+this.getTitleText()+"</span>";
		htmls[htmls.length] = "<span style='white-space:nowrap; position:absolute; font-size:9pt; font-weight:bold; left:26px; top:5px; color:white;'>"+this.getTitleText()+"</span>";
		return htmls.join("\n");
	}
	
	this.themeTop = function() {
		var htmls = [];
		htmls[htmls.length] = "<table border='0' cellpadding='0' cellspacing='0'>";
		htmls[htmls.length] = "<tr>";
		htmls[htmls.length] = "<td>";
		htmls[htmls.length] = "<table width='100%' border='0' cellpadding='0' cellspacing='0'>";
		htmls[htmls.length] = "<tr>";
		htmls[htmls.length] = "<td width='20' nowrap='nowrap' style='background:url(/staticApp/images/common/admin/uif_tl.gif) no-repeat;'/>"
		htmls[htmls.length] = this.getThemeTitle();
		htmls[htmls.length] = "</td>";
		htmls[htmls.length] = "<td width='100%' height='29' style='background:url(/staticApp/images/common/admin/uif_tc.gif);'></td>";
		htmls[htmls.length] = "<td width='26' height='29' nowrap='nowrap' style='background:url(/staticApp/images/common/admin/uif_tr.gif) no-repeat top right;'>";
		htmls[htmls.length] = "<img style='position:relative;top:1px;' src='/staticApp/images/common/admin/ui_close_out.gif' onmouseover='makeButton(this,\"ui_close_over.gif\",\"ui_close_down.gif\")' onclick='"+this.qName()+".close()'>"
		htmls[htmls.length] = "</td>";
		htmls[htmls.length] = "</tr>";
		htmls[htmls.length] = "</table>";
		htmls[htmls.length] = "</td>";
		htmls[htmls.length] = "</tr>";
		htmls[htmls.length] = "<tr>";
		htmls[htmls.length] = "<td>";
		htmls[htmls.length] = "<table border='0' cellpadding='0' cellspacing='0'>";
		htmls[htmls.length] = "<tr>";
		htmls[htmls.length] = "<td width='3' nowrap='nowrap' style='background:url(/staticApp/images/common/admin/uif_ml.gif) repeat-y;'></td>";
		htmls[htmls.length] = "<td style='background-color:#f3f1e6;'>";
		return htmls.join("\n");
	}

	this.themeBtm = function() {
		var htmls = [];
		htmls[htmls.length] = "</td>";
		htmls[htmls.length] = "<td width='3' nowrap='nowrap' style='background:url(/staticApp/images/common/admin/uif_mr.gif) repeat-y;'></td>";
		htmls[htmls.length] = "</tr>";
		htmls[htmls.length] = "</table>";
		htmls[htmls.length] = "</td>";
		htmls[htmls.length] = "</tr>";
		htmls[htmls.length] = "<tr>";
		htmls[htmls.length] = "<td>";
		htmls[htmls.length] = "<table width='100%' border='0' cellpadding='0' cellspacing='0'>";
		htmls[htmls.length] = "<tr>";
		htmls[htmls.length] = "<td width='20' height='3' nowrap='nowrap' style='background:url(/staticApp/images/common/admin/uif_bl.gif) no-repeat;'></td>";
		htmls[htmls.length] = "<td width='100%' height='3' style='background:url(/staticApp/images/common/admin/uif_bc.gif);'></td>";
		htmls[htmls.length] = "<td width='20' height='3' nowrap='nowrap' style='background:url(/staticApp/images/common/admin/uif_br.gif) no-repeat;'></td>";
		htmls[htmls.length] = "</tr>";
		htmls[htmls.length] = "</table>";
		htmls[htmls.length] = "</td>";
		htmls[htmls.length] = "</tr>";
		htmls[htmls.length] = "</table>";
		return htmls.join("\n");
	}
	
	this.initRender = function() {
		if( this.popover && this.overlay ) {
			if( !this.useTheme && this.enableOuterClose ) {
				this.overlay.onclick = new Function( this.qName()+".cancel();");
				
				if(this.backgroundWhite)
					this.display.style.backgroundColor = "white";
			}
			return;
		}
		this.hider("OBJECT,EMBED");
		if( this.ie6 ) this.hideCombo();

		this.popover = document.createElement("DIV");
		this.overlay = document.createElement("DIV");
		this.display = document.createElement("DIV");
		this.popover.style.position = "absolute";
		this.popover.style.width = "100%";
		this.popover.style.height = "100%";
		this.popover.style.left = "0px";
		this.popover.style.top = "0px";
		this.popover.style.zIndex = 1000000;
		if( this.opacity ) {
			this.popover.style.backgroundColor = "#000000";
			this.popover.style.filter = "alpha(opacity="+this.opacity+")";
			this.popover.style.opacity = this.opacity/100.0;
		}
		this.overlay.style.position = "absolute";
		this.overlay.style.width = "100%";
		this.overlay.style.height = "100%";
		this.overlay.style.left = "0px";
		this.overlay.style.top = "0px";
		this.overlay.style.zIndex = 1000000;

		if( !this.useTheme && this.enableOuterClose ) {
			this.overlay.onclick = new Function( this.qName()+".cancel();");
			
			if(this.backgroundWhite)
				this.display.style.backgroundColor = "white";
		}

		this.display.style.position = "absolute";
		//this.display.style.padding = "3px";
		this.display.onclick = new Function(this.qName()+".click();");
		this.overlay.appendChild( this.display );

		document.body.appendChild( this.popover );
		document.body.appendChild( this.overlay );
	}
	
	this.hideCombo = function() { this.visibles("SELECT"); }
	this.showCombo = function() { this.visibles(); }
	this.hider = function(tags) { this.visibles(tags); }
	this.shower = function(tags) { this.visibles(); }
	this.visibles = function(tags) {
		if( tags ) {
			var value = "hidden";
			var tg = tags.split(",");
			for(var x=0;x<tg.length;x++) {
				var rr = document.getElementsByTagName(tg[x]);
				for(var i=0;i<rr.length;i++) {
					if( rr[i].style.visibility==value ) continue;
					this.hideObjs[this.hideObjs.length] = rr[i];
					rr[i].style.visibility = value;
				}
			}
		} else {
			for(var x=0;x<this.hideObjs.length;x++ ) {
				this.hideObjs[x].style.visibility = "";
			}
		}
	}

	this.render = function(x,y) {
		this._x = x;
		this._y = y;
		this.initRender();
		this.onscroll();
		this.resetAfters();

		var htmls = [];
		if( this.useTheme ) {
			htmls[htmls.length] = this.themeTop();
			htmls[htmls.length] = this.getHTML();
			htmls[htmls.length] = this.themeBtm();
		} else {
			htmls[htmls.length] = this.getHTML();
		}

		this.display.innerHTML = htmls.join("\n");
		var sx = (this.overlay.offsetWidth - this.display.offsetWidth) * 0.5 + (x?x:0);
		var sy = (this.overlay.offsetHeight - this.display.offsetHeight) * 0.5 + (y?y:0);
		this.display.style.left = Math.max(sx,0) + "px";
		this.display.style.top = Math.max(sy,0) + "px";
		return this.onAfter();
	}
	
	this.resetAfters = function() {
		this.afters = [];
	}
	
	this.addCreate = function(fn,args) {
		if( !this.crts ) return this;
		this.crts[this.crts.length] = { "fn":fn,"args":args };
		return this;
	}

	this.addAfter = function(fn,args) {
		this.afters[this.afters.length] = { "fn":fn,"args":args };
		return this;
	}

	this.onAfter = function() {
		if( this.crts ) {
			for(var s in this.crts) {
				var obj = this.crts[s];
				if( typeof(obj.fn)!="function" ) continue;
				obj.fn.apply( this,obj.args );
			}
			this.crts = null;
		}
		for(var s in this.afters) {
			var obj = this.afters[s];
			if( typeof(obj.fn)!="function" ) continue;
			obj.fn.apply( this,obj.args );
		}
		return this;
	}
	
	this.setThemeOff = function() {
		this.useTheme = false;
		return this;
	}
	this.setThemeOn = function() {
		this.useTheme = true;
		return this;
	}
}
jpLayer.prototype = new jpObject;

function jpTree(items,cfgs,name) {
	this._super = this.superClass;
	this.superClass = this;

	this.opens = null;
	this.cfgs = cfgs;
	this.item = items;
	this.name = name?name:(""+this.id);
	this.vidx = 0;
	this.idep = 0;
	this.curr = null;
	this.saveCurr = true;

	if( !this.cfgs ) this.cfgs = [];
	if( !this.cfgs.base ) this.cfgs.base = '';
	if( !this.cfgs.rootClose ) this.cfgs.rootClose = '/static/js/jp/img/root_close.gif';
	if( !this.cfgs.rootOpen ) this.cfgs.rootOpen = '/static/js/jp/img/root_open.gif';
	if( !this.cfgs.fclose ) this.cfgs.fclose = '/static/js/jp/img/folder_close.gif';
	if( !this.cfgs.fopen ) this.cfgs.fopen = '/static/js/jp/img/folder_open.gif';
	if( !this.cfgs.file ) this.cfgs.file = '/static/js/jp/img/folder_leaf.gif';

	this.quots = function(text) {
		return text.replace(new RegExp("'","g"),"\\'").replace(new RegExp('"',g),'\\"');
	}

	this.collapse = function(elemID,isRoot) {
		var el = document.getElementById('tdiv_'+this.id+'_'+elemID);
		if( el ) {
			var opened = (el.style.display=='none')?true:false;
			el.style.display = opened?'block':'none';
			var ei = document.getElementById('tii_'+this.id+'_'+elemID);
			if(ei) ei.src = (!opened)?(this.cfgs.base+(isRoot?this.cfgs.rootClose:this.cfgs.fclose)):(this.cfgs.base+(isRoot?this.cfgs.rootOpen:this.cfgs.fopen));
			this.opens[elemID] = (opened)?elemID:'';
			var kv = [];
			for(var s in this.opens) if( this.opens[s] ) kv[kv.length] = s;
			jpCookie.setValue('jpTree_'+this.name,kv.join(",") );
		}
		return el;
	}
	
	this.saveSelected = function(bool) {
		this.saveCurr = bool;
		if( !this.saveCurr )
			jpCookie.remove('jpTree_'+this.name+'_c');
		return this;
	}
	
	this.setCurr = function(iidx) {
		if( !this.saveCurr ) return;
		jpCookie.setValue('jpTree_'+this.name+'_c',iidx );
		return this;
	}

	this.click = function(iidx,href,num) {
		var le = document.getElementById("tia_"+this.id+"_"+this.curr);
		if( le ) { le.style.backgroundColor = ''; le.style.color = ''; }
		le = document.getElementById("tia_"+this.id+"_"+(this.curr=iidx));
		if( le ) { le.style.backgroundColor = 'blue'; le.style.color = 'white'; }
		this.setCurr( iidx );
		if( href.substr(0,11).toLowerCase()=='javascript:' ) {
			eval( href.split(':',2)[1] );
		} else {
			document.location = href;
		}
		return false;
	}
	
	this.recursive = function(item,isRoot) {
		var htmls = [];
		var itemName = item[0].split("::")[0];
		var itemID = item[0].split("::")[1];
		this.vidx ++;
		this.iidx = itemID ? itemID : this.vidx;
		
		var exb = item.length>3;
		var stc = this.curr==this.iidx ? 'background-color:blue;color:white;' : '';
		htmls[htmls.length] = '<TABLE class="tree_table" width="100%" border="0" cellpadding="0" cellspacing="0">';
		htmls[htmls.length] = '<TR class="tree_tr">';
		if( exb ) {
			htmls[htmls.length] = '<TD class="tree_icon_td" align="center" nowrap width="16" style="white-space:nowrap;cursor:pointer;cursor:hand;" onclick="'+this.qName()+'.collapse(\''+this.iidx+'\','+(isRoot?true:false)+');"><img class="tree_icon_folder" id="tii_'+this.id+'_'+this.iidx+'" src="'+(this.cfgs.base+(isRoot?this.cfgs.rootClose:this.cfgs.fclose))+'"></TD>';
		} else {
			htmls[htmls.length] = '<TD class="tree_icon_td" align="center" nowrap width="16"><img class="tree_icon_file" id="tii_'+this.id+'_'+this.iidx+'" src="'+(this.cfgs.base+(isRoot?this.cfgs.rootOpen:this.cfgs.file))+'"></TD>';
		}
		htmls[htmls.length] = '<TD class="tree_anchor_td" nowrap style="white-space:nowrap;cursor:pointer;cursor:hand;"><a class="tree_anchor" id="tia_'+this.id+'_'+this.iidx+'" style="padding:2px;padding-left:3px;'+stc+'" onclick="return '+this.qName()+'.click(\'' + this.iidx + '\',\'' + item[1].replace(/'/g,"\\'") + '\')">' + itemName + '</a></TD>';
		htmls[htmls.length] = '<TD width="100%"></TD>';
		htmls[htmls.length] = '</TR>';
		htmls[htmls.length] = '</TABLE>';

		if( exb ) {
			htmls[htmls.length] = '<DIV id="tdiv_'+this.id+'_'+this.iidx+'" class="tree_div" style="display:none; padding-left:16px;">';
			for(var i=3;i<item.length;i++ )
				htmls[htmls.length] = this.recursive( item[i] );
			htmls[htmls.length] = '</DIV>';
		}
		return htmls.join("\n");
	}

	this.openItems = function() {
		for(var s in this.opens) {
			if( !this.opens[s] ) continue;
			this.collapse( this.opens[s],s==1 );
		}
	}

	this.getHTML = function() {
		return this.recursive(this.item[0],true);
	}

	this.render = function() {
		document.write( this.getHTML() );
		this.openItems();
	}

	this.renderTo = function(elem) {
		elem.innerHTML = this.getHTML();
		this.openItems();
	}
	
	this.initCookies = function() {
		this.opens = [];
		this.curr = ""+jpCookie.getValue('jpTree_'+this.name+'_c' );
		var trops = ""+jpCookie.getValue('jpTree_'+this.name);
		if( !trops ) return;
		var iis = trops.split(',');
		for(var x=0;x<iis.length;x++ ) {
			if( !iis[x] ) continue;
			this.opens[iis[x]] = iis[x];
		}
		return this;
	}

	this.add();
	this.initCookies();
}
jpTree.prototype = new jpObject;
