var imgMap = [];
var mediaMap = [];
var attachmentMap = [];

var exmayExtensionMap = [];

function exmayGetImgCacheMap(){
	return imgMap;
};

function exmayGetMediaCacheMap(){
	return mediaMap;
};

function exmayGetExtensionMap(){
	return exmayExtensionMap;
};

function exmayGetCacheMap(){
	return attachmentMap;
};

/*为引导图追加图片*/
function exmaySelectPicture(name,selectImg) {
	$("#exmay-picture-group-" + name).attr("src", selectImg).show();
	
};
	
/*删除编辑器图片*/
function exmayDeleteEditorImg(id) {
	var $html = $("#exmay-content").exmayEditor("get$Html");
	var $img = $html.find("img#" + id);
	var $src = $img.attr("src");
	if ($img && $src != undefined && $src != "undefined" && $src != "") {
		$img.remove();
		$("#exmay-content").exmayEditor("setHtml", $html.html());
	}
};

/*移除编辑器图片*/	
function exmayRemoveEditorImg(target) {
	var $target = $(target);
	$target.parents("li").remove();
	var $html = $("#exmay-content").exmayEditor("get$Html");
	var id = $target.attr("id");
	var $img = $html.find("img#" + id);
	var $src = $img.attr("src");
	if ($img && $src != undefined && $src != "undefined" && $src != "") {
		$img.remove();
		$("#exmay-content").exmayEditor("setHtml", $html.html());
	}
};

/*为引导图追加图片*/
function exmaySelectPicture(name,selectImg) {
	$("#exmay-picture-group-" + name).attr("src", selectImg).show();
	
};

// 设置缩略图
function exmaySetThumbnail(name,selectImg){
	$("#exmay-"+name).val(selectImg.replace(Exmay.path.upload,''));
}

/*编辑器插入数据*/
function exmayInsertImgToEditor(imgSrc,imgId){
	$("#exmay-content").exmayEditor("insertHtml", '<img id="' + imgId + '"  src="' + imgSrc + '" style="max-width:100%;"/>');
};

/*修改编辑器中的图片*/
function exmayUpdateEditorImg(target, exmayImg) {
	var $target = $(target);
	var imgId = $target.attr("id");
	var $html = $("#exmay-content").exmayEditor("get$Html");
	var $img = $html.find("img#" + imgId);
	var $src = $img.attr("src");
	if ($img && $src != undefined && $src != "undefined" && $src != "") {
		$img.attr("src", exmayImg);
		$img.attr("data-cke-saved-src", exmayImg);
		$("#exmay-content").exmayEditor("setHtml", $html.html());
	};
};

/*统计编辑器中的图片数量*/
function exmayPictureGroup() {
	CKEDITOR.tools.setTimeout(function() {
		var $html = $("#exmay-content").exmayEditor("get$Html");
		var $imgs = $html.find("img");
		
		$imgs.each(function(i) {
			if ($(this).attr("src").indexOf(Exmay.path.site+'/exmay/framework/editor/images') != -1 || $(this).hasClass("exmay-ui-attachment-img")) {
				$imgs.splice(i, 1);
				exmayImg($imgs);
			}
		});
		if($imgs.length > 0) {
			$("#exmay-picture-group-count").text($imgs.length);
			$("#exmay-picture-group").show().attr("src", $imgs.eq(0).attr("src"));
		} else {
			$("#exmay-picture-group-count").text(0);
			$("#exmay-picture-group").attr("src", "").hide();
		}
	}, 1000);
};	

/*统计编辑器中的媒体数量*/
function exmayMediaGroup() {
	CKEDITOR.tools.setTimeout(function() {
		var len = 0;
		var $html = $("#exmay-content").exmayEditor("get$Html");
		var $media = $html .find(".exmay-ui-midea");
		if($media.length > 0) {
			$("#exmay-media-group-count").text($media.length);
			$("#exmay-midea-group").show().attr("src", Exmay.path.img+"/file/ext/"+$media.eq(0).attr("type")+".png");
		} else {
			$("#exmay-midea-group-count").text(0);
			$("#exmay-midea-group").attr("src", "").hide();
		}
	}, 1000);
};		

/*统计编辑器中的附件数量*/
function exmayAttachmentGroup() {
	CKEDITOR.tools.setTimeout(function() {
		var len = 0;
		var $html = $("#exmay-content").exmayEditor("get$Html");
		var $attachment = $html .find(".exmay-ui-attachment");
		var $imgs = $attachment.find(".exmay-ui-attachment-img");
		if($attachment.length > 0) {
			$("#exmay-attachment-group-count").text($attachment.length);
			$("#exmay-attachment-group").show().attr("src", $imgs.eq(0).attr("src"));
		} else {
			$("#exmay-attachment-group-count").text(0);
			$("#exmay-attachment-group").attr("src", "").hide();
		}
	}, 1000);
};	

