var zoom=100;
var iMoveDivState = 0;
var isExistGetWordDIV=false;
var originX,originY;
var drag = false;
var dragX,dragY;
var iGetWordOrPicture = 0;

function autoResize(){
	var ch = document.documentElement.clientHeight

    if(document.getElementById("readBody")!= null){
		document.getElementById("readBody").style.height=ch-30+"px"
	}
}

function img_zoom(e, o)  //图片鼠标滚轮缩放
{
		zoom = parseInt(o.style.zoom, 10) || 100;
		zoom += event.wheelDelta / 12;
		if(zoom>500) zoom = 500;
		if(zoom<50) zoom = 50;
		if (zoom > 0) o.style.zoom = zoom + '%';
	  return false;
}

function zoom_click(i)  //图片鼠标滚轮缩放
{
		//zoom = parseInt(o.style.zoom, 10) || 100;
		zoom += i;
		if(zoom>500) zoom = 500;
		if(zoom<50) zoom = 50;
		if (zoom > 0) document.getElementById("oImg").style.zoom = zoom + '%';
	  return false;
}
	
function changeGetWordState(i)	//开始提取文字
{	
		
		isExistGetWordDIV=false;
		iMoveDivState=1;
		iGetWordOrPicture=i;
		document.getElementById("oImg").style.cursor="crosshair";
		document.getElementById("divMove").style.display="none";		
		document.getElementById("divMove").style.cursor="crosshair";
		document.getElementById("divMove").innerHTML = "";
		
}
	
function _myMouseDown(o)	//按下鼠标
{	
	if(o.id=='oImg'){
		if(iMoveDivState==1){
			iMoveDivState=2;
			if(document.all){originX=event.x;originY=event.y;}else{originX=event.pageX;originY=event.pageY;}
		}
	}
	if(o.id=='divTitle'){
			dragStart();
	}
}

function dragStart()	//开始拖动
{	
		if(!drag && isExistGetWordDIV) {
	    var x,y;
	    if(document.all){
	    	x = event.x;
	    	y = event.y;
	    }else{
	    	x = e.pageX;
	    	y = e.pageY;
	    }
    	dragX = x - document.getElementById('divMove').offsetLeft;
			dragY = y - document.getElementById('divMove').offsetTop;
			drag=true;
		}
}
	


function _myMouseMove(e,o){	//鼠标移动
	if(iMoveDivState==2){
    showGetWordDiv(e);
	}
	else{
		draging(e);
	}
}

function showGetWordDiv(e){	//拉伸区域
	var x,y;
    if(document.all){
    	x = event.x;
    	y = event.y;
    }else{
    	x = e.pageX;
    	y = e.pageY;
    }
		document.getElementById('divMove').style.display="block";
		document.getElementById('divMove').style.width = Math.abs(x-originX) + "px";
		document.getElementById('divMove').style.height = Math.abs(y-originY)+ "px";
		
		if(x > originX){
			document.getElementById('divMove').style.left = originX+ "px";
		}else {
			document.getElementById('divMove').style.left = x+ "px";
		}
		
		if(y > originY){
			document.getElementById('divMove').style.top = originY+ "px";
		}else {
			document.getElementById('divMove').style.top = y+ "px";
		}
}

function draging(e)	//拖动
{	
		if(drag && isExistGetWordDIV) {
	    var x,y;
	    if(document.all){
	    	x = event.x;
	    	y = event.y;
	    }else{
	    	x = e.pageX;
	    	y = e.pageY;
	    }
			document.getElementById('divMove').style.left = (x - dragX)+ "px";
			document.getElementById('divMove').style.top = (y - dragY) + "px";
			if(x < (document.getElementById('oImg').offsetLeft + dragX)) 
				document.getElementById('divMove').style.left = document.getElementById('oImg').offsetLeft + "px";
			if(x > (document.getElementById('oImg').offsetWidth*zoom/100 + document.getElementById('oImg').offsetLeft - document.getElementById('divMove').offsetWidth + dragX))
				document.getElementById('divMove').style.left = document.getElementById('oImg').offsetWidth*zoom/100 + document.getElementById('oImg').offsetLeft - document.getElementById('divMove').offsetWidth + "px";
			if(y < (document.getElementById('oImg').offsetTop + dragY))
				document.getElementById('divMove').style.top = document.getElementById('oImg').offsetTop;
			if(y > (document.getElementById('oImg').offsetHeight*zoom/100 + document.getElementById('oImg').offsetTop - document.getElementById('divMove').offsetHeight + dragY) )
			 	document.getElementById('divMove').style.top = document.getElementById('oImg').offsetHeight*zoom/100 + document.getElementById('oImg').offsetTop - document.getElementById('divMove').offsetHeight;
			
		}
}

