/* Paraselene Copyright (c) 2009, 2010  Akira Terasaki */
Paraselene=function(){
this.get_id=function(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
*/
this.browser=0;
this.init=function(){
	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;
	}
};

this.max=function(a,b){
	if(!a)	return b;
	if(!b)	return a;
	if(a>b)	return a;
	return b;
};

this.min=function(a,b){
	if(!a)	return b;
	if(!b)	return a;
	if(a<b)	return a;
	return b;
};

this.WindowSize=function(){
	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);
};

this.DocumentSize=function(){
	var size=new paraselene.WindowSize();
	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);
};

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

this.TagXY=function(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;
	}
};

this.tag_position=function(tag,x,y){
	var win=new paraselene.WindowSize();
	var size=new paraselene.TagSize(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";
};

this.tag_center=function(tag,x,y){
	var sc_x=document.documentElement.scrollLeft || document.body.scrollLeft;
	var sc_y=document.documentElement.scrollTop || document.body.scrollTop;
	var size=new paraselene.TagSize(tag);
	paraselene.tag_position(tag,x-size.width/2+sc_x,y-size.height/2+sc_y);
};

this.tag_move=function(tag,x,y){
	if((x===0)&&(y===0))	return;
	var xy=new paraselene.TagXY(tag);
	paraselene.tag_position(tag,xy.x+x,xy.y+y);
};

this.move_obj;
this.move_x=0;
this.move_y=0;

this.mous_up_x=0;
this.mous_up_y=0;

this.get_event=function(ev){
	if(ev)	return ev;
	return window.event;
};

this.dialog_set_event=function(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;
	};
};

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

this.safe_add_event=function(id,kind,func){
	var tag=paraselene.get_id(id);
	if(!tag)	return;
	paraselene.add_event(tag,kind,func);
};

this.kill_event=function(ev){
	try {
		ev=paraselene.get_event(ev);
		if(ev.preventDefault){
			ev.preventDefault();
		}
		else{
			ev.returnValue=false;
		}
	}
	catch(e){}
};

this.form_kill=false;

this.get_http=function(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;
};

this.json_cnt=0;

this.make_dummy=function(){
	var p=new Date().getTime().toString(36) + paraselene.json_cnt.toString(36);
	paraselene.json_cnt++;
	return p;
};

this.json_url_get=function(p){
	var url="paraselene.json.na?dmy="+paraselene.make_dummy();
	url=url+"&key="+p;
	return url;
};

this.back_kill_f=false;
this.back_id;
this.back_kill=function(){
	if(!paraselene.back_kill_f) return;
	history.forward();
	paraselene.back_id=setTimeout(paraselene.back_kill,10);
};

this.back_kill_start=function(){
	if(paraselene.back_id){
		clearTimeout(paraselene.back_id);
	}
	else{
		document.location.href=document.location.href.split('#')[0]+"#__paraselene__";
	}
	paraselene.back_kill_f=true;
	paraselene.back_kill();
};

this.page_drop_f=false;
this.comet_page=new Array();
this.comet_count=function() {
	var cnt=0;
	for (var k in paraselene.comet_page){
		if(paraselene.comet_page[k]==true)	cnt++;
	}
	return cnt;
};

this.popup=new Array();

this.back_kill_stop=function(){
	if(!paraselene.page_drop_f){
		if(paraselene.comet_count()>0)	return;
		if(paraselene.popup.length>0)	return;
	}
	paraselene.back_kill_f=false;
};

this.tag_remove=function(tag){
	var out=tag.parentNode;
	if(out) out.removeChild(tag);
	if(paraselene.popup.length===0)	paraselene.back_kill_stop();
};

this.z_order=function(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.tag_remove(pop);
		top=paraselene.popup.pop();
		if(top){
			paraselene.ClassZOrder(top,"paraseleneTopOrder");
			paraselene.popup.push(top);
		}
		return;
	}
	if(top){
		paraselene.popup.push(top);
	}
	if(pop==top)	return;
	if(!del_f && top){
		paraselene.ClassZOrder(top,"paraseleneNextOrder");
	}

	var des=new Array();
	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.tag_remove(pop);
		top=paraselene.popup.pop();
		if(top){
			paraselene.ClassZOrder(top,"paraseleneTopOrder");
			paraselene.popup.push(top);
		}
		return;
	}
	paraselene.ClassZOrder(pop,"paraseleneTopOrder");
	pop.style.zIndex=last_z;
	des.push(pop);
};

this.modal_count=0;

this.back_resize=function(){
	if(paraselene.modal_count===0)	return;
	var size=new paraselene.DocumentSize();
	var x=size.width;
	var y=size.height;
	for (var i=0;i<paraselene.popup.length;i++){
		size=new paraselene.TagSize(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);
};

this.dialog_open=function(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){
		paraselene.ClassZOrder(top,"paraseleneNextOrder");
		paraselene.popup.push(top);
	}
	paraselene.popup.push(base);

	var size=new paraselene.WindowSize();
	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();
};

this.ClassZOrder=function(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];
	}
};

