<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<form id="moduleTemplateForm" action="index.php?r=Article.edit_template" method="post">
	<input type="hidden" name="data[ChannelModuleTemplate][id]" value="<!--{$id}-->"/>
	<input type="hidden" id="module_type" value="<!--{$type}-->"/>
	<div class="ubbEditor" style="padding-top:3px; width:99%">
		<textarea id="content" name="data[ChannelModuleTemplate][<!--{$type}-->]"><!--{$data}--></textarea>
	</div>
	<div class="ubbAttr" style="padding:5px 0;">
		<button type="submit" class="butT">保存</button>
	</div>
</form>
<script>
$(document).ready(function(){
	$("#moduleTemplateForm").submit(function(){
		$(this).ajaxSubmit({
			success: function(str){
				var data = eval('(' + str + ')');
				if (data.status == 1) {
					$("#cpanelBox_"+$("#module_type").val()).html(data.content);
				} else if (data.status == 0) {
					alert('保存失败');
				}
			}
		});
		return false;
	});

	var $Editor = $("#content").addClass("cleditorTextarea").cleditor({
		width: "100%",
		height: 120,
		controls: "bold italic underline break | undo redo | image link file |",
		customBind: function(editor){
			$.ajax({
				url:"index.php?r=Layout.getUbbTemplate",
				type:"get",
				data:{template:'basic-function'},
				cache:false,
				success:function(html){
					$("#ubbArea").empty();
					$(html).appendTo("#ubbArea");
					$(".ubbcode").bind('click',function(){
						$.cleditor.cRange(editor);
						$.cleditor.exec(editor,$(this).text());
					});
				}
			});
			
			$(".ubbmenu").click(function(){
				var btn_name = $(this).text();
				if($(this).attr("value")!=''){
					$.ajax({
						url:"index.php?r=Layout.getUbbTemplate",
						type:"get",
						data:{template:$(this).attr("value")},
						cache:false,
						success:function(html){
							$(".ubbTool").slideUp(1000);
							$("#ubbArea").empty();
							$("#ubbmenu-status").html(btn_name);
							$(html).appendTo("#ubbArea");
							$(".ubbcode").bind('click',function(){
								$.cleditor.cRange(editor);
								$.cleditor.exec(editor,$(this).text());
							});
							
							//绑定资源调用向导
							$(".useubbwizard").bind('click',function(){
								var channel_id=0,channel_text=$(this).attr("value");
								if(channel_text == '' || channel_text== undefined) return false;
								
								var isAll = false;
								var channels = new Array();
								
								switch(channel_text){
									case "article": channel_id = 1;break;
									case "software": channel_id = 2;break;
									case "theme": channel_id = 8;break;
									case "game": channel_id = 3;break;
									case "picture": channel_id = 9;break;
									case "forum": channel_id = 5;break;
									case "link": channel_id = 7;break;
									case "music": channel_id = 10;break;
								}
								
								$.ajax({
									url:"index.php?r=Layout.getChannelByModule",
									type:"get",
									data:{channel_module_id:channel_id},
									cache:false,
									success:function(ChannelResponseText){
										var fc = $.fn.firesbox.start(ChannelResponseText, {hideOnContentClick: false});
										//绑定checkall事件
										fc.find("#checkall").unbind("click").bind("click", function(){
											if($(this).attr("checked")){
												$(".channelbox").each(function(){
													$(this).attr("checked",true);
												});
												isAll = true;
											}else{
												$(".channelbox").each(function(){
													$(this).attr("checked",false);
												});
												isAll = false;
											}
										});
										//绑定单个选框事件
										fc.find(".channelbox").unbind("click").bind("click", function(){
											var channel_id = $(this).val();
											if($(this).attr("checked")){
												if($.inArray(channel_id,channels) == -1){
													channels.push(channel_id);
												}
											}else{
												if($.inArray(channel_id,channels) != -1){
													channels = $.grep(channels,function(n,i){return n!=channel_id;});
												}
												
												if(isAll){
													isAll = false;
													$("#checkall").attr("checked",false);
													$(".channelbox").each(function(){
														if($(this).attr("checked")){
															if($.inArray($(this).val(),channels) == -1){
																channels.push($(this).val());
															}
														}
													});
												}
											}
										});
										
										fc.find(".nextBtn").unbind("click").bind("click", function(){
											if(!isAll && channels.length<1){
												alert("请选择频道！");
												return false;
											}
											var channel = "";
											if(isAll){
												channel = "0";
											}else{
												$(channels).each(function(k,v){
													if(channel == ""){
														channel = v;
													}else{
														channel+="|"+v;
													}
												});
											}
											switch(channel_text){
												case "article": bindArticle(editor,channel);break;
												case "software": bindSoftware(editor,channel);break;
												case "theme": bindTheme(editor,channel);break;
												case "game": bindGame(editor,channel);break;
												case "picture": bindPicture(editor,channel);break;
												case "forum": bindForum(editor,channel);break;
												case "link": bindLink(editor,channel);break;
												case "music": bindMusic(editor,channel);break;
											}
										});
									}
								});
							});
							
							//绑定资源新增向导
							$(".useubbwizard-new").bind('click',function(){
								var channel_id=0,channel_text=$(this).attr("value");
								if(channel_text == '' || channel_text== undefined) return false;
								
								var isAll = false;
								var channels = new Array();
								
								switch(channel_text){
									case "article": channel_id = 1;break;
									case "software": channel_id = 2;break;
									case "theme": channel_id = 8;break;
									case "game": channel_id = 3;break;
									case "picture": channel_id = 9;break;
									case "bbs": channel_id = 5;break;
									case "link": channel_id = 7;break;
								}
								
								$.ajax({
									url:"index.php?r=Layout.getChannelByModule",
									type:"get",
									data:{channel_module_id:channel_id},
									cache:false,
									success:function(ChannelResponseText){
										var fc = $.fn.firesbox.start(ChannelResponseText, {hideOnContentClick: false});
										//绑定checkall事件
										fc.find("#checkall").unbind("click").bind("click", function(){
											if($(this).attr("checked")){
												$(".channelbox").each(function(){
													$(this).attr("checked",true);
												});
												isAll = true;
											}else{
												$(".channelbox").each(function(){
													$(this).attr("checked",false);
												});
												isAll = false;
											}
										});
										//绑定单个选框事件
										fc.find(".channelbox").unbind("click").bind("click", function(){
											var channel_id = $(this).val();
											if($(this).attr("checked")){
												if($.inArray(channel_id,channels) == -1){
													channels.push(channel_id);
												}
											}else{
												if($.inArray(channel_id,channels) != -1){
													channels = $.grep(channels,function(n,i){return n!=channel_id;});
												}
												if(isAll){
													isAll = false;
													$("#checkall").attr("checked",false);
													$(".channelbox").each(function(){
														if($(this).attr("checked")){
															if($.inArray($(this).val(),channels) == -1){
																channels.push($(this).val());
															}
														}
													});
												}
											}
										});
										
										fc.find(".nextBtn").unbind("click").bind("click", function(){
											if(!isAll && channels.length<1){
												alert("请选择频道！");
												return false;
											}
											var channel = "";
											if(isAll){
												channel = "0";
											}else{
												$(channels).each(function(k,v){
													if(channel == ""){
														channel = v;
													}else{
														channel+="|"+v;
													}
												});
											}
											bindResourcesNew(editor,channel,channel_text);
										});
									}
								});
							});
							
							//绑定广告
							$(".useubbADwizard").bind('click',function(){
								bindADWizard(editor);
							});
							
							$(".useubbsearchwizard").bind('click',function(){
								$.ajax({
									url:"index.php?r=Layout.getSearchInputWizard",
									type:"get",
									data:{},
									cache:false,
									success:function(HtmlResponseText){
										var fc = $.fn.firesbox.start(HtmlResponseText, {hideOnContentClick: false});
										
										 fc.find(".cancelBtn").unbind("click").bind('click',function(){
											$.fn.firesbox.close();
										});
										fc.find(".insertBtn").unbind("click").bind('click',function(){
											var input_name = $("#firesbox_content").find("#input-name").val();
											var input_size = $("#firesbox_content").find("#input-size").val();
											var input_value = $("#firesbox_content").find("#input-value").val();
											if(input_name == ""){
												alert("搜索框名不能为空！");
												return false;
											}
											$.cleditor.cRange(editor);
											$.cleditor.exec(editor,"[input name="+input_name+" size="+input_size+" value="+input_value+"]");
											
											$.fn.firesbox.close();
										});
									}
								});
							});
							
							$(".useubbsearchbuttonwizard").bind('click',function(){
								var channel_id=0,channel_text=$(this).attr("value");
								if(channel_text == '' || channel_text== undefined) return false;
								var $channel_id = 0;
								switch(channel_text){
									case "article": channel_id = 1;break;
									case "software": channel_id = 2;break;
									case "theme": channel_id = 8;break;
									case "game": channel_id = 3;break;
									case "picture": channel_id = 9;break;
									case "forum": channel_id = 5;break;
								}
								$.ajax({
									url:"index.php?r=Layout.getSearchChannelByModule",
									type:"get",
									data:{channel_module_id:channel_id},
									cache:false,
									success:function(ChannelResponseText){
										var fc = $.fn.firesbox.start(ChannelResponseText, {hideOnContentClick: false});
										//绑定checkall事件
										fc.find(".channelbox").unbind("click").bind("click", function(){
											if ($(this).attr("checked")) {
												$channel_id = $(this).val();
											}
										});
										fc.find(".nextBtn").unbind("click").bind("click", function(){
											switch(channel_text){
												case "article": bindSearchArticle(editor,$channel_id);break;
												case "software": bindSearchSoftware(editor,$channel_id);break;
												case "theme": bindSearchTheme(editor,$channel_id);break;
												case "game": bindSearchGame(editor,$channel_id);break;
												case "picture": bindSearchPicture(editor,$channel_id);break;
												case "forum": bindSearchForum(editor,$channel_id);break;
											}
										});
									}
								});
							});
						}
					});
				}
			});
		  }
      });
});
</script>