//页面初始化
$(document).ready(function(){
	
	//公告栏设置
	if($("#a_content").length !== 0){
		
		if(CheckCookieEnabled() == true){
			if(document.cookie.indexOf("ann") !== -1){
				var ann_state = getCookie("ann");
			}
		}
		else{
			var ann_state = 0;	
		}
		if(ann_state == 1){
			$("#a_content").attr("style","display:block");			
		}
		if(ann_state == 0){
			$("#a_content").attr("style","display:none");			
		}	
	
		if($("#a_content").is(":hidden")){
			$("#a_close").attr("title", "展开");
			var btnimgurl = $("#ann_btn").attr("src");
			btnimgurl = btnimgurl.replace(btnimgurl.split("/")[btnimgurl.split("/").length-1], "a_down_btn.png");
			$("#ann_btn").attr("src", btnimgurl);
			$("#ann_btn").attr("onclick", "ann_down()");
			
		}
		else{
			$("#a_close").attr("title", "收起");
			var btnimgurl = $("#ann_btn").attr("src");
			btnimgurl = btnimgurl.replace(btnimgurl.split("/")[btnimgurl.split("/").length-1], "a_up_btn.png");
			$("#ann_btn").attr("src", btnimgurl);
			$("#ann_btn").attr("onclick", "ann_up()");	
		}
	}
	
	//已选留言处理
	if($("#top_selectall").length !== 0 && $("#footer_selectall").length !== 0){
		if(CheckCookieEnabled() == true){
			if(document.cookie.indexOf("SelMsg") !== -1){
				var SelMsg = getCookie("SelMsg").split(",");
				var Cboxarr = new Array();
				var IsAllSel = true;
				var Cboxarr = document.getElementsByName('delete_message[]');
				for(var i=0; i<Cboxarr.length; i++){
					if(Cboxarr[i].value !== ''){
						if($.inArray(Cboxarr[i].value, SelMsg) >= 0){
							Cboxarr[i].checked = true;
						}
						else{
							IsAllSel = false;	
						}
					}
				}
				if((Cboxarr.length) == 2){
					IsAllSel = false;	
				}
				if(IsAllSel == true && $("#top_selectall").length !== 0 && $("#footer_selectall").length !== 0){
					document.getElementById('top_selectall').checked = true;
					document.getElementById('footer_selectall').checked = true;
				}
			}
		}
	}

	//显示已选留言数量	
	ShowSelMsgNum();
	
	//批量回复留言页面ID
	if($("#ReplyMsgId").length !== 0){
		if(CheckCookieEnabled() == true){
			if(document.cookie.indexOf("SelMsg") !== -1){
				var SelM = getCookie("SelMsg").split(",");
				if(SelM.length == 1 && SelM[0] == ''){
					$("#ReplyMsgId").attr("value", 'null');
				}
				else{
					$("#ReplyMsgId").attr("value", getCookie("SelMsg"));
				}				
			}
			else{
				$("#ReplyMsgId").attr("value", 'null');	
			}
		}
	}
	
});

//显示已选留言数量
function ShowSelMsgNum(){
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("SelMsg") !== -1){
			var SelM = getCookie("SelMsg").split(",");	
			if($(".selected_num").length !== 0){
				if(SelM.length == 1 && SelM[0] == ''){
					$(".selected_num").html(0);
				}
				else{
					$(".selected_num").html(SelM.length);	
				}
			}
		}
	}
}

//创建XMLHttpRequest对象
function createXMLhttp(){
	var xmlhttp;
	if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlhttp;
}

//设置置顶留言
function settop(mID, sURL){
	if(confirm("确认将此留言设置为置顶吗？") == true)	{
		window.location.href = sURL + '/admin/index.php?action=settop&id=' + mID;	
	}
	else{
		return false;	
	}
}

//取消置顶留言
function canceltop(mID, sURL){
	if(confirm("确认将此留言取消置顶吗？") == true)	{
		window.location.href = sURL + '/admin/index.php?action=canceltop&id=' + mID;	
	}
	else{
		return false;	
	}
}

