/**
 * 大众版飞信短息别称编辑器
 * 
 * @version 1.0.1 
 * @date 2014-12-25
 * @author gaohongfeipc@feinno.com
*/
if (!window.Fetion)
    window.Fetion = { "ui": { "SmsNickName": {},"API":{} } };
function $(id){
		return document.getElementById(id);
}
function htmldecode(s){  
    var div = document.createElement('div');  
    div.innerHTML = s;  
    return div.innerText || div.textContent;  
}
Fetion.ui.SmsNickName = {
	id:"",
    names: $("names"),
	isCancel:false,
	progressBar:$("progressBar"),
	progress:$("progress"),
	cancel:$("cancel"),
	addNickName:function(data){
		var html = '<div class="lm-name">'+htmldecode(data.name)+'</div><div class="lm-user"><input type="text" value="'+htmldecode(data.nickName)+'"></div>';
        var li = document.createElement("li");
		li.title=htmldecode(data.name);
		li.innerHTML= html;
		li.data = data;
		this.names.appendChild(li);
		li.childNodes[0].innerText=data.name;
		li.input = li.childNodes[1].childNodes[0];
		li.input.value=htmldecode(data.nickName);
		li.input.onchange=function(e){
			li.data.nickName= this.value;
		}
	}
}
Fetion.ui.API = {
		/**
		/* 注册c++通知接口
		/**/
		register: function() {
			/**
			/* 短息别称初始加载
			/* outJson = [{id:"1",name:"",nickName:""},{{id:"2",name:"",nickName:""}},....]
			/**/
			SM.ajax('SmsNickName', "loadNames", "", "", function(status, outTxt, outJson) {
				if ((status == 200 || status == 188) && outJson != null) {
					var liLength = Fetion.ui.SmsNickName.names.childNodes.length;
					for(var i=liLength;i>0;i--){
						Fetion.ui.SmsNickName.names.removeChild(Fetion.ui.SmsNickName.names.childNodes[0]);
					}
					for (var i = 0; i < outJson.length; i++) {
						Fetion.ui.SmsNickName.addNickName(outJson[i]);
					}
					Fetion.ui.API.setHeight(document.body.offsetHeight);
				}
			});
			/**
			/*	短息别称获取数据通知
			/**/
			SM.ajax('SmsNickName', "getData", "", "", function(status, outTxt, outJson) {
				if ((status == 200 || status == 188)) {
					Fetion.ui.API.setData();
				}
			});
		},
		/**
		/* 获取短息别称高度
		/**/
		setHeight: function(height) {
			SM.exec('SmsNickName', "getHeight", height, { "height": height });
		},
		/**
		/* 设置最新数据
		/**/
		setData: function() {
			var data = [];
			var liLength = Fetion.ui.SmsNickName.names.childNodes.length;
			for(var i=0;i<liLength;i++){
				Fetion.ui.SmsNickName.names.childNodes[i].data.nickName = Fetion.ui.SmsNickName.names.childNodes[i].childNodes[1].childNodes[0].value;
				data[i] = Fetion.ui.SmsNickName.names.childNodes[i].data;
			}
			SM.exec('SmsNickName', "setData", "", data);
		}
    }
var loaded = false;
function AfterSMCompleted() {
if( !loaded )
    Fetion.ui.API.register(); //注册接口
loaded = true;
}