// JavaScript Document

//
// Author : Akihito Okura
//

// mouse position
var pos_x;
var pos_y;

// currently displayed page
var current_page;

// range of browser
var client_width;
var client_height;

// current pages
var cur_aboutus="messages";
var cur_service="right-acquisition";
var cur_recruit="message2you";
var cur_mailto="";

function on_menu(id_name){
	var box=document.getElementById(id_name);
	
	if(!box) return;
	
	if(id_name=="aboutus"||id_name=="services"||id_name=="careers"||id_name=="location"){
		box.src="./img/on_j_"+id_name+".gif";
	}else{
		box.src="../img/on_j_"+id_name+".gif";
	}
	
	if(current_page!=id_name){
		box.style.border="1px dotted #888";
	}
}

function off_menu(id_name){
	if(current_page==id_name) return;
	
	var box=document.getElementById(id_name);
	if(id_name=="aboutus"||id_name=="services"||id_name=="careers"||id_name=="location"){
		box.src="./img/off_j_"+id_name+".gif";
	}else{
		box.src="../img/off_j_"+id_name+".gif";
	}
	box.style.border="1px dotted #fff";
}

function show_content(id_name){
	
	var cnt_array=['messages','philosophy','information','attorney','right-acquisition','right-utilization',,'etc-service','contactus','message2you','open-office','training','aday','workplace','open-office-form','application-form','recruit-form','etc-form']
	
	if(id_name=='messages'||id_name=='philosophy'||id_name=='information'||id_name=='attorney'){
		cur_aboutus=id_name;
	}
	if(id_name=='right-acquisition'||id_name=='right-utilization'||id_name=='etc-service'||id_name=='contactus'){
		cur_service=id_name;
	}
	if(id_name=='message2you'||id_name=='open-office'||id_name=='aday'||id_name=='workplace'||id_name=='training'){
		cur_recruit=id_name;
	}
	if(id_name=='open-office-form'||id_name=='application-form'||id_name=='recruit-form'||id_name=='etc-form'){
		cur_mailto=id_name;
	}
	
	var box;
	var i;
	for(i=0;i<cnt_array.length;i++){
		box=document.getElementById(cnt_array[i]);
		if(box){
			box.style.display="none";
		}
	}
	
	box=document.getElementById(cur_aboutus);
	if(box){
		box.style.display="block";
	}
	box=document.getElementById(cur_service);
	if(box){
		box.style.display="block";
	}
	box=document.getElementById(cur_recruit);
	if(box){
		box.style.display="block";
	}
	box=document.getElementById(cur_mailto);
	if(box){
		box.style.display="block";
	}
	
	// for english site
	if(cur_recruit=='message2you'){
		if(!document.getElementById(cur_recruit)){
			box=document.getElementById('workplace');
			if(box){
				box.style.display="block";
			}
		}
	}
}

function show_image(oj){
	var res=decodeURIComponent(oj.responseText);
	a=res.split(":");
	document.getElementById('image-box').innerHTML=a[0];
}

function hide_image(){
	document.getElementById('image-box').style.display="none";
}

function show_attorney(oj){
	var res=decodeURIComponent(oj.responseText);
	a=res.split(":");
	document.getElementById('attorney-box').innerHTML=a[0];
}

function hide_attorney(){
	document.getElementById('attorney-box').style.display="none";
}

function getMouseX(e){
	if(window.opera){
		return e.clientX;
  }else if(document.all){
  	return document.documentElement.scrollLeft+event.x;
	}else if(document.layers||document.getElementById){
  	return e.pageX;
  }
}

function getMouseY(e){
	if(window.opera){
  	return e.clientY;
	}else if(document.all){
  	return document.documentElement.scrollTop+event.y;
	}else if(document.layers||document.getElementById){
  	return e.pageY;
  }
}

function getMouseXY(e){
	pos_x=getMouseX(e);
	pos_y=getMouseY(e);
}

function move_box(box_name){

	var box=document.getElementById(box_name);
	
	var box_width;
	var box_height;

	if(box_name=="attorney-box"){
		box_width=435;
		box_height=325;
	}else{
		box_width=370;
		box_height=300;
	}
	
	var scroll_height;
	if(document.all){
		client_width = document.documentElement.clientWidth+document.documentElement.scrollLeft;
		client_height = document.documentElement.clientHeight+document.documentElement.scrollTop;
		scroll_height=document.documentElement.scrollTop;
	}else if(document.layers||document.getElementById){
		client_width = window.innerWidth+window.pageXOffset;
		client_height = window.innerHeight+window.pageYOffset;
		scroll_height=window.pageYOffset;
	}
	
	var box_top=pos_y-box_height-30;
	if(box_top<scroll_height+10) box_top=scroll_height+10;
	if(pos_y<box_height+scroll_height+30) box_top=pos_y+30;
	
	
	var box_left=pos_x-box_width/2;
	if(box_left+box_width>client_width-10) box_left=client_width-box_width-10;
	if(box_left<10) box_left=10;
	
	if(box_top>client_height-box_height-20){
		box_top=pos_y-box_height/2;
		box_left=pos_x+20;
		if(box_top<320) box_top=320;
		if(box_top>client_height-box_height-20) box_top=client_height-box_height-20;
	}
	
	//for flash avoidance
	if(box_top<320) box_top=pos_y+30;

	box.style.left=box_left+"px";
	box.style.top=box_top+"px";
}

