function load(title,url){
	var width  = arguments[2] ? arguments[2] : '350px';
	var height = arguments[3] ? arguments[3] : '350px';
	parent.parent.layer.open({
		type:2,
		title:title,
		area: [width, height],
		content: [url]
	});
}

function confirms(title,url,data){
	parent.parent.layer.confirm(title, {icon:3,shift:6}, function(index){
		$('#blank').attr('src',url);
		parent.parent.layer.close();
	});
}

function setcover(id,field){
	var img= $('#upload_multi_'+field+'_imageView img');
	$('#upload_multi_{$att["field"]}_cover').val($('#upload_multi_'+field+'_path_'+id).val());
	for(var i=0,l=img.length;i<l;i++){			
		if (img[i].id=='img_multi_'+field+id){
			$('#'+img[i].id).parent('div').css('border','2px solid #417EB7');	
		}else{
			$('#'+img[i].id).parent('div').css('border','2px solid #f1f1f1');	
		}
	}
}

//获取模板
function selecttpl(paint){
	var layerid = parent.parent.layer.open({
		type:2,
		title:'选择模板',
		area: ['450px', '400px'],
		content:'?r=gettpl&type='+paint,
	});
}

function submit(obj,param){
	var action = $('#'+obj).attr('action');
	$('#'+obj).attr('action',action+param);
	$('#'+obj).submit();
}


function checkall(obj, chk){
	if (chk == null){
		chk = 'checkbox';
	}
	elems = $(obj+':input');
	$(elems).each(function(){
		if (this.name == chk || this.name == chk + "[]"){
			$(this).attr('checked',obj.checked)
		}
	})
}

function makevideo(field){
   var endNum =  $("#multi_amount_"+field).val();
   var videolist  = $("#multi_videolist_"+field);
   var hlist = videolist.find("p").length;

   if(endNum>100) endNum = 100;
   if (endNum>hlist){
	   var  startNum = hlist+1;
	   for(startNum;startNum<=endNum;startNum++){
		  videolist.append( "<p id=\"vl"+startNum+"\"><input name=\""+field+"[list]["+startNum+"][name]\" type=\"text\" id=\""+field+"_name"+startNum+"\" value=\"第"+startNum+"集\" size=\"10\"  class=\"insty\"/>&nbsp;&nbsp;<input type=\"text\" id=\""+field+"_path"+startNum+"\" name=\""+field+"[list]["+startNum+"][path]\" value=\"http://\" class=\"insty\" size=\"30\"/>&nbsp;<input type=\"hidden\" id=\""+field+"_width"+startNum+"\" name=\""+field+"[list]["+startNum+"][width]\" value=\"500\"/><input type=\"hidden\" id=\""+field+"_height"+startNum+"\" name=\""+field+"[list]["+startNum+"][height]\" value=\"400\"/><input type=\"hidden\" id=\""+field+"_autostart"+startNum+"\" value=\"true\" name=\""+field+"[list]["+startNum+"][autostart]\" /><input type=\"hidden\" id=\""+field+"_note"+startNum+"\" name=\""+field+"[list]["+startNum+"][note]\" /><input  onclick=\"multimedia('"+field+"',"+startNum+")\" name=\"video\" type=\"button\" id=\""+startNum+"\" value=\"选取\"  class=\"scbtn7 insertfile\"/></p>");
		}
	}else{
		startNum = parseInt(endNum)+1
		for(startNum;startNum<=hlist;startNum++){
			videolist.find("#vl"+startNum).remove();
		}
	}
}


function multimedia(obj,sort){
	var editor = KindEditor.editor({
		uploadJson :'?r=files-upload',
		fileManagerJson :'?r=files-manager',
		allowFileManager : true
	});

	editor.loadPlugin('insertmedia', function() {
		editor.plugin.mediaDialog({
		  mediaUrl       : $('#'+obj+'_path'+sort).val(),
		  mediaWidth     : $('#'+obj+'_width'+sort).val(),
		  mediaHeight    : $('#'+obj+'_height'+sort).val(),
		  mediaAutostart : $('#'+obj+'_autostart'+sort).val(),
		  mediaNote      : $('#'+obj+'_note'+sort).val(),
		  clickFn : function(url,title,width,height,autostart,note) {
			  $('#'+obj+'_path'+sort).val(url);
			  $('#'+obj+'_width'+sort).val(width);
			  $('#'+obj+'_height'+sort).val(height);
			  $('#'+obj+'_autostart'+sort).val(autostart);
			  $('#'+obj+'_note'+sort).val(note);
			  editor.hideDialog();
			}
		});
	});
}




function topMenu(obj,nid) {
    $('.topmenu a').removeClass('active');
    $(obj).addClass('active');
    $(".sonpane").hide();
    $("#" + nid).show().find('a').eq(0).trigger("click");
}


function runAction(obj, url, nid) {
    $(".sonpane").find('a').removeClass('active');
    $(obj).addClass('active');
    $("#CenterFrame").attr('src', url);
    return true;
}

//ajax 轮询读取消息
function cometMsg(timer,is_init){
	
	if(is_init == true){
		readMsg();
		is_init = false;
		cometMsg(timer,is_init);
	}else{
		window.setInterval("readMsg()",timer);
	}
}

//ajax 读取消息
function readMsg(){
	if($(".user-box_cont").css('display')=='none'){
		$.get("?r=message", 
		{"timed": new Date().getTime()}, 
		function (json) {
			if (json.success==1){
				if (json.news==1){
					showmsg();
					window.setTimeout(hidemsg,5000);
				}
				$(".user-box b").text(json.count);
				$(".user-box_cont").html(json.content);
			}else{
				$(".user-box b").text(json.count);
				$(".user-box_cont").html('<div style="margin-top:35px;font-size:15px;">暂无消息</div>');					
			}
		},'json');
	}
}

