//bodys = document.getElementsByTagName("body");
//alert(bodys[0].firstChild);

function load_msg(table_id, desc){

	if(!document.getElementById){
		alert('????????????????????????????');
		return;
	}
	
	var table = document.getElementById(table_id);
	var rows = table.getElementsByTagName("tbody").item(0).childNodes;
	var iface_row;
	var mid_skip_begin,mid_skip_end;
	var ri_insert0,ri_insert1;
	var prev_id=-1;
	
	for(var i=0; i<rows.length; i++){ // scan table
		//alert(rows[i].nodeName);
		if(rows[i].nodeName != "TR") continue;
		if(!rows[i].id) {iface_row=rows[i]; continue;}
		var id = rows[i].id;
		if(id - prev_id != 1){
			ri_insert0=rows[i].rowIndex-1;
			ri_insert1=rows[i].rowIndex;
			mid_skip_begin=prev_id*1 + 1;
			mid_skip_end = id*1-1;
		}
		prev_id=id;
	}
	
	if(!mid_skip_begin){
		return;
	}
	
	var ri_insert;
	var b_complete_table = false;
	var default_range = 10;
	var mid_range_begin,mid_range_end;
	
	if(desc){
	 ri_insert=ri_insert1;
	 mid_range_end = mid_skip_end;
	 mid_range_begin = mid_range_end-default_range;
	 if(mid_range_begin < mid_skip_begin){
		 mid_range_begin = mid_skip_begin;
		 b_complete_table=true;
	 } 
	}else{
	 ri_insert = ri_insert0;
	 mid_range_begin = mid_skip_begin;
	 mid_range_end = mid_range_begin+default_range;
	 if(mid_range_end > mid_skip_end){
		 mid_range_end = mid_skip_end;
		 b_complete_table=true;
	 }   
	}
	 
	var mid_range = mid_range_begin + "-" + mid_range_end;
	
	//alert(mid_skip_begin + ":" + mid_skip_end + ":" + ri_insert0 + ":" + ri_insert1);
	var tid = table_id.substring(3);
	var url = "/alabout/message_disp.phtml?tid[]="+tid+"&mid="+ mid_range + "&mode=ajax2";
		
	var handler = function(text){

		text = "var list="+text;
		
		eval(text);
		//alert(text);
		for(var i=0; i<list.length; i++){
			var mid=list[i][0];
			var tid=list[i][1];
			var c0 = list[i][2];
			var c1 = list[i][3];
			
			var new_row=table.insertRow(ri_insert++);
			new_row.id=mid;
			var cell0=new_row.insertCell(0);
			cell0.vAlign="top";
			var cell1=new_row.insertCell(1);
			//alert(cell0);
			cell0.innerHTML=c0;
			cell1.innerHTML=c1;
			
		
		}
	
		if(b_complete_table){
			table.deleteRow(iface_row.rowIndex);
		}
		
	}

	getPage(url, handler);


}

function Test(n){
 this.n = n;
}

Test.prototype.show = Test_show;
function Test_show(){
 alert(this.n); 
}

obj0 = new Test(3);
obj1 = new Test(5);
//obj0.show();
//obj1.show();

function resizeIframe(frame){

var id = frame.id;
var f;

var width=800;
var height=600;

if(document.getElementById){
 f = document.getElementById(id).contentWindow;
 width = f.document.body.scrollWidth;
 height = f.document.body.scrollHeight+20;
}else if(document.frames){
 f = document.frames(id);
 width = f.document.body.scrollWidth;
 height = f.document.body.scrollHeight+20;
}

if(width>800)width=800;
if(height>600)height=600;
frame.width= width;
frame.height = height;

}

function getCookie(key) {
	tmp = document.cookie+";";
	tmp1 = tmp.indexOf(key, 0);

	if (tmp1 != -1) {
		tmp = tmp.substring(tmp1,tmp.length);
		start = tmp.indexOf("=",0)+1;
		end = tmp.indexOf(";",start);
		return(unescape(tmp.substring(start,end)));
	}

	return("");
}


