// browser.js
// (c)2005 Douglas Crockford
// 2005-05-14

/*global alert, document, event, isBoolean, isFunction, isNumber, isObject,
         isString, isUndefined, object, setTimeout, stringify */

YNH.width = YNH.height = Infinity;
YNH.ie = navigator.appName == 'Microsoft Internet Explorer';
YNH.slidespace = [0.0625, 0.1875, 0.4375, 0.6875, 0.8125, 0.875, 0.9375, 1];
YNH.slidetime = 100;
YNH.dropzone = [];

(function (u, v) {
    YNH.gecko = u.indexOf('Gecko') >= 0;
    if (u.indexOf('Opera') >= 0) {
        YNH.opera = true;
        YNH.ie = false;
        YNH.gecko = false;
    }
    YNH.version = YNH.ie ? parseFloat(v.substr(21)) : parseFloat(v);
})(navigator.userAgent, navigator.appVersion);

function alarm() {
    var d = '', i;
    for (i = 0; i < arguments.length; ++i) {
        d += stringify(arguments[i]) + ' ';
    }
    alert(d);
    return arguments[0];
}

YNH.createElement = function (tagName, type, name, id) {
    if (type || name || id) {
        var s = document.createElement('span');
        s.innerHTML = '<' + tagName +
            (type ? ' type="' + type + '"' : '') +
            (name ? ' name="' + name + '"' : '') +
            (id   ? ' id="'   + id   + '"' : '') + '>';
        return s.firstChild;
    }
    return document.createElement(tagName);
};

YNH.createText = function (t) {
    return document.createTextNode(t);
};