//公告栏收起
function ann_up(){
	$("#a_content").slideUp();
	$("#a_close").attr("title", "展开");
	var btnimgurl = $("#ann_btn").attr("src");
	btnimgurl = btnimgurl.replace(btnimgurl.split("/")[btnimgurl.split("/").length-1], "a_down_btn.png");
	$("#ann_btn").attr("src", btnimgurl);
	$("#ann_btn").attr("onclick", "ann_down()");
	SetCookie("ann", 0, 90);	
}

//公告栏展开
function ann_down(){
	$("#a_content").slideDown();
	$("#a_close").attr("title", "收起");
	var btnimgurl = $("#ann_btn").attr("src");
	btnimgurl = btnimgurl.replace(btnimgurl.split("/")[btnimgurl.split("/").length-1], "a_up_btn.png");
	$("#ann_btn").attr("src", btnimgurl);
	$("#ann_btn").attr("onclick", "ann_up()");
	SetCookie("ann", 1, 90);
}

//获取QQ头像单选按钮及提示区域div
window.onload = function(){
	var RadioGroup1_6 = document.getElementById("RadioGroup1_6");
	var post_must_qq = document.getElementById("post_must_qq");	
}

//QQ头像已选中
function qqavatar(){
	post_must_qq.innerHTML = "*";	
}

//QQ头像未选中
function otheravatar(){
	post_must_qq.innerHTML = "";
	$("#QQ_InputShowMessage").text("");
	var value = document.getElementsById("QQinput").value;
	MessageInputCheck('QQnr', value);
}

//刷新验证码
function LoadCheckCode(){
	$("#CheckCodeImg").attr("src", "index.php?action=checkcode&skey=post&r" + Math.random()); 
}