// XMLHttps????????
function createXMLHttp()
{
	try {
		return new ActiveXObject ("Microsoft.XMLHTTP");
	}catch(e){
		try {
			return new XMLHttpRequest();
		}catch(e) {
			return null;
		}
	}
	return null;
}



function rf(n){

	//parent_height = document.body.clientHeight);
	
	url="/alabout/vote_form.phtml?tid="+n+"#msg";
	wname = "alabout_report";
	wstatus = "";
	
	$type = getCookie("rwp");
	//alert($type);
	if($type == "normal"){
	}else if($type == "no"){
		wname="_self";
		wstatus="";
	}else{
		width = 800;
		height = 400;
		
		parent_width = document.body.clientWidth;
		if(width > parent_width){
			width = parent_width;
		}
	
		//	if(height > parent_height){
		//		height = parent_height;
		//	}		
		
		wstatus = 'width=' + width + ',height=' + height + ',scrollbars=yes,titilebar=yes';
	}	
	
	w=window.open(url, wname, wstatus);
}

function getPage(pageURL, handler) {
   	
	xmlhttp = createXMLHttp();
	
	if (xmlhttp)
	{
		//var self = layer;
		try{
		xmlhttp.onreadystatechange = function(){setPageData(handler);}
		xmlhttp.open('GET', pageURL);
		xmlhttp.send(null);
		}catch(e){
			alert(e);
		}finally{
			;
		}
	}else{
		alert("XMLHttpRequest失?");
	}
	
	return "";
	
}


function setPageData(handler)
{

	if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		handler(xmlhttp.responseText);
		
		//layer.setChildHtml(xmlhttp.responseText);
	}
}




//
// MyLayerViewer???
//
function MyLayerViewer(layerId){

  //
  this.layerObj;
  this.layerId="";

  //??????
  this.nMouseXPos = 0;
  this.nMouseYPos = 0;

  //
  this.nInitMouseXPos = 0;
  this.nInitMouseYPos = 0;

  //???????
  this.nLayerWidth=0;
  this.nLayerHeight=0;

  // ?????
  this.nRectLeftTopX=0;
  this.nRectLeftTopY=0;
  this.nRectRightBottomX = 0;
  this.nRectRightBottomY = 0;

  //
  this.nRectTopMargin = 50;
  this.nRectLeftMargin = 100;
  this.nRectRightMargin = 100;
  this.nRectBottomMargin = 50;

  this.nRectRightMargin

  //
  this.flag=1;

  this.bLoaded=true;
  
  // 
  this.enableAutoHide = 1;

  // ???????
  this.enableAutoPosX = 1;
  this.enableAutoPosY = 1;
  //

  this.init(layerId);
  
}

function MyLayerViewer_SetRectMargin(nLeft, nTop, nRight, nBottom){
  this.nRectLeftMargin = nLeft;
  this.nRectTopMargin = nTop;
  this.nRectRightMargin = nRight;
  this.nRectBottomMargin = nBottom;
}

function MyLayerViewer_SetAutoHide(bVal){
  this.enableAutoHide = bVal;
}

function MyLayerViewer_SetAutoPos(bPosX, bPosY){
  this.enableAutoPosX = bPosX;
  this.enableAutoPosY = bPosY;
}

function MyLayerViewer_SetLayerPosition(nTop, nLeft){
 
  if (document.layers){
    //???????????
    this.layerObj.left = nTop;
    this.layerObj.top = nLeft;
  }
  //IE?NC6???
  else if (document.getElementById)    {
	
    //???????????
    this.layerObj.style.left = nTop;
    this.layerObj.style.top = nLeft;

  }

}


function MyLayerViewer_setChildHtml(strHtml){
  if (document.layers){
	//alert(strHtml);
  	document.all[this.layerId+"_child"].write(strHtml);
  	document.all[this.layerId+"_child"].close();
  }else if(document.getElementById){
 	//alert(this.layerId);
	document.getElementById(this.layerId+"_child").innerHTML = strHtml;
 }else{
 	 ;
  }
  this.repaint();
}

 
 


