//初始化选择文件框
function initFile(obj,pictureName){
	var name = "";
	if(pictureName == "" || pictureName == null || pictureName == "noFiles"){
		name = "No File ...";
	}else{
		name = pictureName.substring(pictureName.lastIndexOf("\\")+1,pictureName.length);
	}
	obj.ace_file_input({
		no_file:name,
		btn_choose:'选择',
		btn_change:'改变',
		droppable:false,
		onchange:null,
		thumbnail:false
	});
}

//文件上传ajax文件上传
function getUploadFile(obj,_elemId,_removeId,picPathId,smallpicPathId,imgSpanId,smallpicObj){
	var _href = obj.attr('href');
	$.ajaxFileUpload({
		url:_href, 
		secureuri:false,
		fileElementId:_elemId,
		dataType: 'json',
		success: function (result, status) {
			var path = result.data['url'];
			removeFile(_elemId,_removeId,picPathId,result);
			if (result.status == 'ok') {
				var orgImg = path.substring(0,path.indexOf(","));
				var smallImg = path.substring(path.indexOf(",")+1);
				$("#"+picPathId).val(orgImg);
				$("#"+smallpicPathId).val(smallImg);
				$("#"+smallpicObj).attr("src",smallImg);
				$("#"+smallpicObj).css("display","block");
				$("#"+imgSpanId).css("display","block");
				var result = 0;
				removeFile(_elemId,_removeId,picPathId,result);
				bootbox.alert("上传成功！");
				if(obj.hasClass("uploadOrView")){
					obj.html("预览");
				}
			}else if (result.status == 'error') {
				$("#"+picPathId).val("");
				$("#"+smallpicPathId).val("");
				$("#"+smallpicObj).css("display","none");
				$("#"+imgSpanId).css("display","none");
			    bootbox.alert(result.msg);
			    if(obj.hasClass("uploadOrView")){
					obj.html("上传");
				}
			}
		},
		error: function (data, status, e)
		{
			$("#"+picPathId).val("");
			$("#"+smallpicPathId).val("");
			$("#"+smallpicObj).css("display","none");
			$("#"+imgSpanId).css("display","none");
			removeFile(_elemId,_removeId,picPathId);
			console.log(e);
			if(obj.hasClass("uploadOrView")){
				obj.html("上传");
			}
		}
	});
	return false;
}


//上传文件时conle file 删除 file
function removeFile(_elemId,_removeId,picPathId,result){
	var filename = $('#'+_elemId).parent().find("span").attr("data-title");
	var isChrome = navigator.userAgent.toLowerCase().match(/chrome/) != null;//判断是否是谷歌浏览器
	if(filename==null || filename==''){
		filename = "No File ...";
	}
	var option = {}; 
	var defaultOptions = {
			no_file:'No File ...',
			btn_choose:'选择',
			btn_change:'改变',
			droppable:false,
			onchange:null,
			thumbnail:false
	};
	if(isChrome && result == 0){
		option = {no_file:filename };
	}else if(isChrome && result == 1){
		option = {no_file:'No File ...' };
	}else{
		option = {no_file:filename };
	}
	var opts = $.extend(defaultOptions,option);
	var newfile = $("#"+_elemId).clone();
	$("#"+_removeId).find(".ace-file-input").remove();
	$("#"+_removeId+" #"+picPathId).after(newfile);
	$("#"+_elemId).ace_file_input(opts);
}