/*
    YNH takes a dom object or a dom id and returns a YNH wrapper.
*/
YNH.dom = function (d) {
    if (isString(d)) {
        d = YNH.getElement(d);
    }
    if (!d) {
        return;
    }
    if (d.ynh) {
        return d.ynh;
    }

    var n = d.tagName.toLowerCase(),
        s = YNH.tag[n],
        t = new YNH.dom();

    function mouse(e) {
        var m = new YNH.event(e);
        t.fire(m);
        m.done(m.tagName != 'input' && m.tagName != 'textarea');
    }

    function mousedown(e) {
        YNH.dragging = false;
        if (YNH.select) {
            YNH.select = null;
        } else {
            var m = new YNH.event(e);
            YNH.select = t;
            if (m.ctrlKey) {
                YNH.specialbutton = true;
                if (YNH.noSpecialClick) {
                    YNH.select = null;
                    return;
                }
            } else {
                YNH.specialbutton = false;
                t.fire(m);
            }
            m.done(m.tagName != 'input' && m.tagName != 'textarea');
        }
    }

    function mouseover(e) {
        if (YNH.ie && event.srcElement.tagName == 'BODY') {
            return;
        }
        t.mousingover = true;
        if (!YNH.select && !YNH.dragging) {
            mouse(e);
        }
    }

    function mouseout(e) {
        t.mousingover = false;
        YNH.eraseTip();
        if (!YNH.dragging) {
            mouse(e);
        }
    }

    t.tagName = n;
    t.dom = d;
    d.ynh = t;
    d.onmousedown = mousedown;
    d.onmouseout  = mouseout;
    d.onmouseover = mouseover;
    d.ondragstart = d.ondrag = Function.returnFalse;
    if (!YNH.noSpecialClick) {
        d.oncontextmenu = Function.returnFalse;
    }
    if (isFunction(s)) {
        s(t, n, d.type);
    }
    return t;
}.
    inherits(YNH).
    method('addTip', function (s) {
        YNH.eraseTip();
        if (isUndefined(this.tip)) {
            if (s) {
                this.tip = s;
                this.on('mouseover', function (m) {
                    var i, l, t;
                    if (this.tip) {
                        YNH.eraseTip();
                        if (this.getStyle('position') == 'absolute') {
                            l = this.getTrueLeft() + 40;
                            t = this.getTrueTop() + this.getOuterHeight();
                        } else {
                            l = m.x + 32;
                            t = m.y + 8;
                        }
                        i = new YNH.tag('div').
                            setClass(YNH.tipClass).
                            appendText(this.tip).
                            moveTo(l, t).
                            moveInside().
                            show();
                        YNH.tip = i;
                    }
                });
            }
        } else {
            this.tip = s || '';
        }
        return this;
    }).
    method('appendChild', function (d) {
        if (d) {
            if (isString(d)) {
                return this.appendText(d);
            }
            if (d.dom) {
                d = d.dom;
            }
            this.dom.appendChild(d);
        }
        return this;
    }).
    method('appendText', function (t) {
        if (t) {
            this.dom.appendChild(YNH.createText(t));
        }
        return this;
    }).
    method('appendSibling', function (d) {
        if (d) {
            if (isString(d)) {
                d = YNH.createText(d);
            } else {
                if (d.dom) {
                    d = d.dom;
                }
            }
            this.dom.parentNode.appendChild(d);
        }
        return this;
    }).
    method('drag', function (m) {
        var l,
            r = YNH.resize,
            s = YNH.select,
            t,
            x,
            y;
        if (s && YNH.dragging) {
            l = m.x - YNH.dragx;
            t = m.y - YNH.dragy;
            x = s.getLeft() - l;
            y = s.getTop()  - t;
            /******
             * 
             * 
             * 
             * 
             * 
             * 
             * 
             * 
             * 
             * 
             * 
             * 
             * 
             * 
             * 
            //MAX DRAGING TODO ALEX PERSO MODIF
    
            ******/
            if(l>303){
            	l=303;
            }
            if(t>228){
            	t=228;
            }
            switch (YNH.ninth) {
            case 0:
                s.moveTo(l, t);
                break;
            case -4:
                s.moveTo(l, t);
                r(s.getOuterWidth() + x, s.getOuterHeight() + y);
                break;
            case -3:
                s.moveTo(l, s.getTop());
                r(s.getOuterWidth() + x, s.getOuterHeight());
                break;
            case -2:
                s.moveTo(l, s.getTop());
                r(s.getOuterWidth() + x, YNH.dragheight - y);
                break;
            case -1:
                s.moveTo(s.getLeft(), t);
                r(s.getOuterWidth(), s.getOuterHeight() + y);
                break;
            case 1:
                r(s.getOuterWidth(), YNH.dragheight - y);
                break;
            case 2:
                s.moveTo(s.getLeft(), t);
                r(YNH.dragwidth - x, s.getOuterHeight() + y);
                break;
            case 3:
                r(YNH.dragwidth - x, s.getOuterHeight());
                break;
            case 4:
                r(YNH.dragwidth - x, YNH.dragheight - y);
                break;
            }
        }
        s.moveInside();
        return this;
    }).
    method('dropzone', function () {
        YNH.dropzone.push(this);
        return this;
    }).
    method('erase', function () {
        var d = this.dom;
        this.tip = '';
        if (d) {
            d.onmouseover = d.onmouseout = d.onmouseup = d.onmousedown =
                d.oncontextmenu = d.ondrag = d.ondragstart = d.ynh = null;
            this.
                hide().
                later(0, function (d) {
                    if (d && d.parentNode) {
                        d.parentNode.removeChild(d);
                    }
                });
            this.dom = null;
        }
        return this;
    }).
    method('getHeight', function () {
        var d = this.dom, s;
        if (YNH.ie) {
             if (document.compatMode != "CSS1Compat") {
                return d.clientHeight -
                    (parseInt(this.getStyle('paddingTop')    || 0)) -
                    (parseInt(this.getStyle('paddingBottom') || 0));
            }
            return d.style.pixelHeight;
        }
        return parseInt(getComputedStyle(d, null).height);
    }).
    method('getLeft', function () {
        return parseInt(this.getStyle('left'), 10);
    }).
    method('getNinth', function (m, w) {
        var n = 0,
            x = m.x - this.getTrueLeft(),
            y = m.y - this.getTrueTop();
        w = w || 16;
        if (x < w) {
            n -= 3;
        } else if (this.getOuterWidth() - x < w) {
            n += 3;
        }
        if (y < w) {
            n -= 1;
        } else if (this.getOuterHeight() - y < w) {
            n += 1;
        }
        return n;
    }).
    method('getOuterHeight', function () {
        return this.dom.offsetHeight;
    }).
    method('getOuterWidth', function () {
        return this.dom.offsetWidth;
    }).
    method('getStyle', function (name) {
        var s = (global.getComputedStyle ?
            getComputedStyle(this.dom, null) :
            this.dom.currentStyle) || this.dom.style;
        return name ? s[name] : s;
    }).
    method('getTop', function () {
        return parseInt(this.getStyle('top'), 10);
    }).
    method('getTrueLeft', function () {
        var d = this.dom, s, l = 0;
        do {
            s = d.style;
            l += parseInt(s.left) || 0;
            d = d.parentNode;
        } while (d != document.body);
        return l;
    }).
    method('getTrueTop', function () {
        var d = this.dom, s, t = 0;
        do {
            s = d.style;
            t += parseInt(s.top) || 0;
            d = d.parentNode;
        } while (d != document.body);
        return t;
    }).
    method('getVisibility', function () {
         return this.getStyle('visibility') != 'hidden';
    }).
    method('getWidth', function () {
        var d = this.dom;
        if (YNH.ie) {
             if (document.compatMode != "CSS1Compat") {
                return d.clientWidth -
                    (parseInt(this.getStyle('paddingLeft')  || 0)) -
                    (parseInt(this.getStyle('paddingRight') || 0));
            }
            return d.style.pixelWidth;
        }
        return parseInt(getComputedStyle(d, null).width);
    }).
    method('getZIndex', function (zIndex) {
        return +this.getStyle('zIndex');
    }).
    method('hide', function () {
        return this.
            setVisibility();
    }).
    method('moveBy', function (left, top) {
        return this.moveTo(this.getLeft() + (left || 0),
            this.getTop() + (top || 0));
    }).
    method('moveInside', function (left, top, right, bottom) {
        if (arguments.length == 1) {
            var d = left;
            left   = d.getLeft();
            top    = d.getTop();
            right  = left + d.getWidth();
            bottom = top + d.getHeight();
        }
        if (!isNumber(left)) {
            left = document.body.scrollLeft;
        }
        if (!isNumber(right)) {
            right = YNH.width + document.body.scrollLeft;
        }
        if (!isNumber(top)) {
            top = document.body.scrollTop;
        }
        if (!isNumber(bottom)) {
            bottom = YNH.height + document.body.scrollTop;
        }
        var l = Math.max(Math.min(right - this.getWidth(), this.getLeft()),
                left),
            t = Math.max(Math.min(bottom - this.getHeight(), this.getTop()),
                top);
        if (l != this.getLeft() || t  != this.getTop()) {
            this.moveTo(l, t);
        }
        return this;
    }).
    method('moveOutside', function (left, top, right, bottom) {
        if (arguments.length == 1) {
            var j = left;
            left   = j.getLeft();
            top    = j.getTop();
            right  = left + j.getWidth();
            bottom = top  + j.getHeight();
        }
        var l = (this.getLeft() + this.getWidth()) - left,
            t = (this.getTop() + this.getHeight()) - top,
            r = right  - this.getLeft(),
            b = bottom - this.getTop(),
            d = Infinity,
            s = 0;
        if (l < 0 || r < 0 || t < 0 || b < 0) {
            return this;
        }
        if (l < d && l <= this.getLeft()) {
            d = l;
            s = 1;
        }
        if (t < d && t <= this.getTop()) {
            d = t;
            s = 2;
        }
        if (r < d && r < YNH.width - this.getLeft() - this.getWidth()) {
            d = r;
            s = 3;
        }
        if (b < d && b < YNH.height - this.getTop() - this.getHeight()) {
            d = b;
            s = 4;
        }
        switch (s) {
        case 1:
            this.moveBy(-l, 0);
            break;
        case 2:
            this.moveBy(0, -t);
            break;
        case 3:
            this.moveBy(r, 0);
            break;
        case 4:
            this.moveBy(0, b);
            break;
        }
        return this;
    }).
    method('moveTo', function (left, top) {
        if (this.position == 'static' || !this.position) {
            this.setPosition();
        }
        if (YNH.ie) {
            this.dom.style.pixelLeft = left || 0;
            this.dom.style.pixelTop  = top  || 0;
        } else {
            this.dom.style.left = (left || 0) + 'px';
            this.dom.style.top  = (top  || 0) + 'px';
        }
        return this;
    }).
    method('resetSize', function () {
        var s = this.dom.style;
        if (s) {
            s.width = s.height = '';
        }
        return this;
    }).
    method('setClass', function (n) {
        this.dom.className = n;
        return this;
    }).
    method('setClip', function (left, top, right, bottom) {
        if (!isNumber(right)) {
            right = this.getOuterWidth();
        }
        if (!isNumber(bottom)) {
            bottom = this.getOuterHeight();
        }
        this.setStyle('clip', 'rect(' + (top || 0) + 'px ' + right + 'px ' +
            bottom + 'px ' + (left || 0) + 'px)');
        return this;
    }).
    method('setCursor', function (n) {
        return this.setStyle('cursor', n || (YNH.ie ? 'hand' : 'pointer'));
    }).
    method('setFontSize', function (fontSize) {
        if (isNumber(fontSize)) {
            fontSize += 'px';
        }
        return this.setStyle('fontSize', fontSize);
    }).
    method('setHeight', function (h) {
        if (YNH.ie) {
            var s = this.dom.currentStyle || this.dom.style;
            this.dom.style.pixelHeight = document.compatMode != "CSS1Compat" ?
                (parseInt(s.paddingTop)        || 0) +
                (parseInt(s.paddingBottom)     || 0) +
                (parseInt(s.borderTopWidth)    || 0) +
                (parseInt(s.borderBottomWidth) || 0) + h : h;
        } else {
            this.dom.style.height = h + 'px';
        }
        return this;
    }).
    method('setHTML', function (h) {
        var d = this.dom;
        if (!isString(h)) {
            h = stringify(h);
        }
        if (h) {
            d.innerHTML = h;
        } else {
            while (d.firstChild) {
                d.removeChild(d.firstChild);
            }
        }
        return this;
    }).
    method('setOuterHeight', function (h) {
        if (YNH.ie && document.compatMode != "CSS1Compat") {
            this.dom.style.pixelHeight = h;
        } else {
            var s = this.getStyle();
            this.dom.style.height = (h -
               ((parseInt(s.paddingTop)        || 0) +
                (parseInt(s.paddingBottom)     || 0) +
                (parseInt(s.borderTopWidth)    || 0) +
                (parseInt(s.borderBottomWidth) || 0))) + 'px';
        }
        return this;
    }).
    method('setOuterSize', function (w, h) {
        return this.
            setOuterWidth(w).
            setOuterHeight(h).
            setClip(0, 0, w, h);
    }).
    method('setOuterWidth', function (w) {
        if (YNH.ie && document.compatMode != "CSS1Compat") {
            this.dom.style.pixelWidth = w;
        } else {
            var s = this.getStyle();
            this.dom.style.width = (w -
               ((parseInt(s.paddingLeft)      || 0) +
                (parseInt(s.paddingRight)     || 0) +
                (parseInt(s.borderLeftWidth)  || 0) +
                (parseInt(s.borderRightWidth) || 0))) + 'px';
        }
        return this;
    }).
    method('setParent', function (p) {
        if (p && isObject(p.dom)) {
            p.appendChild(this);
            return this;
        }
        if (isString(p)) {
            p = YNH.getElement(p);
        }
        if (!p) {
            p = document.body;
        }
        p.appendChild(this.dom);
        return this;
    }).
    method('setPosition', function (n) {
        this.dom.style.position = n || 'absolute';
        return this;
    }).
    method('setSize', function (w, h) {
        return this.
            setWidth(w).
            setHeight(h).
            setClip();
    }).
    method('setStyle', function (style, value) {
        if (this.dom) {
            this.dom.style[style] = value;
        }
        return this;
    }).
    method('setVisibility', function (v) {
        this.dom.style.visibility = v ? 'inherit' : 'hidden';
        return this;
    }).
    method('setWidth', function (w) {
        if (YNH.ie) {
            var s = this.dom.currentStyle || this.dom.style;
            this.dom.style.pixelWidth = document.compatMode != "CSS1Compat" ?
                (parseInt(s.paddingLeft)      || 0) +
                (parseInt(s.paddingRight)     || 0) +
                (parseInt(s.borderLeftWidth)  || 0) +
                (parseInt(s.borderRightWidth) || 0) + w : w;
        } else {
            this.dom.style.width = w + 'px';
        }
        return this;
    }).
    method('setZIndex', function (z) {
        return this.
            setStyle('zIndex', isNumber(z) ? z : '');
    }).
    method('show', function () {
        return this.
            setVisibility(true);
    }).
    method('slide', function (left, top, width, height) {
        var l = this.getLeft(),
            t = this.getTop(),
            w = this.getWidth(),
            h = this.getHeight(),
            s = YNH.slidespace.length - 1,
            f = Math.floor,
            self = this,
            dl, dt, dw, dh, i, m, n;

        function sub(m, l, t, w, h) {
            self.later(m, function doit() {
                this.
                    moveTo(l, t).
                    setSize(w, h).
                    setClip();
            });
        }

        if (!isNumber(left)) {
            left = l;
        }
        if (!isNumber(top)) {
            top = t;
        }
        if (!isNumber(width)) {
            width = w;
        }
        if (!isNumber(height)) {
            height = h;
        }
        dl = left - l;
        dt = top - t;
        dw = width - w;
        dh = height - h;
        m = 0;
        for (i = 0; i < s; i += 1) {
            n = YNH.slidespace[i];
            sub(m, f(dl * n + l), f(dt * n + t), f(dw * n + w), f(dh * n + h));
            m += YNH.slidetime;
        }
        sub(m, left, top, width, height);
        return this;
    }).
    method('startDrag', function (m, ninth, resize) {
        YNH.eraseEphemeral();
        if (!YNH.specialbutton) {
            YNH.select = this;
            YNH.dragwidth = this.getOuterWidth();
            YNH.dragheight = this.getOuterHeight();
            if (m) {
                YNH.dragx = m.x - this.getLeft();
                YNH.dragy = m.y - this.getTop();
            } else {
                YNH.dragx = Math.floor(YNH.dragwidth  / 2);
                YNH.dragy = Math.floor(YNH.dragheight / 2);
            }
            YNH.dragging = true;
            YNH.ninth = ninth || 0;
            if (ninth) {
                var self = this;
                YNH.resize = function (w, h) {
                    (resize || self.setOuterSize).apply(self, [w, h]);
                    return self;
                };
            }
        }
        return this;
    }).
    method('stopDrag', function (m) {
        YNH.dragging = false;
        return this;
    });

