
var _optionIco = null;
function deletePhoto() {
	if (window.confirm("\u60a8\u786e\u5b9a\u8981\u5220\u9664\u8fd9\u5f20\u7167\u7247\u5417\uff1f")) {
		YAHOO.util.Dom.get("delPhotoForm").submit();
	}
}
function addToAlbum(obj, photoId) {
	_optionIco = obj;
	var pos = YAHOO.util.Dom.getXY(obj);
	YAHOO.util.Dom.addClass(obj, "button_locked");
	obj.onclick = new Function("cancelToAlbum('" + photoId + "');");
	var h = parseFloat(YAHOO.util.Dom.getStyle(obj, "height"));
	if (document.all) {
		h = obj.clientHeight;
	}
	var dialog = YAHOO.util.Dom.getElementsByClassName("tool-btn-dialog", "DIV")[0];
	YAHOO.util.Dom.setStyle(dialog, "left", pos[0] + "px");
	YAHOO.util.Dom.setStyle(dialog, "top", (pos[1] + h) + "px");
	YAHOO.util.Dom.setStyle(dialog, "display", "block");
	var divs = dialog.getElementsByTagName("DIV");
	divs[0].innerHTML = "您的相册列表正在加载中...";
	YAHOO.util.Dom.setStyle(divs[1], "display", "none");
	YAHOO.util.Dom.setStyle(divs[2], "display", "none");
	var url = "doAlbum.do?method=getUserAlbumsWithRels";
	var data = "photoId=" + photoId;
	var opt = {success:function (o) {
		var albumList = Albums.formatXML(o.responseXML);
		if (albumList.length == 0) {
			alert("您还没有创建相册,请先去创建相册!");
			cancelToAlbum(photoId);
		} else {
			divs[0].innerHTML = "从下面列表中选择一个相册";
			YAHOO.util.Dom.setStyle(divs[1], "display", "block");
			YAHOO.util.Dom.setStyle(divs[2], "display", "block");
			createAlbumList(photoId, albumList, divs[1]);
		}
	}, failure:function (o) {
		alert("相册列表加载失败,请稍候重试!");
		cancelToAlbum(photoId);
	}};
	YAHOO.util.Connect.asyncRequest("POST", url, opt, data);
}
function cancelToAlbum(photoId) {
	_optionIco.onclick = new Function("addToAlbum(this, '" + photoId + "');");
	YAHOO.util.Dom.removeClass(_optionIco, "button_locked");
	var dialog = YAHOO.util.Dom.getElementsByClassName("tool-btn-dialog", "DIV")[0];
	YAHOO.util.Dom.setStyle(dialog, "display", "none");
	_optionIco = null;
}
function addFavorite(photoId) {
	var url = "doPhoto.do?method=addFavorite";
	var data = "photoId=" + photoId;
	var opt = {success:function (o) {
		var result = o.responseText;
		if (result == "ajax_failure") {
			alert("\u6536\u85cf\u5931\u8d25\uff01");
		} else {
			alert("\u7167\u7247\u5df2\u7ecf\u6210\u529f\u6536\u85cf\uff01");
			var link = YAHOO.util.Dom.getElementsByClassName("add-faves", "A")[0];
			link.className = "faves";
			link.title = "\u53d6\u6d88\u6536\u85cf";
			link.href = "javascript:removeFavorite('" + photoId + "')";
		}
	}, failure:function (o) {
		alert("\u7f51\u7edc\u5f02\u5e38\uff01");
	}};
	YAHOO.util.Connect.asyncRequest("POST", url, opt, data);
}
function removeFavorite(photoId) {
	var url = "doPhoto.do?method=removeFavorite";
	var data = "photoId=" + photoId;
	var opt = {success:function (o) {
		if (o.responseText == "ajax_success") {
			alert("\u53d6\u6d88\u6536\u85cf\u6210\u529f\uff01");
			var link = YAHOO.util.Dom.getElementsByClassName("faves", "A")[0];
			link.className = "add-faves";
			link.title = "\u6536\u85cf";
			link.href = "javascript:addFavorite('" + photoId + "')";
		} else {
			alert("\u53d6\u6d88\u6536\u85cf\u5931\u8d25\uff01");
		}
	}, failure:function (o) {
		alert("\u53d6\u6d88\u6536\u85cf\u5931\u8d25\uff01");
	}};
	YAHOO.util.Connect.asyncRequest("POST", url, opt, data);
}
function createAlbumList(photoId, albumList, panel) {
	var ul = panel.getElementsByTagName("UL")[0];
	ul.innerHTML = "";
	var parentDiv = ul.parentNode;
	if(albumList.length > 13){
		YAHOO.util.Dom.setStyle(parentDiv, "height", "360px");
		YAHOO.util.Dom.setStyle(parentDiv, "overflow", "auto");
		YAHOO.util.Dom.setStyle(parentDiv, "position", "relative");
	}else{
		YAHOO.util.Dom.setStyle(parentDiv, "height", "");
		YAHOO.util.Dom.setStyle(parentDiv, "overflow", "");
		YAHOO.util.Dom.setStyle(parentDiv, "position", "");
	}
	for (var i = 0; i < albumList.length; i++) {
		var album = albumList[i];
		var li = document.createElement("LI");
		var link = null;
		if (album.backupInfo && album.backupInfo == "photoIn") {
			link = document.createElement("SPAN");
			YAHOO.util.Dom.addClass(link, "in-album");
			var cover = document.createElement("IMG");
			YAHOO.util.Dom.setStyle(cover, "width", "25px");
			YAHOO.util.Dom.setStyle(cover, "height", "25px");
			YAHOO.util.Dom.setStyle(cover, "margin-right", "3px");
			cover.src = album.albumCover;
			link.appendChild(cover);
			link.appendChild(document.createTextNode(album.albumName));
			var del = document.createElement("A");
			del.href = "javascript:deletePhotoFormAlbum('" + photoId + "', '" + album.albumId + "', '" + album.albumName + "')";
			YAHOO.util.Dom.addClass(del, "delete-x floatleft");
			del.title = "从"+album.albumName+"相册中移除";
			del.innerHTML = "\u79fb\u9664";
			li.appendChild(link);
			li.appendChild(del);
		} else {
			link = document.createElement("A");
			link.href = "javascript:updateAlbumPhotoRels('" + photoId + "','" + album.albumId + "','" + album.albumName + "')";
			var cover = document.createElement("IMG");
			YAHOO.util.Dom.setStyle(cover, "width", "25px");
			YAHOO.util.Dom.setStyle(cover, "height", "25px");
			YAHOO.util.Dom.setStyle(cover, "margin-right", "3px");
			cover.src = album.albumCover;
			link.appendChild(cover);
			link.appendChild(document.createTextNode(album.albumName));
			li.appendChild(link);
		}
		ul.appendChild(li);
	}
}
function deletePhotoFormAlbum(photoId, albumId, albumNanme) {
	if (window.confirm("您确定要将这张照片从 " + albumNanme + " 中移除?")) {
		var url = "doPhoto.do?method=moveFormAlbum";
		var data = "photoId=" + photoId + "&albumId=" + albumId;
		var opt = {success:function (o) {
			var result = o.responseText;
			if (result == "ajax_failure") {
				alert("\u79fb\u9664\u5931\u8d25\uff01");
			} else {
				alert("照片已经被从 " + albumNanme + " 相册中移除");
				cancelToAlbum(photoId);
				WidgetPhotoViewer.removeWidget(albumId);
			}
		}, failure:function (o) {
			alert("\u7f51\u7edc\u5f02\u5e38\uff01");
		}};
		YAHOO.util.Connect.asyncRequest("POST", url, opt, data);
	}else{
		cancelToAlbum(photoId);
		return;
	}
}
function updateAlbumPhotoRels(photoId, albumId, albumNanme) {
	if(!window.confirm("您确定要将该照片添加到 " + albumNanme + " 相册？")){
		cancelToAlbum(photoId);
		return;
	}
	var url = "doPhoto.do?method=sendToAlbum";
	var data = "albumId=" + albumId + "&photoId=" + photoId;
	var opt = {success:function (o) {
		var result = o.responseText;
		if (result == "ajax_failure") {
			alert("\u7167\u7247\u8f6c\u79fb\u5931\u8d25\uff01");
		} else {
			alert("此照片已被成功添加到您指定的 "+albumNanme+" 相册！");
			cancelToAlbum(photoId);
			WidgetPhotoViewer.addWidget(albumId, albumNanme);
		}
	}, failure:function (o) {
		alert("\u7f51\u7edc\u5f02\u5e38\uff01");
	}};
	YAHOO.util.Connect.asyncRequest("POST", url, opt, data);
}