function adapt_pagesize(){

	var box=document.getElementById('content-box');
	
	if(document.all){
		client_height = document.documentElement.clientHeight+document.documentElement.scrollTop;
	}else if(document.layers||document.getElementById){
		client_height = window.innerHeight+window.pageYOffset;
  }
	
	var cb_height=client_height-(10+46+140+40+20);
	//pad_top+head+title+pad_bottom+pad_in_contetnt_box
	
	box.style.height=cb_height+"px";
}

// currently displayed page
var photo_num;
var max_photo=5;//numbe of hidden-box
var max_photo_num=13;//number of photos
var photo_num_array = new Array(max_photo_num);

function rand_photo_num(){
	
	var i,j;
	var rand_array = new Array(max_photo_num);
	
	for(i=0;i<max_photo_num;i++){
		var rand = Math.random();
		rand_array[i]=rand;
		photo_num_array[i]=rand;
	}
	
	rand_array.sort();
	
	for(i=0;i<max_photo_num;i++){
		for(j=0;j<max_photo_num;j++){
			if(rand_array[i]==photo_num_array[j]){
				photo_num_array[j]=i+1;
				break;
			}
		}
	}
}

function move_photo(){

	photo_num++;
	
	//if(photo_num>max_photo) return;
	if(photo_num>max_photo){
		setTimeout("move_photo_array(0)",100);
		return;
	}
	
	var ux=(480-200)/(max_photo-1);
	var uy=(250-134)/(max_photo-1);

	x1 = YAHOO.util.Dom.getX("large-photo")-2;
	y1 = YAHOO.util.Dom.getY("large-photo")-2;
	x2 = YAHOO.util.Dom.getX("large-photo")-2+ux*(photo_num-1);
	y2 = YAHOO.util.Dom.getY("large-photo")-2+(250-134)-uy*(photo_num-1);
	
	var box=document.getElementById('moving-box');
	
	move_smoothly(x1,y1,x2,y2,0);
}

function move_smoothly(x1,y1,x2,y2,num){
	
	var box_id="moving-box"+photo_num;
	var box=document.getElementById(box_id);
	box.style.visibility="visible";
	
	var smooth=20;
	
	num++;
	
	if(num>smooth){
		//var rand = Math.floor( Math.random() * max_photo_num )+1;
		//var photo_name="photo/photo"+rand+".jpg";
		
		//var photo_name="photo/photo"+photo_num+".jpg";
		
		var photo_name="photo/photo"+photo_num_array[photo_num]+".jpg";
		
		box.innerHTML="<img src=\""+photo_name+"\" />";
		//x3 = YAHOO.util.Dom.getX("last-photo")-2;
		//y3 = YAHOO.util.Dom.getY("last-photo")-2;
		//setTimeout("fade_smoothly("+x2+","+y2+","+x3+","+y3+",0)",5000);
		setTimeout("move_photo()",100);
		return;
	}
	
	ux=(x2-x1)/(smooth);
	uy=(y1-y2)/(smooth);
	
	box.style.left=x1+ux*num+"px";
	box.style.top=y1-uy*num+"px";
	
	box.style.width=500+(200-500)*(num/smooth)+"px";
	box.style.height=250+(134-250)*(num/smooth)+"px";

	setTimeout("move_smoothly("+x1+","+y1+","+x2+","+y2+","+num+")",1);
}

function move_photo_array(num){
	var i;
	var smooth=20;
	
	var x1 = YAHOO.util.Dom.getX("large-photo")-2;
	var y1 = YAHOO.util.Dom.getY("large-photo")-2;
	
	if(num>smooth){
		setTimeout("show_text()",1);
		return;
	}
	
	num++;
	
	for(i=1;i<max_photo+1;i++){
		var box_id="moving-box"+i;
		var box=document.getElementById(box_id);
		
		var top=box.style.top;
		var left=box.style.left;
		
		top=top.replace("px","");
		left=left.replace("px","");
		
		var ux=(left-x1)/2;
		var uy=(top-y1)/2;
		if(num==smooth){
			ux=x1-left;
			uy=top-y1;
		}
		
		box.style.left=(left-ux)+"px";
		box.style.top=(top-uy)+"px";
	}
	
	setTimeout("move_photo_array("+num+")",30);
}

