/*
'=====================================================================
' Module Name:
' File name: photo.js
' Author: william.lu
' Project : my_bebo
' Copyright (c) all rights reserved by juuyou.com Co., LTD.
' Create on 2009/02/03
' Version: 1.0
'
'=====================================================================
'*********************************************************************
' Usage: Photo页JS函数集
'
'*********************************************************************
*/
strPhotoCategoryOption = "";

/**  定义juuyou.photo命名空间  **/
juuyou.photo = {};

/**  验证并提交  **/
juuyou.photo.checkCommentSubmit = function(){
	if($obj("txtAreaComment").value.length == 0){
		alert("不能提交空的信息");
		return false;
	}
	return juuyou.common.validWordLength('txtAreaComment', 'txt_word_cnt', 300);
}

/**  验证并提交  **/
juuyou.photo.checkCreateAlbumSubmit = function(){
	if(juuyou.common.trim($obj("txt_album_name").value) == ""){
		alert("请输入相册名字");
		return false;
	}
	return juuyou.common.validWordLength('txt_album_description', 'txt_word_cnt', 250);
}

/**  验证并提交  **/
juuyou.photo.checkPhotoEditSubmit = function(){
	if(juuyou.common.trim($obj("txt_photo_name").value) == ""){
		alert("请输入相片说明文字");
		return false;
	}
	return true;
}

//绑定专辑密码输入框显示
$(document).ready(function(){	
	$(".j_album_pwd_a").live("click", function(){
		$("#j_album_pwd_div").html($("#j_hid_album_pwd_div").html());
		$("#j_album_pwd_div #hid_album_id").val($(this).attr("aid"));
		$("#j_album_pwd_div").show();
		$("#j_drop_div").show();
	});	
});

$(document).ready(function(){	
	$(".j_album_pwd_close_a").live("click", function(){
		$("#j_album_pwd_div").hide();
		$("#j_drop_div").hide();
	});	
});

//添加新专辑层显示
juuyou.photo.iniAlbumAddDiv = function(){
	$("#j_photo_newalbum_div").html($("#j_hid_photo_newalbum_div").html());
	$("#j_photo_newalbum_div").show();
	$("#j_drop_div").show();
}
juuyou.photo.cancelAlbumAddDiv = function(){
	$("#j_photo_newalbum_div").hide();
	$("#j_drop_div").hide();
}

//绑定专辑添加层权限密码显示动作
$(document).ready(function(){	
	$(".j_album_access_type").live("click", function(){
		var strAccessType = juuyou.common.getRadioBtnValue("rd_access_type");
		if(strAccessType == "2"){
			$("#j_photo_newalbum_div .j_album_access_pwd").show();
		}else{
			$("#j_photo_newalbum_div #pwd_access_pwd").val("");
			$("#j_photo_newalbum_div .j_album_access_pwd").hide();
		}
	});	
});

//验证专辑密码
juuyou.photo.checkAlbumPwdDiv = function(){
	var strAlbumPwd = $("#j_album_pwd_div #pwd_album").val();
	var strAlbumId = $("#j_album_pwd_div #hid_album_id").val();
	var strMemberId = $("#j_album_pwd_div #hid_member_id").val();
	if($.trim(strAlbumPwd) == ""){
		$("#j_album_pwd_div #pwd_album").focus();
		return false;
	}

	$.ajax({
 		type:"POST",
		url:"../json/photo/checkAlbumPwd.php",
		async:true,
		data: {
			pwd_access_pwd: strAlbumPwd,
			hid_album_id: strAlbumId
		},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
				$("#j_album_pwd_div .j_album_pwd_wrong_p").html("<font color='red'>密码不正确！</font>");
				$("#j_album_pwd_div #pwd_album").focus();
			}else{
				location.href="http://saywo.com/photo/photoAlbumContact.php?memberId=" + strMemberId + "&albumId=" + strAlbumId;
			}
		}
	});
	
}