this.url_cut=function(url){
	var parts=url.split("//");
	if(url.length<2)	return undefined;
	var tail=parts[1];
	for(var i=2;i<parts.length;i++){
		tail=tail+"/"+parts[i];
	}
	var dir=tail.split("/");
	var head=parts[0]+"//"+dir[0];
	var foot="";
	for(var i=1;i<dir.length;i++){
		foot=foot+"/"+dir[i];
	}
	if(foot=="")foot="/";
	return new Array(head,foot);
};

this.jump_url=undefined;
this.location_replace=function(next){
	var org=undefined;
	if(this.jump_url){
		org=this.url_cut(this.jump_url);
	}
	this.jump_url=undefined;
	if(!org){
		org=this.url_cut(window.location.href);
	}
	var tgt=this.url_cut(next);
	return org[0]+tgt[1];
};

this.forwarding=false;
this.action=function(obj){
	var resize_f=false;
	for(var i=0;i<obj.data.length;i++){
		var val=obj.data[i];
		if(val.location){
			paraselene.form_reset();
			paraselene.page_drop_f=true;
			paraselene.back_kill_stop();
			if(val.location_mine){
				window.location.href=this.location_replace(val.location);
			}
			else{
				window.location.href=val.location;
			}
			paraselene.forwarding=true;
			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;
		}
	}
};

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

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

	var na_test = url.split("\.");
	if ( na_test[na_test.length-1]!="na")	return undefined;

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

this.error_page=function(svr){
	var err=svr.responseText;
	document.open();
	document.write(err);
	document.close();
};

this.get_a=function(o,url,key){
	if(paraselene.get_a_calling)	return false;
	url=paraselene.url_check(url,"paraselene_a_ajax="+key);
	if(!url)	return true;
	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";
		}
	);
	document.body.style.cursor="wait";
	svr.open('GET',url,false);
	svr.send('');
	return true;
};

this.agency_a=function(o,key){
	if(paraselene.forwarding)	return false;
	if(!o)	return false;
	var org=o.getAttribute("href");
	if(!org)	return false;
	if(org.charAt(0)=='#')	return false;
	this.jump_url=org;
	var frg=org.split("#");
	var href=frg[0].split("?");
	href[0]=href[0]+"?paraselene_dummy="+paraselene.make_dummy();
	if(href[1]){
		href[0]=href[0]+"&"+href[1];
	}
	if(frg[1]){
		href[0]=href[0]+"#"+frg[1];
	}
	paraselene.get_a(o,href[0],key);
	try{
		o.setAttribute("href",org);
	}
	catch(e){}
	return false;
};

this.called_form;
this.form_org_action;

this.agency_form=function(o,key){
	if(paraselene.forwarding)	return false;
	if(!o)	return false;
	if(!o.action)	return false;
	if(o.target) {
		if ("_self"!=o.target)	return true;
	}
	if(paraselene.called_form){
		return false;
	}
	if(paraselene.form_kill)	return false;
	paraselene.form_org_action=o.action;
	var url=paraselene.url_check(o.action,undefined);
	if(!url)	return true;
	this.jump_url=o.action;
	o.target="paraseleneDragonsTail";
	o.paraselene_form_ajax.value=key;
	o.paraselene_dummy.value=paraselene.make_dummy();
	paraselene.called_form=o;
	return true;
};

this.form_reset=function(){
	if(!paraselene.called_form)return;
	paraselene.called_form.action=paraselene.form_org_action;
	paraselene.called_form.target="_self";
	paraselene.called_form.paraselene_form_ajax.value=-1;
};

this.get_form=function(key){
	paraselene.form_reset();
	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_get(key),false);
	svr.send('');
};

this.comet_svr;
this.comet_callback=function(){
	paraselene.comet_svr=paraselene.get_http(function(s){
		if(paraselene.comet_count()===0)	return;
		if(s.status===200){
			paraselene.action(eval('('+s.responseText+')'));
			setTimeout(paraselene.comet_callback,200);
			return;
		}
		setTimeout(paraselene.comet_callback,5000);
	});
	paraselene.comet_svr.open('GET',paraselene.json_url_get('paraselene_comet'),true);
	paraselene.comet_svr.send('');
};

this.comet_start=function(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();
};

this.comet_stop=function(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();
};

this.form_ok=function (key){
	if(!paraselene.called_form)	return;
	paraselene.get_form(key);
};

this.form_comp=function(){
	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("form_ok")>0)	return;
	document.open();
	document.write(cont);
	document.close();
};

this.get_key=function(ev){
	ev=paraselene.get_event(ev);
	if(ev.charCode)	return ev.charCode;
	else if(ev.which)	return ev.which;
	return ev.keyCode;
};
this.kill_enter=function(ev){
	var k=paraselene.get_key(ev);
	if(k===13||k===3){
		paraselene.kill_event(ev);
		return false;
	}
	return true;
};

};

var paraselene;
if(!paraselene){
	paraselene=new Paraselene();
	paraselene.init();
	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;
	};
}
