function setCookie(name, value, days) {
	var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = '; expires='+date.toGMTString();
	}
	document.cookie = name+'='+value+expires+'; path=/; domain=.dm.game.mop.com;';
}
function getCookie(name) {
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	for(var i=0; i<ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}
	function dmstatRefresh(){
//		document.getElementById("ssi").src = "http://dmstat.www.wangyou.com/s.html?"+Math.random();
	}

function init()
{
	var onlyself = parseInt(getCookie("cartoonBox=onlySelf"));
	
	//默认为图片本身尺寸
	if (onlyself != 0)	 
	{
		onlyself = 1;
//		document.getElementById("self").src = "/images/viewtool/btn-05-2.gif";
	}
	else
	{
		onlyself = 0;
//		document.getElementById("wellsize").src = "/images/viewtool/btn-06-2.gif";
	}
}


// ------------------- 鼠标左键移动图片 ---------------------//
var Obj='';
document.onmouseup=MUp;
document.onmousemove=MMove;

//传入被捕获的移动对象
function MDown(Object)
{
	Obj=Object.id
	document.all(Obj).setCapture()
	pX=event.x-document.all(Obj).style.pixelLeft;
	pY=event.y-document.all(Obj).style.pixelTop;
	var height = 0;
	var width = 0;
}

function MMove()
{
	if(Obj!='')
	{
		document.all(Obj).style.left=event.x-pX;
		document.all(Obj).style.top=event.y-pY;
	}
}
function MUp()
{
	if(Obj!='')
	{
		document.all(Obj).releaseCapture();
		Obj='';
	}
}


//其它操作方法
function loadpic()
{
	//载入完图片后获取图片的信息
	cartoonpic = document.getElementById("iphoto");
	picW = cartoonpic.width;
	picH = cartoonpic.height;
	selfW = picW;
	selfH = picH;

	var onlyself = parseInt(getCookie("cartoonBox=onlySelf"));
	if (onlyself == 0)
	{
		wellsize();
	}


//	var Pop = document.getElementById("LoadPop");
//	Pop.innerHTML = "载入完成！";

		
	//alert("mygod:"+picW);
//	var objPic = document.getElementById("hand");
	var objPic = document.getElementById("iphoto");
	
	var thisSW = document.body.clientWidth;
	//alert(thisW);
	objPic.style.posLeft = (thisSW-picW)/2;
	
}

//放大
function zoom()
{
	if (picW > 0 && picH > 0)
		{
			picH = picH * 130 / 100;
			picW = picW*130/100;
			cartoonpic.height = picH;
			cartoonpic.width = picW;
		}
		dmstatRefresh();
}


//缩小
function mini()
{
	if (picW > 0 && picH > 0)
		{
			if (picW > 100 || picH > 100)
			{
				picH = picH * 70 / 100;
				picW = picW * 70 / 100;
				cartoonpic.height = picH;
				cartoonpic.width = picW;
			}
		}
		dmstatRefresh();
		
}

//还原原大小
function self()
{
	cartoonpic.height = selfH;
	cartoonpic.width = selfW;
	setCookie("cartoonBox=onlySelf","1",604800000,"/");
	
//	document.getElementById("self").src = "/images/viewtool/btn-05-2.gif";
//	document.getElementById("wellsize").src = "/images/viewtool/btn-06.gif";
				var thisSW = document.body.clientWidth;
				cartoonpic.style.posLeft = (thisSW-cartoonpic.width)/2;
				cartoonpic.style.posTop = 1;

				picH = cartoonpic.height;
				picW = cartoonpic.width;
		dmstatRefresh();

}

//最佳尺寸
function wellsize()
{

	var oBody = document.body;

	var tH = oBody.clientHeight;
	var tW;
//	var fitHeight=660;	//图片外的DIV最合适的高度
	if (tH > 690)
	{
		tH = 580;
		tW = 900;
	}
	else
	{
		tH = 480;
		tW = 900;
	}
	//var tH =	500;
	//var tW = 800;
	//document.write("here:"+tH);

	if (selfH > tH)	
	{
		cartoonpic.height = tH;
		cartoonpic.width = selfW*tH/selfH;
	}
	else if (selfW > tW)
	{
		cartoonpic.width = tW;
		cartoonpic.height= selfH*tW/selfW; 
	}
	setCookie("cartoonBox=onlySelf","0",604800000,"/");
//	document.getElementById("wellsize").src = "/images/viewtool/btn-06-2.gif";
//	document.getElementById("self").src = "/images/viewtool/btn-05.gif";

//	var objPic = document.getElementById("hand");
	var objPic = document.getElementById("iphoto");
	var thisSW = document.body.clientWidth;
	//alert(thisW);
//	objPic.style.posLeft = (thisSW-selfW)/2;
	objPic.style.posLeft = (thisSW-cartoonpic.width)/2;
	objPic.style.posTop = 1;
	
				picH = cartoonpic.height;
				picW = cartoonpic.width;
		dmstatRefresh();
				

}


//所有文档图片自动缩放
/*
function picautosize() 
{ 
    for(i=0; i<document.images.length;i++)
	{
        if(document.images[i].height>500)
		{
            document.images[i].height=500
        } 
		else 
		{
            document.images[i].height=document.images[i].height
        }
    }
}
*/



//图片鼠标滚轮缩放
function piczoom(e, o)  
{
	var p = event.wheelDelta/120;
	if (p < 0)
	{
		mini();
	}
	else
	{
		zoom();
	}
/*
  var zoom = parseInt(o.style.zoom, 10) || 100;
  zoom += event.wheelDelta / 12;
  if (zoom > 0) o.style.zoom = zoom + '%';
  return false;
*/
}

//鼠标事件 onmousewheel="return piczoom(event,this)" 图片处调用
function document.onmousedown()
{
	//if(event.button==0) {alert("0 没键?:)");}  
    //if(event.button==1)        { alert("1 左键");}
    //if(event.button==2)   { alert("2 右键"); } 
    //if(event.button==3)        { alert("3 按住右键点左键"); }
    if(event.button==4) //中键 
	{
		self();
		//nexppage();
	}
    //if(event.button==5) {alert("5  左中键");} 
    //if(event.button==6) { alert("6 按住右键点中键");}
    //if(event.button==7) {alert("7 三键全按");} 
}


//键盘事件
function keydown()
{
	//空格
	if ( event.keyCode== 32)
	{
		nextpage();
	}

	//pageup
	if (event.keyCode== 33)
	{
		prevpage();
	}
	//pagedown
	if (event.keyCode== 34)
	{
		nextpage();
	}

	//最佳尺寸:num del
	if (event.keyCode == 110)
	{
		wellsize();
	}
		
	//缩小:-
	if (event.keyCode== 109)
	{
		mini();
	}
	//放大:+
	if (event.keyCode== 107)
	{
		zoom();
	}
	//恢复:*
	if (event.keyCode== 106)
	{
		self();
	}

	if (event.keyCode==13)
	{
		//location=bookpage
	}
	//上一页
	if (event.keyCode==37)
	{
	//	prepage();	//左键
	}
	//下一页
	if (event.keyCode==39)
	{
	//	nextpage();	//右键
	}

	//滚动条向下
	if (event.keyCode == 40)
		document.all.picwin.scrollTop = document.all.picwin.scrollTop + 100;
	//滚动条向上
	if (event.keyCode == 38)
		document.all.picwin.scrollTop = document.all.picwin.scrollTop - 100;

	//滚动条向左
	if (event.keyCode == 37)
		document.all.picwin.scrollLeft = 0;
	//滚动条向右
	if (event.keyCode == 39)
		document.all.picwin.scrollLeft = 9999;

}


function rmouse()
{
	if (selfH == cartoonpic.height)
	{
		wellsize();
	}
	else
	{
		self();
	}
	
	event.cancelBubble = true
	event.returnValue = false;
	return false;
}

//一直原始大小
function onlySelf()
{
	setCookie("cartoonBox=onlySelf","1",604800000,"/");
	self();
	
}

//一直最适大小
function onlyWell()
{
	setCookie("cartoonBox=onlySelf","0",604800000,"/");
	wellsize();

}

//双页
function doublePic()
{
	setCookie("cartoonBox=pages","2",604800000,"/");
}

//单页
function onePic()
{
	setCookie("cartoonBox=pages","1",604800000,"/");
}