YNH.setEphemeral = function (o) {
    if (o != YNH.ephemeral) {
        if (o) {
            YNH.eraseEphemeral();
        }
        YNH.ephemeral = o;
    }
};

YNH.eraseEphemeral = function (r) {
    YNH.eraseTip();
    var e = YNH.ephemeral;
    if (e) {
        YNH.ephemeral = null;
        e.erase(r);
    }
};

YNH.eraseTip = function () {
    var t = YNH.tip;
    if (t) {
        YNH.tip = null;
        t.erase();
    }
};

YNH.event = function (e) {
    this.event = e = e || event || {};
    this.type = e.type;
    this.charCode = e.charCode || e.keyCode || e.which;
    this.target = e.target || e.srcElement;
    this.tagName = ((this.target && this.target.tagName) || '').toLowerCase();
    this.ctrlKey = e.button > 1 || e.ctrlKey;
    this.shiftKey = e.shiftKey;
    this.x = e.pageX || (e.clientX + (document.body.scrollLeft || 0));
    this.y = e.pageY || (e.clientY + (document.body.scrollTop  || 0));
}.
    method('done', function (p) {
        var e = this.event;
        e.cancelBubble = true;
        if (e.stopPropagation) {
            e.stopPropagation();
        }
        if (p) {
            e.returnValue = false;
            if (e.preventDefault) {
                e.preventDefault();
            }
        }
    });

