//Based on http://wema.sourceforge.jp/
//Wema is GPL thus this file is GPL


// do nothing
function do_nothing(){
  ;
}

// browser check

Gekko = IE = 0;

if (document.all) IE = 1;
else if (document.getElementById) Gekko = 1;

obj =  0;
offsetX = offsetY = 0;
// mouse position
mouseX='';
mouseY='';
moveStartmouseX = '';
moveStartmouseY = '';

stickyMoving = false; // 付箋が動く前はfalse
gridsize = 8; // グリッドサイズ(px)



function getElement(id){
  if(Gekko){
	return document.getElementById(id);
  }else{
	return document.all(id);
  }
}

/*
function show(id){
  if(Gekko){
	// イベントハンドラをnullにしないとテキスト選択機能と干渉する
	document.getElementById(id).onmousedown = null;
	document.onmouseup = null;
	document.onmousemove = null;
  }
  getElement(id).style.visibility = "visible";
  getElement(id).style.zIndex = 2;
}

function hide(id){
  if(Gekko){
	document.getElementById(id).onmousedown = down;
	document.onmouseup = up;
	document.onmousemove = move;
  }
  getElement(id).style.visibility = "hidden";
}
*/

function init() {
   if (Gekko) list = document.getElementsByTagName("div");
   else if (IE) list = document.all.tags("div");

   nlist = new Array();
   for (i = 0; i < list.length; i ++) {
	  if(list[i].id.indexOf('id')==0)
		nlist.push(list[i]);
   }
	
   list = nlist;
	
   for (i = 0; i < list.length; i ++) {
	  list[i].onmousedown = down;
   }


   document.onmouseup = up;
   document.onmousemove = move;

//   setLines();
}

function down(e) {
   obj = this;
   if (Gekko) {
	  offsetX = e.pageX - parseInt(obj.style.left);
	  offsetY = e.pageY - parseInt(obj.style.top);
	  stickyMoving = false;
	  moveStartmouseX = e.pageX;
	  moveStartmouseY = e.pageY;
   }
   else if (IE) {
	  offsetX = event.clientX + document.body.scrollLeft - this.style.posLeft;
	  offsetY = event.clientY + document.body.scrollTop - this.style.posTop;
	  stickyMoving = false;
	  moveStartmouseX = document.body.scrollLeft+event.clientX;
	  moveStartmouseY = document.body.scrollTop+event.clientY;
   }
  for (i = 0; i < list.length; i ++)
	 list[i].style.zIndex = 1;
  obj.style.zIndex = 2;
  
   // 編集窓のテキストエリアをドラッグするとき窓が動かないように
	if (Gekko && (e.target.localName == "TEXTAREA" || e.target.localName == "INPUT")) {
		obj = null;
	}
	else if (IE && (event.srcElement.name == "body" || event.srcElement.name == "ln")) {
		obj = null;
	}
   return false;
}

function move(e) {
   if(Gekko){
	  mouseX=e.pageX;
	  mouseY=e.pageY;
   }if(IE){
	  mouseX=document.body.scrollLeft+event.clientX;
	  mouseY=document.body.scrollTop+event.clientY;
   }
   if (obj) {
	  if (!stickyMoving) {
		if (Math.abs(mouseX - moveStartmouseX) > gridsize / 2 ||
			Math.abs(mouseY - moveStartmouseY) > gridsize / 2) {
		  stickyMoving = true;
		}
	  }
	  if (Gekko) {
		 if (stickyMoving) {
			// obj.style.left = e.pageX - offsetX + "px";
			// obj.style.top = e.pageY - offsetY + "px";
			tmpleft = e.pageX - offsetX;
			tmptop = e.pageY - offsetY;
			tmpleft -= tmpleft % gridsize;
			tmptop -= tmptop % gridsize;
			obj.style.left = tmpleft + "px"; // ここで描画発生
			obj.style.top = tmptop + "px";	 // ここで描画発生
		 }
	  }
	  else if (IE) {
		 if (stickyMoving) {
			// obj.style.left = event.clientX + document.body.scrollLeft - offsetX;
			// obj.style.top = event.clientY + document.body.scrollTop - offsetY;
			tmpleft = event.clientX + document.body.scrollLeft - offsetX;
			tmptop = event.clientY + document.body.scrollTop - offsetY;
			tmpleft -= tmpleft % gridsize;
			tmptop -= tmptop % gridsize;
			obj.style.left = tmpleft; // ここで描画発生
			obj.style.top = tmptop;	  // ここで描画発生
		 }
	  }
		
	  // 付箋貼り付け予定地を編集窓の位置にセット
//	  if(obj.id=="edit_box" && frm != null){
//		 frm.l.value=obj.style.left;
//		 frm.t.value=obj.style.top;
//	  }
		
	  //objのリンク線を再描画
	  return false;
   }
}

function up(e) {
   stickyMoving = false;
   obj = null;
}

onload = init;


