﻿

//操作对话框
function delphoto()
{
	openWindow('myalbum_del_dialog.php?pid='+photo_pid, 330, 280, '删除照片');
}

function delphoto_do(v_pid)
{
	document.delphotoform.pid.value = v_pid;
	document.delphotoform.from.value = $("returnlink").href;
	document.delphotoform.submit();
}

function editphoto()
{
	openWindow('photo_edit_dialog.php?pid='+photo_pid , 460, 350, '修改照片标题和属性');
}

function editphoto_do(v_pid , v_title , v_albumid)
{
	var str = window.location.href.split("#");
	document.editform.from.value = str[0] + "&pid=" + photo_pid + "&puid=" + photo_puid + "&gid=" + photo_gid;
	document.editform.pid.value = v_pid;
	document.editform.albumid.value = v_albumid;
	document.editform.title.value = v_title;
	document.editform.submit();
}

function sendtogrpalbum()
{
	openWindow('sendtogrpalbum_dialog.php?pid='+photo_pid, 330, 180, '复制到群相册');
}

function sharephoto(v_syscode)
{
	openWindow('sharephoto_dialog.php?type=im&uid=' + photo_puid + '&pid=' + photo_pid + '&syscode=' + v_syscode, 460, 450, '照片分享');
}

function tagaddsubmit()
{
	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) / $("photo1").width);
	var y = parseInt( (10000*(y2+y1)/2) / $("photo1").height);
	var width = parseInt( 10000*(x2 - x1) / $("photo1").width);
	var height = parseInt( 10000*(y2 - y1) / $("photo1").height);
	var mark_uid = f_seluid;
	var title = encodeURIComponent($("tipinput").value);

	var url = "/photo/markadd.php";
	var pars = "x="+x+"&y="+y+"&width="+width+"&height="+height+"&photouid="+photo_puid+"&pid="+photo_pid+"&mark_uid="+mark_uid+"&title="+title;
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { tagaddajaxshow(req); } });
}

function tagdelsubmit(markid , mark_uid)
{
	var url = "/photo/markdel.php";
	var pars = "markid="+markid+"&pid="+photo_pid+"&mark_uid="+mark_uid;
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { tagdelajaxshow(req); } });
}

function tagtitles()
{
	$("tagtitles").innerHTML = "";
	
	var html;
	if (photo_vuid == photo_puid)
	{
		html = '<div class="zp_bqr_w" style="z-index:_ZINDEX_;" onmouseover="tagaddborder(_ORDERNUM_);s(\'tagdel_ORDERNUM_\');" onmouseout="tagdelborder(_ORDERNUM_);h(\'tagdel_ORDERNUM_\');">_TITLE_<div id="tagdel_ORDERNUM_" class="zp_bqr_n"><table><tr><td nowrap="nowrap">_TITLE_[<a href="javascript:tagdelsubmit(_MARKID_,\'_MARK_UID_\');" class="gbh">×</a>]</td></tr></table></div></div><div class="l">_COMMA_</div>';
	}
	else
	{
		html = '<div class="zp_bqr_w" style="z-index:_ZINDEX_;" onmouseover="tagaddborder(_ORDERNUM_);s(\'tagdel_ORDERNUM_\');" onmouseout="tagdelborder(_ORDERNUM_);h(\'tagdel_ORDERNUM_\');">_TITLE_<div id="tagdel_ORDERNUM_" class="zp_bqr_n"><table><tr><td nowrap="nowrap">_TITLE_</td></tr></table></div></div><div class="l">_COMMA_</div>';
	}
	for(var i=0 ; i<mark_list[markdata].length ; i++)
	{
		var html2 = html;
		if (mark_list[markdata][i].mark_uid != "0" && mark_list[markdata][i].mark_uid != "")
		{
			html2 = html2.replace(/_TITLE_/g , "<a href=/home/?uid=" + mark_list[markdata][i].mark_uid + " class=sl>" + mark_list[markdata][i].title + "</a>");
		}
		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].markid);
		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 photoinit()
{
	if (!IsIE())
	{
		h("rotatediv");
	}

	if (photo_gid)
	{
		h("divmark");
	}
	else
	{
		s("divmark");
	}

	var obj = $("photo1");
	obj.width = $("realwidth").value;
	obj.height = $("realheight").value;
   
	var obj = $("photo2");
	obj.width = $("realwidth").value;
	obj.height = $("realheight").value;

	showPhotoMoreInfo();

	var viewmode = getViewMode();
	if(viewmode == "part")
	{
		partviewmode();
	}
	else
	{
		wholeviewmode();
	}
	resetmask();
	
	photodivonmouseout();

	showiconlist(photo_puid, photo_pid, photo_gid, album_id, photo_pos);
}