YNH.getElement = function (id) {
    if (document.all) {
        return document.all[id];
    }
    return document.getElementById(id);
};
/*
    YNH.tag creates a dom object and returns its YNH wrapper.
*/
YNH.tag = function (tagName, type, name, id) {
    return YNH.dom(YNH.createElement(tagName, type, name, id)).
        setParent();
};

YNH.tag.button = YNH.tag.input = YNH.tag.textarea = function (t, n, y) {
    var d = t.dom;

    function handler(e) {
        t.fire(new YNH.event(e));
    }

    function keypresshandler(e) {
        var m = new YNH.event(e);
        if (m.charCode == 13) {
            m.type = 'enterkey';
        } else if (m.charCode == 27) {
            m.type = 'escapekey';
        }
        t.fire(m);
    }

    if (d.addEventListener) {
        d.addEventListener('blur',   handler, false);
        d.addEventListener('change', handler, false);
        d.addEventListener('focus',  handler, false);
        d.addEventListener('select', handler, false);
        d.addEventListener('keypress', keypresshandler, false);
    } else {
        d.onblur = d.onchange = d.onfocus = d.onselect = handler;
        d.onkeypress = keypresshandler;
    }
    t.blur = function () {
        d.blur();
        return t;
    };
    t.focus = function () {
        if (t.getVisibility()) {
            d.focus();
        }
        return t;
    };
    t.getValue = function () {
        return d.value;
    };
    t.select = function () {
        if (t.getVisibility()) {
            d.focus();
            d.select();
        }
        return t;
    };
    t.setValue = function (s) {
        d.value = s || '';
        return t;
    };
    if (n == 'button') {
        t.on('enterkey', 'fire', 'click');
    } else if (n == 'input') {
        switch (y) {
        case 'checkbox':
        case 'radio':
            t.on('enterkey', 'fire', 'click');
            t.getCheck = function () {
                return d.checked;
            };
            t.setCheck = function (b) {
                d.checked = !!b;
                return t;
            };
            break;
        case 'button':
        case 'submit':
        case 'reset':
            t.on('enterkey', 'fire', 'click');
            t.setLabel = function (s) {
                t.value = s;
                return t;
            };
        }
    }
};

