/**
 * 大众版飞信短息附件
 * 
 * @version 1.0.1 
 * @date 2014-11-20
 * @author gaohongfeipc@feinno.com
*/
if (!window.Fetion)
    window.Fetion = { "ui": { "SmsAnnex": {},"API":{} } };
function $(id){
		return document.getElementById(id);
}
function byteLength(str){
	return str.replace(/[^x00-xFF]/g,'**').length;
}
Fetion.ui.SmsAnnex = {
	id:"",
    mainPanel: $("panel"),
    files: $("files"),
	isCancel:false,
	progressBar:$("progressBar"),
	progress:$("progress"),
	cancel:$("cancel"),
	addSmsAnnex:function(data){
		var fileNm = data.fileName;
		var filetype = "";
		//if(byteLength(fileNm)>12){
			filetype = fileNm.substring(fileNm.lastIndexOf(".")-1 ,fileNm.length);
			fileNm = fileNm.substring(0,fileNm.length-filetype.length+1);
			//}
		var html = "<img src='"+data.path+"' alt='' /><div style='-moz-user-select:-moz-none;-moz-user-select:none;-o-user-select:none;-khtml-user-select:none;-webkit-user-select:none;-ms-user-select:none;user-select:none;overflow:hidden;text-overflow:ellipsis;width:124px;white-space:nowrap;'>"+fileNm+"</div><a class='del-dw' style='display:none'></a> ";
         
		/*var html ="";
		$("ctrl").style.display = "";
		$("ctrl").innerHTML=data.fileName;
		var txtWidth = $("ctrl").offsetWidth;
		$("ctrl").style.display = "none";
		if(filetype!=""&&txtWidth>92){
			if(byteLength(filetype.substring(0,1))==2){
				html = "<img src='"+data.path+"' alt='' /><div style='float:left;overflow:hidden;text-overflow:ellipsis;width:92px;white-space:nowrap;'>"+data.fileName+"</div><span style='folat:left;margin-left:-2px;'>"+filetype+"</span><a class='del-dw' style='display:none'></a> ";
			}else{
				html = "<img src='"+data.path+"' alt='' /><div style='float:left;overflow:hidden;text-overflow:ellipsis;width:92px;white-space:nowrap;'>"+data.fileName+"</div><span style='folat:left;margin-left:-6px;'>"+filetype+"</span><a class='del-dw' style='display:none'></a> ";
			}
		}else{
			html = "<img src='"+data.path+"' alt='' /><div style='float:left;overflow:hidden;text-overflow:ellipsis;width:92px;white-space:nowrap;'>"+data.fileName+"</div><span></span><a class='del-dw' style='display:none'></a> ";
		}*/
		//var html = "<img src='"+data.path+"' alt='' />"+fileNm+"<a class='del-dw' style='display:none'></a> ";
		var li = document.createElement("li");
		li.title=data.fileName+" ("+data.size+")";
		li.innerHTML= html;
		li.id = data.fid;
		li.className="un-select";
		this.files.appendChild(li);
		li.onmouseover=function(e){
			this.childNodes[2].style.display="";
		}
		li.onmouseout=function(e){
			this.childNodes[2].style.display="none";
		}
		li.childNodes[2].fid=li.id;
		li.childNodes[2].onclick=function(e){
			Fetion.ui.API.deleteFile(this.fid);
			e.stopPropagation();
		}
		li.ondblclick=function(e){
			Fetion.ui.API.openAnnex(this.id);
		}
	},
	init:function(){
		this.cancel.onclick=function(e){
			Fetion.ui.SmsAnnex.mainPanel.style.display="none";
			Fetion.ui.SmsAnnex.progressBar.style.display="none";
			Fetion.ui.API.cancel();
		}
	},
	loadAnnex:function(data){
		var liLength = Fetion.ui.SmsAnnex.files.childNodes.length;
		for(var i=liLength;i>0;i--){
			Fetion.ui.SmsAnnex.files.removeChild(Fetion.ui.SmsAnnex.files.childNodes[0]);
			
		}
		Fetion.ui.SmsAnnex.mainPanel.style.display="";
		Fetion.ui.SmsAnnex.progressBar.style.display="none";
		 for (var i = 0; i < data.length; i++) {
			Fetion.ui.SmsAnnex.addSmsAnnex(data[i]);
		 }
	},
	loadProgress:function(){
		Fetion.ui.SmsAnnex.mainPanel.style.display="none";
		Fetion.ui.SmsAnnex.progressBar.style.display="";
	},
	setProgress:function(data){
		this.loadProgress();
		Fetion.ui.SmsAnnex.progress.style.width=data.percentage;
	}

}

