function checkImgType(fileName)
{	//验证图片文件后缀，如果还有其它格式，可以添加在right_type;
	var rightType = new Array(".gif", ".jpg", ".jpeg", ".png", ".bmp");
	var rightTypeLen = rightType.length;
	var imgName=fileName.toLowerCase();
	for (ii = 0; ii < rightType.length; ii++)
	{
    	if (imgName.endsWith(rightType[ii]))
    	{
    		return true;//允许的类型
    	}
 	}
 	// 未经过检验
 	return false;
}

// 检查数据是否合法并提交
function checkPhotoForm(){
	var form = $('photo-form');
	var fileName = form.photo.value;
	if (fileName.blank()) {
		alert('请选择要上传的图片');
		return false;
	}
	
	var title = form.title.value.strip();
	if(title.length>100){
		alert("图片描述过长");
		return false;
	}
	
	if(form.tag.length>100){
		alert("标签过长");
		return false;
	}
}

function showEditPhoto(photoId) {
	var photo = $('photo-' + photoId);
	var pos = Position.cumulativeOffset(photo);
	var editForm = $('edit-' + photoId);
	with (editForm.style) {
		width = '200px';
		height = '150px';
		textAlign = 'left';
		backgroundColor = '#FFFFFF';
		position = 'absolute';
		left = pos[0] + 700+'px';
		top = pos[1] + 65 + 'px';
		zIndex = '500';
		border = '4px outset #0066CC';
		padding = '2px';
	}
	// 初始化权限
	var priv = $('priv-value-' + photoId).value;
	var radios = $('edit-' + photoId).priv;
	for (var ii = 0; ii < radios.length; ii++) {
		if (radios[ii].value == priv) {
			radios[ii].checked = true;
			break;
		}
	}
	// 显示表单
	Effect.Appear(editForm);
}
function editPhoto(photoId){
	var editForm = $('edit-' + photoId);
	if (!editForm) {
		return false;
	}
	var name = editForm.name.value;
	var tag = editForm.tag.value;
	var priv = editForm.priv.value;

	if(name.strip().length < 1){
		alert("图片名不能为空");
		return false;
	}
	if(name.strip().length > 100){
		alert("图片名太长了，最多100字");
		return false;
	}
	if(tag.strip().length > 1){
		if(tag.strip().length > 100){
			alert("标签太长了，最多100字");
			return false;
		}
	}

	new Ajax.Request("/photo.do?method=doEditPhoto&id="+photoId, 
		{
			method:"post", 
			parameters:Form.serialize(editForm),
			onSuccess:ajaxOver
		}
	);
	return false;
}

function delPhoto(photoId, userName){
	var del = confirm("删除这张图片?");
	if (del == true) {
		new Ajax.Request("/photo.do?method=deletePhoto", 
			{
				method:"get", 
				parameters:"photoid=" + photoId ,
				onSuccess:function(transport) {
					var data = transport.responseText;
					if(data == "0"){
						new Effect.Highlight(
							'photo-' + photoId,
							{
								startcolor: '#0066CC',
								afterFinish: function() {
									Element.remove('photo-' + photoId);
									if (userName) {
										location.href = '/photo/' + userName;
									}
								}
							}
						);
					}else if(data == "1"){
						alert("未登录，请先登录");
						location.href = "/login.jsp";
					}else if(data == "5"){
						alert("改图片不存在");
						location.reload();
					}else if(data == "6"){
						alert("你没有权限");
						location.href = "/WEB-INF/pages/error.jsp";
					}
				}
			});	
	}
}

function ajaxOver(originalRequest){
	var data = originalRequest.responseText;
	if(data == "0"){
		location.reload();
	}else if(data == "1"){
		alert("未登录，请先登录");
		location.href = "/login.jsp"; 
	}else if(data == "2"){
		alert("图片名不能为空");
		location.reload();
	}else if(data == "3"){
		alert("图片名太长，最多100字");
		location.reload();
	}else if(data == "4"){
		alert("标签太长，最多100字");
		location.reload();
	}else if(data == "5"){
		alert("改图片不存在");
		location.reload();
	}else if(data == "6"){
		alert("你没有权限");
		location.href = "/WEB-INF/pages/error.jsp";
	}
}