function _myMouseUp()	//弹开鼠标
{
	if(iMoveDivState==2){
		if(document.all) {
			isExistGetWordDIV=true;
			iMoveDivState=0;
			document.getElementById("oImg").style.cursor="default";
			document.getElementById("divMove").style.cursor="default";
			fillMoveDIV();
		}
	}
	else{
		dragOver();
	}
}
	
function dragOver()	//停止拖动
{	
		if(drag && isExistGetWordDIV) {
			drag = false;
			dragX = 0;
			dragY = 0;
		}
}

function getText()
{
	document.getElementById('content').innerHTML="<textarea style='width:"+document.getElementById('content').offsetWidth+";height:"+(document.getElementById('divMove').offsetHeight-25)+"'>"+document.getElementById('content').offsetWidth+" "+document.getElementById('content').offsetHeight+"</textarea>";
	document.getElementById('divMove').style.filter = 'alpha(opacity=100)';
	document.getElementById('divMove').style.opacity = '1';
}

function getPic()
{
	document.getElementById('content').innerHTML="<img src='http://www.du8.com/images_www/default_SEPbook_04.gif'  style='width:"+document.getElementById('content').offsetWidth+";height:"+(document.getElementById('divMove').offsetHeight-25)+"' >";
	document.getElementById('divMove').style.filter = 'alpha(opacity=100)';
	document.getElementById('divMove').style.opacity = '1';
}

function closeMoveDiv()
{
	document.getElementById('divMove').style.display='none';
	iMoveDivState=0;
	isExistGetWordDIV=false;
	//removeMoveDIV();
}


function createMoveDIV()
{
     var div = document.createElement("div");
     //div.outerHTML = "<div id=\"divMove\" style=\"width:0;height:0\" onmousemove=\"_myMouseMove(event,this)\"  onmouseup=\"_myMouseUp()\"></div>";
     document.getElementById("readBody").appendChild(div);
     //alert(document.getElementById("divMove"));
}

function fillMoveDIV(){
	if(iGetWordOrPicture==0){
		var divstr = "<div id=\"divTitle\" onmousedown=\"_myMouseDown(this)\" > <span class=\"winclose\" onclick=\"closeMoveDiv()\">[关闭]</span>文字截取 </div>";
	  divstr += "<div class=\"content\" id=\"content\" style=\"clear:both\">";
	  divstr += "    <table height='70%' width='100%'>";
	  divstr += "      <tr>";
	  divstr += "        <td  valign='middle' ondblclick='getText()'><span class='zhailu' onclick='getText()'>点击【确定】截取 </span></td>";
	  divstr += "      </tr>";
	  divstr += "    </table>";
	  divstr += "</div>";
	}  
  if(iGetWordOrPicture==1){
		var divstr = "<div id=\"divTitle\" onmousedown=\"_myMouseDown(this)\" > <span class=\"winclose\" onclick=\"closeMoveDiv()\">[关闭]</span>图片截取 </div>";
	  divstr += "<div class=\"content\" id=\"content\" style=\"clear:both\">";
	  divstr += "    <table height='70%' width='100%'>";
	  divstr += "      <tr>";
	  divstr += "        <td  valign='middle' ondblclick='getText()'><span class='zhailu' onclick='getPic()'>点击【确定】截取 </span></td>";
	  divstr += "      </tr>";
	  divstr += "    </table>";
	  divstr += "</div>";
  }  
  document.getElementById("divMove").innerHTML = divstr;
}

function removeMoveDIV()
{
	document.getElementById("readBody").removeChild(document.getElementById("divMove"));
}



function CopyImage(img)
{
 if (img.tagName != 'IMG') return;
 if (typeof img.contentEditable == 'undefined') return;
 if (!document.body.createControlRange) return;
 var ctrl = document.body.createControlRange();
 img.contentEditable = true;
 ctrl.addElement(img);
 ctrl.execCommand('Copy');
 img.contentEditable = false;
 alert('复制完成，到QQ对话框里按Ctrl-V就可以啦！');
}