function MyLayerViewer_ShowLayer(nNumber, fType, url, nLeft, nTop, auto_hide){

  this.getLayer();
  this.SetAutoHide(auto_hide);
  
  // ???
  strBackColor='white';

  //???????????
  nLayerXPos = nLeft;
  nLayerYPos = nTop;
  if(this.enableAutoPosX){
    nLayerXPos += this.nMouseXPos;
  }
  if(this.enableAutoPosY){
    nLayerYPos += this.nMouseYPos;
   }
    
  this.nInitMouseXPos = this.nMouseXPos;
  this.nInitMouseYPos = this.nMouseYPos;


  // ???????示??????
  strHtml = "<div style='border:2px #aaaaaa solid;'>";

  if(nNumber == 0){ //<img>
    strHtml += "<img id=\"" + this.layerId + "_child\" src=\"" + url + "\">";
  }else if(nNumber == 1){ // <iframe>
    strHtml += "<iframe frameborder=0 border=0 id=\"" + this.layerId + "_child\" onload=\"resizeIframe(this)\" src=\"" + url + "\" width=800 height=1 scrolling=auto></iframe>";
  }else if(nNumber == 2){
  	strHtml += "<div id=\"" + this.layerId + "_child\">now loading...</div>";
  }else{
    strHtml = url;
  }
   strHtml += "</div>";
		
  //NN???
  if (document.layers){
      //???????????
      this.layerObj.left = nLayerXPos;
      this.layerObj.top = nLayerYPos;
      //???
      this.layerObj.bgColor = strBackColor;
      
      //??????
      this.layerObj.document.write(strHtml);
      this.layerObj.document.close();
      			
    }
  //IE?NC6???
  else if (document.getElementById)    {
	
    //???????????
    this.layerObj.style.left = nLayerXPos;
    this.layerObj.style.top = nLayerYPos;

    //???
    this.layerObj.style.background = strBackColor;

    //??????
    this.layerObj.innerHTML = strHtml;
			
    this.UpdateRect(true);
    
  }
 
  if(nNumber == 2){
    var self = this;
    var func = function(responseText){
    			self.setChildHtml(responseText);
    }
  	getPage(url, func);
  }else if(nNumber == 1){
	;  	
  }else if (nNumber == 0){
  	var img = document.getElementById(this.layerId + "_child");
  	var obj = this;
  	obj.bLoaded=false;
  	img.onload = function(){obj.UpdateRect(false);obj.bLoaded=true;}
  }

  this.show();  
  
}

function MyLayerViewer_repaint(){
	this.hide();
	this.show();
}

function MyLayerViewer_show(){

  //NN???
  if (document.layers)
    {
      //?示
      this.layerObj.visibility = "show";	
    }
  //IE?NC6???
  else if (document.getElementById)
    {	
      //?示
      this.layerObj.style.visibility="visible";
  }
}

function MyLayerViewer_hide(){

  //NN???
  if (document.layers)
    {
      //??示
      this.layerObj.visibility = "hide";	
    }
  //IE?NC6???
  else if (document.getElementById)
    {	
      //??示
      this.layerObj.style.visibility="hidden";
  }
}