//加入收藏
function AddFavorite(sURL, sTitle){
	try{
		window.external.addFavorite(sURL, sTitle);
	}catch (e){
		try{
			window.sidebar.addPanel(sTitle, sURL, "");
		}catch (e){
			alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

//判断客户端是否支持Cookie
function CheckCookieEnabled(){
	if(window.navigator.cookieEnabled){
	   return true;
	}
	else{
	   return false;
	}
}

//创建Cookie
function SetCookie(name, value, expiredays){
	var oDate = new Date();
	oDate.setDate(oDate.getDate()+expiredays);
	document.cookie = name + " = " + value + ";expires = " + oDate;
}

//获取cookie
function getCookie(c_name){
	if(document.cookie.length > 0){
		c_start = document.cookie.indexOf(c_name + "=")
		if(c_start!=-1){ 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if(c_end==-1){
				c_end=document.cookie.length
			}
			return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return ""
}

//删除Cookie
function DelCookie(Cname){
	SetCookie(Cname, "none", -1);
}

//弹窗代码
;(function($,window,document,undefined){
		   //默认参数
		    var PARAMS;
		    var DEFAULTPARAMS = { Title: "标题", Content: "",BtnL:"确定",BtnR:"取消" , FunL: new Object, FunR: new Object };
		    $.DialogByZ = {
		        //弹出提示框
		        Alert: function (params) {
		            Show(params,"Alert");
		        },
		        //弹出确认框
		        Confirm: function (params) { Show(params,"Confirm"); },
		        //自动显示弹框
		        Autofade: function (params) { Show(params,"Autofade") },
		        //关闭弹出框
		        Close: function () {
		            $(".zbox-popup,.zbox-popup-backdrop").remove();
		        },
		        //加载图形
		        Loading:function(Url, text){
		        	loadBox(Url, text)
		        }
		
		    };
		        //初始化参数
		    function Init(params) {
		        if (params != undefined && params != null) {
		            PARAMS = $.extend({},DEFAULTPARAMS, params);
		        }
		    };
		    function loadBox(Url, text){
		    	var url=Url;
		    	var dislogContainer=$('<div class="zbox-popup" style="display: block;"><div style="color:#FFF; font-weight:bold; height:50px; line-height:50px; font-size:16px; text-align:center; margin-bottom:10px; background-color:#999;">'+text+'</div><img  id="zchange" src="'+url+'"></div>');
		    	var blackFilter=$('<div class="zbox-popup-backdrop" style="display: block;"></div>');
		    	setTimeout(function(){
		    	  		 $(".zbox-popup").addClass('zbox-popup-in');
		    	  		 $(".zbox-popup-backdrop").addClass('zbox-active');
		    	  	},30)
		    	$("body").append(dislogContainer);
		    }
		    function Show(params, caller){
		    	  Init(params);
		    	  var dislogContainer;
		    	  var dialogInner;
		    	  var dialogBtn;
		    	  var blackFilter=$('<div class="zbox-popup-backdrop" style="display: block;"></div>');
		    	  if(caller=='Autofade'){
		    	  	 dislogContainer=$('<div class="zbox-toast-container"><div class="zbox-toast-message">'
		    	  	 +PARAMS.Content+'</div></div>');
		    	  	 $("body").append(dislogContainer);
		    	  	 setTimeout(function(){
		    	  		 $(".zbox-toast-container").addClass('zbox-active');
		    	  	},30)
		    	  	 setTimeout(function(){
		    	  	 	  $(".zbox-toast-container").remove();
		    	  	 },3000)
		    	  }else{
		    	  	dislogContainer=$('<div class="zbox-popup" style="display: block;"></div>');
		    	  	dialogInner=$('<div class="zbox-popup-inner"><div class="zbox-popup-title">'+PARAMS.Title+'</div><div class="zbox-popup-text">'+PARAMS.Content+'</div></div>');
		    	  	dialogBtn=$('<div class="zbox-popup-buttons"><span class="zbox-popup-button" index="0">'+PARAMS.BtnL+'</span></div>');
		    	  	if(caller=='Confirm'){
		    	  		dialogBtn.append($('<span class="zbox-popup-button R" index="1">'+PARAMS.BtnR+'</span>')); 
		    	  	}
		    	  	dislogContainer.append(dialogInner);
		    	  	dislogContainer.append(dialogBtn);
		    	  	setTimeout(function(){
		    	  		 $(".zbox-popup").addClass('zbox-popup-in');
		    	  		 $(".zbox-popup-backdrop").addClass('zbox-active');
		    	  	},10)
		    	  	$("body").append(blackFilter);
		    	    $("body").append(dislogContainer);
		    	    
		    	  	$(".zbox-popup-button").click(function(){
		    	  		 var indexs=$(this).attr('index');
		    	  		 if(indexs==0){
		    	  		 	//左侧按钮
		    	  		 	if($.isFunction(PARAMS.FunL)){
		    	  		 		    PARAMS.FunL();
					    	}else{
					    			$.DialogByZ.Close(); 
					    	}
		    	  		 }else{
		    	  		 	//右侧按钮 
		    	  		 	if($.isFunction(PARAMS.FunR)){
		    	  		 		    PARAMS.FunR();
					    	}else{
					    			$.DialogByZ.Close(); 
					    	}
		    	  		 }
		    	  		 return false;
		    	  	})
		    	  	 
		    	  }
		    	  //	  
		    }
		})(jQuery,window,document);	
	
//删除留言提示{Mid:Mid, Durl:Durl}
function DelMsg(MessageID, Mid, Surl){
	var Durl = Surl + "/admin/index.php?action=delmsg";
	$.DialogByZ.Confirm({Title: "", Content: "确认删除此留言吗？", FunL:function(){DelMsgCheck(MessageID, Mid, Durl);}, FunR:null});	
}

//执行删除留言操作并提示成功或失败
function DelMsgCheck(MessageID, Mid, Durl){
	$.get(Durl, {id:Mid}, function(data){
		if(data == 1){
			$("#Message_" + MessageID).slideUp();
			$.DialogByZ.Close();
			$.DialogByZ.Alert({Title: "提示", Content: "指定的留言删除成功！", BtnL:"确定"});
		}
		else{
			$.DialogByZ.Close();
			$.DialogByZ.Alert({Title: "提示", Content: "删除失败！", BtnL:"确定"});		
		}
	});
}

//记录勾选的留言
function AddSelMsg(SelMsgArray){
	var SelMsg = getCookie("SelMsg").split(",");
	if(CheckCookieEnabled() == true){		//客户端Cookie可用
		if(document.cookie.indexOf("SelMsg") == -1){	//如果没有设置Cookie
			SetCookie("SelMsg", SelMsgArray, 100);		//创建Cookie并将勾选的留言ID数组加入	
		}
		else{	//如果设置了Cookie
			if(getCookie("SelMsg") == ''){
				SetCookie("SelMsg", SelMsgArray, 100);
			}
			else{
				for(var i = 0; i<SelMsgArray.length; i++){
					if($.inArray(SelMsgArray[i], SelMsg) == -1){
						SelMsg.push(SelMsgArray[i]);
						SetCookie("SelMsg", SelMsg, 100);
					}
				}			
			}
		}
	}
	else{
		CookieNotEnabled();	//客户端Cookie不可用
	}
}

//取消全选的留言
function CelSelMsg(CelMsgArray){
	if(CheckCookieEnabled() == true){		//客户端Cookie可用
		if(document.cookie.indexOf("SelMsg") == -1){	//如果没有设置Cookie
			var SelMsg = getCookie("SelMsg").split(",");	//将Cookie中记录的已选留言转换成数组
			for(var i = 0; i <CelMsgArray.length; i++){	//逐一对比取消勾选的留言是否在Cookie中记录的已选留言中，如果存在则删除Cookie中相应的留言
				SelMsg.splice($.inArray(CelMsgArray[i], SelMsg), 1);
			}
			SetCookie("SelMsg", SelMsg, 100);	//更新已选留言数组
		}
		else{	//如果设置了Cookie
			var SelMsg = getCookie("SelMsg").split(",");	//将Cookie中记录的已选留言转换成数组
			for(var i = 0; i <CelMsgArray.length; i++){	//逐一对比取消勾选的留言是否在Cookie中记录的已选留言中，如果存在则删除Cookie中相应的留言
				SelMsg.splice($.inArray(CelMsgArray[i], SelMsg), 1);
			}
			SetCookie("SelMsg", SelMsg, 100);	//更新已选留言数组
		}
	}
	else{
		CookieNotEnabled();	//客户端Cookie不可用
	}
}

//留言全选
function checkAll(c){
	var arr = document.getElementsByName('delete_message[]');	
	if(c){		//全选
		var Sel = new Array();
		for(var i=0; i<arr.length; i++){
			arr[i].checked = true;		//复选框状态设置为勾选
			if(arr[i].value !== ''){
				Sel.push(arr[i].value);		//将勾选的留言ID添加到数组
			}
		}
		//将勾选的留言记录下来
		AddSelMsg(Sel);
		up_Sel_num();
	}
	else{		//取消全选
		var Cel = new Array();
		for(var i=0; i<arr.length; i++){
			arr[i].checked = false;	
			if(arr[i].value !== ''){
				Cel.push(arr[i].value);		//将取消勾选的留言ID添加到数组
			}			
		}
		CelSelMsg(Cel);		//更新已选留言
		up_Sel_num();
	}
}

//留言单选
function checkOne(c, Id){
	if(c){
		var SelBox = document.getElementById("SelBox_" + Id);
		var Mid = SelBox.value;
		var Sel = new Array();
		Sel.push(Mid);
		AddSelMsg(Sel);
		SetSelAllBox();
		up_Sel_num();
	}
	else{
		var SelBox = document.getElementById("SelBox_" + Id);
		var Mid = SelBox.value;
		var Cel = new Array();
		Cel.push(Mid);
		CelSelMsg(Cel);
		up_Sel_num();
		document.getElementById('top_selectall').checked = false;
		document.getElementById('footer_selectall').checked = false;
	}
}

//判断是否全选，重置头部和底部全选框状态
function SetSelAllBox(){
	
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("SelMsg") !== -1){
			var SelMsg = getCookie("SelMsg").split(",");		
			var IsAllSel = true;
			var Cboxarr = document.getElementsByName('delete_message[]');
			for(var i=0; i<Cboxarr.length; i++){
				if(Cboxarr[i].value !== ''){
					if($.inArray(Cboxarr[i].value, SelMsg) == -1){
						IsAllSel = false;
					}
				}
			}
			if(IsAllSel == true){
				document.getElementById('top_selectall').checked = true;
				document.getElementById('footer_selectall').checked = true;			
			}
		}
	}
	
}

//更新已选留言数
function up_Sel_num(){
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("SelMsg") !== -1){
			var SelMsg = getCookie("SelMsg").split(",");
			if(SelMsg.length == 1 && SelMsg[0] == ''){
				$(".selected_num").html(0);
			}
			else{
				$(".selected_num").html(SelMsg.length);	
			}
		}
		else{
			$(".selected_num").html(0);
		}
	}
}

//批量删除留言提示
function DelSelMsg(Surl){
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("SelMsg") !== -1){
			var SelMsg = getCookie("SelMsg").split(",");
			if(SelMsg.length == 1 && SelMsg[0] == ''){
				$.DialogByZ.Alert({Title: "提示", Content: "您没有勾选任何留言！", BtnL:"确定"});
			}
			else{
				var Durl = Surl + "/admin/index.php?action=delselmsg";
				$.DialogByZ.Confirm({Title: "", Content: "确认删除选中的 <div id='DelSelMsgNum'>" + SelMsg.length + "</div> 条留言吗？", FunL:function(){DelSelMsgCheck(Durl);}, FunR:null});	
			}
		}
	}
}

