﻿//圈人
function tag()
{
	if(rotateing)
	{
		return;
	}
	
	if(tagobj != null)
	{
		tagobj.reset();
	}
	else
	{
		var html = $("taghtml").innerHTML;
		$("taghtml").innerHTML = "";
		
		tagobj = new Cropper.Img(
			'photo1',
			{
				onEndCrop: onEndCrop ,
				hasTip: true,
				tipCss: "width:200px;margin-top:4px;z-index:500;color:#333333;position:absolute;",
				tipHtml : html
			}
		) ;
		
	}
	$("tagtip").style.display = "block"; 
	
	taging = true;
	resetmask();
	window.location.hash='top';
	document.body.onselectstart = function() { return false; };
	new friendSuggest("tipinput" , "请输入人物姓名" , "");
}

function tagclose()
{
	if(tagobj != null)
	{
		tagobj.remove();
	}
	$("tagtip").style.display = "none"; 
	taging = false;
	resetmask();
	document.body.onselectstart = function() {};
}

function onEndCrop( coords, dimensions ) 
{
	$( 'x1' ).value = coords.x1;
	$( 'y1' ).value = coords.y1;
	$( 'x2' ).value = coords.x2;
	$( 'y2' ).value = coords.y2;
}

function tagdelajaxshow(req)
{
	var r = req.responseText;
	if(r == 0)
	{
		return false;
	}
	tagclose();
	
	var newmark = [];
	var j=0;
	var delnum;
	for(var i=0 ; i<mark_list[markdata].length ; i++)
	{
		if(mark_list[markdata][i].markid == r)
		{	
			delnum = i;
			continue;
		}
		newmark[j] =  mark_list[markdata][i];
		j++;
	}
	mark_list[markdata] = newmark;
	tagdelborder(delnum);
	
	tagtitles();
}

function tagaddajaxshow(req)
{
	var r = req.responseText;
	if(r == 0)
	{
		return false;
	}
	eval("r="+r);
	tagobj.reset();
	
	var flag = false;
	for(var i=0 ; i<mark_list[markdata].length ; i++)
	{ 
		if(mark_list[markdata][i].mark_uid == r.mark_uid && r.mark_uid != 0)
		{
			mark_list[markdata][i] = r;
			flag = true;
		}
	}
	if(false == flag)
	{
		mark_list[markdata][mark_list[markdata].length] = r;
	}
	
	tagaddborder(mark_list[markdata].length-1);
	tagtitles();
}

function tagaddborder(ordernum)
{
	if(rotateing)
	{
		return;
	}
	var info = mark_list[markdata][ordernum];
	
	if($('tagborder'+ordernum))
	{
		return;
	}
	
	resetmask();

	var tagborder = document.createElement('div');
	tagborder.style.cssText = "position:absolute;";
	tagborder.style.left = parseInt((info.x-info.width/2)*$('photo1').width/10000) + "px";
	tagborder.style.top = parseInt((info.y-info.height/2)*$('photo1').height/10000) + "px";
	tagborder.style.height = parseInt(info.height*$('photo1').height/10000) + "px";
	tagborder.style.width = parseInt(info.width*$('photo1').width/10000) + "px";
	tagborder.id = 'tagborder'+ordernum;
	tagborder.innerHTML = "<div style=\"width:tagborder.style.width;\"><div style='border:solid #FFFFFF 3px;'><div style='border:solid #D01E3B 3px;width:"
		+(parseInt(tagborder.style.width)-12)+"px;height:"
		+(parseInt(tagborder.style.height)-12)+"px;'></div></div>";
	tagborder.innerHTML += "<center><div style=\"margin-top:5px;border:1px solid #ffffff;background:#D01E3B;color:#ffffff;font: bold 14px;padding:2px;text-align:center;width:" + b_strlen(info.title) + "em;\"><b>"+info.title+"</b></div></center></div>";
	$('photodiv2').appendChild(tagborder);
} 

function tagdelborder(ordernum)
{
	var tagborder = $("tagborder"+ordernum);
	if(tagborder)
	{
		$('photodiv2').removeChild(tagborder);
	}
}

function imageonclick(evnt)
{
	var photopos1 = getpos($("photodiv1"));
	var photopos2 = getpos($("photodiv2"));
	var x = 1;
	if (evnt)
	{
		x = (parseInt(evnt.clientX)-photopos1.left-photopos2.left) / $("photo1").width;
	}
	
	if(x>0.5)
	{
		viewnext();
	}
	
	if (x<=0.5)
	{
		viewprev();
	}
}

