function paraselene_get_id(id){
	if(document.getElementById){
		return document.getElementById(id);
	}else if(document.all){
		return document.all(id);
	}else if(document.layers){
		return document.layers[id];
	}
}

/*
	0:unknown
	100:Opera
	200:Google Chrome
	300:Safari
	400:Firefox
	500:IE
*/
var paraselene_browser=0;
function paraselene_get_agent(){
	var ua=navigator.userAgent;
	if(ua.indexOf("Opera")>=0){
		paraselene_browser=100;
	}
	else if(ua.indexOf("Chrome")>=0){
		paraselene_browser=200;
	}
	else if(ua.indexOf("Safari")>=0){
		paraselene_browser=300;
	}
	else if(ua.indexOf("Firefox")>=0){
		paraselene_browser=400;
	}
	else if(ua.indexOf("MSIE")>=0){
		paraselene_browser=500;
	}
}
paraselene_get_agent();

function paraselene_max(a,b){
	if(!a)	return b;
	if(!b)	return a;
	if(a>b)	return a;
	return b;
}

function paraselene_min(a,b){
	if(!a)	return b;
	if(!b)	return a;
	if(a<b)	return a;
	return b;
}

function ParaseleneWindowSize(){
	this.top=paraselene_max(window.pageYOffset,window.scrollTop);
	this.top=paraselene_max(this.top,document.documentElement.scrollTop);
	this.top=paraselene_max(this.top,document.body.scrollTop);

	this.left=paraselene_max(window.pageXOffset,window.scrollLeft);
	this.left=paraselene_max(this.left,document.documentElement.scrollLeft);
	this.left=paraselene_max(this.left,document.body.scrollLeft);

	this.height=paraselene_min(window.innerHeight,document.body.clientHeight);
	this.height=paraselene_min(this.height,document.documentElement.clientHeight);
	this.height=paraselene_min(this.height,document.body.scrollHeight);

	this.width=paraselene_min(window.innerWidth,document.body.clientWidth);
	this.width=paraselene_min(this.width,document.documentElement.clientWidth);
	this.width=paraselene_min(this.width,document.body.scrollWidth);
}

function ParaseleneDocumentSize(){
	var size=new ParaseleneWindowSize();
	this.height=paraselene_max(document.documentElement.scrollHeight,document.body.scrollHeight);
	this.height=paraselene_max(this.height,size.height);
	this.width=paraselene_max(document.documentElement.scrollWidth,document.body.scrollWidth);
	this.width=paraselene_max(this.width,size.width);
}

function ParaseleneTagSize(tag){
	var rect=tag.getBoundingClientRect();
	this.width=rect.right-rect.left+1;
	this.height=rect.bottom-rect.top+1;
}

function ParaseleneTagXY(tag){
	this.x=parseInt(tag.style.left);
	this.y=parseInt(tag.style.top);
	if (!this.x && !this.y){
		var rect=tag.getBoundingClientRect();
		this.x=rect.left;
		this.y=rect.top;
	}
}

function paraselene_tag_position(tag,x,y){
	var win=new ParaseleneWindowSize();
	var size=new ParaseleneTagSize(tag);
	var sub;

	if(x<win.left)	x=win.left;
	if((x+size.width)>(win.left+win.width)){
		sub=x+size.width-(win.left+win.width);
		x=x-sub;
	}
	if(x<0)	x=0;

	if(y<win.top)	y=win.top;
	if((y+size.height)>(win.top+win.height)){
		sub=y+size.height-(win.top+win.height);
		y=y-sub;
	}
	if(y<0)	y=0;
	tag.style.left=x+"px";
	tag.style.top=y+"px";
}

function paraselene_tag_center(tag,x,y){
	var sc_x=document.documentElement.scrollLeft || document.body.scrollLeft;
	var sc_y=document.documentElement.scrollTop || document.body.scrollTop;
	var size=new ParaseleneTagSize(tag);
	paraselene_tag_position(tag,x-size.width/2+sc_x,y-size.height/2+sc_y);
}

function paraselene_tag_move(tag,x,y){
	if((x===0)&&(y===0))	return;
	var xy=new ParaseleneTagXY(tag);
	paraselene_tag_position(tag,xy.x+x,xy.y+y);
}

var paraselene_move_obj;
var paraselene_move_x;
var paraselene_move_y;

var paraselene_mous_up_x=0;
var paraselene_mous_up_y=0;