//执行批量删除留言操作并提示成功或失败
function DelSelMsgCheck(Durl){
	var xmlhttp = createXMLhttp();
	$.DialogByZ.Close();
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 0 || xmlhttp.readyState == 1 || xmlhttp.readyState == 2 || xmlhttp.readyState == 3){
			$.DialogByZ.Loading(WEB_URL+'/'+SITE_TPL+'/images/loading.png', '正在删除，请稍后……');
		}
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			$.DialogByZ.Close();
			var r = xmlhttp.responseText;
			if(r == 1){
				SetCookie("SelMsg", "", -1);
				up_Sel_num();
				$(".MessageList").load(window.location.href + " .MessageList");
				$("#header_page_url").load(window.location.href + " #header_page_url");
				$("#footer_page_url").load(window.location.href + " #footer_page_url");
				$.DialogByZ.Alert({Title: "提示", Content: "选定的留言删除成功！", BtnL:"确定"});
			}
			else{
				$.DialogByZ.Alert({Title: "提示", Content: "删除失败！", BtnL:"确定"});
			}
		}
	}
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("SelMsg") !== -1){
			var SelMsg = getCookie("SelMsg").split(",");
		}
	}
	xmlhttp.open("POST", Durl, true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("SelMsg=" + SelMsg);		
}