function imageonmousemove(evnt)
{
	var photopos1 = getpos($("photodiv1"));
	var photopos2 = getpos($("photodiv2"));
	var x = y = 1;
	if (evnt)
	{
		x = (parseInt(evnt.clientX)-photopos1.left-photopos2.left) * 10000 / $("photo1").width;
		y = (parseInt(evnt.clientY)-photopos1.top-photopos2.top+parseInt(document.body.scrollTop+document.documentElement.scrollTop)) * 10000 / $("photo1").height;
	}
	
	for(var i=0 ; i<mark_list[markdata].length ; i++)
	{
		var obj = mark_list[markdata][i];
		if(parseInt(obj.width)/2 + parseInt(obj.x) > x && parseInt(obj.x) - parseInt(obj.width)/2 <x
			&& parseInt(obj.height)/2 + parseInt(obj.y) > y && parseInt(obj.y) - parseInt(obj.height)/2 < y)
		{
			tagaddborder(i);
		}
		else
		{
			tagdelborder(i);
		}
	}
	
	if (evnt)
	{
		nx = (parseInt(evnt.clientX)-photopos1.left-photopos2.left) / $("photo1").width;
		
		if(nx>0.5)
		{
			$("photo1").style.cursor = "url("+"../images/right.cur"+"),auto";
			$("photo1").title = "点击跳到下一张>>";
		}
		
		if (nx<=0.5)
		{
			$("photo1").style.cursor = "url("+"../images/left.cur"+"),auto";
			$("photo1").title = "<<点击跳到上一张";
		}
	}
}

//图片客户端处理
function zoomEx(obj, value, solidwidth)
{
	if (zoomlevel == 0)
	{
		obj.width = $("realwidth").value;
		obj.height = $("realheight").value;
	}
	else
	{
		if (solidwidth)
		{
			if(value > 1)
			{
				if (parseInt(obj.width) * value > solidwidth && parseInt(obj.width) < solidwidth)
				{
					obj.height = parseInt(solidwidth * parseInt(obj.height) / parseInt(obj.width));
					obj.width = solidwidth;
				}
				else
				{
					obj.width = parseInt(obj.width) * value;
					obj.height = parseInt(obj.height) * value;
				}
			}
			else
			{
				if (parseInt(obj.width) * value < solidwidth && parseInt(obj.width) > solidwidth)
				{
					obj.height = parseInt(solidwidth * parseInt(obj.height) / parseInt(obj.width));
					obj.width = solidwidth;
				}
				else
				{
					obj.width = parseInt(obj.width) * value;
					obj.height = parseInt(obj.height) * value;
				}
			}
		}
		else
		{
			obj.width = parseInt(obj.width) * value;
			obj.height = parseInt(obj.height) * value;
		}
	}
}

function zoom(value, solidwidth)
{
	if(value > 1)
	{
		zoomlevel += 1;
	}
	else
	{
		zoomlevel -= 1;
	}
	var obj = $("photo1");
	zoomEx(obj, value, solidwidth);

	var obj = $("photo2");
	zoomEx(obj, value, solidwidth);
	
	resetmask();
}

function rotate(value)
{
	if(taging || !IsIE())
	{
		return;
	}
	rotatevalue = (value + rotatevalue) % 4;

	var obj = $("photo1");
	obj.style.filter="progid:DXImageTransform.Microsoft.BasicImage(   Rotation="+rotatevalue+")";

	var obj = $("photo2");
	obj.style.filter="progid:DXImageTransform.Microsoft.BasicImage(   Rotation="+rotatevalue+")";
	
	rotateing = rotatevalue != 0;
	
	v_tmp = $("photodiv4").style.height;
	$("photodiv4").style.height = parseInt($("photodiv4").style.width) + "px";
	$("photodiv4").style.width = parseInt(v_tmp) + "px";

	var v_tmp = $("photodiv2").style.height;
	$("photodiv2").style.height = parseInt($("photodiv2").style.width) + "px";
	$("photodiv2").style.width = parseInt(v_tmp) + "px";

	$('photodiv1').style.height = parseInt($('photodiv2').style.height) + "px";
	$('photodiv3').style.height = parseInt($('photodiv4').style.height) + "px";

	if($('photo1').style.left != "")
	{	
		$('photodiv2').style.left = parseInt($('photo1').style.left) + "px";
		$('photodiv4').style.left = parseInt($('photo2').style.left) + "px";
	}
	if($('photo1').style.top != "")
	{
		$('photodiv2').style.top = parseInt($('photo1').style.top) + "px";
		$('photodiv4').style.top = parseInt($('photo2').style.top) + "px";
	}
}

//图片两种显示模式
function zp_qp_set()
{
	if($("zp_lb").style.display == "none")
	{
		partviewmode();
	}
	else
	{
		wholeviewmode();
	}
}