//相册,群,用户,圈人信息
function showPhotoMoreInfo()
{
	tagclose();
	tagtitles();

	$("albumtitlediv").innerHTML = '';
	$("usertagtextdiv").innerHTML = '';
//	$("comment_noright1").innerHTML = '';
//	$("comment_noright2").innerHTML = '';

	if (photo_gid)
	{
		if (typeof (group_list["_" + photo_gid]) == "undefined")
		{
			getGUAInfo("group", "_" + photo_gid);
			return;
		}
		if (typeof (album_list["g_" + photo_gid + "_" + album_id]) == "undefined")
		{
			getGUAInfo("album", "g_" + photo_gid + "_" + album_id);
			return;
		}
		$("albumtitlediv").innerHTML = '专辑：<a href="/group/group.php?gid=' + photo_gid + '" title="' + group_list["_" + photo_gid] + '" style="color:#333;">' + group_list["_" + photo_gid] + '</a>群的相册专辑 <a href="/group/album.php?gid=' + photo_gid + '&albumid=' + album_id + '" class="sl" title="' + album_list["g_" + photo_gid + "_" + album_id]["title"] + '">' + album_list["g_" + photo_gid + "_" + album_id]["title-short"] + '</a>(' + album_list["g_" + photo_gid + "_" + album_id]["picnum"] + ') ' + album_list["g_" + photo_gid + "_" + album_id]["privacyicon"];

		g_is_privacy_pwd = "0";
	}
	else if (photo_uid == photo_puid)
	{
		if (typeof (album_list["u_" + photo_puid + "_" + album_id]) == "undefined")
		{
			getGUAInfo("album", "u_" + photo_puid + "_" + album_id);
			return;
		}
		$("albumtitlediv").innerHTML = '专辑：<a href="showphoto.aspx?UID=' + photo_puid + '&DiskID=' + album_id + '" class="sl" title="' + album_list["u_" + photo_puid + "_" + album_id]["title"] + '">' + album_list["u_" + photo_puid + "_" + album_id]["title-short"] + '</a>'  + album_list["u_" + photo_puid + "_" + album_id]["privacyicon"];

		g_is_privacy_pwd = (parseInt(album_list["u_" + photo_puid + "_" + album_id]["privacy"]) == 2) ? "1" : "0";
	}
	else
	{
		if (typeof (user_list["_" + photo_puid]) == "undefined")
		{
			getGUAInfo("user", "_" + photo_puid);
			return;
		}
		if (typeof (album_list["u_" + photo_puid + "_" + album_id]) == "undefined")
		{
			getGUAInfo("album", "u_" + photo_puid + "_" + album_id);
			return;
		}
		$("albumtitlediv").innerHTML = '专辑：<a href="/home/?uid=' + photo_puid + '" title="' + user_list["_" + photo_puid]["real_name"] + '" style="color:#333;">' + user_list["_" + photo_puid]["real_name"] + '</a>的专辑<a href="album.php?uid=' + photo_puid + '&albumid=' + album_id + '" class="sl" title="' + album_list["u_" + photo_puid + "_" + album_id]["title"] + '">' + album_list["u_" + photo_puid + "_" + album_id]["title-short"] + '</a>(' + album_list["u_" + photo_puid + "_" + album_id]["picnum"] + ') ' + album_list["u_" + photo_puid + "_" + album_id]["privacyicon"];

		g_is_privacy_pwd = (parseInt(album_list["u_" + photo_puid + "_" + album_id]["privacy"]) == 2) ? "1" : "0";
	}

	if (photo_vuid == photo_puid)
	{
		$("usertagtextdiv").innerHTML = '请拖动鼠标圈出您的朋友，并输入他/她的姓名，这张照片将会自动出现在该好友的相册中。';
	}
	else
	{
		if (typeof (user_list["_" + photo_puid]) == "undefined")
		{
			getGUAInfo("user", "_" + photo_puid);
			return;
		}
		$("usertagtextdiv").innerHTML = '请拖动鼠标圈出您的朋友，并输入他/她的姓名。上传者同意后，这张照片将会自动出现在该好友的相册中。<br />系统会发消息给<a href="/home/?uid=' + photo_puid + '" class="sl">' + user_list["_" + photo_puid]["real_name"] + '</a>确认你的圈人请求。';
	}

	if (typeof (mark_list[photo_pid + "_" + photo_puid + "_" + photo_gid]) == "undefined")
	{
		getGUAInfo("mark", photo_pid + "_" + photo_puid + "_" + photo_gid);
		return;
	}
	markdata = photo_pid + "_" + photo_puid + "_" + photo_gid;
	window.location.href = "#" + markdata;
	tagtitles();

	if (typeof (user_list["_" + photo_puid]) == "undefined")
	{
		getGUAInfo("user", "_" + photo_puid);
		return;
	}

//	$("comment_noright1").innerHTML = '仅<a href="/home/?uid=' + photo_puid + '" class="sl">' + user_list["_" + photo_puid]["real_name"] + '</a>的好友可以对此发表评论';
//	$("comment_noright2").innerHTML = '<a href="/home/?uid=' + photo_puid + '" class="sl">' + user_list["_" + photo_puid]["real_name"] + '</a>关闭了评论功能';

//	$("comment").innerHTML = '<br /><br /><br /><br /><br /><br />';

//	h("comment_hasright");
//	
//	if (!photo_gid)
//	{
//		initComment(photo_vuid, photo_puid, "1", photo_pid);
//		getCommentList();
//		
//		if(parseInt(user_list["_" + photo_puid]["comment_right"]) == 1)
//		{
//			$("comment_hasright").style.display = "block";
//		}
//		else if(parseInt(user_list["_" + photo_puid]["comment_right"]) == -1)
//		{
//			$("comment_noright1").style.display = "block";
//		}
//		else
//		{
//			$("comment_noright2").style.display = "block";
//		}
//	}
}