Fetion.ui.API = {
    /**
    /* 注册c++通知接口
    /**/
    register: function() {
        /**
        /* 短息附件初始加载
        /* outJson = {id:"",files:[{},{}.....]}
        /**/
        SM.ajax('SmsAnnex', "loadAnnex", "", "", function(status, outTxt, outJson) {
            if ((status == 200 || status == 188) && outJson != null) {
                Fetion.ui.SmsAnnex.mainPanel.style.display = "";
                Fetion.ui.SmsAnnex.progressBar.style.display = "none";
                Fetion.ui.SmsAnnex.id = outJson.id;
				if(outJson.files!=null){
					Fetion.ui.SmsAnnex.loadAnnex(outJson.files);
					Fetion.ui.API.setHeight(document.body.offsetHeight);
				}else{
					Fetion.ui.API.setHeight(1);
				}
                
                
            }
        });
        /**
        /* 短息附件处理进度加载
        /**/
        SM.ajax('SmsAnnex', "loadProgress", "", "", function(status, outTxt, outJson) {
            if ((status == 200 || status == 188)) {
                
                Fetion.ui.SmsAnnex.loadProgress();
                Fetion.ui.API.setHeight(document.body.offsetHeight);
                Fetion.ui.SmsAnnex.isCancel = false;
            }
        });
        /**
        /* 短息附件处理进度设置
        /* outJson = {percentage:'10%'}
        /**/
        SM.ajax('SmsAnnex', "setProgress", "", "", function(status, outTxt, outJson) {
            if ((status == 200 || status == 188) && outJson != null) {
                //Fetion.ui.mainPanel.style.display="none";
                //Fetion.ui.progressBar.style.display="";
                //Fetion.ui.API.setHeight(document.body.offsetHeight);
                if (!Fetion.ui.SmsAnnex.isCancel) {
                    Fetion.ui.SmsAnnex.setProgress(outJson);
                }
            }
        });
    },
    /**
    /* 获取短息模板高度
    /**/
    setHeight: function(height) {
        SM.exec('SmsAnnex', "getHeight", "", { "height": height });
    },
    /**
    /* 取消附件通知
    /**/
    cancel: function() {
        SM.exec('SmsAnnex', "cancel", "", { id: Fetion.ui.SmsAnnex.id });
        Fetion.ui.SmsAnnex.mainPanel.style.display = "";
        Fetion.ui.SmsAnnex.progressBar.style.display = "none";
        Fetion.ui.SmsAnnex.isCancel = true;
        Fetion.ui.API.setHeight(document.body.offsetHeight);
    },
    /**
    /* 打开附件
    /**/
    openAnnex: function(fid) {
        SM.exec('SmsAnnex', "openAnnex", "", { "fid": fid });
    },
    /**
    /* 删除文件
    /**/
    deleteFile: function(fid) {
        SM.ajax('SmsAnnex', "delete", "", { "fid": fid }, function(status, outTxt, outJson) {
            if ((status == 200 || status == 188) && outJson != null) {
                Fetion.ui.SmsAnnex.files.removeChild($(outJson.fid));
                if (Fetion.ui.SmsAnnex.files.childNodes.length == 0) {
                    Fetion.ui.API.setHeight(1);
                } else {
                    Fetion.ui.API.setHeight(document.body.offsetHeight);
                }

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