function paraselene_get_event(ev){
	if(ev)	return ev;
	return window.event;
}

function paraselene_dialog_set_event(dialog,tag){
	dialog.onmousedown=function(kmouse){
		paraselene_z_order(dialog,false);
		return true;
	};
	tag.onmousedown=function(kmouse){
		if(paraselene_move_obj)return true;
		kmouse=paraselene_get_event(kmouse);
		paraselene_z_order(dialog,false);
		paraselene_move_obj=dialog;
		paraselene_move_x=kmouse.clientX;
		paraselene_move_y=kmouse.clientY;
		return false;
	};
}

document.onmouseup=function(kmouse){
	kmouse=paraselene_get_event(kmouse);
	paraselene_mous_up_x=kmouse.clientX;
	paraselene_mous_up_y=kmouse.clientY;
	if(!paraselene_move_obj)	return true;
	paraselene_move_obj=undefined;
	return false;
};
document.onmousemove=function(kmouse){
	if(!paraselene_move_obj)return true;
	kmouse=paraselene_get_event(kmouse);
	if(paraselene_browser===500){
		if(kmouse.button==0){
			paraselene_move_obj=undefined;
			return true;
		}
	}
	paraselene_tag_move(paraselene_move_obj,kmouse.clientX-paraselene_move_x,kmouse.clientY-paraselene_move_y);
	paraselene_move_x=kmouse.clientX;
	paraselene_move_y=kmouse.clientY;
	return false;
};

function paraselene_add_event(tag,kind,func){
	if(tag.addEventListener){
		tag.addEventListener(kind,func,false);
	}
	else{
		tag.attachEvent("on"+kind,func);
	}
}

function paraselene_remove_event(tag,kind,func){
	if(tag.removeEventListener){
		tag.removeEventListener(kind,func,false);
	}
	else{
		tag.detachEvent("on"+kind,func);
	}
}

var paraselene_do_fund=new Array(0);
function paraselene_safe_add_event(id,kind,func){
	if(paraselene_do_fund[func])	return;
	paraselene_do_fund[func]=true;
	var tag=paraselene_get_id(id);
	if(!tag)	return;
	try{
		paraselene_remove_event(tag,kind,func);
	}
	catch(e){}
	paraselene_add_event(tag,kind,func);
}

function paraselene_kill_event(ev){
	ev=paraselene_get_event(ev);
	if(ev.preventDefault){
		ev.preventDefault();
	}
	else{
		ev.returnValue=false;
	}
}


function paraselene_a_handler(id,page){
	var tag=paraselene_get_id(id);
	paraselene_add_event(tag,"click",function(ev){
		if(!paraselene_agency_a(tag,page)){
			paraselene_kill_event(ev);
		}
	});
}

var paraselene_form_kill=false;
function paraselene_button_select(flag){
	paraselene_form_kill=flag;
	return true;
}
function paraselene_form_handler(id,page){
	var tag=paraselene_get_id(id);
	paraselene_add_event(tag,"submit",function(ev){
		if(paraselene_form_kill)	return;
		if(!paraselene_agency_form(tag,page)){
			paraselene_kill_event(ev);
		}
	});
}
function paraselene_button_handler(id,flag){
	var tag=paraselene_get_id(id);
	paraselene_add_event(tag,"click",function(ev){
		paraselene_button_select(flag);
	});
}

function ParaseleneAjax(key,callback,msec){
	this.inteval=msec;
	this.callback=callback;
	this.key=key;
	this.http=null;
	this.no=-1;
}

var paraselene_holder=new Array(0);

function paraselene_receive(ps){
	if(ps.http.status!=200){
		if(ps.http.status==503){
			setTimeout('paraselene_holder['+ps.no+'].http_access()',ps.inteval);
		}
		return;
	}
	var obj=eval('('+ps.http.responseText+')');
	ps.callback(obj);
	setTimeout('paraselene_holder['+ps.no+'].http_access()',ps.inteval);
}

