
/**
 * 上传附件
 * @param {type} args配置参数{type:'image',field_name:'thumb',field_alt_name:'thumb_alt',field_id='thumb',cat_id:4,allow_ext:'jpg|png|gif',upload_num=1}
 * 参数说明
 */
function swfUpload(args) {
    var argsStr = (args.attachIndexUrl.indexOf('?') == -1) ? '?' : '&';
	if (args.type == undefined) {
		alert('上传类型参数缺失');
		return false;
	}
	else
		argsStr += 'type=' + args.type;
	if (args.allowExt != undefined)
		argsStr += '&allow_ext=' + args.allowExt;
	if (args.uploadNum != undefined)
		argsStr += '&upload_num=' + args.uploadNum;
	if (args.route != undefined)
		argsStr += '&route=' + args.route;

	art.dialog.open(args.attachIndexUrl + argsStr, {
        title: '上传' + ((args.title == undefined) ? '附件' : args.title) ,
		width: '650px',
		height: '420px',
		lock: true,
		fixed: true,
		ok: function() {
			var iframe = this.iframe.contentWindow;
			if (!iframe.document.body) {
				alert('请等待页面加载...');
				return false;
			}
			if (args.type == 'image')
				imageCallback.apply(this, [iframe, args]);
			else if (args.type == 'images')
				imagesCallback.apply(this, [iframe, args]);
			else if (args.type == 'files')
				filesCallback.apply(this, [iframe, args]);
		

		},
		cancel: true
	});

}

/**
 *单图片上传回调处理
 * @param {type} iframe
 * @param {type} args
 */
function imageCallback(iframe, args) {
	//取得选择的图片
	var in_content = iframe.$("#att-status").html().substring(1);
	var in_id = iframe.$("#att-id").html().substring(1);
	if (in_content == '')
		return false;

	if ($('#' + args.fieldId+ '_preview').attr('src')) {
		$('#' + args.fieldId+ '_preview').attr('src', in_content);
	}
	//返回id|路径
	$('#' + args.fieldId).val(in_content);
}

//多图上传，SWF回调函数
function imagesCallback(iframe, args) {
	var in_content = iframe.$("#att-status").html().substring(1);
	var in_filename = iframe.$("#att-name").html().substring(1);
	var in_ids = iframe.$("#att-id").html().substring(1);
	var str = $('#' + args.fieldId).html();
	var contents = in_content.split('|');
	var filenames = in_filename.split('|');
	var ids = in_ids.split('|');
	if (contents == '')
		return true;
	$.each(contents, function(i, n) {
		var randId = parseInt(Math.random() * 10000 + 10 * i);
		var filename = filenames[i];//.substr(0, filenames[i].indexOf('.'));
		str += "<li id='image" + randId + "'> <input type='text' name='" + args.name + "' value='" + filename + "' style='width:300px;' disabled='disabled' class='input' data-id='" + ids[i] + "' data-path='" + n + "' />&nbsp;&nbsp;<a href=\"javascript:imagePriview('"+ n +"')\" >预览</a>&nbsp;&nbsp;<a href=\"javascript:removeDiv('image" + randId + "','" + args.fieldId + "')\" >移除</a> </li>";
	});

	$('#' + args.fieldId +'_show').append(str);
	refreshValue(args.fieldId);
}

//多文件上传回调
function filesCallback(iframe, args) {
    var in_content = iframe.$("#att-status").html().substring(1);
	var in_filename = iframe.$("#att-name").html().substring(1);
	var in_ids = iframe.$("#att-id").html().substring(1);
	var str = $('#' + args.fieldId).html();
	var contents = in_content.split('|');
	var filenames = in_filename.split('|');
	var ids = in_ids.split('|');
	if (contents == '')
		return true;
	$.each(contents, function(i, n) {
        var ext = n.substr(n.lastIndexOf('.')+1);
		var randId = parseInt(Math.random() * 10000 + 10 * i);
		var filename = filenames[i];//.substr(0, filenames[i].indexOf('.'));
		str += "<li id='file" + randId + "'> " + ((-1 != $.inArray(ext, ['jpg', 'gif', 'png', 'bmp', 'jpeg'])) ? '<a href="javascript:imagePriview(\'' + n + '\')"><img src="' + n + '" width="40px" height="40px" /></a>' : '<img src="/static/images/ext/' + ext + '.png" width="40px" height="40px" />' ) + " <input type='text' name='" + args.name + "' value='" + filename + "' style='width:300px;' disabled='disabled' class='input' data-id='" + ids[i] + "' data-path='" + n + "' />&nbsp;&nbsp;<a href=\"" + args.downUrl + ((args.downUrl.indexOf('?') == -1) ? '?' : '&') + 'id=' + ids[i] + "\" >下载</a>&nbsp;&nbsp;<a href=\"javascript:removeDiv('file" + randId + "','" + args.fieldId + "')\" >移除</a> </li>";
	});

	$('#' + args.fieldId +'_show').append(str);
	refreshValue(args.fieldId);
}


//图片使用dialog查看
function imagePriview(img) {
	if (img == '') {
		return;
	}
	art.dialog({
		title: '图片查看',
		fixed: true,
		width: "600px",
		height: '420px',
		id: "image_priview",
		lock: true,
		background: "#CCCCCC",
		opacity: 0,
		content: '<img  src="' + img + '" />',
		time: 5
	});
}

//移除指定id内容
function removeDiv(id,fieldId) {
    $('#' + id).remove();
	refreshValue(fieldId);
}

//刷新隐藏表单的值
function refreshValue(id) {
	var value = [];
	$("#"+id+"_show").find('input').each(function(index){
		var id = $(this).data('id');
		if($.inArray(id,value) == -1)
			value.push(id);
	});

	$("#"+id).val(value.join(','));
}
