﻿function getStyle(el) {
    var s;
    if (el.currentStyle)
        s = el.currentStyle;
    else {
        s = document.defaultView.getComputedStyle(el, null);
    }
    return s;
}
function getLeftBorderWidth(el) {
    var res = parseInt(getStyle(el).borderLeftWidth, 10);
    return isNaN(res) ? 0 : res;
}
function getTopBorderWidth(el) {
    var res = parseInt(getStyle(el).borderTopWidth, 10);
    return isNaN(res) ? 0 : res;
}
function getRightBorderWidth(el) {
    var res = parseInt(getStyle(el).borderRightWidth, 10);
    return isNaN(res) ? 0 : res;
}
function getBottomBorderWidth(el) {
    var res = parseInt(getStyle(el).borderBottomWidth, 10);
    return isNaN(res) ? 0 : res;
}
function getAbsolutePosition(el) {
    var pos = new Object();
    pos.left = 0;
    pos.top = 0;
    do {
        pos.left += el.offsetLeft;
        pos.top += el.offsetTop;
        el = el.offsetParent
    } while (el != null);
    return pos;
}
function getPosition(el) {
    var pos = new Object();
    pos.left = 0 + getLeftBorderWidth(el);
    pos.top = 0 + getTopBorderWidth(el);
    do {
        pos.left += el.offsetLeft;
        pos.top += el.offsetTop;
        el = el.offsetParent
    } while (el != null);
    if (window.pageYOffset) {
        pos.left -= window.pageXOffset;
        pos.top -= window.pageYOffset;
    }
    else if (document.documentElement) {
        pos.left -= document.documentElement.scrollLeft;
        pos.top -= document.documentElement.scrollTop;
    }
    return pos;
}
function getPageOffset() {
    var offset = new Object();
    if (window.pageYOffset) {
        offset.left = window.pageXOffset;
        offset.top = window.pageYOffset;
    }
    else if (document.documentElement) {
        offset.left = document.documentElement.scrollLeft;
        offset.top = document.documentElement.scrollTop;
    }
    return offset;
}
function isLeftButton(e) {
    var result = true;
    if (navigator.appName == "Microsoft Internet Explorer") {
        if (e.button != 1) {
            result = false;
        }
    }
    else if (navigator.appName == "Netscape") {
        if (e.button != 0) {
            result = false;
        }
    }
    return result;
}
function setOpacity(val, elem) {
    if (elem.style.opacity == undefined) {
        for (var i = 1; i < arguments.length; i++) {
            arguments[i].style.filter = 'alpha(opacity = ' + val + ')';
        }
    }
    else {
        for (var j = 1; j < arguments.length; j++) {
            arguments[j].style.opacity = val / 100;
        }
    }
}
function contains(node1, node2) {
	if (node1.contains)
        return node1.contains(node2);
    else
        return node1.compareDocumentPosition(node2) & Node.DOCUMENT_POSITION_CONTAINED_BY;
}

function ImageButton(container, width, height) {
    var def_URL = null;
    var hl_URL = null;
    var d_URL = null;
    var p_URL = null;
    var pd_URL = null;
    var enabled = true;
    var pushed = false;
    var isHL = false;
    var clickHandler = null;
    var img = document.createElement('input');
    img.style.width = width;
    img.style.height = height;
    img.type = 'image';
    container.appendChild(img);

    this.setURL = function(defURL, hlURL, pURL, dURL, pdURL) {
        def_URL = defURL;
        hl_URL = hlURL;
        p_URL = pURL;
        d_URL = dURL;
        pd_URL = pdURL;
    }
    this.setToolTip = function(toolTip) {
        img.alt = toolTip;
        img.title = toolTip;
    }
    this.setId = function(id) {
        img.id = id;
    }
    this.onClick = function(handler) {
        clickHandler = handler;
    }

    this.enable = function(state) {
        enabled = state;
        setImage();
    }
    this.push = function(state) {
        pushed = state;
        setImage();
    }

    this.getDOM = function() {
        return img;
    }

    var setImage = function() {
        if (pushed && enabled) {
            img.src = p_URL;
            img.style.cursor = "pointer";
        }
        else if (!pushed && enabled) {
            img.src = isHL ? hl_URL : def_URL;
            img.style.cursor = "pointer";
        }
        else if (pushed && !enabled) {
            img.src = pd_URL;
            img.style.cursor = "default";
        }
        else if (!pushed && !enabled) {
            img.src = d_URL;
            img.style.cursor = "default";
        }
    }

    var mouseOver = function(e) {
        if (enabled && !pushed)
            img.src = hl_URL;
        isHL = true;
    }
    var mouseOut = function(e) {
        if (enabled && !pushed)
            img.src = def_URL;
        isHL = false;
    }
    var mouseDown = function(e) {
        if (enabled && !pushed)
            img.src = p_URL;
    }
    var mouseUp = function(e) {
        if (enabled && !pushed) {
            img.src = isHL ? hl_URL : def_URL;
        }
    }
    var click = function(e) {
        if (enabled && clickHandler)
            clickHandler();
    }
    img.onmouseover = mouseOver;
    img.onmouseout = mouseOut;
    img.onmousedown = mouseDown;
    img.onmouseup = mouseUp;
    img.onclick = click;
}