function getGUAInfo(v_type, v_id)
{
	var url = "/photo/item.php";
	var pars = "type=" + v_type + "&id=" + v_id + "&rotate=" + photo_list[photo_pos]["rotate"];
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { getGUAInfoajaxshow(req); } });
}

function getGUAInfoajaxshow(req)
{
	var r = req.responseText;
	eval("r="+r);

	if (parseInt(r["ret"]))
	{
		return;
	}

	if (r["type"] == "group")
	{
		group_list[r["id"]] = r["data"][r["id"]];
	}
	else if (r["type"] == "user")
	{
		user_list[r["id"]] = r["data"][r["id"]];
	}
	else if (r["type"] == "album")
	{
		album_list[r["id"]] = r["data"][r["id"]];
	}
	else if (r["type"] == "mark")
	{
		mark_list[r["id"]] = r["data"][r["id"]];
	}
	else
	{
		return;
	}

	showPhotoMoreInfo();
}

//右侧图标
function findpid(v_puid, v_pid, v_gid)
{
	var i;
	var ret = -1;		//没有找到数据,但是photo_list为空
	if (photo_list.length)
	{
		ret = -2;		//没有找到数据,并且photo_list不为空,并且没有空数据
	}
	for (i=0; i<photo_list.length; i++)
	{
		if (photo_list[i] != null)
		{
			if (parseInt(photo_list[i]["pid"]) == parseInt(v_pid)
				&& parseInt(photo_list[i]["uid"]) == parseInt(v_puid)
				&& parseInt(photo_list[i]["gid"]) == parseInt(v_gid))
			{
				return i;
			}
		}
		else
		{
			ret = -3;	//没有找到数据,但是photo_list还有空数据
		}
	}
	return ret;
}

