Fetion.Web.Chat.defined("Fetion.Web.Chat.IMG");
Fetion.Web.Chat.IMG.create = function (o)
{
    var html = "<div style='display:block;'><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'></div>";

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

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

    var img = span.childNodes[0].childNodes[0];
    var progress = span.childNodes[0].childNodes[1];
	span.xtype="false";
    span.img = img;
	span.img.parent = this;
    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;
		if (this.reading)
            {
                this.childNodes[0].removeChild(this.reading);
                this.reading = null;
				//this.reading.style.visibility = "hidden";
            }
        window.external.LDblClick(data.id, data.type);
    };

    span.onmouseover = function ()
    {
        if (window.mouseButton == 1 || !this.data.path || this.img.rate == 100)
            return;
		var obj =event.srcElement || event.target; 
		var rangeStr = document.selection.createRange().htmlText;
		if(obj&&obj==span.img&&rangeStr==""){
			if(!this.reading){
				var reading = document.createElement("IMG");
				this.childNodes[0].appendChild(reading);
				this.reading = reading;
			}
			reading.onselectstart = "return false";
			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.onmousedown = function(){
				span.ondblclick();
			}

			reading.onmouseleave();
		}
		
    };
	span.ondragstart=function(){
		if (this.reading)
        {
            this.childNodes[0].removeChild(this.reading);
            this.reading = null;
        }
	};
    span.onmouseleave = function ()
    {
        if (window.mouseButton == 1)
            return;
        if (this.reading)
        {
            this.childNodes[0].removeChild(this.reading);
            this.reading = null;
			//this.reading.style.visibility = "hidden";
        }
    };
    span.onmouseout = function (e)
    {
        if (!e) e = window.event;
        var reltg = e.relatedTarget ? e.relatedTarget : e.toElement;
        while (reltg && reltg != this) reltg = reltg.parentNode;
        if (reltg != this)
        {
            if (window.mouseButton == 1)
                return;
            if (this.reading)
            {
                this.childNodes[0].removeChild(this.reading);
                this.reading = null;
				//this.reading.style.visibility = "hidden";
            }
        }
    }

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

    return span;
};
Fetion.Web.Chat.IMG.update = function (ctrl, data)
{
    if (data.status == "overdue")
        data.path = "overdue";
    else 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";
	};
	//if(data.path.indexOf(".png")!=-1||data.path.indexOf(".PNG")!=-1||data.path.indexOf(".GIF")!=-1||data.path.indexOf(".gif")!=-1
	//||data.path.indexOf(".jpg")!=-1||data.path.indexOf(".JPG")!=-1){
		
		data.path = decodeURI(data.path.replace('fetionres:///', 'file:///'));
		if(data.path.indexOf("data:image")==-1){
			data.path = data.path.replace(/\+/g, ' ');
			data.path = data.path.replace(/%3A/g, ':');
		}
	//}
    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.childNodes[0].innerHTML = html;
    var img = ctrl.childNodes[0].childNodes[0];
    ctrl.img = img;
	ctrl.img.parent = ctrl;
    img.onload = this.onload;
    img.onerror = this.onerror;
	img.isLoad = false;
    if (window.addEventListener)
        window.addEventListener("resize", function () { img.onload() }, false);
    else
        window.attachEvent("onresize", function () { img.onload() });
}
Fetion.Web.Chat.IMG.onload = function ()
{
	if(this.src.replace(/.+\./,"").toLowerCase()=="gif"&&this.isLoad){
			return;
	}
    this.style.width = this.style.height = "auto";
    var rect = this.getBoundingClientRect();
    var pRect = { width: rect.right - rect.left, height: rect.bottom - rect.top };
    var width = Math.min(pRect.width, document.documentElement.clientWidth - 130);
    if (window.isChrome)
        var width = Math.min(pRect.width, document.documentElement.clientWidth * 0.73 - 130);
    var wRate = pRect.width > width ? pRect.width / width : 1;
    var height = Math.min(pRect.height, document.documentElement.clientHeight-90);
    var hRate = pRect.height > height ? pRect.height / height : 1;
    //hRate = 1;
    wRate > hRate ? (this.style.width = pRect.width / wRate) : (this.style.height = pRect.height / hRate);

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

	var title = "";
    if (rate < 100){
        title = "显示比例：" + rate + "%，";
	}
	if(title!=""){
		this.title = title + "双击查看原图";
	}
	if(rate==100){
		this.title = "双击查看原图";
	}
	
	if(this.style.height=="auto"&&this.style.offsetHeight>24){
		this.parentNode.style.width=this.style.width;
		this.parentNode.style.height="24px";
	}
    this.parentNode.parentNode.style.overflow = "auto";
    Fetion.Web.Chat.scroll();
	/*this.style.visibility="visible";
	var ctrl = this.parent;
	if(!ctrl.reading){
		var reading = document.createElement("IMG");
		ctrl.childNodes[0].appendChild(reading);
		ctrl.reading = reading;
		reading.onselectstart = "return false";
		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 = ctrl.data;
		reading.onmousedown = function(){
			ctrl.ondblclick();
		}
		reading.onmouseleave();
	}
	ctrl.reading.style.visibility = "hidden";*/
	//this.className="none-border";
	this.isLoad = true;
};
Fetion.Web.Chat.IMG.onerror = function ()
{
    this.src = "chat/img/chat/LoadingImageFailed.png";
};