//增加新专辑检查提交
juuyou.photo.checkAlbumAddDiv = function(){
	var strAlbumName = $("#j_photo_newalbum_div #txt_album_name").val();
	var strAlbumDescription = $("#j_photo_newalbum_div #txt_album_description").val();
	var strAlbumTags = $("#j_photo_newalbum_div #txt_album_tags").val();
	var strAlbumCategory = $("#j_photo_newalbum_div #sel_album_category").val();
	var strAlbumAccessType = juuyou.common.getRadioBtnValue("rd_access_type");
	var strAlbumAccessPwd = $("#j_photo_newalbum_div #pwd_access_pwd").val();
	var strAlbumCopyright = $("#j_photo_newalbum_div #sel_album_copyright").val();
	
	if($.trim(strAlbumName) == ""){
		$("#j_photo_newalbum_div #txt_album_name").focus();
		return false;
	}
	if(strAlbumAccessType == "2" && strAlbumAccessPwd == ""){
		alert("请输入访问密码");
		return false;
	}
	
	$.ajax({
 		type:"POST",
		url:"../json/home/homeAlbumAdd.php",
		async:true,
		data: {
			txt_album_name: strAlbumName,
			txt_album_description: strAlbumDescription,
			txt_album_tags: strAlbumTags,
			sel_album_category: strAlbumCategory,
			rd_access_type: strAlbumAccessType,
			pwd_access_pwd: strAlbumAccessPwd,
			sel_album_copyright: strAlbumCopyright
		},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
			}else{
				juuyou.photo.cancelAlbumAddDiv();
				location.reload();
			}
		}
	});
}

//增加新专辑检查提交
juuyou.photo.checkAlbumAddDivSelect = function(){
	var strAlbumName = $("#j_photo_newalbum_div #txt_album_name").val();
	var strAlbumDescription = $("#j_photo_newalbum_div #txt_album_description").val();
	var strAlbumTags = $("#j_photo_newalbum_div #txt_album_tags").val();
	var strAlbumCategory = $("#j_photo_newalbum_div #sel_album_category").val();
	var strAlbumAccessType = juuyou.common.getRadioBtnValue("rd_access_type");
	var strAlbumAccessPwd = $("#j_photo_newalbum_div #pwd_access_pwd").val();
	var strAlbumCopyright = $("#j_photo_newalbum_div #sel_album_copyright").val();
	
	if($.trim(strAlbumName) == ""){
		$("#j_photo_newalbum_div #txt_album_name").focus();
		return false;
	}
	if(strAlbumAccessType == "2" && strAlbumAccessPwd == ""){
		alert("请输入访问密码");
		return false;
	}
	
	$.ajax({
 		type:"POST",
		url:"../json/home/homeAlbumAdd.php",
		async:true,
		data: {
			txt_album_name: strAlbumName,
			txt_album_description: strAlbumDescription,
			txt_album_tags: strAlbumTags,
			sel_album_category: strAlbumCategory,
			rd_access_type: strAlbumAccessType,
			pwd_access_pwd: strAlbumAccessPwd,
			sel_album_copyright: strAlbumCopyright
		},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
			}else{
				$("#sel_album_id").append(data);
				juuyou.photo.cancelAlbumAddDiv();
			}
		}
	});
}

//图片上传验证
juuyou.photo.checkPhotoUpload = function(){
	var strAlbumId = juuyou.common.getSelectVal("sel_album_id");
	$("#hid_album_id").val(strAlbumId);
	return true;
}

//批量修改图片名称
juuyou.photo.editAllPhotosName = function(){
	if(confirm("确定批量修改图片名称吗？")){
		var strAlbumName = $("#rd_edit_all_photos_name").val();
		for(var i=1; i<=intPhotosCnt; i++){
			$("#txt_photo_name_"+i).val(strAlbumName+"-"+i);
		}
	}else{
		$("#rd_edit_all_photos_name").attr("checked", "");
	}
}

//绑定图片删除动作
$(document).ready(function(){	
	$(".j_delete_image_a").live("click", function(){
		if(confirm("确定删除该图片吗？")){
			strDeletePhotoIds += $(this).parents(".j_photo_list_box").attr("pid") + ",";
			$(this).parents(".j_photo_list_box").hide();
		}
	});
});