function showiconlist(v_puid, v_pid, v_gid, v_albumid, v_start)
{
 
	var pos = findpid(v_puid, v_pid, v_gid);
	
	if (pos < 0)
	{
		if (pos != -2)
		{
			iconlistget(v_albumid, v_start);
		}
		return;
	}

	photo_pos = pos;

	prev_pos2 = (pos + photo_total - 2) % photo_total;
	if (photo_list[prev_pos2] == null)
	{
		iconlistget(v_albumid, prev_pos2);
		return;
	}
	prev_pos = (pos + photo_total - 1) % photo_total;
	if (photo_list[prev_pos] == null)
	{
		iconlistget(v_albumid, prev_pos);
		return;
	}
	next_pos = (pos + 1) % photo_total;
	if (photo_list[next_pos] == null)
	{
		iconlistget(v_albumid, next_pos);
		return;
	}
	next_pos2 = (pos + 2) % photo_total;
	if (photo_list[next_pos2] == null)
	{
		iconlistget(v_albumid, next_pos2);
		return;
	}

	var a_wh;
	$("totaldiv").innerHTML = (photo_pos+1)+'/'+photo_total;
	
	if (prev_pos != photo_pos)
	{
		$("prevlinkdiv").innerHTML = '<a href="javascript:viewprev();" class="sl" title="上一张">上一张</a>';
		$("nextlinkdiv").innerHTML = '<a href="javascript:viewnext();" class="sl" title="下一张">下一张</a>';
	}
	else
	{
		$("prevlinkdiv").innerHTML = '';
		$("nextlinkdiv").innerHTML = '';
	}

	var v_html = '';
	if (prev_pos2 != photo_pos && prev_pos2 != next_pos && prev_pos2 != prev_pos && prev_pos2 != next_pos2)
	{
		a_wh = getPhotoWH(photo_list[prev_pos2]["width"], photo_list[prev_pos2]["height"]);
		v_html += '<div class="zp_r_on" onclick="javascript:viewphoto(' + prev_pos2 + ');" onmouseover="this.className=\'zp_r_on\';" onmouseout="this.className=\'zp_r_on\';" style="width:' + a_wh[0] + 'px; height:' + a_wh[1] + 'px;background: url(' + photo_list[prev_pos2]["small"] + ') center no-repeat;"></div>';
	}

	if (prev_pos != photo_pos && prev_pos != next_pos)
	{
		a_wh = getPhotoWH(photo_list[prev_pos]["width"], photo_list[prev_pos]["height"]);
		v_html += '<div class="zp_r_on" onclick="javascript:viewphoto(' + prev_pos + ');" onmouseover="this.className=\'zp_r_on\';" onmouseout="this.className=\'zp_r_on\';" style="width:' + a_wh[0] + 'px; height:' + a_wh[1] + 'px;background: url(' + photo_list[prev_pos]["small"] + ') center no-repeat;"></div>';
	}

	a_wh = getPhotoWH(photo_list[photo_pos]["width"], photo_list[photo_pos]["height"]);
	v_html += '<div style="border:1px solid #999999;margin:5px auto 5px auto; padding:1px;width:' + a_wh[0] + 'px; height:' + a_wh[1] + 'px;background: url(' + photo_list[photo_pos]["small"] + ') center no-repeat;"></div>';

	if (next_pos != photo_pos)
	{
		a_wh = getPhotoWH(photo_list[next_pos]["width"], photo_list[next_pos]["height"]);
		v_html += '<div class="zp_r_on" onclick="javascript:viewphoto(' + next_pos + ');" onmouseover="this.className=\'zp_r_on\';" onmouseout="this.className=\'zp_r_on\';" style="width:' + a_wh[0] + 'px; height:' + a_wh[1] + 'px;background: url(' + photo_list[next_pos]["small"] + ') center no-repeat;"></div>';
	}

	if (next_pos2 != photo_pos && next_pos2 != next_pos && next_pos2 != prev_pos)
	{
		a_wh = getPhotoWH(photo_list[next_pos2]["width"], photo_list[next_pos2]["height"]);
		v_html += '<div class="zp_r_on" onclick="javascript:viewphoto(' + next_pos2 + ');" onmouseover="this.className=\'zp_r_on\';" onmouseout="this.className=\'zp_r_on\';" style="width:' + a_wh[0] + 'px; height:' + a_wh[1] + 'px;background: url(' + photo_list[next_pos2]["small"] + ') center no-repeat;"></div>';
	}

	$("iconlist").innerHTML = v_html + '<div class="w80">&nbsp;</div>';

	if (photo_gid)
	{
		h("sharediv");
	}
	else
	{
		//$("sharelink").href = "javascript:sharephoto('" + photo_list[photo_pos]["syscode"] + "');";
		s("sharediv");
	}
}

function iconlistget(v_albumid, v_start)
{   
   
	var url = "../Album/json.aspx";
	var pars = "uid=" + photo_uid + "&type=" + photo_type + "&albumid=" + v_albumid + "&start=" + (Math.floor(v_start / 100) * 100);
	
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { iconlistajaxshow(req); } });
	
}