$(function(){
	
	/*为编辑框绑定onchange时间，事件的监听是：图片组*/
	$("#exmay-content").exmayEditor( {
		onChange : function() {
			exmayPictureGroup();
			var libary = $("#exmay-libary").val();
			if(libary != ""){
				if(libary =='attachment'){
					exmayAttachmentGroup()
				}else if(libary == 'media'){
					exmayMediaGroup();
				}
			}
		}
	});	
	
	function exmayEditorData(){
		return $("#exmay-content").exmayEditor("get$Html");
	};
	function exmayDialogTitle(type){
		switch(type){
			case "image" :return "图片库";break;
			case "media" :return "多媒体库";break;
			case "attachment" :return "附件库";break;
		}
	};
	
	function exmayOpenLibraryDialog(library) {
		$.exmayDialog( {
			title : exmayDialogTitle(library),
			url :Exmay.path.admin+"/file/attachment/upload/?library="+library,
			width : 960,
			height : 600,
			buttons : [ {
				text : "确定",
				icon : "icon-ok",
				submit : "true",
				handler : function() {
					getDialog(this).exmayDialog("destroy");
				}
			}, {
				text : "取消",
				icon : "icon-cancel",
				handler : function() {
					getDialog(this).exmayDialog("destroy");
				}
			} ]
		});			
	};
	
	function exmayParserImages() {
		var $htmlEditor = exmayEditorData();
		imgMap = new Array();
		var $img = $htmlEditor.find("img");
		var flag = false;
		$img.each(function(){
			var imgId  = $(this).attr("id");
			var imgClass = $(this).attr("class");
			var imgSrc = $(this).attr("src");
			if(imgClass != "exmay-ui-attachment-img"){
				if (imgId == undefined || imgId == 'undefined' || imgId == "") {
					imgId = "exmay-picture-"+exmayYMD();
					$(this).attr("id",imgId);
					flag = true;
				}
				
				if(imgSrc.indexOf(Exmay.path.site) == -1){
					imgMap.push("{url:'"+imgSrc+"',id:'"+imgId+"',status:'net'}");
				}else{
					if (imgSrc.indexOf(Exmay.path.site+'/exmay/framework/editor/images') == -1) {
						imgMap.push("{url:'"+imgSrc+"',id:'"+imgId+"',status:'server'}");
					}
				}
			}
		});
			
		if(flag){
			$("#exmay-content").exmayEditor("setHtml", $htmlEditor.html());
		}		
	};	
	
	function exmayPaserMedias() {
		
	};
	
	function exmayParserAttachments() {
		var $htmlEditor = exmayEditorData();
		var $attachment = $htmlEditor.find(".exmay-ui-attachment");
		$attachment.each(function(){
			var id = $(this).attr("id");
			var url = $(this).find(".exmay-ui-attachment-img").attr("src");
			var path = $(this).find(".exmay-ui-attachment-path").attr("href");
			var alias = $(this).find(".exmay-ui-attachment-alias").text();
			var size = $(this).find(".exmay-ui-attachment-size").text();
			
			var price = $(this).find(".exmay-ui-attachment-price").text();
			var description = $(this).find(".exmay-ui-attachment-description").text();
			var password = $(this).find(".exmay-ui-attachment-password").text();
			var credit = $(this).find(".exmay-ui-attachment-credit").text();
			var serverData = "";
			if(path.indexOf(Exmay.path.site) != -1){
				serverData = "{id:'"+id+"',description:'"+description+"',path:'"+path+"',url:'"+url+"',size:'"+size+"',alias:'"+alias+"',price:'"+price+"',password:'"+password+"',credit:'"+credit+"',status:'editor',type:'server'}";
			}else{
				serverData = "{id:'"+id+"',description:'"+description+"',path:'"+path+"',url:'"+url+"',size:'"+size+"',alias:'"+alias+"',price:'"+price+"',password:'"+password+"',credit:'"+credit+"',status:'editor',type:'net'}";
			}
			var data = eval("(" + serverData + ")");
			
			if(attachmentMap.length == 0){
				attachmentMap.push(data);
			}
		});
	};
	
	$(".exmay-ui-library").click(function(){
			var library = $(this).parents("li").attr("library");
			if(library == 'image'){
				exmayParserImages();
			}else if(library == 'media'){
				exmayPaserMedias();
			}else if (library == 'attachment'){
				exmayParserAttachments();
			}
			$("#exmay-library").val(library);
			exmayOpenLibraryDialog(library);
	});
	
	exmayPictureGroup();
});