function setUserHeadImage(photoId) {
	if(!window.confirm("您确定要将该照片设置为您的头像吗？")){
		return;
	}
	var url = "doPhoto.do?method=setPhotoToHeadImage";
	var data = "&photoId=" + photoId;
	var opt = {success:function (o) {
		var result = o.responseText;
		if (result == "ajax_failure") {
			alert("头像设置失败，请稍后重试！");
		} else {
			alert("头像已设置成功！\n如果您还看不到效果，请重新登录一次即可！");
		}
	}, failure:function (o) {
		alert("\u7f51\u7edc\u5f02\u5e38\uff01");
	}};
	YAHOO.util.Connect.asyncRequest("POST", url, opt, data);
}

function showHotLink(obj){
	var target = YAHOO.util.Dom.get("hotLink");
	if(YAHOO.util.Dom.getStyle(target,"display")=="block"){
		YAHOO.util.Dom.removeClass(obj, "button_locked");
		YAHOO.util.Dom.setStyle(target,"display","none");
		return;
	}
	YAHOO.util.Dom.addClass(obj, "button_locked");
	var pos = YAHOO.util.Dom.getXY(obj);
	pos[1] = pos[1] + 20;
	YAHOO.util.Dom.setStyle(target, "left", pos[0] + "px");
	YAHOO.util.Dom.setStyle(target, "top", pos[1] + "px");
	YAHOO.util.Dom.setStyle(target,"display","block");
}
function copy(obj){
	obj.select();
	try{
		var copy = obj.value;
		if (window.clipboardData){
		window.clipboardData.setData("Text", copy);}
		else if (window.netscape){
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=copy;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);}
		window.alert("链接复制成功，您可以插入到其他网站，或通过QQ/MSN发给您的朋友！");
		return false;
		}catch(e){
		window.alert("复制失败！可能不支持该浏览器！\n建议您使用 Ctrl+C 手动复制链接!");
		return false;
	}
}