YNH.tag.fieldset = function (t) {
    var d = t.dom, l = d.getElementsByTagName('legend');
    if (l && l[0]) {
        l = l[0];
    } else {
        l = document.createElement('legend');
        d.insertBefore(l, d.firstChild);
    }
    t.setLabel = function (s) {
        l.innerHTML = s;
        return this;
    };
};

YNH.tag.form = function (t) {
    var d = t.dom;

    function handler(e) {
        t.fire(new YNH.event(e));
    }

    if (d.addEventListener) {
        d.addEventListener('reset',  handler, false);
        d.addEventListener('submit', handler, false);
    } else {
        d.onreset = d.onsubmit = handler;
    }
    t.reset = function () {
        d.reset();
        return t;
    };
    t.submit = function () {
        d.submit();
        return t;
    };
};

YNH.tag.img = function (t, n) {
    var d = t.dom;
    d.onload = function (e) {
        var m = new YNH.event(e);
        t.fire(m);
        m.done();
    };
    t.setAlign = function (a) {
        d.align = a;
        return t;
    };
    t.setSrc = function (url) {
        d.src = url.supplant();
        return t;
    };
};

YNH.tag.table = function (t) {
    t.getBody = function () {
        var b = t.dom.tBodies;
        if (b && b.length) {
            b = b[b.length - 1];
            return b.ynh || YNH.dom(b);
        }
        return YNH.tag('tbody').
            setParent(t);
    };
    t.appendChild = function (d) {
        var b, n;
        if (!isArray(d) && isObject(d)) {
            if (d.dom) {
                d = d.dom;
            }
            n = d.tagName.toLowerCase();
            if (n == 'tbody' || n == 'thead' || n == 'tfoot' ||
                    n == 'caption' || n == 'col' || n == 'colgroup') {
                t.dom.appendChild(d);
                return this;
            }
        }
        b = t.getBody();
        b.appendChild.apply(b, [d]);
        return t;
    };
};