function getPhotoComment(photoId) {
	var url = '/photo_comment.do?method=list';
	var comments = $('photo-comment-' + photoId);
	if(comments.style.display == "block") {
		comments.style.display = "none";
	} else {
		comments.style.display = "block";
	}
	if(comments.innerHTML.strip() == "") {
		
		Element.addClassName(comments, 'working-img');
	
		new Ajax.Request(
		url,
		{
			method: 'get',
			parameters: 'id=' + photoId + '&' + getDateString(),
			onSuccess: function(request) {
					Element.removeClassName(comments, 'working-img');
					comments.innerHTML = request.responseText;

			}
		}

		);
	}
}

function addPhotoComment(photoId) {
	var url = '/photo_comment.do?method=add';
	form = $('comment-form-' + photoId);
	if (form.content.value.strip() == '') {
		alert("请输入评论再按添加按钮");
		return;
	}
	
	Element.addClassName(form.content, 'working');
	new Ajax.Request(
		url,
		{
			method: 'post',
			parameters: Form.serialize(form),
			onSuccess: function(request) {
				if(request.responseText.substring(0,5)=="Error") {
					alert(request.responseText);
					
				} else {
					if($('comment-list-' + photoId).innerHTML.strip()=='还没有评论呢')
						$('comment-list-' + photoId).innerHTML = request.responseText;
					else
				    	$('comment-list-' + photoId).innerHTML += request.responseText;				
				}
				Element.removeClassName(form.content, 'working');
			}
		}
	);
	// 先将留言输入框恢复位置
	if (form.parentNode.id.indexOf('container') < 0)
		cancelPhotoReply(photoId);
	form.content.value = '';
}

function deletePhotoComment(commentId) {
	var url = '/photo_comment.do?method=del';
	var del = confirm("确实要删除这条评论吗？");
	if (del == true) {
		new Ajax.Request(
			url,
			{
				method: 'get',
				parameters: 'id=' + commentId + '&' + getDateString(),
				onSuccess: function(request) {
					if(request.responseText.substring(0,5)=="Error") {
						alert(request.responseText);
					} else {
						highlightDelete('comment-' + commentId);
					} 
				}
			}
		);
	}
}

function replyPhotoComment(photoId, commentId) {
	var form = $('comment-form-' + photoId);
	form.parentNode.removeChild(form);
	if (commentId != 0)
		$('comment-' + commentId).appendChild(form);
	else
		$('comment-' + photoId + '-' + commentId).appendChild(form);
	form.rid.value = commentId;
	$('cancel-' + photoId).style.display = "inline";
	form.content.focus();
}

function cancelPhotoReply(photoId) {
	var form = $('comment-form-' + photoId);
	form.parentNode.removeChild(form);
	$('cf-container-' + photoId).appendChild(form);
	form.rid.value = -1;
	$('cancel-' + photoId).style.display = "none";
	form.content.focus();
}

function ajaxPhoto(user){
	if(user == 0){
		user='';
	}
	new Ajax.Updater("photo-list", 
  	"/photo.do?method=ajaxLatestPhoto&user=" + user, 
  	{
  		method:"get"
  	}
  	);	
}

function ajaxPhotoComment(user){
	if(user == 0){
		user='';
	}
	new Ajax.Updater("commentList", 
  	"/photo_comment.do?method=ajaxLatestPhotoComment&user=" + user + "&" + getDateString(), 
  	{
  		method:"get"
  	}
  	);	
}

function popSpecialFriendListInScrip(event) {
		var element = Event.element(event);
		var pos = Position.cumulativeOffset(element); // pos: [offsetLeft, offsetTop]
		var dimms = Element.getDimensions(element);
		var accessDiv = $("friend-name-list");

	if(accessDiv.style.display == "none") {
		accessDiv.style.left = pos[0] + 'px';
		accessDiv.style.top = pos[1] + dimms.height + 'px';
		
		accessDiv.style.display = "block"
	} else {
		accessDiv.style.display = "none"
	}
	if(accessDiv.innerHTML.strip() == "")  
	    new Ajax.Updater(accessDiv, "/friend.do?", {method:"get", parameters:"method=chooseFriend&specialfriend=4"});	
}