//
// ??????conditinal_hide?????????
//
function MyLayerViewer_UpdateRect(init){

  if(!this.layerObj)
   return;
   
  var nLayerLeft;
  var nLayerTop;

  //NN???
  if (document.layers){
      nLayerLeft = parseInt(this.layerObj.left);
      nLayerTop = parseInt(this.layerObj.top);
  }
  //IE?NC6???
  else if (document.getElementById){
    nLayerLeft = parseInt(this.layerObj.style.left);
    nLayerTop = parseInt(this.layerObj.style.top);

  }

  // ???????
  // ??????????????????????????????
  this.nRectLeftTopX = nLayerLeft - this.nRectLeftMargin;
  this.nRectLeftTopY = nLayerTop - this.nRectTopMargin;
  this.nRectRightBottomX = nLayerLeft + this.layerObj.scrollWidth + this.nRectRightMargin;
  this.nRectRightBottomY = nLayerTop + this.layerObj.scrollHeight +this.nRectBottomMargin;
  
  //?????????l???
  if(this.nRectRightBottomX < this.nInitMouseXPos + this.nRectRightMargin){
      this.nRectRightBottomX = this.nInitMouseXPos + this.nRectRightMargin;
  }

  str = "Debug Info: " +this.layerObj.style.visibility 
    + " Rect(" +this.nRectLeftTopX +"," + this.nRectLeftTopY + ")" 
    + "(" +this.nRectRightBottomX +"," + this.nRectRightBottomY + ")" 
    +   " Layer Size(" + this.layerObj.scrollWidth +"," + this.layerObj.scrollHeight + ")" 
    + " Mouse(" + this.nMouseXPos +"," + this.nMouseYPos + ")";
  window.status=str;

}

//
// ??????????自?????
//
function MyLayerViewer_ConditionalHide(){

 if(!this.layerObj || !this.enableAutoHide || !this.bLoaded)
   return;
		
  // IE, NN6
  if (document.getElementById){	
    if(this.layerObj.style.visibility == "visible"){
     
      if( this.nMouseXPos < this.nRectLeftTopX || this.nMouseXPos > this.nRectRightBottomX ||
       this.nMouseYPos < this.nRectLeftTopY || this.nMouseYPos > this.nRectRightBottomY)
	{
	  this.hide();
	}
    }
  }
}


 function MyLayerViewer_getLayer(){
  if(!this.layerObj){
  	this.initLayer();
  }
  return this.layerObj;
 	
 } 
 
 function MyLayerViewer_createLayer(layerId, left, top, width, height){

   if(layerId == ""){
     var layerNo = Math.floor(Math.random()*1600000);
     this.layerId = '_MyLayer_' + layerNo;
   }else{
     this.layerId = layerId;
   }
   
	if(document.getElementById){ 

	    var layerObj = document.createElement('div');
	    layerObj.setAttribute('id', this.layerId);
	    layerObj.style.position = 'absolute';
	    layerObj.style.left = left + 'px';
	    layerObj.style.top = top + 'px';
	    layerObj.width = width + 'px';
	    layerObj.height = height + 'px';
	    //layerObj.style.zIndex = 1000;
	
	    document.body.appendChild(layerObj);
	     
	    return layerObj;
	       
   }else if(document.all){ //ie4
   
      var layerHtml = 
       "<div id=\""+ this.layerId + "\" "
       + "style=\"position: absolute; "
       + "left: " + left + "px;"
       + "top: " + top + "px;"
       + "width: " + width + "px;"
       + "height: " + height + "px;"
       + "z-index: 100;"
       + "\">"
       + "</div>";

     document.body.insertAdjacentHTML("BeforeEnd", layerHtml);
     //alert(document.all[this.layerId]);
     return document.all[this.layerId];
     
   }else if(document.layers){      //n4?
  
      var layoj = new Layer(width)              //??????
          layName          = this.layerId         //????? 
          layoj.left       = left               //left?? 
          layoj.top        = top                //top?? 
          layoj.height     = height             //??
          layoj.visibility = 'visible'          //???
    
  	    return layoj                              //????oj???
    }
   
   
   return null;
   
 }
 
 function MyLayerViewer_init(layerId){
   this.layerId = layerId;
   
   //
   // ??????????????
   //
   //Netscape???
   if (navigator.appName=="Netscape"){

     var self = this;
     function nMouse(e){
       self.nMouseXPos = e.pageX;
       self.nMouseYPos = e.pageY;

       //self.UpdateRect(false);
       self.ConditionalHide();
     }

     //NN4
     if (document.layers){
       window.captureEvents(Event.MOUSEMOVE);
       window.onMouseMove = nMouse;
     }
     //NC6
     else{
       document.onmousemove = nMouse;
     }
     
   }
   //IE???
   else{

     var self = this;
     function iMouse(){
       // ?????????????
       self.nMouseXPos = event.clientX+document.body.scrollLeft;
       self.nMouseYPos = event.clientY+document.body.scrollTop;
       
       //self.UpdateRect(false);
       self.ConditionalHide(); 
     }
	
     document.onmousemove = iMouse;

  }

 }
  
 function MyLayerViewer_initLayer(){
    	
  // ??????or??
  if(document.getElementById && document.getElementById(this.layerId)){
    this.layerObj = document.getElementById(this.layerId);
  }else if(document.all && document.all[this.layerId]){
    this.layerObj = document.all[this.layerId];
  }else if(document.layers && document.layers[this.layerId]){
    this.layerObj = document.layers[this.layerId];
  }else{
    this.layerObj = this.createLayer(this.layerId, 0,0,0,0);
  } 	 


  //
   // ????????
   //
   //Netscape???
   if (navigator.appName=="Netscape"){
      var self = this;
      this.layerObj.onclick =function(){self.hide();}   
   }
   //IE???
   else{
     var self = this;
     this.layerObj.onclick = function(){self.hide()}
  }
 
 
}