function TextItem(text) {
    var index = 0;
    var clickHandler = null;
    var defStyle;
    var hlStyle;
    var item = document.createElement('div');
    item.innerHTML = text;
    item.className = defStyle;

    var mouseOver = function() {
        item.className = hlStyle;
    }
    var mouseOut = function() {
        item.className = defStyle;
    }
    var click = function(e) {
        if (clickHandler) {
            item.className = defStyle;
            clickHandler(index);
        }
    }

    item.onmouseover = mouseOver;
    item.onmouseout = mouseOut;
    item.onclick = click;

    this.text = text;

    this.onClick = function(handler) {
        clickHandler = handler;
    }
    this.setIndex = function(i) {
        index = i;
    }
    this.getIndex = function() {
        return index;
    }

    this.setDefStyle = function(cssName) {
        defStyle = cssName;
        item.className = cssName;
    }
    this.setHLStyle = function(cssName) {
        hlStyle = cssName;
    }
    this.highLight = function(state) {
        if (state)
            item.className = hlStyle;
        else
            item.className = defStyle;
    }

    this.getDOM = function() {
        return item;
    }
}

function DropDown() {
    var isShow = false;
    var list = document.createElement('div');

    list.style.display = 'block';
    list.style.position = 'absolute';
    list.style.overflow = 'auto';

    list.style.borderStyle = 'solid';
    list.style.borderColor = '#a378be';
    list.style.borderWidth = '1px';
    list.style.backgroundColor = 'white';

    var firstClick;
    var prevHandler;
    var bodyClick = function(event) {
        if (!event)
            event = window.event;
        if (event.srcElement.parentNode == list)
            return;
        if (!firstClick) {
            hide();
        }
        firstClick = false;
    }
    var hide = function() {
        if (isShow) {
            document.body.removeChild(list);
            isShow = false;
            document.body.onclick = prevHandler;
        }
    }

    this.isShow = function() {
        return isShow;
    }
    this.show = function(x, y, width, height) {
        list.style.width = width + 'px';
        list.style.height = height + 'px';
        list.style.left = x + 'px';
        list.style.top = y + 'px';
        if (!isShow) {
            document.body.appendChild(list);
            prevHandler = document.body.onclick;
            document.body.onclick = bodyClick;
            isShow = true;
            firstClick = true;
        }
    }
    this.hide = function() {
        hide();
    }
    this.content = function() {
        return list;
    }
}

function ComboBox(text, btn) {
    var items = new Array();
    var selectHandler = null;
    var textChangedHandler = null;
    var selIndex = -1;

	var dropDown = new DropDown();

    var nodeClick = function(i) {
        selIndex = i;
        text.value = items[i].text;
        dropDown.hide();
        if (selectHandler)
            selectHandler(text.value);
    }
    var downClick = function() {
        if (dropDown.isShow())
            dropDown.hide();
        else {
            var pos = getAbsolutePosition(text);
            pos.top += text.offsetHeight
            dropDown.show(pos.left, pos.top, text.offsetWidth, 100);
        }
    }
    var textKeyPress = function(event) {
        if (!event)
            event = window.event;
        if (event.keyCode == 13) {
            if (textChangedHandler)
                textChangedHandler(text.value);
        }
    }

    text.onkeyup = textKeyPress;
	btn.onclick = downClick;

    this.getItem = function(index) {
        return items[index];
    }
    this.count = function() {
        return items.length;
    }
    this.add = function(item) {
        item.onClick(nodeClick);
        item.setIndex(items.length);
        item.setDefStyle('comboBoxDefItem');
        item.setHLStyle('comboBoxHLItem');
        items.push(item);
        dropDown.content().appendChild(item.getDOM());
    }
    this.clear = function() {
        items = new Array();
        dropDown.content().innerHTML = '';
    }
    this.getSelected = function() {
        return items[selIndex];
    }
    this.setText = function(t) {
        text.value = t;
    }
    this.onSelect = function(handler) {
        selectHandler = handler;
    }
    this.onTextChanged = function(handler) {
        textChangedHandler = handler;
    }
}