function forwardPhoto(id) {
	var left = 310;
	var top = 450;
	var popup = $('forward-list');
	if (!popup) {
		popup = document.createElement('div');
		popup.id = 'forward-list';
		popup.style.border = '2px solid #cccccc';
		popup.style.position = 'absolute';
		popup.style.left = left + 'px';
		popup.style.top = top + 'px';
		popup.style.width = '250px';
		popup.style.zIndex = '500';
		popup.style.textAlign = 'left';
		popup.style.background = '#FFFFFF';
		
		var head = document.createElement('div');
		head.id = 'forward-list-head';
		head.style.height = '15px';
		head.style.padding = '5px';
		head.style.color = '#FFFFFF';
		head.style.backgroundColor = '#155FA9';
		head.innerHTML = '<a onclick="closeForwardList()" class="right hand"><img src="/images/icon_close_window.gif"　alt="关闭" title="关闭"／></a>选择推荐给哪些好友</div>';
		popup.appendChild(head);
		
		var body = document.createElement('div');
		body.id = 'forward-list-body';
		body.style.height = '400px';
		body.style.overflow = 'auto';
		body.style.padding = '10px';
		body.innerHTML = '<div class="working-img"></div>';
		popup.appendChild(body);
		
		var foot = document.createElement('div');
		foot.id = 'forward-list-foot';
		foot.style.height = '25px';
		foot.style.borderTop = '2px solid #CCCCCC';
		foot.style.padding = '5px';
		foot.innerHTML = '<a onclick="doForwardPhoto(' + id + ')" class="right hand"><img src="/images/button_send.gif" alt="OK" /></a>';
		popup.appendChild(foot);
		
		document.body.appendChild(popup);
		
		new Ajax.Request(
			'/friend.do?method=chooseFriend&' + getDateString(),
			{
				method: 'get',
				onSuccess: function(request) {
					$(body.id).innerHTML = request.responseText;
				}
			}
		);
	} else {
		popup.style.left = left + 'px';
		popup.style.top = top + 'px';
		$('forward-list-head').innerHTML = '<a onclick="closeForwardList()" class="right hand"><img src="/images/icon_close_window.gif"　alt="关闭" title="关闭"／></a>选择推荐给哪些好友</div>';
		$('forward-list-body').style.display = 'block';
		$('forward-list-foot').style.display = 'block';
		uncheckAllFriends();
		popup.style.display = 'block';
	}
}

function doForwardPhoto(id) {
	var friendNames = $('friend-list').innerHTML;
	var popup = $('forward-list');
	
	new Ajax.Request(
		'/photo.do?method=forward&id=' + id + '&fn=' + encodeURIComponent(friendNames),
		{
			method: 'get',
			onSuccess: function(request) {
				$('forward-list-foot').style.display = 'none';
				$('forward-list-body').style.display = 'none';
				$('forward-list-head').innerHTML = '<b>推荐成功</b>';
				setTimeout("closeForwardList()", 1000);
			}
		}
	);
}

function ajaxPhotoTags(userName){
	var url = '/photo.do?method='
	if(userName){
		url += 'ajaxUserTags&user=' + userName;
	} else {
		url += 'ajaxFriendsTags';
	}
	url += '&' + getDateString();
	new Ajax.Updater("photo-tags", url, {method:'get'});
}

function checkKeyword(){
	var keyword = document.getElementById("key").value;
	keyword = keyword.strip();
	if(keyword!=null && keyword.length!=0)
	{
		return true;
	}else
	{
		alert("你不说,我怎么知道你要找什么呢?");
		return false;
	}
}

function setPhotoUrl() {
	var urls = $('urls').value;
	if (urls.strip() != '') {
		urls = urls.split(',');
		
		if ($('check-thumb').checked) {
			window.opener.SetUrl(urls[0]);
		} else {
			window.opener.SetUrl(urls[1]);
		}
	}
	window.close();
}

