Fetion.Web.Chat.defined("Fetion.Web.Chat.CE");
Fetion.Web.Chat.CE.create = function (o)
{
    var html = "<img title='双击查看原图' src='chat/img/chat/LoadingImage_s.gif'>";
    html += "<img src='chat/img/chat/blank.png' style='display:none;background-color:#CCEEFF;filter:alpha(opacity=50);font-size:1px'>";

    var span = document.createElement("SPAN");
    span.innerHTML = html;

    if (o.parent)
        o.parent.appendChild(span);

    var img = span.childNodes[0];
    var progress = span.childNodes[1];
    span.img = img;
    span.progress = progress;

    var rect = img.getBoundingClientRect();
    progress.style.width = rect.right - rect.left;
    progress.style.height = rect.bottom - rect.top;
    span.progress.style.marginLeft = -(rect.right - rect.left);

    span.data = o;
    span.ondblclick = function ()
    {
        var data = this.data;
        window.external.LDblClick(data.id, data.type);
    };

    span.onmouseover = function ()
    {
        if (window.mouseButton == 1 || this.reading || !this.data.path || this.img.rate == 100)
            return;
        var reading = document.createElement("IMG");
        this.appendChild(reading);
        this.reading = reading;
        reading.title = "点击查看原图";
        reading.style.marginLeft = "-24px";
        reading.style.cursor = "hand";
        reading.onmouseover = function ()
        {
            this.src = "chat/img/chat/Magnifier_Hover.png";
        };
        reading.onmouseleave = function ()
        {
            this.src = "chat/img/chat/Magnifier_Normal.png";
        };
        reading.data = this.data;
        reading.onclick = this.ondblclick;
        reading.onmouseleave();
    };
    span.onmouseleave = function ()
    {
        if (window.mouseButton == 1)
            return;
        if (this.reading)
        {
            this.removeChild(this.reading);
            this.reading = null;
        }
    };

    if (o.path)
        this.update(span, o);

    return span;
};
Fetion.Web.Chat.CE.update = function (ctrl, data)
{
    if (data.status == "failure")
        data.path = "failure";

    var img = ctrl.img;
    var progress = ctrl.progress;
    if (!data.path)
    {
        if (!data.progress)
            data.progress = 0;

        var rect = img.getBoundingClientRect();
        var height = (rect.bottom - rect.top) / 100 * (100 - data.progress);
        if (height <= 0)
            return;
        progress.style.height = height;

        //data.progress++;
        //if (data.progress >= 100)
        //data.path = data.path_;
        //setTimeout(function () { Fetion.Web.Chat.IMG.update(ctrl, data) });

        return;
    }

    var imgFilePath = unescape(data.path);
    var fileName = imgFilePath.substring(imgFilePath.lastIndexOf("\\") + 1);
    ctrl.style.overflow = "hidden";
    ctrl.onclick = function(e){
		if(Fetion.Web.Chat.SelectImg){
			Fetion.Web.Chat.SelectImg.img.className="none-border";
		}
		Fetion.Web.Chat.SelectImg = this;
		this.img.className="double-border";
	};

    var html = "<img title='双击查看原图' class='none-border'  src='" + data.path + "' id='" + data.id + "' name='" + fileName + "' type='" + data.type + "' shortcut='" + data.textValue + "'>";
    if (data.status == "failure")
    {
        html = "<img title='点击重新加载' src='chat/img/chat/LoadingImageFailed.png' style='cursor:hand'>";
        ctrl.onclick = function ()
        {
            // 不再是双击查看原图的提示，而应该是点击重新加载,todo
            var rst = window.external.DownLoadObj(this.data.id);
            if (rst)
            {
                // 更新为loading图;todo
                this.innerHTML = "<img title='双击查看原图' src='chat/img/chat/LoadingImage_s.gif'>";
            }
        };
    }

    ctrl.innerHTML = html;
    var img = ctrl.childNodes[0];
    ctrl.img = img;
    img.onload = this.onload;
    img.onerror = this.onerror;
}
Fetion.Web.Chat.CE.onload = function ()
{
    this.onload = null;
    var pRect = this.getBoundingClientRect();
    var width = Math.min(pRect.right - pRect.left, 300);
    var wRate = 1;
    if (this.width > width)
        wRate = this.width / width;
    var height = Math.min(pRect.bottom - pRect.top, 300);
    var hRate = 1;
    if (this.height > height)
        hRate = this.height / height;
    //msgDIV.innerHTML = "width:" + this.width + ",wRate:" + wRate + ",height:" + this.height + ",hRate:" + hRate;
    wRate > hRate ? (this.width = this.width / wRate) : (this.height = this.height / hRate);

    var pRect = this.getBoundingClientRect();
    var rate = parseInt(100 / (wRate > hRate ? wRate : hRate));
    this.rate = rate;

    var title = "";
    if (rate < 100)
        title = "显示比例：" + rate + "%，";
    this.title = title + "双击查看原图";

    this.parentNode.style.overflow = "auto";

    Fetion.Web.Chat.scroll();
};
Fetion.Web.Chat.CE.onerror = function ()
{
    this.src = "chat/img/chat/LoadingImageFailed.png";
};