var quanobj = null;
var lastOrderNum = null;
var photoLeft = YAHOO.util.Dom.getX('focusphoto'); 
var photodivLeft = YAHOO.util.Dom.getX('photodiv'); 
var photodivTop = YAHOO.util.Dom.getY('photodiv');


 	  //初始化
		function photoinit()
		{	
			quanclose();	
			//if (typeof (mark_list[photo_id + "_" + photo_uid]) == "undefined")
			if (mark_list[photo_id + "_" + photo_uid+"_"+mark_type]=="")
			{
				getMarkInfo(photo_id + "_" + photo_uid+"_"+mark_type);
				//tagtitles();
				//return;
			}
			//markdata = photo_id + "_" + photo_uid;
			tagtitles();
			//window.location.href = "#" + markdata;
		}
		
		function s(s){if (!$(s)) return; $(s).style.display = "block";}
		function h(s){if (!$(s)) return; $(s).style.display = "none";}
		
		function tagtitles()
		{
			$("tagtitles").innerHTML = "";
			var html;
			if (photo_vuid == photo_uid)
			{
				html = '<div style="z-index:_ZINDEX_;float:left;position:relative;margin-right:5px;" class="lh18" onmouseover="tagaddborder(_ORDERNUM_);s(\'tagdel_ORDERNUM_\');" onmouseout="tagdelborder(_ORDERNUM_);h(\'tagdel_ORDERNUM_\');">_TITLE_<div id="tagdel_ORDERNUM_" style="background:#EEF0F4 none repeat scroll 0%;border:1px solid#D8DFEA;display:none;left:-5px;position:absolute;top:-3px;"><table><tr><td nowrap="nowrap">_TITLE_[<a href="javascript:quandelsubmit(_MARKID_,\'_MARK_UID_\');" style="color:#D01E3B;font-weight:bold;text-decoration:none;">×</a>]</td></tr></table></div></div><div class="left lh18">_COMMA_</div>';
			}
			else
			{
				html = '<div style="z-index:_ZINDEX_;float:left;position:relative;margin-right:5px;" class="lh18" onmouseover="tagaddborder(_ORDERNUM_);s(\'tagdel_ORDERNUM_\');" onmouseout="tagdelborder(_ORDERNUM_);h(\'tagdel_ORDERNUM_\');">_TITLE_<div id="tagdel_ORDERNUM_" style="background:#EEF0F4 none repeat scroll 0%;border:1px solid#D8DFEA;display:none;left:-5px;position:absolute;top:-3px;"><table><tr><td nowrap="nowrap">_TITLE_</td></tr></table></div></div><div class="left lh18">_COMMA_</div>';
			}
			for(var i=0 ; i<mark_list[markdata].length; i++)
			{
				var html2 = html;
				if(i%10==0 && i!=0){
					html2+="<div class='clear'></div>";	
				}
				if (mark_list[markdata][i].mark_uid != "0" && mark_list[markdata][i].mark_uid != "")
				{
					if(mark_list[markdata][i].title_uid != "0"){
						html2 = html2.replace(/_TITLE_/g , "<a href=/home.php?uid=" + mark_list[markdata][i].title_uid + " class=sl>" + mark_list[markdata][i].title + "</a>");
					}else{
						html2 = html2.replace(/_TITLE_/g ,  mark_list[markdata][i].title);	
					}
				}
				else
				{
					html2 = html2.replace(/_TITLE_/g , mark_list[markdata][i].title);
				}
				html2 = html2.replace(/_ORDERNUM_/g , i);
				html2 = html2.replace(/_ZINDEX_/g , 10000-i);
				html2 = html2.replace(/_MARKID_/g , mark_list[markdata][i].mark_id);
				html2 = html2.replace(/_MARK_UID_/g , mark_list[markdata][i].mark_uid);
				html2 = html2.replace(/_COMMA_/g , i==mark_list[markdata].length-1?"":",");
				$("tagtitles").innerHTML += html2;
			}
			
			if (mark_list[markdata].length)
			{
				$("tagframe").style.display = "block";
			}
			else
			{
				$("tagframe").style.display = "none";
			}
		}
		
		function getMarkInfo(v_id)
		{
			var url = "/mark/mark_item.php";
			var pars = "id=" + v_id;
			pars += "&rid="+parseInt(Math.random()*999999);
			var myAjax = new Ajax.Request(url, {method: "post",asynchronous: false, parameters: pars, onComplete: function (req) { getMarkInfoajaxshow(req); } });
		}
		
		function getMarkInfoajaxshow(req)
		{
			var r = req.responseText;
			eval("r="+r);
			
			if (r["status"]=="false")
			{
				return;
			}
		
			if (r["data"])
			{
				mark_list[r["id"]] = r["data"];
			}
			else
			{
				return;
			}
		
		}
 
	  function quan(){
			if(quanobj!=null){
				quanobj.reset();
				fsobj.resetParam();
			}else{
				var html=$("quanhtml").innerHTML;
				$("quanhtml").innerHTML="";
				quanobj = new Cropper.Img(
					'focusphoto',
					{
						onEndCrop: onEndCrop,
						hasTip: true,
						tipCss: "width:200px;margin-top:4px;z-index:500;color:#333333;position:absolute;",
						tipHtml : html
					}
				);
				fsobj.resetParam();
			}
			$("quantip").style.display = "block"; 
			imageonmousemove();
		}
		
		function quanclose(){
			if(quanobj!=null){
				quanobj.remove();
			}
			$("quantip").style.display="none";
			imageonmousemove();
		}
		
		function onEndCrop( coords, dimensions ) {
			$( 'x1' ).value = coords.x1;
			$( 'y1' ).value = coords.y1;
			$( 'x2' ).value = coords.x2;
			$( 'y2' ).value = coords.y2;
		}
		
		function quanaddsubmit()
		{
			 if ($("tipinput").value.length == 0)
			 {
				 alert("请输入人物的姓名");
				$("tipinput").focus();
				 return;
			 }
			 var x1 = parseInt($("x1").value);
			 var x2 = parseInt($("x2").value);
			 var y1 = parseInt($("y1").value);
			 var y2 = parseInt($("y2").value);
			
			 var x = parseInt( (10000*(x2+x1)/2) / $("focusphoto").width);
			 var y = parseInt( (10000*(y2+y1)/2) / $("focusphoto").height);
			 var width = parseInt( 10000*(x2 - x1) / $("focusphoto").width);
			 var height = parseInt( 10000*(y2 - y1) / $("focusphoto").height);
			 var title = encodeURIComponent($("tipinput").value);
			 var title_uid = parseInt($("tipinput_uid").value);
			
			 var url ="/mark/add_mark.php";
			 var pars = "x="+x+"&y="+y+"&width="+width+"&height="+height+"&photo_uid="+photo_uid+"&photo_id="+photo_id+"&mark_uid="+photo_vuid+"&title="+title+"&title_uid="+title_uid+"&type="+mark_type;
			 pars += "&rid="+parseInt(Math.random()*999999);
			 var myAjax = new Ajax.Request(url, {method: "post",asynchronous: false, parameters: pars, onComplete: function (req) { quanaddajaxshow(req); } });
		}
		
		function quanaddajaxshow(req)
		{
			 var r = req.responseText;
			 
			 if(r == 0)
			 {
			 	return false;
			 }
			 eval("r="+r);
			 quanobj.reset();
		
			 if(r.mark_uid != 0){
				mark_list[markdata][mark_list[markdata].length] = r;
				lastOrderNum = mark_list[markdata].length-1;
				tagaddborder(lastOrderNum);
			 	tagtitles();
			}else{
				quanclose();
			}
			
			 
		}
		
		function quandelsubmit(markid , mark_uid)
		{
			var url = "/mark/del_mark.php";
			var pars = "mark_id="+markid+"&photo_id="+photo_id+"&mark_uid="+mark_uid;
			pars += "&rid="+parseInt(Math.random()*999999);
			var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { quandelajaxshow(req); } });
		}
		
		function quandelajaxshow(req)
		{
			var r = req.responseText;
			if(r == 0)
			{
				return false;
			}
			quanclose();
			
			var newmark = [];
			var j=0;
			var delnum;
			for(var i=0 ; i<mark_list[markdata].length ; i++)
			{
				if(mark_list[markdata][i].mark_id == r)
				{	
					delnum = i;
					continue;
				}
				newmark[j] =  mark_list[markdata][i];
				j++;
			}
			mark_list[markdata] = newmark;
			tagdelborder(delnum);
			
			tagtitles();
		}
		
		//圈中框
		function tagaddborder(ordernum)
		{
             if(lastOrderNum != null) {tagdelborder(lastOrderNum)}
			 var info = mark_list[markdata][ordernum];
			
			 if($('tagborder'+ordernum))
			 {
			 	return;
			 }
			
			imageonmousemove();
			
			 var tagborder = document.createElement('div');
			 tagborder.style.cssText = "position:absolute;";
			 tagborder.style.left = (photodivLeft +(photoLeft-photodivLeft)+ parseInt(((info.x-info.width/2)*$('focusphoto').width/10000))) + "px";
			 if($("quantip").style.display == "block"){
			 	tagborder.style.top = (photodivTop +50+ parseInt(((info.y-info.height/2)*$('focusphoto').height/10000))) + "px";
			 }else{
				tagborder.style.top = (photodivTop + parseInt(((info.y-info.height/2)*$('focusphoto').height/10000))) + "px";	 
			}
			 tagborder.style.height = parseInt(info.height*$('focusphoto').height/10000) + "px";
			 tagborder.style.width = parseInt(info.width*$('focusphoto').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 #3775b2 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:#3775b2;color:#ffffff;font: bold 14px;padding:2px;text-align:center;width:" + b_strlen(info.title) + "em;\"><b>"+info.title+"</b></div></center></div>";
			 $('photodiv').appendChild(tagborder);
		} 
		
		function tagdelborder(ordernum)
		{
			var tagborder = $("tagborder"+ordernum);
			if(tagborder)
			{
				$('photodiv').removeChild(tagborder);
			}
		}
		
		