//private
MyLayerViewer.prototype.createLayer = MyLayerViewer_createLayer;
MyLayerViewer.prototype.ConditionalHide = MyLayerViewer_ConditionalHide;
MyLayerViewer.prototype.UpdateRect = MyLayerViewer_UpdateRect;
MyLayerViewer.prototype.hide = MyLayerViewer_hide;
MyLayerViewer.prototype.show = MyLayerViewer_show;
MyLayerViewer.prototype.repaint = MyLayerViewer_repaint;
MyLayerViewer.prototype.init = MyLayerViewer_init;
MyLayerViewer.prototype.initLayer = MyLayerViewer_initLayer;
MyLayerViewer.prototype.getLayer = MyLayerViewer_getLayer;

//public
MyLayerViewer.prototype.ShowLayer = MyLayerViewer_ShowLayer;
MyLayerViewer.prototype.SetAutoHide = MyLayerViewer_SetAutoHide;
MyLayerViewer.prototype.SetRectMargin = MyLayerViewer_SetRectMargin;
MyLayerViewer.prototype.SetAutoPos = MyLayerViewer_SetAutoPos;
MyLayerViewer.prototype.setChildHtml = MyLayerViewer_setChildHtml;


//
// ??????
//
var myviewer = new MyLayerViewer("");
myviewer.SetAutoPos(0,1);
myviewer.SetRectMargin(100, 120, 100, 100);


//
//
//

function MyLayeredObject(arrayObj, width, height){

  this.textAA = arrayObj; 
  this.width = width;
  this.height = height;
  this.nMoveX = 5;
  this.nMoveY = 5;
  
  this.parent;
}



function MyLayeredObject_onmouseover(obj){
	if(confirm("??????")){
		obj.parent.Stop();
		//alert(obj.textAA);
	}
}
 
function MyLayeredObject_setParent(parent){
	this.parent = parent;
	//alert(this.parent);
}

function MyLayeredObject_GetHtml(){
    myRnd = Math.floor(Math.random()*this.textAA.length);
    return "<font size=2>" + this.textAA[myRnd] + "</font>";
}  
MyLayeredObject.prototype.GetHtml= MyLayeredObject_GetHtml;
MyLayeredObject.prototype.setParent= MyLayeredObject_setParent;
MyLayeredObject.prototype.onmouseover = MyLayeredObject_onmouseover;
 


  

function MyWanderingAA(layer, layered_object){

  // ?????
  this.layer = layer;
  this.layer.style.width = layered_object.width;
  this.layer.style.height = layered_object.height;
  //this.layer.style.zIndex = 1000;
  
  this.layered_object = layered_object;
  this.layered_object.setParent(this);
  
  this.nWaitLuckInetrval=10000;
  this.nLuck = 1;
  this.vectorX = 1;
  this.vectorY = 1
  this.nMoveX = 5;
  this.nMoveY = 5;

  this.bRunning = 0;
  this.bStopWait=0;

  init(this);
  
  function init(obj){
  	obj.layer.onmouseover = function () { obj.b_mouseover=true;}
  	obj.layer.onmouseout = function() { obj.b_mouseover=false;}
  }
}



