/****************************************************************
* file: 	tcn.js
* author:	toy
* date:		20jun09
* re:		general js utils
*
* rev history
* [toy 20jun09]		orig version
*****************************************************************/

function $() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}

// DO NOT ERASE OR DELETE!!!!
function displayPopupDivORIG(e) {

    var posx = 0;
    var posy = 0;
    if (!e) { var e = window.event };

    // determine target DIV
    var targ = e.target ? e.target : e.srcElement;

    // calculate mouse coordinates
    if (e.pageX || e.pageY) {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        posx = e.clientX;
        posy = e.clientY;
        // check for scroll offsets in IE 6
        if (document.documentElement.scrollLeft || document.documentElement.scrollTop) {
            posx += document.documentElement.scrollLeft;
            posy += document.documentElement.scrollTop;
        }
    }

    // assign attributes to pop-up DIV element and append it to web document tree
    var div = document.getElementById('popup');
    if (!div) {
        var div = document.createElement('div');
        div.setAttribute('id', 'popup');
        div.className = 'popupdiv';
        div.appendChild(document.createTextNode('This is a sample content for pop-up DIV element.'));
        document.getElementsByTagName('body')[0].appendChild(div);
    }
    // move pop-up DIV element
    div.style.top = posy + 5 + 'px';
    div.style.left = posx + 5 + 'px';
}

function displayPopupDiv(e) {

    var posx = 0;
    var posy = 0;
    if (!e) { var e = window.event };

    // determine target DIV
    var targ = e.target ? e.target : e.srcElement;

    // calculate mouse coordinates
    if (e.pageX || e.pageY) {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        posx = e.clientX;
        posy = e.clientY;
        // check for scroll offsets in IE 6
        if (document.documentElement.scrollLeft || document.documentElement.scrollTop) {
            posx += document.documentElement.scrollLeft;
            posy += document.documentElement.scrollTop;
        }
    }

    // assign attributes to pop-up DIV element and append it to web document tree
    var div = document.getElementById('popup');
    if (!div) {
        var div = document.createElement('div');
        div.setAttribute('id', 'popup');
        div.className = 'popupdiv';
        div.appendChild(document.createTextNode('This is a sample content for pop-up DIV element.'));
        document.getElementsByTagName('body')[0].appendChild(div);
    }
    // move pop-up DIV element
    div.style.top = posy + 5 + 'px';
    div.style.left = posx + 5 + 'px';
}

// remove pop-up DIV element
function hidePopupDiv() {
    var div = document.getElementById('popup');
    if (!div) { return };
    div.parentNode.removeChild(div);
}

// activate pop-up DIV elements
function activatePopupDivs() {
    var divs = document.getElementsByTagName('div');
    if (!divs) { return };
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].className == 'thumbnailI') {
            // display pop-up DIV element
            divs[i].onclick = displayPopupDiv;
            
            // hide pop-up DIV element
            divs[i].onmouseout = hidePopupDiv;
        }
    }
}

// activate pop-up DIV elements when web page has been loaded
/*
window.onload = function() {
    if (document.getElementById && document.createElement && document.createTextNode) {
        activatePopupDivs();
    }
}
*/


function findPos(obj) {
    var curleft = curtop = 0;

    // check if browser supports offsetParent
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}