function copyLink(obj){
	try{
		var copy = obj.href;
		if (window.clipboardData){
		window.clipboardData.setData("Text", copy);}
		else if (window.netscape){
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=copy;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);}
		window.alert("分享链接复制成功，您可以插入到其他网站，或通过QQ/MSN发给您的朋友！");
		return false;
		}catch(e){
		window.alert("复制失败！可能不支持该浏览器！\n建议您使用 Ctrl+C 手动复制链接!");
		return false;
	}
}

function viewExif(){
	var target = YAHOO.util.Dom.get("photo_exif");
	if(undefined == target.clock){
		target.clock = null;
		YAHOO.util.Event.on(target, "mouseover", function(){
			YAHOO.util.Dom.setStyle(target, "display", "block");
			if(target.clock != null){
				window.clearTimeout(target.clock);
				target.clock = null;
			}
		});
		YAHOO.util.Event.on(target, "mouseout", function(){
			if(target.clock != null){
				window.clearTimeout(target.clock);
				target.clock = null;
			}
			target.clock = window.setTimeout(function(){
				YAHOO.util.Dom.setStyle(target, "display", "none");
			}, 500);
		});
	}
	YAHOO.util.Dom.setStyle(target, "display", "block");
	if(target.clock != null){
		window.clearTimeout(target.clock);
		target.clock = null;
	}
}
function hideExif(){
	var target = YAHOO.util.Dom.get("photo_exif");
	if(target.clock != null){
		window.clearTimeout(target.clock);
		target.clock = null;
	}
	target.clock = window.setTimeout(function(){
		YAHOO.util.Dom.setStyle(target, "display", "none");
	}, 500);
}