function MyWanderingAA_SetMoveOption(x, y){
	this.nMoveX = x;
	this.nMoveY = y;
}

function MyWanderingAA_SetWaitLuckOption(luck, interval){
	this.nLuck = luck;
	this.nWaitLuckInterval = interval;
}

function MyWanderingAA_WaitLuck(){

  var myRnd = Math.floor(Math.random()*this.nLuck);
  //alert(myRnd);
  if(myRnd == 0){
    this.Start();
  }

  var self = this;
  var closure = function(){self.WaitLuck();}
  setTimeout(closure, this.nWaitLuckInterval);

}

function MyWanderingAA_Start(){
 
  if(this.bRunning){
    return;
  }
  //alert("start");
  this.layer.style.visibility="visible";
  this.layer.style.left = document.body.scrollLeft+10;
  this.layer.style.top = document.body.scrollTop+10;
  this.bRunning=1;

  this.Wander();
 
}

// not tested
function MyWanderingAA_Stop(){
  this.layer.style.visibility = "hidden";
  this.bStopWait = 1;
}

function MyWanderingAA_Wander(){

  if(this.StopWait){
    this.bStopWait = 0;
    this.bRunning = 0;
    return;
  }

  myRndX = Math.floor(Math.random()*this.layered_object.nMoveX+1);
  myRndY = Math.floor(Math.random()*this.layered_object.nMoveY+1);

  var text = new Array(2);

  var html = this.layered_object.GetHtml();
  this.layer.innerHTML = html;//"<font size=2>" +  this.textAA[myRndI] + "</font>";

 
  posX = parseInt(this.layer.style.left);
  posY = parseInt(this.layer.style.top);
	
  var str = "LayerDebugInfo:";
  str += "(" +document.body.scrollWidth + "," + document.body.scrollHeight + ")";
  str += "(" +document.body.clientWidth + "," + document.body.clientHeight + ")";
  str += "(" +document.body.scrollLeft + "," + document.body.scrollTop + ")";
  str +=  " P1(" +posX + "," + posY + ")";
  str += " P2(" + (posX + this.layer.scrollWidth) + "," + (posY + this.layer.scrollHeight) + ")";	
  str += " Vector(" + this.vectorX + "," + this.vectorY + ")";
  //window.status = str;
  

  
//  var minX = 20;
//  var minY = 20;
//  var maxX = document.body.scrollWidth - this.layer.scrollWidth-50;
//  var maxY = document.body.scrollHeight - this.layer.scrollHeight-50;

  var minX = document.body.scrollLeft;
  var maxX = document.body.scrollLeft+document.body.clientWidth - this.layer.scrollWidth-50;
  var minY = document.body.scrollTop;
  var maxY = document.body.scrollTop+document.body.clientHeight - this.layer.scrollHeight- 50;
    
  if( posX < minX){
    this.vectorX=1;
  }else if( posX > maxX){
    this.vectorX=-1;
  }
	
  if( posY < minY ){
    this.vectorY=1;
  }else if( posY > maxY){
    this.vectorY =-1;
  }

  this.layer.style.left = posX+myRndX*this.vectorX;
  this.layer.style.top = posY+myRndY*this.vectorY;
  

  var self = this;
  var closures = function(){self.Wander();}

  setTimeout(closures, 100);
  
}



MyWanderingAA.prototype.Start = MyWanderingAA_Start;
MyWanderingAA.prototype.Wander = MyWanderingAA_Wander;
MyWanderingAA.prototype.WaitLuck = MyWanderingAA_WaitLuck;
MyWanderingAA.prototype.Stop = MyWanderingAA_Stop; // not tested
MyWanderingAA.prototype.SetWaitLuckOption = MyWanderingAA_SetWaitLuckOption;
MyWanderingAA.prototype.SetMoveOption = MyWanderingAA_SetMoveOption;