YNH.tag.tbody = YNH.tag.tfoot = YNH.tag.thead = function (t) {
    t.appendChild = function (d) {
        var di, i, n, r;
        if (!isArray(d)) {
            if (isObject(d)) {
                if (d.dom) {
                    d = d.dom;
                }
                n = d.tagName.toLowerCase();
                if (n == 'tr') {
                    t.dom.appendChild(d);
                    return t;
                }
            }
            d = [d];
        }
        r = document.createElement('tr');
        for (i = 0; i < d.length; i += 1) {
            di = d[i];
            n = '';
            if (isObject(di)) {
                if (di.dom) {
                    di = di.dom;
                }
                n = di.tagName.toLowerCase();
            } else {
                di = YNH.createText(di);
            }
            if (n != 'td' && n != 'th') {
                n = di;
                di = document.createElement('td');
                di.appendChild(n);
            }
            r.appendChild(di);
        }
        t.dom.appendChild(r);
        return t;
    };
};

YNH.tag.tr = function (t) {
    t.setParent = function (p) {
        if (isString(p)) {
            parent = YNH.getElement(p);
        } else if (p && isObject(p.dom)) {
            p = p.dom;
        }
        if (!p) {
            p = document.body;
        }
        if (p.tagName.toLowerCase() == 'table') {
            p = p.getBody();
        }
        p.appendChild(t.dom);
        return t;
    };

    t.appendChild = function (d) {
        var n = '';
        if (isObject(d)) {
            if (d.dom) {
                d = d.dom;
            }
            n = d.tagName.toLowerCase();
        } else {
            d = YNH.createText(d);
        }
        if (n != 'td' && n != 'th') {
            n = d;
            d = document.createElement('td');
            d.appendChild(n);
        }
        t.dom.appendChild(d);
        return t;
    };
};

