var isDebug = false;
var PcAPI;
(function ($) {
    $.extend({
		dropDownListId:"",
		initBaseSet:function(settings){
				//初始化控件
				for(item in settings){
					$.setCheckBox({key:item,value:settings[item],"SmsDisableFlag":settings.SmsDisableFlag});
					
				}
				$.setMainplaneRadio({"CloseToExit": settings.CloseToExit});
				$.setLoginStaut({"PresenceAfterLogon":settings.PresenceAfterLogon});
				//$.setCmsOption({"SmsOnlineStatus":settings.SmsOnlineStatus});
				$.setCmsLayout({"SmsNicknameConfig":settings.SmsNicknameConfig,"UserNickName":settings.UserName,"SmsDisableFlag":settings.SmsDisableFlag});
				//绑定事件
				//打开文件夹
				$("#OpenFolder").bind("click",function(){
					PcAPI.OpenWindow({'type':'OpenFolder','key':'CustomFolderPath'});
	        }).hover(function() {
					$(this).addClass("btn01_0");
				}, function() {
					$(this).removeClass("btn01_0");
				}).bind("mousedown",function(){
					$(this).addClass("btn01_1");
				}).bind("mouseup",function(){
					$(this).removeClass("btn01_1");
				});
	        $('.wrap_dx')[0].style.backgroundColor = settings.bgColor;
	        $('.wrap_dx')[0].style.backgroundImage = "url(" + settings.themeSrc + ")";
		},
		/****************************
		* 功能：根据名称获得地址栏的参数值
		* 参数：name
		*****************************/
		getQueryStringByName:function getQueryStringByName(name) {
			var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));            
			if (result == null || result.length < 1) {                
				return "";            
			}            
			return result[1];        
		},
		/****************************
		* 功能：根据key值取得checkBox对象
		* 参数：settings
		*****************************/
		setCheckBox:function(settings){
		var type = settings&&settings.SmsDisableFlag?settings.SmsDisableFlag:0;
			if((type==1||type==2)&&settings.key=="CCtoSmsConfig"){
			$("a[key=CCtoSmsConfig]").parent().addClass("disable");
				return;
			}
			if((type==1||type==2)&&settings.key=="RecvOfflineFileSms"){
				$("a[key=RecvOfflineFileSms]").parent().addClass("disable");
				return;
			}
			if(settings.key=='RecInvPubPhone'&&settings.value==0){
				$("a[key=RecInvPubPhone]").parent().addClass("disable");
				return;
			}else if(settings.value==1){
				settings.value="true";
			}else if(settings.value==2){
				settings.value="false";
			}
			var checkBox = $.extend({
					value: null,   //  值
					key: null, //键
					html:null
				}, settings);
			/****************************
			* 功能：初始化checkBox对象
			*****************************/
			function init(){
					checkBox.html = $("a[key="+checkBox.key+"]");
					if(checkBox.value){
						if(checkBox.value=="true"){
							checkBox.html.find("span").addClass("choose");
						}
						
					}
					checkBox.html.attr("value",checkBox.value);
					
					checkBox.html.unbind().bind("click",function(){
						var safeNotifyVal = 0;
						var EveryLoginSmsBox = $("a[key=EveryLoginSms]");
						var DiffMachineLoginSmsBox = $("a[key=DiffMachineLoginSms]");
						var DiffAreaLoginSmsBox = $("a[key=DiffAreaLoginSms]");
						var OtherTerminalLoginSmsBox = $("a[key=OtherTerminalLoginSms]");
						if(checkBox.key=="DiffMachineLoginSms"||checkBox.key=="DiffAreaLoginSms"||checkBox.key=="OtherTerminalLoginSms"){
								if(checkBox.html.attr("value")=="true"){
									checkBox.html.attr("value","false");
									checkBox.html.find("span").removeClass("choose");
									checkBox.html.find("span").removeClass("choose_hover");
									//PcAPI.ChangeConfigItem({'key':checkBox.key,'value':'false'});
									EveryLoginSmsBox.attr("value","false");
									EveryLoginSmsBox.find("span").removeClass("choose");
									EveryLoginSmsBox.find("span").removeClass("choose_hover");
									//PcAPI.ChangeConfigItem({'key':'EveryLoginSms','value':'false'});
								}else{
									if(DiffMachineLoginSmsBox.attr("value")=="true"&&DiffAreaLoginSmsBox.attr("value")=="true"&&OtherTerminalLoginSmsBox.attr("value")=="true"){
										EveryLoginSmsBox.attr("value","true");
										//checkBox.html.find("span").addClass("choose");
										EveryLoginSmsBox.find("span").addClass("choose_hover");
										//PcAPI.ChangeConfigItem({'key':checkBox.key,'value':'true'});
									}
									checkBox.html.attr("value","true");
									checkBox.html.find("span").addClass("choose_hover");
								}
								if(DiffMachineLoginSmsBox.attr("value")=="true"){
									safeNotifyVal = safeNotifyVal+ 4;
								}
								if(DiffAreaLoginSmsBox.attr("value")=="true"){
									safeNotifyVal = safeNotifyVal+ 8;
								}
								if(OtherTerminalLoginSmsBox.attr("value")=="true"){
									safeNotifyVal = safeNotifyVal+ 16;
								}
								PcAPI.ChangeConfigItem({'key':'SafeNotify','value':safeNotifyVal.toString()});
						}else if(checkBox.key=="EveryLoginSms"){
								if(checkBox.html.attr("value")=="true"){
									EveryLoginSmsBox.attr("value","false");
									EveryLoginSmsBox.find("span").removeClass("choose");
									EveryLoginSmsBox.find("span").removeClass("choose_hover");
								}else{
									EveryLoginSmsBox.attr("value","true");
									//checkBox.html.find("span").addClass("choose");
									EveryLoginSmsBox.find("span").addClass("choose");
									DiffMachineLoginSmsBox.attr("value","true");
									DiffMachineLoginSmsBox.find("span").addClass("choose");
									DiffAreaLoginSmsBox.attr("value","true");
									DiffAreaLoginSmsBox.find("span").addClass("choose");
									OtherTerminalLoginSmsBox.attr("value","true");
									OtherTerminalLoginSmsBox.find("span").addClass("choose");
								}
								//PcAPI.ChangeConfigItem({'key':checkBox.key,'value':checkBox.value});
									if(EveryLoginSmsBox.attr("value")=="true"){
										safeNotifyVal = safeNotifyVal+ 1;
									}
									if(DiffMachineLoginSmsBox.attr("value")=="true"){
										safeNotifyVal = safeNotifyVal+ 4;
									}
									if(DiffAreaLoginSmsBox.attr("value")=="true"){
										safeNotifyVal = safeNotifyVal+ 8;
									}
									if(OtherTerminalLoginSmsBox.attr("value")=="true"){
										safeNotifyVal = safeNotifyVal+ 16;
									}
								PcAPI.ChangeConfigItem({'key':'SafeNotify','value':safeNotifyVal.toString()});
						}else if(checkBox.key=="NoLoginPc"||checkBox.key=="EveryLogin"){
							var checkVal="0";
							var NoLoginPcBox = $("a[key=NoLoginPc]");
							var EveryLoginBox = $("a[key=EveryLogin]");
							if(checkBox.key=="EveryLogin"&&checkBox.html.attr("value")=="false"){
									checkBox.html.attr("value","true");
									checkBox.html.find("span").addClass("choose");
									//checkBox.html.find("span").removeClass("choose");
									//checkBox.html.find("span").removeClass("choose_hover");
									NoLoginPcBox.attr("value","true");
									NoLoginPcBox.find("span").addClass("choose");
							}else if(checkBox.key=="EveryLogin"&&checkBox.html.attr("value")=="true"){
									checkBox.html.attr("value","false");
									checkBox.html.find("span").removeClass("choose");
									checkBox.html.find("span").removeClass("choose_hover");
							}else if(checkBox.key=="NoLoginPc"&&checkBox.html.attr("value")=="true"){
								checkBox.html.attr("value","false");
								checkBox.html.find("span").removeClass("choose");
								checkBox.html.find("span").removeClass("choose_hover");
								EveryLoginBox.attr("value","false");
								EveryLoginBox.find("span").removeClass("choose");
								EveryLoginBox.find("span").removeClass("choose_hover");
							}else{
								checkBox.html.attr("value","true");
								checkBox.html.find("span").addClass("choose");
							}
							if(EveryLoginBox.attr("value")=="true"){
								checkVal="1";
							}else if(NoLoginPcBox.attr("value")=="true"&EveryLoginBox.attr("value")=="false"){
								checkVal="2";
							}else{
								checkVal="0";
							};
							PcAPI.ChangeConfigItem({'key':'ForceSmsLogon','value':checkVal});
						}else{
							setSelect();
							//alert(checkBox.html.attr("key"));
							var checkVal = checkBox.html.attr("value");
							var keyId = checkBox.html.attr("key");
							if(keyId=="ShowSessionWindow"){
								keyId="ShowNewMessageWnd";
								if(checkVal=="true"){
									checkVal='2';
								}else{
									checkVal='1';
								}
							}
							PcAPI.ChangeConfigItem({'key':keyId,'value':checkVal});
						}
					})
					
					
			};
			/****************************
			* 功能：设置checkBox是否选择的状态和值
			*****************************/
			var setSelect = function(){
				var value = checkBox.html.attr("value");
				
				if(value=="true"){
					checkBox.html.attr("value","false");
					checkBox.html.find("span").removeClass("choose");
					checkBox.html.find("span").removeClass("choose_hover");
					$("div.arrow_p>a.arrow_t").unbind();
					$("div.arrow_p>a.arrow_b").unbind();
				}else{
					checkBox.html.attr("value","true");
					//checkBox.html.find("span").addClass("choose");
					checkBox.html.find("span").addClass("choose_hover");
						if(checkBox.key=="AwayOnIdle"){
							$.allset.setMinTime({"AwayOnIdle":"true","AwayOnIdleMinutes":$(".min_txt").val()});
						}
				}
				
			};
			init();
			return checkBox;
		},
		/****************************
		* 功能：设置主面板单选按钮
		* 参数：settings:{CloseToExit:true/false}
		*****************************/
        setMainplaneRadio:function(settings){
			//$(".mainplane").find("")
			var value = "false";
			var radio1= $("a.notexit");
			var radio2 = $("a.exit");
			if(settings.CloseToExit=="false"){
				radio1.attr("selected","false");
				radio1.find("span").removeClass("radio_auto").addClass("radio_on");
				radio2.find("span").attr("selected","true");
				radio2.find("span").removeClass("radio_on").addClass("radio_auto");
			}else{
				radio1.attr("selected","true");
				radio1.find("span").removeClass("radio_on").addClass("radio_auto");
				radio2.attr("selected","false");
				radio2.find("span").removeClass("radio_auto").addClass("radio_on");
			}
			radio1.unbind().bind("click",function(){
				radio1.attr("selected","true");
				radio1.find("span").removeClass("radio_auto").addClass("radio_on");
				$(this).find("span").removeClass("auto_hover").addClass("on_hover");
				radio2.attr("selected","false");
				radio2.find("span").removeClass("radio_on").addClass("radio_auto");
				value = "false";
				PcAPI.ChangeConfigItem({'key':"CloseToExit",'value':value});
			});
			
			radio2.unbind().bind("click",function(){
				radio1.attr("selected","false");
				radio1.find("span").removeClass("radio_on").addClass("radio_auto");
				radio2.attr("selected","true");
				radio2.find("span").removeClass("radio_auto").addClass("radio_on");
				$(this).find("span").removeClass("auto_hover").addClass("on_hover");
				value = "true";
				PcAPI.ChangeConfigItem({'key':"CloseToExit",'value':value});
			});
			return value;
		},
		

		/****************************
		* 功能：设置登录状态下拉列表
		* 参数：settings:{PresenceAfterLogon:"online"}
		*****************************/
		setLoginStaut:function(settings){
			var value=settings&&settings.PresenceAfterLogon?settings.PresenceAfterLogon:"online";
			$("#status>input.sel_1").val($("#status_list>ul>li>a[v="+value+"]").text());
			$("#status>input.sel_1").attr("v",value);
			$("#status_list").hide();
			$("#status_list>ul>li").hover(function() {
					$(this).addClass("hover");
				}, function() {
					$(this).removeClass("hover");
				}).bind("click",function(){
					var a = $(this).find("a");
						$("#status>input.sel_1").val(a.text());
						$("#status>input.sel_1").attr("v",a.attr("v"));
						PcAPI.ChangeConfigItem({'key':"PresenceAfterLogon",'value':a.attr("v")});
						
						//绑定
						$("#all_status>input.sel_1").val(a.text());
						$("#all_status>input.sel_1").attr("v",a.attr("v"));
				});
			$("#status").unbind().bind("click",function(e){
				var target  = $(e.target);
				if($.dropDownListId&&$.dropDownListId=="status_list"){
					$("#"+$.dropDownListId).hide();
					$.dropDownListId=undefined;
					$(this).addClass("person_0").removeClass("person_1");
				}else if(!$.dropDownListId&&$.dropDownListId!="status_list"){
					$("#status_list").show();
					$(this).addClass("person_1");
					$.dropDownListId="status_list";
				}else {
					$("#"+$.dropDownListId).hide();
					$("#status_list").show();
					$(this).addClass("person_1");
					$.dropDownListId="status_list";
				}
			});
			
			$(document).bind("click",function(e){
				var target  = $(e.target);
				if(target.closest("#status").length == 0){
					$("#status_list").hide();
					$("#status").removeClass("person_0");
					$("#status").removeClass("person_1");
				}
			});
		},

		/****************************
		* 功能：设置短信布局
		* 参数：settings:{SmsNicknameConfig:true/false}
		*****************************/
		setCmsLayout:function(settings){
			var smsDisableFlag = settings&&settings.SmsDisableFlag?settings.SmsDisableFlag:0;
			if(smsDisableFlag==2){
				$("dd[k=layout]").addClass("disable");
				return;
			}
		var value=settings&&settings.SmsNicknameConfig?settings.SmsNicknameConfig:0;
		//设置下拉列表内容

			$("#layout_list").hide();
			$("#layout_list>ul>li").hover(function() {
					$(this).addClass("hover");
					var type=$(this).find("a").attr("v");
					if(type==0){
					    $("#base_showNameTemp").html(settings.UserNickName + ":最近好吗?");
					}else if(type==1){
						$("#base_showNameTemp").html("最近好吗?【"+settings.UserNickName+"】");
					}else if(type==2){
						$("#base_showNameTemp").html("最近好吗?");
					}
				}, function() {
					$(this).removeClass("hover");
				}).bind("click",function(){
					var a = $(this).find("a");
					$("#sms_layout>input.sel_1").val(a.text());
					PcAPI.ChangeConfigItem({'key':"SmsNicknameConfig",'value':a.attr("v")});
					
					//绑定
					$("#all_sms_layout>a>input.sel_1").val(a.text());
				});
				//
				var html = $("a[key=SmsNicknameConfig_key]");
				html.find("span").addClass("choose");
				$("#sms_layout>input.sel_1").val($("#layout_list>ul>li>a[v="+value+"]").text());

				$("#sms_layout").unbind().bind("click",function(e){
					//初始下拉列表
					if(value==0){
						$("#base_showNameTemp").html(settings.UserNickName + ":最近好吗?");
					}else if(value==1){
						$("#base_showNameTemp").html("最近好吗?【"+settings.UserNickName+"】");
					}else if(value==2){
						$("#base_showNameTemp").html("最近好吗?");
					}
					var target  = $(e.target);
					if($.dropDownListId&&$.dropDownListId=="layout_list"){
						$("#"+$.dropDownListId).hide();
						$.dropDownListId=undefined;
						$(this).removeClass("s_msg_1").addClass("s_msg_0");
					}else if(!$.dropDownListId&&$.dropDownListId!="layout_list"){
						$("#layout_list").show();
						$(this).addClass("s_msg_1");
						$.dropDownListId="layout_list";
					}else{
						$("#"+$.dropDownListId).hide();
						$("#layout_list").show();
						$(this).addClass("s_msg_1");
						$.dropDownListId="layout_list";
					}
				});
				
				
			$(document).bind("click",function(e){
				var target  = $(e.target);
				if(target.closest("#sms_layout").length == 0){
					$("#layout_list").hide();
					$("#sms_layout").removeClass("s_msg_0");
					$("#sms_layout").removeClass("s_msg_1");
				}
			});
		}
		
		
	});

	//alert(checkbox.value);
	
}
)(jQuery);

/*
$(document).ready(function () {
/*$.getCheckBox({key:"FetionAutoRun",value:0})
$.getCheckBox({key:"AutoLogon",value:0})
	//var data = PcAPI.InitConfigData();
	var data = eval("("+decodeURI($.getQueryStringByName('data'))+")");
	$.initWindos(data);
});*/
