/**
 * 大众版飞信短息模板
 * 
 * @version 1.0.1 
 * @date 2014-11-20
 * @author gaohongfeipc@feinno.com
*/
if (!window.Fetion)
    window.Fetion = { "ui": { "SmsTmp": {},"API":{} } };
function $(id){
		return document.getElementById(id);
}
function byteLength(str){
	return str.replace(/[^x00-xFF]/g,'**').length;
}
function getStrByBtLength(str,length){
	var bytesCount = 0;
	var i = 0;
	if (str != null){
		for (i = 0; i < str.length&&bytesCount<=length-1; i++){
			var c = str.charAt(i);
			if (/^[\u0000-\u00ff]$/.test(c)){
				bytesCount += 1;
			}else{
				bytesCount += 2;
			}
		}
		return str.substring(0,i);
	}
	return ""; 
}	
Fetion.ui.SmsTmp = {
    mainPanel: $("list"),
	addBtn:$("addBtn"),
    newID: function() {
        while (true) {
            var tmpName = "ui_" + parseInt(Math.random() * 1000000);
            var obj = window[tmpName];
            if (!obj)
                return tmpName;
        }
    },
    /**
    /* 初始化
    /**/
    init: function() {
		this.addBtn.onclick=function(e){
			Fetion.ui.API.openEditWindow({isnew:0})
		}
    },
	addSmsTmp:function(data){
		data.viewContent = data.viewContent.replace(/ /g,"&nbsp;");
		var html = data.isSysTemp?data.viewContent:(data.viewContent+"<a class='edit-md' id='edit_"+data.id+"'><span></span></a><a class='del-md' id='del_"+data.id+"'><span></span></a>");
        var li = document.createElement("li");
		li.innerHTML= html;
		li.id = data.id;
		var content = "";
		var title=data.content.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&").replace(/&quot;/g,"\\");
		if(byteLength(title)>400){
			title =getStrByBtLength(title,400)+"...";
		}
		while(byteLength(title)>50){
			var tempStr = getStrByBtLength(title,50);
			content =content+tempStr+"\n";

			title = title.replace(tempStr,"");
		}
		content =content+title;
		li.title=content;
		this.mainPanel.appendChild(li);
		if(!data.isSysTemp){
			$("edit_"+data.id).onclick=function(e){
				Fetion.ui.API.openEditWindow({isnew:1,id:this.id.substring(5)});
				e.stopPropagation();
			}
			$("del_"+data.id).onclick=function(e){
				Fetion.ui.API.deleteSmsTmp({id:this.id.substring(4)});
				e.stopPropagation();
			}
		}
		li.onclick=function(e){
			if((e.target.className=="edit-md"||e.target.parentElement.className=="edit-md")||
			(e.target.className=="del-md"||e.target.parentElement.className=="del-md")){
				return;
			}
			Fetion.ui.API.selectSmsTmp({"id":this.id});
		}
		li.onselectstart=function(e){
			return false;
		}
	},
	delSmsTmp:function(id){
		var o=$(id);
		o.parentElement.removeChild(o);
		Fetion.ui.API.setHeight(document.body.offsetHeight);
	}

}

Fetion.ui.API = {
    /**
    /* 注册c++通知接口
    /**/
    register: function() {
        /**
        /* 短息模板初始加载
        /**/
        SM.ajax('SmsTmp', "load", "", "", function(status, outTxt, outJson) {
            if ((status == 200 || status == 188) && outJson != null) {//alert(JSON.stringify(outJson));
				Fetion.ui.SmsTmp.mainPanel.innerHTML="";
                for (var i = 0; i < outJson.length; i++) {
                    Fetion.ui.SmsTmp.addSmsTmp(outJson[i]);
                }
            }
			Fetion.ui.API.setHeight(document.body.offsetHeight);
        });
        /**
        /* 添加短息模板
        /**/
        SM.ajax('SmsTmp', "addSmsTmp", "", "", function(status, outTxt, outJson) {
            if ((status == 200 || status == 188) && outJson != null) {
                //alert("out="+JSON.stringify(outJson));
                Fetion.ui.SmsTmp.addSmsTmp(outJson);
                Fetion.ui.API.setHeight(document.body.offsetHeight);
            }
        });
    },
	/**
    /* 选中一条模板
    /**/
    selectSmsTmp: function(params) {
        SM.exec('SmsTmp', "selectSmsTmp", "", params);
    },
	/**
	/* 打开编辑短信模板窗口
	/* params={isnew:1,id:'123oiiuo3242132'}
	/* isnew : 是否是新建 (0:新建,1:更新)
	**/
	openEditWindow:function(params){
		SM.exec('SmsTmp', "openEditWindow", "", params);
	},
    /**
    /* 删除短息模板
    /**/
    deleteSmsTmp: function(params) {
        SM.ajax('SmsTmp', "deleteSmsTmp", "", params, function(status, outTxt, outJson) {
            if ((status == 200 || status == 188)) {
               Fetion.ui.SmsTmp.delSmsTmp(params.id);
            }
        });
    },
    /**
    /* 获取短息模板高度
    /**/
    setHeight: function(height) {
        SM.exec('SmsTmp', "getHeight", height, "");
    }

}
Fetion.ui.SmsTmp.init();
var loaded = false;
function AfterSMCompleted() {
if( !loaded )
    Fetion.ui.API.register(); //注册接口
loaded = true;
}