//展开消息框
function showmsg(){
	$(".user-box_nor").fadeOut("slow");
	$(".user-box_show").fadeIn("slow");
	$(".user-box_cont").animate({height: 'show', opacity: 'show'}, "slow");
}

//隐藏消息框
function hidemsg(){
	$(".user-box_show").fadeOut("hide");
	$(".user-box_nor").fadeIn( "hide");
	$(".user-box_cont").animate({height: 'hide', opacity: 'hide'}, "hide");
};

$(function () {
	$('#user-box').hover(function(){
		readMsg();
		showmsg();
	}, function() {
		if(!$('.user-box_cont').is(":animated")){
			hidemsg();
		}
	});

	$('.itab ul li').click(function(){
		$(this).addClass('selected').siblings().removeClass('selected');
		$('.tabson:eq('+$(this).index()+')').show().siblings('.tabson').hide();	
	})
    $(".topmenu a:eq(0)").trigger('click');
    $(".sonpane:visible").find('a').eq(0).trigger('click');
})


KindEditor.ready(function(K) {
	var U = '?r=files-upload';
	var M = '?r=files-manager';
	//遍历编辑器
	var ea='';
	var e = K('textarea[plugin=editor]');	

	if(e){
		e.each(function(){
			//创建编辑器
			var ec = K.create('textarea[name='+this.name+']', {
				uploadJson:U,
				fileManagerJson : M,
				allowFileManager : true,
				filterMode : false,
				extraFileUploadParams : {
					multisid:$(this).attr('sid'),
					multiuid:$(this).attr('uid'),
					multisuper:$(this).attr('super'),
				},
				afterBlur:function(){ec.sync();},
			});
		});
		

	}

	var et = K.editor({
		uploadJson : U,
		fileManagerJson : M,
		allowFileManager : true,
	});

	//上传按钮
	var u = K('input[plugin=upload]');	
	if(u){
		u.each(function(){
			var filetyle  = this.filetyle == ''  ?  'imgFile' : this.filetyle;
			var upload = K.uploadbutton({
				button : this,
				fieldName : filetyle,
				url : U+'?dir=file',
			});
		})
	}
	
	
	//图片上传弹出层+本地+网络
	var u = K('input[plugin=image]');	
	K("input[plugin=image]").click(function(){
		var name = this.name;
		var view = this.view  == false ?  false : true;
		var showlocal  = this.showlocal  == false ?  false : true;
		var showremote = this.showremote == false ?  false : true;
		et.loadPlugin('image', function() {
			et.plugin.imageDialog({
				showLocal  : showlocal,
				showRemote : showremote,
				clickFn : function(url) {
					K('#'+name).val(url);
					if (view){K('#'+name+'_view').attr('src',url)}
					et.hideDialog();
				}
			});
		});
	});

	//批量上传弹出框
	K("input[plugin=multiimage]").click(function(){
		et.extraFileUploadParams = {
			multisid:K(this).attr('sid'),
			multiuid:K(this).attr('uid'),
			multisuper:K(this).attr('super'),
		}
		var name = this.name;
		var view = this.view  == false ?  false : true;
		et.loadPlugin('multiimage', function() {
			et.plugin.multiImageDialog({
				clickFn : function(list) {
					if (view){
						var imglist   = $('#'+name+'_view');
						var imglength = imglist.find('.imagebox').length;
						    imglength = imglength > 0 ? imglength : 0;
						K.each(list, function(i, data) {
							imglist.append('<div class="imagebox"  id="'+name+'_imgbox_'+(i+imglength)+'"><div style="width:160px;height:160px;margin-bottom:10px;border:2px solid #f1f1f1; display: table-cell; vertical-align:middle;"><img src="' + data.url + '" id="img_'+name+(i+imglength)+'" onclick="setcover('+(i+imglength)+')" style="max-height:155px;max-width:150px;_width:155px;_height:155px;"></div><input type="hidden"  id="'+name+'_path_'+(i+imglength)+'"  name="'+name+'['+(i+imglength)+'][path]" value="'+data.url+'"><div class="batact"><p>排序：<input type="text"  class="insty" style="height:24px" name="'+name+'['+(i+imglength)+'][sort]" size="10"><a href="javascript:0" onclick=$("#multimg_imgbox_'+i+'").remove() style="margin-left:10px">删除</a></p><p>描述：<input type="text" name="'+name+'['+(i+imglength)+'][note]" size="18" class="insty" style="height:24px"></p></div>');
						})
					}
					et.hideDialog();
				}
			});
		});
		
	});
	

	//上传文件弹出框
	K("input[plugin=file]").click(function(){
		et.loadPlugin('insertfile', function() {
			et.plugin.fileDialog({
				clickFn : function() {
					et.hideDialog();
				}
			});
		});
	});
	
	//上传视频弹出框
	K("input[plugin=media]").click(function(){
		et.loadPlugin('insertmedia', function() {
			et.plugin.mediaDialog({
				clickFn :function(url,title,width,height,autostart) {
					et.hideDialog();
				}
			});
		});
	});

	//上传浏览服务器
	K("input[plugin=filemanager]").click(function(){
		var node = this.name;
		var view = this.view  == false ?  false : true;
		et.loadPlugin('filemanager', function() {
			et.plugin.filemanagerDialog({
				viewType : 'VIEW',
				dirName : 'image',
				clickFn : function(url,title) {
					K('#'+node).val(url);
					if (view){K('#'+node+'_view').attr('src',url)}
					et.hideDialog();
				}}
			);
		});
	});
})