function paraselene_get_http(f){
	var obj=undefined;
	if(window.XMLHttpRequest){
		obj=new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		try{
			obj=new ActiveXObject('Msxml2.XMLHTTP');
		}catch(e){
			obj=new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
	if(!obj)return undefined;

	switch(paraselene_browser){
	case 100:
	case 200:
	case 300:
	case 400:
		obj.onload=function(){
			f(obj);
		};
		break;
	default:
		obj.onreadystatechange=function(){
			if(obj.readyState==4){
				f(obj);
			}
		};
		break;
	}

	return obj;
}

var paraselene_json_cnt=0;

function paraselene_make_dummy(){
	var p=new Date().getTime().toString(36) + paraselene_json_cnt.toString(36);
	paraselene_json_cnt++;
	return p;
}

function paraselene_json_url_gaet(p){
	var url="paraselene.json.na?paraselene_dmy="+paraselene_make_dummy();
	url=url+"&key="+p;
	return url;
}

ParaseleneAjax.prototype={
	http_access:function(){
		this.http=paraselene_get_http(function(){
				return function(){
					paraselene_receive(this);
				};
			}
		);
		if(!this.http)	return;
		var param=this.key;
		this.http.open('GET',paraselene_json_url_gaet(param),true);
		this.http.send('');
	}
};

function paraselene_ajax_entry2(key,callback,msec){
	paraselene_holder.push(new ParaseleneAjax(key,callback,msec));
	paraselene_holder[paraselene_holder.length-1].no=paraselene_holder.length-1;
	paraselene_holder[paraselene_holder.length-1].http_access();
}

function paraselene_ajax_entry(key,callback,msec){
	paraselene_ajax_entry2(key,callback,msec);
}

var paraselene_back_kill_f=false;

function paraselene_back_kill(){
	if(!paraselene_back_kill_f) return;
	history.forward();
	setTimeout(paraselene_back_kill,10);
}

function paraselene_back_kill_start(){
	if(paraselene_back_kill_f)return;
	document.location.href=document.location.href.split('#')[0]+"#__paraselene__";
	paraselene_back_kill_f=true;
	paraselene_back_kill();
}

var paraselene_page_drop_f=false;
var paraselene_comet_page=new Array();
function paraselene_comet_count() {
	var cnt=0;
	for (var k in paraselene_comet_page){
		if(paraselene_comet_page[k]==true)	cnt++;
	}
	return cnt;
}

function paraselene_back_kill_stop(){
	if(!paraselene_page_drop_f){
		if(paraselene_comet_count()>0)	return;
		if(paraselene_popup.length>0)	return;
	}
	paraselene_back_kill_f=false;
}

var paraselene_popup=new Array(0);

function paraselene_remove(tag){
	var out=tag.parentNode;
	if(out) out.removeChild(tag);
	if(paraselene_popup.length===0)	paraselene_back_kill_stop();
}

function paraselene_z_order(pop,del_f){
	if(!pop)	return;
	if(paraselene_modal_count>0 && del_f==false)	return;
	var top=paraselene_popup.pop();
	if(pop==top&&del_f){
		paraselene_remove(pop);
		top=paraselene_popup.pop();
		if(top){
			paraseleneClassZOrder(top,"paraseleneTopOrder");
			paraselene_popup.push(top);
		}
		return;
	}
	if(top){
		paraselene_popup.push(top);
	}
	if(pop==top)	return;
	if(!del_f && top){
		paraseleneClassZOrder(top,"paraseleneNextOrder");
	}

	var des=new Array(0);
	var flag=false;
	var last_z=0;
	while(paraselene_popup.length>0){
		var old=paraselene_popup.shift();
		last_z=old.style.zIndex;
		if(pop==old){
			flag=true;
			continue;
		}
		if(flag)old.style.zIndex=old.style.zIndex-1;
		des.push(old);
	}
	paraselene_popup=des;
	if(del_f){
		paraselene_remove(pop);
		top=paraselene_popup.pop();
		if(top){
			paraseleneClassZOrder(top,"paraseleneTopOrder");
			paraselene_popup.push(top);
		}
		return;
	}
	paraseleneClassZOrder(pop,"paraseleneTopOrder");
	pop.style.zIndex=last_z;
	des.push(pop);
}

var paraselene_modal_count=0;

function paraselene_back_resize(){
	if(paraselene_modal_count===0)	return;
	var size=new ParaseleneDocumentSize();
	var x=size.width;
	var y=size.height;
	for (var i=0;i<paraselene_popup.length;i++){
		size=new ParaseleneTagSize(paraselene_popup[i]);
		x=paraselene_max(x,size.width);
		y=paraselene_max(y,size.height);
	}
	var back=paraselene_get_id("paraseleneEclipse");
	back.style.height=y+"px";
	back.style.width=x+"px";
	setTimeout(paraselene_back_resize,200);
}

function paraselene_dialog_open(dialog){
	var point=paraselene_get_id("paraseleneDragonsHead");
	if(!point)	return;
	var pageid=dialog.page_id;
	if(dialog.active){
		var act=paraselene_get_id(pageid);
		paraselene_z_order(act,false);
		return;
	}

	var z=paraselene_popup.length+dialog.base_z+1;
	if(dialog.modal==true){
		var back=paraselene_get_id("paraseleneEclipse");
		if(paraselene_modal_count<1){
			back.style.zIndex=z;
			back.style.display="block";
			paraselene_modal_count++;
			paraselene_back_resize();
		}
		else{
			back.style.zIndex++;
			paraselene_modal_count++;
		}
	}
	var base=document.createElement("div");
	base.id=pageid;
	base.style.zIndex=z;

	var body=document.createElement("div");
	body.id=dialog.id;
	body.style.background=dialog.bg;
	body.innerHTML=dialog.body;
	if(dialog.move==true){
		base.className="paraselenePopupBase paraseleneTopOrder";
		base.style.borderColor=dialog.border;
		base.style.background=dialog.bg2;

		var head=document.createElement("div");
		head.className="paraselenePopupHead paraseleneGradient";
		head.style.borderColor=dialog.border;

		var a=document.createElement("span");
		a.className="paraselenePopupHead";
		a.innerHTML=dialog.title;
		head.appendChild(a);

		body.className="paraselenePopupBody";
		body.style.borderColor=dialog.border;

		base.appendChild(head);
	}
	else{
		base.className="paraseleneLiteBase paraseleneTopOrder";
		base.style.background=dialog.bg;
	}
	base.appendChild(body);

	var top=paraselene_popup.pop();
	if(top){
		paraseleneClassZOrder(top,"paraseleneNextOrder");
		paraselene_popup.push(top);
	}
	paraselene_popup.push(base);

	var size=new ParaseleneWindowSize();
	point.appendChild(base);
	if(dialog.move==true){
		paraselene_tag_center(base,paraselene_mous_up_x,paraselene_mous_up_y);
		paraselene_dialog_set_event(base,head);
	}
	else{
		paraselene_tag_center(base,size.width/2,size.height/2);
	}
	paraselene_back_kill_start();
}

function paraseleneClassZOrder(tag,n){
	var org=tag.className.split(" ");
	tag.className=n;
	for(var i=0;i<org.length;i++){
		if(org[i]=="paraseleneTopOrder"||org[i]=="paraseleneNextOrder")	continue;
		tag.className=tag.className+" "+org[i];
	}
}

function paraselene_action(obj){
	var resize_f=false;
	for(var i=0;i<obj.data.length;i++){
		var val=obj.data[i];
		if(val.location){
			paraselene_page_drop_f=true;
			paraselene_back_kill_stop();
			window.location.href=val.location;
			continue;
		}
		if(val.open){
			paraselene_dialog_open(val.open);
			resize_f=true;
			if(val.open.cmd)	paraselene_exec(val.open.cmd);
			continue;
		}
		if(val.close){
			var dialog=val.close;
			var pop=paraselene_get_id(dialog.page_id);
			if(pop){
				paraselene_z_order(pop,true);
			}
			if(dialog.modal==true){
				var back=paraselene_get_id("paraseleneEclipse");
				if(paraselene_modal_count>0)	paraselene_modal_count--;
				if(paraselene_modal_count==0) {
					back.style.display="none";
				}
				else{
					back.style.zIndex--;
				}
			}
			resize_f=true;
			continue;
		}
		if(val.tag){
			var update_tag=val.tag;
			var target=paraselene_get_id(update_tag.id);
			if(target)	target.innerHTML=update_tag.body;
			resize_f=true;
			if(update_tag.cmd)	paraselene_exec(update_tag.cmd);
			continue;
		}
		if(val.exec){
			paraselene_exec(val.exec.cmd);
			continue;
		}
	}
}

function paraselene_exec(exec){
	for(var i=0;i<exec.length;i++){
		eval(exec[i]);
	}
}

function paraselene_url_check(url,param){
	if(!navigator.cookieEnabled)	return undefined;
	var url_test=url.split("?");
	var jsession=url_test[0].split(";jsessionid");
	url=jsession[0];

	if(url_test[1]){
		url=url+"?"+url_test[1];
		if(param)	url=url+"&"+param;
	}
	else if(param)	url=url+"?"+param;
	return url;
}

function paraselene_error_page(svr){
	document.open();
	document.write(svr.responseText);
	document.close();
}

function paraselene_get_a(o,url,key){
	url=paraselene_url_check(url,"paraselene_a_ajax="+key);
	if(!url)	return false;
	var org=o.onclick;
	var svr=paraselene_get_http(function(svr){
			if(svr.status!=200){
				paraselene_error_page(svr);
			}
			else{
				o.onclick=org;
				paraselene_action(eval('('+svr.responseText+')'));
			}
			document.body.style.cursor="auto";
		}
	);
	o.onclick=function(){return false;};
	document.body.style.cursor="wait";
	svr.open('GET',url,false);
	svr.send('');
	return true;
}

function paraselene_agency(id,page,f){
	var tag=paraselene_get_id(id);
	if(!tag)	return;
	f(tag,page);
}

function paraselene_agency_a(o,key){
	var org=o.getAttribute("href");
	if(!org)	return false;
	var href=org.split("?");
	href[0]=href[0]+"?paraselene_dummy="+paraselene_make_dummy();
	if(href[1]){
		href[0]=href[0]+"&"+href[1];
	}
	var ret=false;
	if(!paraselene_get_a(o,href[0],key)){
		ret=true;
	}
	o.setAttribute("href",org);
	return ret;
}

var paraselene_called_form;

function paraselene_agency_form(o,key){
	if(!o.action)	return false;
	if(paraselene_form_kill)	return true;
	if(paraselene_called_form){
		return false;
	}
	if(o.target) {
		if ("_self"!=o.target)	return true;
	}
	var url=paraselene_url_check(o.action,undefined);
	if(!url)	return true;
	o.target="paraseleneDragonsTail";
	o.paraselene_form_ajax.value=key;
	o.paraselene_dummy.value=paraselene_make_dummy();
	paraselene_called_form=o;
	return true;
}

function paraselene_get_form(key){
	paraselene_called_form.target="_self";
	paraselene_called_form.paraselene_form_ajax.value=-1;
	var svr=paraselene_get_http(function(svr){
			if(svr.status!=200){
				paraselene_error_page(svr);
			}
			else{
				paraselene_action(eval('('+svr.responseText+')'));
			}
			document.body.style.cursor="auto";
			paraselene_called_form=undefined;
		}
	);
	document.body.style.cursor="wait";
	svr.open('GET',paraselene_json_url_gaet(key),false);
	svr.send('');
}

var paraselene_comet_svr;
function paraselene_comet_callback(){
	paraselene_comet_svr=paraselene_get_http(function(s){
		if(paraselene_comet_count()===0)	return;
		if(s.status===200){
			paraselene_action(eval('('+s.responseText+')'));
			paraselene_comet_timer=setTimeout(paraselene_comet_callback,200);
			return;
		}
		paraselene_comet_timer=setTimeout(paraselene_comet_callback,5000);
	});
	paraselene_comet_svr.open('GET',paraselene_json_url_gaet('paraselene_comet'),true);
	paraselene_comet_svr.send('');
}

function paraselene_comet_start(id){
	var pre=paraselene_comet_count();
	if(paraselene_comet_page[id])	return;
	paraselene_comet_page[id]=true;
	if(pre>0)	return;
	paraselene_back_kill_start();
	paraselene_comet_callback();
}

function paraselene_comet_stop(id){
	if(!paraselene_comet_page[id])	return;
	paraselene_comet_page[id]=false;
	if(paraselene_comet_count()>0)	return;
	if(paraselene_comet_svr){
		paraselene_comet_svr.abort();
		paraselene_comet_svr=undefined;
	}
	paraselene_back_kill_stop();
}

function paraselene_form_ok(key){
	if(!paraselene_called_form)	return;
	paraselene_get_form(key);
}

function paraselene_form_comp(){
	if(!paraselene_called_form)	return;
	var tail=paraselene_get_id("paraseleneDragonsTail");
	var doc=tail.contentDocument;
	if(!doc){
		doc=tail.contentWindow.document;
	}
	var cont;
	var head=doc.getElementsByTagName("head");
	if(head){
		if(head[0])	cont=head[0].innerHTML;
	}
	cont+=doc.body.innerHTML;
	if(cont.indexOf("paraselene_form_ok")>0)	return;
	document.open();
	document.write(cont);
	document.close();
}