//验证处理照片上传编辑
juuyou.photo.checkPhotoUploadEdit = function(){
	var strDeletePhotoIdsTemp = strDeletePhotoIds.substr(0, strDeletePhotoIds.length-1);
	var strAlbumCoverId = juuyou.common.getRadioBtnValue("rd_album_cover");
	var strFeaturedIds = "";

	var obj = document.getElementsByName("cbx_featured_album");
	for(var i=0; i<obj.length; i++){
		if(obj[i].checked){
			strFeaturedIds += (obj[i].value) + ",";
		}
	}
	if(strFeaturedIds.length > 0){
		strFeaturedIds = strFeaturedIds.substr(0, strFeaturedIds.length-1);
	}
	
	$("#hid_delete_ids").val(strDeletePhotoIdsTemp);
	$("#hid_album_cover_id").val(strAlbumCoverId);
	$("#hid_featured_ids").val(strFeaturedIds);

}

//验证处理照片上传编辑
juuyou.photo.checkAlbumEdit = function(){
	var strDeletePhotoIdsTemp = strDeletePhotoIds.substr(0, strDeletePhotoIds.length-1);
	var strAlbumCoverId = juuyou.common.getRadioBtnValue("rd_album_cover");
	var strFeaturedIds = "";

	var strAlbumName = $("#j_photo_newalbum_div #txt_album_name").val();
	var strAlbumAccessType = juuyou.common.getRadioBtnValue("rd_access_type");
	var strAlbumAccessPwd = $("#j_photo_newalbum_div #pwd_access_pwd").val();
	
	if($.trim(strAlbumName) == ""){
		$("#j_photo_newalbum_div #txt_album_name").focus();
		return false;
	}
	if(strAlbumAccessType == "2" && strAlbumAccessPwd == ""){
		alert("请输入访问密码");
		return false;
	}

	var obj = document.getElementsByName("cbx_featured_album");
	for(var i=0; i<obj.length; i++){
		if(obj[i].checked){
			strFeaturedIds += (obj[i].value) + ",";
		}
	}
	if(strFeaturedIds.length > 0){
		strFeaturedIds = strFeaturedIds.substr(0, strFeaturedIds.length-1);
	}
	
	$("#hid_delete_ids").val(strDeletePhotoIdsTemp);
	$("#hid_album_cover_id").val(strAlbumCoverId);
	$("#hid_featured_ids").val(strFeaturedIds);
		
}

//绑定图片目录选项
$(document).ready(function(){	
	if(strPhotoCategoryOption){
		$(".j_photo_category_sel").html(strPhotoCategoryOption);
		$(".j_photo_category_sel").attr("value",strSelectedAlbum);
	}
});

//显示图片名编辑层
juuyou.photo.showPhotoNameEditDiv = function(){
	$("#div_photo_name_show").hide();
	$("#div_photo_name_edit").html($("#div_photo_name_edit_hid").html());
	$("#div_photo_name_edit #txt_photo_name").val($("#div_photo_name_show a").attr("val"));
}
juuyou.photo.cancelPhotoNameEditDiv = function(){
	$("#div_photo_name_show").show();
	$("#div_photo_name_edit").html("");
}
juuyou.photo.submitPhotoNameEditDiv = function(){
	var strPhotoName = $.trim($("#div_photo_name_edit #txt_photo_name").val());
	if(strPhotoName == ""){
		$("#div_photo_name_edit #txt_photo_name").focus();
		return false;
	}

	$.ajax({
 		type:"POST",
		url:"../json/photo/photoNameEdit.php",
		async:true,
		data: {
			txt_photo_name: strPhotoName,
			hid_photo_id: strPhotoId
		},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
			}else{
				$("#div_photo_name_show").html(data);
				juuyou.photo.cancelPhotoNameEditDiv();
			}
		}
	});
}

