﻿function ShowHideDiv(id,Speed,Step){
	/*
	  显示隐藏Div
	  id 要显示隐藏的Div的id
	  Speed 展开速度,越小越快
	  Step 展开步距,越大越快
	*/
	var _self=this;
	_self.obj=document.getElementById(id);
    _self.objInitHeight=_self.obj.offsetHeight;
	_self.objInitWidth=_self.obj.offsetWidth;
	_self.obj.style.height="0px";
	_self.obj.style.width="0px";
	_self.HTimer=null;
	_self.WTimer=null;
	_self.SPEED=Speed;
    _self.STEP=Step;
	
	_self.OpenHeight=function(){
		var curH=parseInt(_self.obj.style.height.replace('px',''))
		clearTimeout(_self.HTimer);
		_self.obj.style.width=(_self.objInitWidth+ 'px');
		_self.obj.style.visibility='visible';
	    if(curH+_self.STEP<_self.objInitHeight){
			curH+=_self.STEP
			_self.obj.style.height=(curH + 'px');
		    _self.HTimer=window.setTimeout(_self.OpenHeight,_self.SPEED);
	    }
		else{
			_self.obj.style.height=(_self.objInitHeight + 'px');
		}
    }
	
	_self.CloseHeight=function(){
		var curH=parseInt(_self.obj.style.height.replace('px',''))
		clearTimeout(_self.HTimer)
	    if(curH>_self.STEP){
			curH-=_self.STEP
			_self.obj.style.height=(curH + 'px');
		    _self.HTimer=window.setTimeout(_self.CloseHeight,_self.SPEED);
	    }
		else{
			_self.obj.style.height='0px';
			_self.obj.style.visibility='hidden';
		}
    }
	
	_self.OpenWidth=function(){
		var curW=parseInt(_self.obj.style.width.replace('px',''))
		clearTimeout(_self.WTimer);
		_self.obj.style.height=(_self.objInitHeight+ 'px');
		_self.obj.style.visibility='visible';
	    if(curW+_self.STEP<_self.objInitWidth){
			curW+=_self.STEP
			_self.obj.style.width=(curW + 'px');
		    _self.WTimer=window.setTimeout(_self.OpenWidth,_self.SPEED);
	    }
		else{
		    _self.obj.style.width=(_self.objInitWidth + 'px');
		}
    }
	
	_self.CloseWidth=function(){
		var curW=parseInt(_self.obj.style.width.replace('px',''))
		clearTimeout(_self.WTimer)
	    if(curW>_self.STEP){
			curW-=_self.STEP
			_self.obj.style.width=(curW + 'px');
		    _self.WTimer=window.setTimeout(_self.CloseWidth,_self.SPEED);
	    }
		else{
			_self.obj.style.width='0px';
			_self.obj.style.visibility='hidden';
		}
    }
	
	_self.OpenWidthHeight=function(){
		_self.OpenW();
		_self.OpenH();
	}
	
	_self.CloseWidthHeight=function(){
	    _self.CloseWidth();
		_self.CloseHeight();
	}
	
	_self.OpenH=function(){
		var curH=parseInt(_self.obj.style.height.replace('px',''))
		clearTimeout(_self.HTimer);
		_self.obj.style.visibility='visible';
	    if(curH+_self.STEP<_self.objInitHeight){
			curH+=_self.STEP
			_self.obj.style.height=(curH + 'px');
		    _self.HTimer=window.setTimeout(_self.OpenH,_self.SPEED);
	    }
		else{
			_self.obj.style.height=(_self.objInitHeight + 'px');
		}
    }
	
	_self.OpenW=function(){
		var curW=parseInt(_self.obj.style.width.replace('px',''))
		clearTimeout(_self.WTimer);
		_self.obj.style.visibility='visible';
	    if(curW+_self.STEP<_self.objInitWidth){
			curW+=_self.STEP
			_self.obj.style.width=(curW + 'px');
		    _self.WTimer=window.setTimeout(_self.OpenW,_self.SPEED);
	    }
		else{
		    _self.obj.style.width=(_self.objInitWidth + 'px');
		}
    }	
}

function MoveDiv(id,Speed,Step){
	/*
	  移动Div
	  id 要显示隐藏的Div的id
	  Speed 展开速度,越小越快
	  Step 展开步距,越大越快度
	*/
    var _self=this;
	_self.obj=document.getElementById(id);
	_self.SPEED=Speed;
	_self.STEP=Step;
	_self.HTimer=null;
	_self.VTimer=null;
	_self.LeftMove=function(n){
		var curX=parseInt(_self.obj.style.left.replace('px',''));
		clearTimeout(_self.HTimer);
		if(n>0){
		   if(n>_self.STEP){
		      n-=_self.STEP;
		      curX-=_self.STEP;
		      _self.obj.style.left=(curX + 'px');
		      _self.HTimer=window.setTimeout(function(){_self.LeftMove(n)},_self.SPEED);
		   }
		   else{
			  curX-=n;
			  n=0;
			  _self.obj.style.left=(curX + 'px');
		   }
		}
	}
	
	_self.RightMove=function(n){
		var curX=parseInt(_self.obj.style.left.replace('px',''));
		clearTimeout(_self.HTimer);
		if(n>0){
		   if(n>_self.STEP){
		      n-=_self.STEP;
		      curX+=_self.STEP;
		      _self.obj.style.left=(curX + 'px');
		      _self.HTimer=window.setTimeout(function(){_self.RightMove(n)},_self.SPEED);
		   }
		   else{
			  curX+=n;
			  n=0;
			  _self.obj.style.left=(curX + 'px');
		   }
		}
	}
	
	_self.UpMove=function(n){
		var curY=parseInt(_self.obj.style.top.replace('px',''));
		clearTimeout(_self.VTimer);
		if(n>0){
		   if(n>_self.STEP){
		      n-=_self.STEP;
		      curY-=_self.STEP;
		      _self.obj.style.top=(curY + 'px');
		      _self.VTimer=window.setTimeout(function(){_self.UpMove(n)},_self.SPEED);
		   }
		   else{
			  curY-=n;
			  n=0;
			  _self.obj.style.top=(curY + 'px');
		   }
		}
	}
	
	_self.DownMove=function(n){
		var curY=parseInt(_self.obj.style.top.replace('px',''));
		clearTimeout(_self.VTimer);
		if(n>0){
		   if(n>_self.STEP){
		      n-=_self.STEP;
		      curY+=_self.STEP;
		      _self.obj.style.top=(curY + 'px');
		      _self.VTimer=window.setTimeout(function(){_self.DownMove(n)},_self.SPEED);
		   }
		   else{
			  curY+=n;
			  n=0;
			  _self.obj.style.top=(curY + 'px');
		   }
		}
	}
	
	_self.MoveTo=function(x,y){
	    var curX=parseInt(_self.obj.style.left.replace('px',''));	
		var curY=parseInt(_self.obj.style.top.replace('px',''));
		if(x>curX){
		   _self.LeftMove(x-curX)	
		}
		else if(x<curX){
		   _self.RightMove(curX-x)	
		}
		if(y>curY){
		   _self.DownMove(y-curY)	
		}
		else if(y<curY){
		   _self.UpMove(curY-y)	
		}
	}
}