YNH.tag.td = YNH.tag.th = function (t) {
    t.setColSpan = function (s) {
        t.dom.colSpan = s;
        return t;
    };
    t.setRowSpan = function (s) {
        t.dom.rowSpan = s;
        return t;
    };
};

YNH.tipClass = 'tip';


YNH.method('later', YNH.ie && YNH.version < 5.5 ? function (msec, m) {
    var a = YNH.slice(arguments, 2),
        s = '______later' + (++YNH.seq),
        self = this;
    if (!isFunction(m)) {
        m = this[m];
    }
    YNH[s] = function () {
        m.apply(self, a);
        delete YNH[s];
    };
    setTimeout('YNH[' + s.quote() + ']();', msec);
    return this;
} : function (msec, m, p) {
    var self = this;
    setTimeout(function () {
        var f = isString(m) ? self[m] : m;
        if (p === undefined) {
            f.apply(self);
        } else {
            f.apply(self, isArray(p) ? p : [p]);
        }
    }, msec);
    return this;
});


global.onresize = function (e) {
    var db = document.body;
    YNH.width  = db.clientWidth  || window.innerWidth ||
        screen.availWidth - screenLeft;
    YNH.height = db.clientHeight || window.innerHeight ||
        screen.availHeight - screenTop;
    YNH.body.fire('resize');
};

global['onload'] = function (e) {
    onload = null;
    onresize();
    document.onmousemove = function (e) {
        if (YNH.select && YNH.dragging) {
            var a, i, m = new YNH.event(e), o, x, y;
            YNH.select.fire(m);
            m.done(true);
            a = YNH.dropzone;
            i = a.length;
            while (i) {
                i -= 1;
                o = a[i];
                if (o != YNH.select) {
                    if (o.dom) {
                        x = m.x - o.getTrueLeft();
                        y = m.y - o.getTrueTop();
                        if (x >= 0 && x <= o.getWidth() && y >= 0 &&
                                y <= o.getHeight()) {
                            if (YNH.dropselect) {
                                if (YNH.dropselect != o) {
                                    m.type = 'dragout';
                                    m.subject = YNH.select;
                                    YNH.dropselect.fire(m);
                                    m.type = 'dragover';
                                    YNH.dropselect = o;
                                    o.fire(m);
                                }
                                return false;
                            } else {
                                m.type = 'dragover';
                                m.subject = YNH.select;
                                YNH.dropselect = o;
                                o.fire(m);
                                return false;
                            }
                        }
                    } else {
                        a.splice(i, 1);
                    }
                }
            }
            if (YNH.dropselect) {
                m.type = 'dragout';
                m.subject = YNH.select;
                YNH.dropselect.fire(m);
                YNH.dropselect = null;
            }
            return false;
        }
    };

    document.onmouseup = function (e) {
        var m = new YNH.event(e),
            s = YNH.select,
            v;
        YNH.select = null;
        YNH.dragging = false;
        YNH.eraseEphemeral();
        if (s) {
            v = s.mousingover;
            if (YNH.specialbutton && v) {
                m.type = 'specialclick';
                s.fire(m);
            } else {
                s.fire(m);
                s.later(0, function () {
                    if (v) {
                        m.type = 'click';
                        s.fire(m);
                        if (s.dom) {
                            m.type = 'mouseover';
                            s.fire(m);
                        }
                    }
                });
            }
        }
        m.done(YNH.specialbutton);
        if (YNH.dropselect) {
            m.type = 'dragout';
            m.subject = s;
            YNH.dropselect.fire(m);
            m.type = 'drop';
            YNH.dropselect.fire(m);
            YNH.dropselect = null;
        }
    };

    YNH.body.fire('load');
    global['on' + (YNH.ie ? 'before' : '') + 'unload'] = function (e) {
        YNH.body.fire('unload');
    };
};

YNH.body = new YNH.dom(document.body);
