//Code by Jarry.Li ,CopyRight @ Jiarry.126.com,All right reserved !
//homepage : http://Jiarry.blogchina.com  
//E-mail:jiarry@hotmail.com
//使用方法:
/**************
<div id="confirm" 
style="display:none;width:200px;height:100px;background-color:#77c;z-index:11;position:absolute;text-align:center;" 
rel="drag"
onmousedown="dargit(this,event);
">
</div>
**************/

var ey=0,ex=0,lx=0,ly=0,canDrg=false,thiso=null;//
var x, y,rw,rh;
	var divs=document.getElementsByTagName("div");
	for (var i=0;i<divs.length;i++){	
	  if(divs[i].getAttribute("rel")=="drag"){
	    divs[i].onmousemove=function(){
		   thismove(this);//实时得到当前对象与鼠标的值以判断拖动及关闭区域;
		}
	  }
	}

function thismove(o){
    rw=parseInt(x)-parseInt(o.style.left);
    rh=parseInt(y)-parseInt(o.style.top);
	//document.title=rw+"=文本区域="+rh;
    if(rh<=20 && rw>=180);//document.title=rw+"||20*20||"+rh;//右上角20*20的关闭区域
	if(rh<=20 && rw<180 );//document.title=rw+"||可选||"+rh;//绝对拖动条选择区域;
}
function dargit(o,e){
thiso = o;
canDrg = true;
 if(!document.all){
      lx = e.clientX; ly = e.clientY;
      }else{
        lx = event.x; ly = event.y;
	  }
 if(document.all) thiso.setCapture();
 
 try{//设置渐变色;

  if(rh<=20 && rw<180 ){//设定在拖动区域
  if(document.all)thiso.filters.Alpha.opacity=50;
  else thiso.style.MozOpacity=50/100;
  }

}catch(e){
 // alert(e.toString());
}
	st(o);//置前或置后
 
}

document.onmousemove = function(e){
if(!document.all){ x = e.clientX; y = e.clientY; }else{ x = event.x; y = event.y; }
if(canDrg){
  if(rh<=20 && rw<180 ){//如果要设定拖动区域可以作判断
	var ofsx = x - lx;
	thiso.style.left = (parseInt(thiso.style.left) + ofsx) +"px";
	lx = x;
	var ofsy = y - ly;
	thiso.style.top = (parseInt(thiso.style.top) + ofsy) +"px";
	ly = y;
	if(parseInt(thiso.style.left)<=0)thiso.style.left=0+"px";
	if(parseInt(thiso.style.top)<=0)thiso.style.top=0+"px";
 }else{
		canDrg=false;
  }
	//window.status=thiso.style.left+"left:top"+thiso.style.top+" rh:rw"+rh+"+"+rw +" x:"+x+"  y:"+y  +" lx:"+lx+"  ly:"+ly;
 }
}

document.onmouseup=function(){
	    canDrg=false;//拖拽变量设为false
 try{//设置渐变色;

  if(document.all)thiso.filters.Alpha.opacity=80;
  else thiso.style.MozOpacity=80/100;

}catch(e){
 // alert(e.toString());
}
	    if(document.all && thiso != null){
	    	//ie下，将清捕获;
         thiso.releaseCapture();
	     thiso = null;
   }
}


function set(obj){
     obj=obj.parentNode.parentNode;
     if(obj.getAttribute("rel"));
	   //obj.style.zIndex=1;
	   
}
function st(o){
/*
var p = o.parentNode;
if(p.lastChild != o){
  p.appendChild(o);
}*/


if(rh<=20 && rw>=180){
canDrg=false;
//如果选择的是关闭区域;
		  //window.status=rw+"|"+rh;
   closeconfirm();
		  /*
		  if(p.firstChild == o) return;
		  p.insertBefore(o, p.firstChild);
		  */
        }
}

//设定层的显示位置;
function setposition(obj,e){
  var scrollT = 0,scrollL = 0 ,scrollH = 0 , objTop = 0; 
  if (window.innerHeight){
	  scrollT = window.pageYOffset;
	  scrollL = window.pageXOffset;
  }else if (document.documentElement && document.documentElement.scrollTop) {
    scrollT = document.documentElement.scrollTop;
	scrollL = document.documentElement.scrollLeft;
 }else if (document.body) {
	scrollT = document.body.scrollTop;
	scrollL = document.body.scrollLeft;
  }



        var maskHeight = window.document.body.scrollHeight;
        var maskWidth = window.document.body.offsetWidth ;
   	    $("mask").style.height = maskHeight + "px";
		$("mask").style.width = maskWidth + "px";

  	    var w = parseInt(obj.style.width);
   	    var h = parseInt(obj.style.height); 
		//document.body.style.overflow = "hidden";
		//document.title =    window.document.body.offsetWidth  +" | "+ window.document.body.scrollHeight;
   	    obj.style.left=(window.document.body.offsetWidth + scrollL -w)/2+"px";
        //var objTop =scrollT + window.screen.height/5 + "px";// (window.document.body.offsetHeight + scrollT - h )/2  + "px";//

   	 objTop = window.screen.height/5;
	if(e){
      if(e.clientY <= window.screen.height/5 )objTop = e.clientY - 20;
	} 

   	    obj.style.top= scrollT + objTop + "px";
}




//获取随机颜色值;
function GetColor()
{
	var r = Math.floor(Math.random() * 255).toString(16);
	var g = Math.floor(Math.random() * 255).toString(16);
	var b = Math.floor(Math.random() * 255).toString(16);
	r = r.length == 1 ? "0" + r : r;
	g = g.length == 1 ? "0" + g : g;
	b = b.length == 1 ? "0" + b : b;
	return "#" + r + g + b;
}