//显示专辑说明编辑层
juuyou.photo.showAlbumDescriptionEditDiv = function(){
	$("#div_album_description_show").hide();
	$("#div_album_description_edit").html($("#div_album_description_edit_hid").html());
	$("#div_album_description_edit #txt_album_description").val($("#div_album_description_show a").attr("val"));
}
juuyou.photo.cancelAlbumDescriptionEditDiv = function(){
	$("#div_album_description_show").show();
	$("#div_album_description_edit").html("");
}
juuyou.photo.submitAlbumDescriptionEditDiv = function(){
	var strAlbumDescription = $.trim($("#div_album_description_edit #txt_album_description").val());

	if(strAlbumDescription.length > 300){
		alert("不能超过300字符");
		return false;
	}
	
	$.ajax({
 		type:"POST",
		url:"../json/photo/alubmDescriptionEdit.php",
		async:true,
		data: {
			txt_album_description: strAlbumDescription,
			hid_album_id: strAlbumId
		},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
			}else{
				$("#div_album_description_show").html(data);
				juuyou.photo.cancelAlbumDescriptionEditDiv();
			}
		}
	});
}

//显示专辑Tags编辑层
juuyou.photo.showAlbumTagsEditDiv = function(){
	$("#div_album_tags_show").hide();
	$("#div_album_tags_edit").html($("#div_album_tags_edit_hid").html());
	$("#div_album_tags_edit #txt_album_tags").val($("#div_album_tags_show a").attr("val"));
}
juuyou.photo.cancelAlbumTagsEditDiv = function(){
	$("#div_album_tags_show").show();
	$("#div_album_tags_edit").html("");
}
juuyou.photo.submitAlbumTagsEditDiv = function(){
	var strAlbumTags = $.trim($("#div_album_tags_edit #txt_album_tags").val());

	$.ajax({
 		type:"POST",
		url:"../json/photo/alubmTagsEdit.php",
		async:true,
		data: {
			txt_album_tags: strAlbumTags,
			hid_album_id: strAlbumId
		},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
			}else{
				$("#div_album_tags_show").html(data);
				juuyou.photo.cancelAlbumTagsEditDiv();
			}
		}
	});
}

//显示专辑目录编辑层
juuyou.photo.showAlbumCategoryEditDiv = function(){
	$("#div_album_category_show").hide();
	$("#div_album_category_edit").html($("#div_album_category_edit_hid").html());
	$("#sel_album_category").attr("value", $("#div_album_category_show a").attr("val"));
}
juuyou.photo.cancelAlbumCategoryEditDiv = function(){
	$("#div_album_category_show").show();
	$("#div_album_category_edit").html("");
}
juuyou.photo.submitAlbumCategoryEditDiv = function(){
	var strAlbumCategory = $.trim($("#div_album_category_edit #sel_album_category").val());

	$.ajax({
 		type:"POST",
		url:"../json/photo/alubmCategoryEdit.php",
		async:true,
		data: {
			txt_album_category: strAlbumCategory,
			hid_album_id: strAlbumId
		},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
			}else{
				$("#div_album_category_show").html(data);
				juuyou.photo.cancelAlbumCategoryEditDiv();
			}
		}
	});
}

//显示专辑访问权限编辑层
$(document).ready(function(){	
	$(".j_album_access_type_1").live("click", function(){
		var strAccessType = juuyou.common.getRadioBtnValue("rd_access_type");
		
		if(strAccessType == "2"){
			$("#div_album_access_type_edit .j_album_access_pwd").show();
		}else{
			$("#div_album_access_type_edit #pwd_access_pwd").val("");
			$("#div_album_access_type_edit .j_album_access_pwd").hide();
		}
	});	
});
juuyou.photo.showAlbumAccessTypeEditDiv = function(){
	$("#div_album_access_type_show").hide();
	$("#div_album_access_type_edit").html($("#div_album_access_type_edit_hid").html());
	juuyou.common.setRadioBtnValue("rd_access_type", $("#div_album_access_type_show a").attr("val"));
	// $("input[@type=radio][@checked]").val();    
}
juuyou.photo.cancelAlbumAccessTypeEditDiv = function(){
	$("#div_album_access_type_show").show();
	$("#div_album_access_type_edit").html("");
}
juuyou.photo.submitAlbumAccessTypeEditDiv = function(){
	var strAlbumAccessType = juuyou.common.getRadioBtnValue("rd_access_type");
	var strAlbumAccesPwd = $.trim($("#div_album_access_type_edit #pwd_access_pwd").val());
	
	if(strAlbumAccessType == "2" && strAlbumAccesPwd == ""){
		$("#div_album_access_type_edit #pwd_access_pwd").focus();
		return false;
	}

	$.ajax({
 		type:"POST",
		url:"../json/photo/alubmAccessTypeEdit.php",
		async:true,
		data: {
			txt_album_access_type: strAlbumAccessType,
			txt_album_access_pwd: strAlbumAccesPwd,
			hid_album_id: strAlbumId
		},
		success:function(data){
			if($.trim(data) == juuyou.common.json.failure){
			}else{
				$("#div_album_access_type_show").html(data);
				juuyou.photo.cancelAlbumAccessTypeEditDiv();
			}
		}
	});
}