function iconlistajaxshow(req)
{  
  
	var r = req.responseText;
	
	eval("r="+r);

	if (parseInt(r["ret"]))
	{
		return;
	}

	photo_total = parseInt(r["total"]);
	if (!photo_total)
	{
		return;
	}

	var i;
	for (i=photo_list.length; i<photo_total; i++)
	{
		photo_list[i] = null;
	}

	var start = parseInt(r["start"]);
	for (i=0; i<r["list"].length; i++)
	{
		photo_list[i+start] = r["list"][i];
	}

	showiconlist(photo_puid, photo_pid, photo_gid, album_id, (start + 100) % photo_total);
}

function viewphoto(new_pos)
{
	if (new_pos >= photo_total)
	{
		return;
	}
	if (photo_list[new_pos] == null)
	{
		return;
	}

	photo_pid = parseInt(photo_list[new_pos]["pid"]);//图片id
	getPicComment(photo_pid);
	document.getElementById("photoID").value=photo_list[new_pos]["pid"]
	photo_puid = parseInt(photo_list[new_pos]["uid"]);//用户ID
	photo_gid = parseInt(photo_list[new_pos]["gid"]);
	photo_pos = parseInt(new_pos);
	album_id = parseInt(photo_list[new_pos]["albumid"]);//专辑ID
	markdata = "0_0_0";
	tagobj = null;
	taging = false;
	zoomlevel = 0;
	rotatevalue = 0;
	rotateing = false;

	rotate(0);

	var p_rotate = parseInt(photo_list[photo_pos]["rotate"]);
	if (p_rotate == 1 || p_rotate == 3)
	{
		$("realwidth").value = photo_list[photo_pos]["height"];
		$("realheight").value = photo_list[photo_pos]["width"];
	}
	else
	{
		$("realwidth").value = photo_list[photo_pos]["width"];
		$("realheight").value = photo_list[photo_pos]["height"];
	}

	initloadingdiv();
	s("loadingdiv");
	$("photo1").src = photo_list[photo_pos]["big"];
	$("photo2").src = photo_list[photo_pos]["big"];
   
	$("phototitlediv").innerHTML = '<strong class="f13" style="color:#333;">' + photo_list[photo_pos]["title"] + '</strong>';
	$("phototimediv").innerHTML = '时间：' + photo_list[photo_pos]["ctime"];

	if (photo_puid == photo_vuid && !photo_gid)
	{
		$("displaydiv1").style.display = "inline";
		$("rotatediv").style.display = "none";
		s("diveditphoto");
		s("sendtogrp");
		//$("rotatediv2").style.display = "inline";
		s("divdelphoto");
	}
	else
	{
		$("displaydiv1").style.display = "none";
		$("rotatediv").style.display = "inline";
		h("diveditphoto");
		h("sendtogrp");
		$("rotatediv2").style.display = "none";
		h("divdelphoto");
	}

//	$("newwindow").href = "showpic.php?url=" + encodeURIComponent(photo_list[photo_pos]["big"]) + "&title=" + encodeURIComponent(photo_list[photo_pos]["title-uri"]);
	
	photoinit();
}

function photo_setlogo()
{
	var str = window.location.href.split("#");
	window.location = "/set/logo.php?url=" + encodeURIComponent(photo_list[photo_pos]["big"]) + "&back=" + encodeURIComponent(str[0]);
}

function dbrotate(direction)
{
	if ($("realheight").value > 998)
	{
		bConf = confirm("该照片高度过大，旋转会对照片尺寸进行压缩。你确定要旋转图片吗？");
	}
	else
	{
		bConf = confirm("你确定要旋转图片吗？");
	}
	if (bConf)
	{
		var url = "/photo/rotate.php";
		var pars = "direction=" + direction + "&pid=" + photo_pid;
		var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { dbrotateajaxshow(req); } });
	}
}

function dbrotateajaxshow(req)
{
	var r = req.responseText;
	eval("r="+r);

	if (parseInt(r["ret"]))
	{
		return;
	}

	var pos = findpid(photo_vuid, parseInt(r["pid"]), 0);
	if (pos < 0)
	{
		return;
	}

	photo_list[pos]["rotate"] = parseInt(r["rotate"]);
	photo_list[pos]["big"] = r["big"];
	photo_list[pos]["small"] = r["small"];

	if (pos == photo_pos)
	{
		viewphoto(pos);
	}
}
function getPicComment(picID)
{
       var actv=new ActiveXObject("Microsoft.XMLHTTP");
        actv.open("post","../Album/getPicComent.aspx?picID="+picID,false);
        actv.send("");
        var b=actv.responseText;  
        document.getElementById("sppl").innerHTML=b;
        
}