//留言填写验证
function MessageInputCheck(name, value){
	var xmlhttp = createXMLhttp();		//创建ajax对象
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
			var r = xmlhttp.responseText;
			$("#"+name+"_InputShowMessage").html(r);
		}
	}
	xmlhttp.open("POST", WEB_URL+"/index.php?action=checkinput", true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("inputname=" + name + "&value=" + value);
}

//QQ号码验证
function MessageInputQQCheck(name, value){
	var RadioGroup1 = document.getElementsByName("RadioGroup1");
	if(RadioGroup1[6].checked == true){
		MessageInputCheck(name, value);
		$("#QQnr_InputShowMessage").html("");
	}
	else{
		MessageInputCheck('QQnr', value);
	}
}

//跳转到批量回复留言页面
function RepSelMsg(){
	if(CheckCookieEnabled() == true){
		if(document.cookie.indexOf("SelMsg") !== -1){
			var SelMsg = getCookie("SelMsg").split(",");
			if(SelMsg.length == 1 && SelMsg[0] == ''){
				$.DialogByZ.Alert({Title: "提示", Content: "您没有勾选任何留言！", BtnL:"确定"});
			}
			else{
				window.location.href = WEB_URL + '/index.php?action=reply&id=sel';	
			}
		}
	}
}

//Cookie不可用提示
function CookieNotEnabled(){
	alert('您的浏览器Cookie不可用，您必须设置您的浏览器Cookie为可用，才能正常使用本功能！');
}