//初始化喜欢动作
$(document).ready(function(){
	$(".l_like").live("click", function(){	
		$.ajax({
	 		type:"POST",
			url:"../json/photo/photoActiveLike.php",
			async:true,
			data: {
				aid: strActiveId,
				act: "like"
			},
			success:function(data){
				if($.trim(data) == juuyou.common.json.failure){
				}else{
					$(".j_like_span").hide();
					$(".j_likes").replaceWith(data);
				}
			}
		});
		return false;
	});
});

//初始化图片左右浏览箭头动作
$(document).ready(function(){
	$("#img_photo").live("mousemove", function(e){	
		var strPrevId = $(this).attr("prevId");
		var strNextId = $(this).attr("nextId");
		var objI=$("#j_photo_show").offset();
		var intMouseX = e.originalEvent.x;

		if(!intMouseX){
			intMouseX = e.clientX; 
		}
		//alert(objI.left + "  " + intMouseX);
		if(blIsIE){
			var intX = 300;
		}else{
			var intX = objI.left + 270;
		}
		if(intMouseX > intX){
			$(this).attr("title", "点击跳到下一张>>");
			$obj("img_photo").style.cursor = "url(http://img.saywo.com/images/edit/right.cur),auto";
			//$(this).attr("style", "cursor: url(http://img.saywo.com/images/edit/right.cur), auto;");
			strPNId = strNextId;
		}else{
			$(this).attr("title", "<<点击跳到上一张");
			$obj("img_photo").style.cursor = "url(http://img.saywo.com/images/edit/left.cur),auto";
			//$(this).attr("style", "cursor: url(http://img.saywo.com/images/edit/left.cur), auto;");
			strPNId = strPrevId;
		}
	});
	
	$("#img_photo").live("mouseover", function(e){	
		var strPrevId = $(this).attr("prevId");
		var strNextId = $(this).attr("nextId");
		var objI=$("#j_photo_show").offset();
		var intMouseX = e.originalEvent.x;

		if(!intMouseX){
			intMouseX = e.clientX; 
		}
		
		if(blIsIE){
			var intX = 300;
		}else{
			var intX = objI.left + 270;
		}
		if(intMouseX > intX){
			$(this).attr("title", "点击跳到下一张>>");
			$obj("img_photo").style.cursor = "url(http://img.saywo.com/images/edit/right.cur),auto";
			//$(this).attr("style", "cursor: url(http://img.saywo.com/images/edit/right.cur), auto;");
			strPNId = strNextId;
		}else{
			$(this).attr("title", "<<点击跳到上一张");
			$obj("img_photo").style.cursor = "url(http://img.saywo.com/images/edit/left.cur),auto";
			//$(this).attr("style", "cursor: url(http://img.saywo.com/images/edit/left.cur), auto;");
			strPNId = strPrevId;
		}
	});
	
	$("#img_photo").live("click", function(){
		if(strPNId != "" && strPhotoId != strPNId){
			window.location="http://saywo.com/photo/photoAlbumBig.php?memberId=" + strMemberId + "&albumId=" + strAlbumId + "&photoId=" + strPNId;
		}
	});
});




