/// <reference name="MicrosoftAjax.js"/>

var objText;
var objTarget;
var status = false;
if (navigator.userAgent.indexOf("Firefox") > 0) {
    window.constructor.prototype.__defineGetter__("event", function() {
        var o = arguments.callee.caller;
        var e;
        while (o != null) {
            e = o.arguments[0];
            if (e && (e.constructor == Event || e.constructor == MouseEvent)) {
                return e;
            }
            o = o.caller;
        }
        return null;
    });
    Event.prototype.__defineGetter__("srcElement", function() {
        var _b = this.target;
        while (_b && _b.nodeType != 1) {
            _b = _b.parentNode;
        }
        return _b;
    });
    Event.prototype.__defineGetter__("offsetX", function() {
        return this.layerX;
    });
    Event.prototype.__defineGetter__("offsetY", function() {
        return this.layerY;
    });
    Event.prototype.__defineSetter__("cancelBubble", function(b) {
        if (b) {
            this.stopPropagation();
        }
        return b;
    });
}

//左坐标
function getAbsoluteLeft(ob)
{ var s_el = 0; el = ob; while (el) { s_el = s_el + el.offsetLeft; el = el.offsetParent; }; return s_el }

//上坐标
function getAbsoluteTop(ob)
{ var s_el = 0; el = ob; while (el) { s_el = s_el + el.offsetTop; el = el.offsetParent; }; return s_el }
document.onmousedown = function() {
    showSmilDiv(false);
}

if (self.pageYOffset != undefined)//判断是否为ff
{
    window.onscroll = hiddenOnScroll;
}
else if (document.documentElement) {
    document.documentElement.onscroll = hiddenOnScroll;
}

function hiddenOnScroll() {
    if (objTarget == null) return;
    if (objTarget.attributes["name"].value == "topsmile") {
        document.getElementById("smilePannel").style.visibility = "hidden";
    }
}



function createDiv() {
    document.write("<div id=\"smilePannel\" style=\"background:#ffffff;border:1px; border-color:Gray; border-style:solid; z-index:10000; visibility:hidden; width:336px; height:72px; position:absolute;overflow:hidden;\" >");
    document.write("<div id=\"smilebg\" style=\"background:Transparent url(/Image/editor/simles.gif) no-repeat; z-index:2; width:336px; height:72px; top:0px; left:0px;position:absolute; overflow:hidden;\"");
    document.write("onmousemove=\"setShadom();\" onmousedown=\"setSmile();\">");
    document.write("</div>");
    document.write("<div id=\"shadom\" style=\"z-index:1; top:1px; left:1px; width:22px; height:22px; background-color:#C8E7F9;position:absolute; border:1px solid #5EB8EE; overflow:hidden;\" ></div>");
    document.write("</div>");

}
function showSmilDiv(show, obj, tid) {
    status = show;
    if (show) {
        if (tid != null)
            objText = document.getElementById(tid);
        if (obj != null)
            objTarget = obj;

        document.getElementById("smilePannel").style.left = getAbsoluteLeft(objTarget) + objTarget.width + "px";
        if (objTarget.attributes["name"].value == "topsmile") {
            document.getElementById("smilePannel").style.zIndex = document.getElementById("smilePannel").style.zIndex + 2;
            document.getElementById("smilePannel").style.top = getAbsoluteTop(objTarget) + document.documentElement.scrollTop + "px";
        } else {
            document.getElementById("smilePannel").style.top = getAbsoluteTop(objTarget) + "px";
        }
        document.getElementById("smilePannel").style.visibility = "visible";
    } else {
        document.getElementById("smilePannel").style.visibility = "hidden";
    }
}
function setShadom() {
    var x = event.offsetX;
    var y = event.offsetY;
    var e_x = 24 * Math.floor(x / 24);
    var e_y = 24 * Math.floor(y / 24);
    document.getElementById("shadom").style.top = e_y + "px";
    document.getElementById("shadom").style.left = e_x + "px";

}
function setSmile() {
    var x = event.offsetX;
    var y = event.offsetY;
    var xi = Math.floor(x / 24);
    var yi = Math.floor(y / 24);
    var index = yi * 14 + xi;
    var smStr = "[:" + index + "]";
    objText.value += smStr;
}
createDiv();