function resetmask()
{
	if (rotatevalue % 2)
	{
		if (taging)
		{
			if (parseInt($('photo1').height) < 200)
			{
				$('photodiv2').style.width = "200px";
				$('photodiv4').style.width = "200px";
			}
			else
			{
				$('photodiv2').style.width = parseInt($('photo1').height) + "px";
				$('photodiv4').style.width = parseInt($('photo2').height) + "px";
			}
			$('photodiv2').style.height = (parseInt($('photo1').width)+200) + "px";
		}
		else
		{
			$('photodiv2').style.width = parseInt($('photo1').height) + "px";
			$('photodiv2').style.height = parseInt($('photo1').width) + "px";
			$('photodiv4').style.width = parseInt($('photo2').height) + "px";
		}
		$('photodiv4').style.height = parseInt($('photo2').width) + "px";
	}
	else
	{
		if (taging)
		{
			if (parseInt($('photo1').width) < 200)
			{
				$('photodiv2').style.width = "200px";
				$('photodiv4').style.width = "200px";
			}
			else
			{
				$('photodiv2').style.width = parseInt($('photo1').width) + "px";
				$('photodiv4').style.width = parseInt($('photo2').width) + "px";
			}
			$('photodiv2').style.height = (parseInt($('photo1').height)+200) + "px";
		}
		else
		{
			$('photodiv2').style.width = parseInt($('photo1').width) + "px";
			$('photodiv2').style.height = parseInt($('photo1').height) + "px";
			$('photodiv4').style.width = parseInt($('photo2').width) + "px";
		}
		$('photodiv4').style.height = parseInt($('photo2').height) + "px";
	}
	
	$('photodiv1').style.height = parseInt($('photodiv2').style.height) + "px";
	$('photodiv3').style.height = parseInt($('photodiv4').style.height) + "px";

	if($('photo1').style.left != "")
	{	
		$('photodiv2').style.left = parseInt($('photo1').style.left) + "px";
		$('photodiv4').style.left = parseInt($('photo2').style.left) + "px";
	}
	if($('photo1').style.top != "")
	{
		$('photodiv2').style.top = parseInt($('photo1').style.top) + "px";
		$('photodiv4').style.top = parseInt($('photo2').style.top) + "px";
	}
	
	imageonmousemove();
}

function partviewmode()
{
	var v_maxsize = 720;
	while ($("photo1").width > v_maxsize)
	{
		zoom(0.833, v_maxsize);
	}
	
	$("zp_lb").style.display = "block";
	$("zp_qp").src = "/q/images/zp_qp1_on.gif";
	$("zp_m").className = "zp_m";
	$("photodiv1").className = "zp_zs2";
	$('photodiv2').style.textAlign = "left";
	$("photodiv3").className = "zp_zs2";
	$('photodiv4').style.textAlign = "left";
	setCookie("viewmode", "part");
}

function wholeviewmode()
{
	if (zoomlevel)
	{
		zoomlevel = 1;
		zoom(0.833, 0);
	}

	$("zp_lb").style.display = "none";
	$("zp_qp").src = "/q/images/zp_qp2_on.gif";
	$("zp_m").className = "zp_m2";
	$("photodiv1").className = "zp_zs3";
	$('photodiv2').style.textAlign = "left";
	$("photodiv3").className = "zp_zs3";
	$('photodiv4').style.textAlign = "left";
	setCookie("viewmode", "whole");
}

function photodivonmouseover()
{
	$("zp_qp").style.visibility = "visible";
	var viewmode = getViewMode();
	if(viewmode == "part" && parseInt($("realwidth").value)<731)
	{
		$("zp_qp").style.visibility = "hidden";
	}
}

function photodivonmouseout()
{
	$("zp_qp").style.visibility = "hidden";
	var viewmode = getViewMode();
	if(viewmode == "part" && parseInt($("realwidth").value)>=731)
	{
		$("zp_qp").style.visibility = "visible";
	}
}

function getViewMode()
{
	var viewmode = getCookie("viewmode");
	viewmode = (viewmode != "whole") ? "part" : viewmode;
	return viewmode;
}

function getPhotoWH(v_width, v_height)
{
	var a_wh = new Array;
	a_wh[0] = a_wh[1] = 60;
	v_width = parseInt(v_width);
	v_height = parseInt(v_height);
	if (v_width > v_height)
	{
		a_wh[1] = Math.ceil(a_wh[0] * v_height / v_width);
	}
	else
	{
		a_wh[0] = Math.ceil(a_wh[1] * v_width / v_height);
	}
	return a_wh;
}

function viewprev()
{
	viewphoto((photo_pos + photo_total - 1) % photo_total);
}

function viewnext()
{
	viewphoto((photo_pos + 1) % photo_total);
}

function onphotoload(v_img)
{
	if (photo_list && photo_list[photo_pos] && photo_list[photo_pos]["small"])
	{
		if (v_img.src == photo_list[photo_pos]["small"])
		{
			$("photo1").src = photo_list[photo_pos]["big"];
			$("photo2").src = photo_list[photo_pos]["big"];
		}
		else
		{
			h("loadingdiv");
		}
	}
	else
	{
		h("loadingdiv");
	}
	v_img.alt = "";
}

function initloadingdiv()
{
	var pTop = ($("realheight").value - 42) / 2;
	if (pTop < 0)
	{
		pTop = 0;
	}
	else if (pTop > 200)
	{
		pTop = 200;
	}
	if (parseInt($("realwidth").value) > 720)
	{
		$("loadingdiv").style.width = "720px";
	}
	else
	{
		$("loadingdiv").style.width = $("realwidth").value + "px";
	}
	$("loadingdiv").style.paddingTop = pTop + "px";
}

