//圈人
var zoomlevel = 0;
var g_taghtml = "";

function $$(id)
{
return document.getElementById(id);
}


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;
	}
	$$("usertagtextdiv").innerHTML = '<img src="/UserManage/images/success.gif"> 圈人已成功！请继续圈人，或点击按钮“结束圈人”操作';
	
	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; z-index:1000;";
	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=\"z-index:1000; 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)-photopos2.left) / $$("photo1").width;
	}
	
	if(x>0.5)
	{
		viewnext();
	}
	
	if (x<=0.5)
	{
		viewprev();
	}
}

function piceonclick(evnt)
{

	var photopos1 = getpos($$("photodiv1"));
	var photopos2 = getpos($$("photodiv2"));
	var x = 1;
	if (evnt)
	{
		x = (parseInt(evnt.clientX)-photopos2.left) / $$("photo1").width;
	}
	
	if(x>0.5)
	{
		Photonext();
	}
	
	if (x<=0.5)
	{
		Photoprev();
	}
}

function getpos(element)
{
        if ( arguments.length != 1 || element == null )
        {
               return null;
        }
        var elmt = element;
        var offsetTop = elmt.offsetTop;
        var offsetLeft = elmt.offsetLeft;
        var offsetWidth = elmt.offsetWidth;
        var offsetHeight = elmt.offsetHeight;
        while( elmt = elmt.offsetParent )
        {
                // add this judge
                if ( elmt.style.position == 'absolute'
//              || elmt.style.position == 'relative'
                || ( elmt.style.overflow != 'visible' && elmt.style.overflow != '' ) )
                {
                        break;
                }
                offsetTop += elmt.offsetTop;
                offsetLeft += elmt.offsetLeft;
        }
        return {top:offsetTop, left:offsetLeft, right:offsetWidth+offsetLeft, bottom:offsetHeight+offsetTop };
}


function imageonmousemove(evnt)
{
	//alert("1");
	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;
	}
	if (evnt)
	{
		nx = (parseInt(evnt.clientX)-photopos2.left) / $$("photo1").width;
		
		if(nx>0.5)
		{
			$$("photo1").style.cursor = "url("+"/UserManage/images/right.cur"+"),auto";
			$$("photo1").title = "点击跳到下一张>>";
		}
		
		if (nx<=0.5)
		{
			$$("photo1").style.cursor = "url("+"/UserManage/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);	
	resetmask();
}

function rotate(value)
{
	if(taging || !IsIE())
	{
		return;
	}
	rotatevalue = (value + rotatevalue) % 4;

	var obj = $$("photo1");
	if (rotatevalue)
	{
		obj.style.filter="progid:DXImageTransform.Microsoft.BasicImage(   Rotation="+rotatevalue+")";
	}
	else
	{
		obj.style.filter="";
	}

	rotateing = rotatevalue != 0;
	
	var v_tmp = 0;
	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";

	if($$('photo1').style.left != "")
	{	
		$$('photodiv2').style.left = parseInt($$('photo1').style.left) + "px";
	}
	if($$('photo1').style.top != "")
	{
		$$('photodiv2').style.top = parseInt($$('photo1').style.top) + "px";
	}
}

//图片两种显示模式
function zp_qp_set()
{
	if($$("zp_lb").style.display == "none")
	{
		partviewmode();
	}
	else
	{
		wholeviewmode();
	}
}

function resetmask()
{


	if (parseInt($$('photo1').width) < 200)
	{
		$$('photodiv2').style.width = "200px";
	}
	else
	{
		$$('photodiv2').style.width = parseInt($$('photo1').width) + "px";
	}

	if($$('photo1').style.left != "")
	{	
		$$('photodiv2').style.left = parseInt($$('photo1').style.left) + "px";
	}
	if($$('photo1').style.top != "")
	{
		$$('photodiv2').style.top = parseInt($$('photo1').style.top) + "px";
	}
	
	imageonmousemove();
}

function partviewmode()
{
	var v_maxsize = 780;
	while ($$("photo1").width > v_maxsize)
	{
		zoom(0.833, v_maxsize);
	}
	
	$$("zp_lb").style.display = "block";
	$$("zp_qp").src = "/UserManage/images/zp_qp1_on.gif";
	if($$("zp_m"))
	{
		$$("zp_m").className = "zp_m";
	}
	$$("photodiv1").className = "zp_zs2";
	$$('photodiv2').style.textAlign = "left";
	setCookie("viewmode", "part");
	initloadingdiv();
}

function wholeviewmode()
{
	if (zoomlevel)
	{
		zoomlevel = 1;
		zoom(0.833, 0);
	}

	$$("zp_lb").style.display = "none";
	$$("zp_qp").src = "/UserManage/images/zp_qp2_on.gif";
	if($$("zp_m"))
	{
		$$("zp_m").className = "zp_m2";
	}
	$$("photodiv1").className = "zp_zs3";
	$$('photodiv2').style.textAlign = "left";
	setCookie("viewmode", "whole");

	$$("loadingdiv").style.width = $$("realwidth").value;
	$$("loadingdiv").style.paddingLeft = "0px";
}

function photodivonmouseover()
{
	$$("zp_qp").style.visibility = "visible";
	var viewmode = getViewMode();
	if(viewmode == "part" && parseInt($$("realwidth").value)<791)
	{
		$$("zp_qp").style.visibility = "hidden";
	}
}

function photodivonmouseout()
{
	$$("zp_qp").style.visibility = "hidden";
	var viewmode = getViewMode();
	if(viewmode == "part" && parseInt($$("realwidth").value)>=791)
	{
		$$("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()
{
	window.location = "/UserManage/Home/Pic.aspx?id=" + photo_total;
}

function viewnext()
{
	//viewphoto((photo_pos + 1) % photo_total);
	window.location = "/UserManage/Home/Pic.aspx?id=" + photo_pos;
}


function Photoprev()
{
	window.location = "/PhotoAlbum/pic_" + photo_total + ".aspx";
}

function Photonext()
{
	window.location = "/PhotoAlbum/pic_" + photo_pos + ".aspx";
}