/*
	添加图片到图片组中.
	* path:/2011/07/31/
	* small:140x140_20110731102258241.jpg
	* src:20110731102258241.jpg
*/
function exmayPictureMap(attachmentId,path,small,src){
	exmayAddPictureData(exmayRandom(),attachmentId,path,small,src,Exmay.path.upload+path+src);
	$("#thunmimg").find("textarea:eq(0)").focus();
	exmaySetThumbNailImg();
};

function exmaySetThumbNail(id){
	var ckEQ = $("#exmay-tr-"+id).find("input[type='checkbox'][name='imgsrcs']");
	$("#exmay-thumbnail").val(ckEQ.attr("path")+ckEQ.attr("small"));
	$("#exmay-masterMap").val(ckEQ.attr("path")+ckEQ.val());
}

function exmaySetThumbNailImg(){
	var ckEQ = $("input[type='checkbox'][name='imgsrcs']:eq(0)");
	var smallPath = ckEQ.attr("path");
	var smallFile = ckEQ.attr("small");
	if(smallPath==undefined || smallFile==undefined || smallPath=="" || smallFile == ""){
		smallPath = "";
		smallFile = "";
	}
	$("#exmay-thumbnail").val(smallPath+smallFile);
	$("#exmay-masterMap").val(smallPath+ckEQ.val());
}

function exmayAddPictureGroupData(serverData){
	var opts = eval("("+serverData+")");
	if(exmayUpload.getStats().files_queued == 0){
		$("#exmay-picture-upload").slideUp('slow');
	}
	if(exmayCheckImg(exmayGetFileExt(opts.fileName))){
		exmayAddPictureData(exmayRandom(),opts.id,opts.filePath,opts.name,opts.fileName,opts.path);
	}else{
		exmayAddZipData(opts);
	}
}

function exmayAddZipData(opts){
	var id = exmayRandom();
	$("#zipFile").prepend("<div style='margin-top:5px;'id='exmay-tr-"+id+"'>&nbsp;&nbsp;<img width='32' height='32' src='${imgPath}/resource/rar_box.png'/>"+opts.fileName+"&nbsp;&nbsp;&nbsp;<a href='javascript:;' onclick=exmayUpzipPicture('"+opts.fileName+"','"+opts.filePath+"')>解压</a>&nbsp;&nbsp;<a href='javascript:;' onclick=exmayDeletePicture('"+id+"')>移除</a>&nbsp;&nbsp;<a href='javascript:;' onclick=exmayDeleteFile('"+id+"','"+opts.id+"','"+opts.fileName+"','"+opts.filePath+"')>删除</a></div>");
}

function exmayDeletePicture(id){
	$("#exmay-tr-"+id).remove();
};

function exmayDeleteFile(id,attachmentId,fileName,path){
	var base = Exmay.path.admin;
	if(base == undefined || Exmay.string.isEmpty(base)) {
		base = Exmay.path.site + "/exmay"
	}
	var timg = $("#exmay-thumbnail").val();
	$.exmayMessagebox.confirm("删除提示","确认删除该文件！(将从磁盘中和数据库中一并删除！)",function(ok){
		if(ok){
			$.ajax({
				type:"post",
				url: base +"/file/attachment/delete-file/?ajax=true",
				data:{"id":attachmentId,"name":fileName,"path":path},
				success:function(ok){
					exmayInitLoading("亿美提示:删除成功！");
					exmayDeletePicture(id);
					if(timg!="" && timg.indexOf(fileName)!=-1){
						exmaySetThumbNailImg();
					}
					$(".exmay-ui-picture-group").each(function(){
						var src = $(this).find("input[type='checkbox'][name='imgsrcs']").val();
						if(src==fileName || src.indexOf(fileName)!=-1){
							$(this).remove();
						}
					});
				}
			});
		}
	});
}


function exmayCheckPicture(){
	$("#thunmimg>div").find("div:eq(2)").click(function(){
		$(this).parent().css("border","1px solid #808000");
		$(this).parent().attr("title","exmay");
		$(this).siblings().find("textarea").attr("disabled","disabled");
		$(this).find("input[type='checkbox'][name='ids']").show();
		$(this).find("input[type='checkbox'][name='ids']").attr("checked","checked");
		$(this).find("input[type='checkbox'][name='imgsrcs']").removeAttr("checked");
		$(this).find("input[type='checkbox'][name='imgsrcs']").attr("checked",false);
		
	});

	$("#thunmimg>div").find("div:eq(0)").click(function(){
		$(this).parent().css("border","1px solid #808000");
		$(this).parent().attr("title","exmay");
		$(this).siblings().find("textarea").attr("disabled","disabled");
		$(this).siblings().find("input[type='checkbox'][name='ids']").show();
		$(this).siblings().find("input[type='checkbox'][name='ids']").attr("checked",true);
		$(this).siblings().find("input[type='checkbox'][name='imgsrcs']").removeAttr("checked");
		$(this).siblings().find("input[type='checkbox'][name='imgsrcs']").attr("checked","");
	});
};

function exmayCheckPictureDisabled(){
	$("#thunmimg>div").find("div:eq(2)").dblclick(function(){
		$(this).parent().css("border","1px solid #BED8FE");
		$(this).parent().attr("title","");
		$(this).siblings().find("textarea").attr("disabled","");
		$(this).find("input[type='checkbox'][name='ids']").hide();
		$(this).find("input[type='checkbox'][name='ids']").attr("checked","");
		$(this).find("input[type='checkbox'][name='imgsrcs']").removeAttr("checked");
		$(this).find("input[type='checkbox'][name='imgsrcs']").attr("checked",true);
	});

	$("#thunmimg>div").find("div:eq(0)").dblclick(function(){
		$(this).parent().css("border","1px solid #BED8FE");
		$(this).parent().attr("title","");
		$(this).siblings().find("textarea").attr("disabled","");
		$(this).siblings().find("input[type='checkbox'][name='ids']").hide();
		$(this).siblings().find("input[type='checkbox'][name='ids']").attr("checked","");
		$(this).siblings().find("input[type='checkbox'][name='imgsrcs']").removeAttr("checked");
		$(this).siblings().find("input[type='checkbox'][name='imgsrcs']").attr("checked",true);
	});
};