function show_text(){
	
	var box=document.getElementById('text-box');
	box.style.visibility="visible";
	
	x1 = YAHOO.util.Dom.getX("large-photo")-2;
	y1 = YAHOO.util.Dom.getY("large-photo")-2;
	
	box.style.left=x1+"px";
	box.style.top=y1+155+"px";
	
	var str="<p>test</p>";
	
	box.innerHTML=str;
	
}

//////////////////
//
// test functions
//


function fade_smoothly(x2,y2,x3,y3,num){
	
	var box=document.getElementById('moving-box');
	var smooth=20;
	
	box.innerHTML="";
	
	num++;
	
	if(num>smooth){
		setTimeout("move_photo()",0);
		return;
	}
	
	ux=(x3-x2)/(smooth);
	uy=(y3-y2)/(smooth);
	
	box.style.left=x2+ux*num;
	box.style.top=y2+uy*num;
	
	box.style.width=366-(366-80)*(num/smooth);
	box.style.height=245-(245-10)*(num/smooth);

	setTimeout("fade_smoothly("+x2+","+y2+","+x3+","+y3+","+num+")",10);
}

function partial_view(){
	
	var box=document.getElementById('top-image');
	
	var top=pos_y-20;
	var bottom=pos_y+20;
	var right=pos_x+20;
	var left=pos_x-20;
	
	box.style.clip="rect(10px,20px,30px,10px)";
	//box.style.clip="rect("+top+" "+right+" "+bottom+" "+left+")";
}

function all_view(){
	var top=pos_y-20;
	var bottom=pos_y+20;
	var right=pos_x+20;
	var left=pos_x-20;
	enlarge_view(top,right,bottom,left);
}

function enlarge_view(top,bottom,right,left){
	
	var box=document.getElementById('top_image');
	box.style.clip='rect('+top+'px '+right+'px '+bottom+'px '+left+'px)';

	if(right>400 && top<0 && bottom>300 && left <0){
		return;
	}
		

	for(i=0;i<3;i++){
		top--;
		bottom++;
		right++;
		left--;
	}
	
	if(right>400 && top<0 && bottom>300 && left <0){
		top=-10;
		bottom=310;
		right=410;
		left=-10;
	}

	setTimeout("enlarge_view('"+top+"','"+right+"','"+bottom+"','"+left+"')",1);
}

function trans_image(e){
	e.style.visibility="visible";
}

function show_msg(num){
	var id= "msg"+num;
	var id2= "msg_info"+num;
	
	var box=document.getElementById(id);
	var box2=document.getElementById(id2);
	
	if(box.style.display=="block"){
		box.style.display="none";
		box2.innerHTML="display next";
	}else{
		box.style.display="block";
		box2.innerHTML="hide display";
	}
}

function show_title(e){
	
	var client_width;
	var client_height;
	
	var box2=document.getElementById('content-box');
	
	if(document.all){
		client_width = document.documentElement.clientWidth+document.documentElement.scrollLeft;
		client_height = document.documentElement.clientHeight+document.documentElement.scrollTop;
	}else if(document.layers||document.getElementById){
		client_width = window.innerWidth+window.pageXOffset;
		client_height = window.innerHeight+window.pageYOffset;
  	}
	
	var top=getMouseY(e)-5-380+box2.scrollTop;
	var bottom=getMouseY(e)+5-380+box2.scrollTop;
	var right=getMouseX(e)+5-client_width/2+135;
	var left=getMouseX(e)-5-client_width/2+135;
	
	//alert(e.pageY+" "+client_width);
	enlarge_title(top,bottom,right,left);
}


function enlarge_title(top,bottom,right,left){
	
	var box=document.getElementById('top-image');
	box.style.clip="rect("+top+"px "+right+"px "+bottom+"px "+left+"px)";

	if(right>400 && top<0 && bottom>300 && left <0){
		return;
	}

	for(i=0;i<3;i++){
		top--;
		bottom++;
		right++;
		left--;
	}
	
	if(right>400 && top<0 && bottom>300 && left <0){
		top=-10;
		bottom=310;
		right=410;
		left=-10;
	}
	
	//if(num>299) return;
	//var box_name="title-"+num;
	//var test=document.getElementById(box_name);
	//test.innerHTML="<img src=\"img/title-back-col.gif\" />";
	//test.style.visibility="visible";
	
	setTimeout("enlarge_title('"+top+"','"+bottom+"','"+right+"','"+left+"')",1);
}