function resizePhotos() {
	var photos = document.getElementsByClassName('album-photo');
	photos.each(
		function(photo) {
			Event.observe(photo, 'load', function(){resizePhoto(photo);});
		}
	);
}

function resizePhoto(photo) {
	if (photo.height > 150) {
		photo.width = 30000 / photo.height;
	} else {
		photo.style.marginTop = (150 - photo.height) / 2 + 'px';
	}
}

/* photo slider */
var previousId = -1;
var currentId = -1;
var nextId = -1;
var canPrevious = false;
var canNext = false;

function initSlider(userName, id) {
	currentId = id;
	new Ajax.Request(
		'/photo.do?method=previous',
		{
			method: 'get',
			parameters: 'user=' + userName + '&id=' + id + '&' + getDateString(),
			onSuccess: function(transport) {
				var response = transport.responseText;
				var colonIndex = response.indexOf(':');
				if (colonIndex > -1) {
					previousId = parseInt(response.substring(0, colonIndex));
					canPrevious = true;
					$('previous-photo').innerHTML = response.substring(colonIndex + 1);
					resizeSmallPhotos();
				}
			}
		}
	);
	
	new Ajax.Request(
		'/photo.do?method=next',
		{
			method: 'get',
			parameters: 'user=' + userName + '&id=' + id + '&' + getDateString(),
			onSuccess: function(transport) {
				var response = transport.responseText;
				var colonIndex = response.indexOf(':');
				if (colonIndex > -1) {
					nextId = parseInt(response.substring(0, colonIndex));
					canNext = true;
					$('next-photo').innerHTML = response.substring(colonIndex + 1);
					resizeSmallPhotos();
				}
			}
		}
	);
}

function previousPhoto(userName) {
	if (previousId != -1 && canPrevious) {
		canPrevious = false; // 先禁用
		$('next-photo').innerHTML = $('current-photo').innerHTML;
		$('current-photo').innerHTML = $('previous-photo').innerHTML;
		$('previous-photo').innerHTML = '<div class="working-img"></div>';
		new Ajax.Request(
			'/photo.do?method=previous',
			{
				method: 'get',
				parameters: 'user=' + userName + '&id=' + previousId + '&' + getDateString(),
				onSuccess: function(transport) {
					var response = transport.responseText;
					var colonIndex = response.indexOf(':');
					if (colonIndex > -1) {
						nextId = currentId;
						currentId = previousId;
						previousId = parseInt(response.substring(0, colonIndex));
						canPrevious = true;
						$('previous-photo').innerHTML = response.substring(colonIndex + 1);
						resizeSmallPhotos();
					}
				}
			}
		);
	}
}

function nextPhoto(userName) {
	if (nextId != -1 && canNext) {
		canNext = false; // 先禁用
		$('previous-photo').innerHTML = $('current-photo').innerHTML;
		$('current-photo').innerHTML = $('next-photo').innerHTML;
		$('next-photo').innerHTML = '<div class="working-img"></div>';
		new Ajax.Request(
			'/photo.do?method=next',
			{
				method: 'get',
				parameters: 'user=' + userName + '&id=' + nextId + '&' + getDateString(),
				onSuccess: function(transport) {
					var response = transport.responseText;
					var colonIndex = response.indexOf(':');
					if (colonIndex > -1) {
						previousId = currentId;
						currentId = nextId;
						nextId = parseInt(response.substring(0, colonIndex));
						canNext = true;
						$('next-photo').innerHTML = response.substring(colonIndex + 1);
						resizeSmallPhotos();
					}
				}
			}
		);
	}
}

function resizeSmallPhotos() {
	var photos = document.getElementsByClassName('small-photo');
	photos.each(
		function(photo) {
			Event.observe(photo, 'load', function(){resizeSmallPhoto(photo);});
		}
	);
}

function resizeSmallPhoto(photo) {
	if (photo.height > 75) {
		photo.width = 7500 / photo.height;
	} else {
		photo.style.marginTop = (75 - photo.height) / 2 + 'px';
	}
}