/* general event registration */
function registerEvent(target, evt, callBack) {
    if (target.addEventListener) {
        target.addEventListener( evt, callBack, false );
    } else
    if (target.attachEvent) {
        target.attachEvent( 'on' + evt, callBack );
    } else
    if (target.onload) {
        var oldLoad = target.onload;
        target.onload = function ( e ) { oldLoad(e); callBack(); }
    } else {
        target.onload = callBack;
    }
}
/* find a DOM element by css class */
function findDomElByClass(obj, cssClass) {
    if (!obj || ! cssClass)
        return null;
    if (obj.className == cssClass)
        return obj;
    for ( var x=0; x<obj.childNodes.length; x++ ) {
        var tmp = findDomElByClass( obj.childNodes[x], cssClass );
        if ( tmp != null )
            return tmp;
    }
    return null;
}
function maximize(obj){
    PositionUtils.maximize(obj);
}
function getBestRelativePosition(objRel, objThe, offset){
    var posRel = PositionUtils.getObjectPosition(objRel);
    var posAbs = PositionUtils.getObjectPosition(objRel,true);
    var sizeVP = PositionUtils.getViewportSize();
    //var sizeDoc = PositionUtils.getDocumentSize();
    var posScroll = PositionUtils.getScrollingPosition();
    var x=0,y=0;
    // try below the relative obj as default
    if ((posAbs.y + objRel.offsetHeight + offset + objThe.offsetHeight) > (posScroll[1] + sizeVP[1])) {
        // move to top
        y = posRel.y - objThe.offsetHeight - offset;
    } else {
        y = posRel.y + objRel.offsetHeight + offset;
    }
    if (posAbs.x + objThe.offsetWidth > posScroll[0] + sizeVP[0]) {
        // move to left
        x = posRel.x + objRel.offsetWidth - objThe.offsetWidth;
    } else {
        x = posRel.x;
    }
    return {x:x,y:y};
}
/*
  Return the key code value of the current event
  Parameters:
   event - *DOMEvent* the event generated by the browser
  Returns:
   *int* Return the key code value of the current event
*/
function getKeyCode(event) {
    return event.keyCode || event.which;
}
/* Stop an event from propagating and triggering the default action */
function killEvent(e) {
    if (!e.stopPropagation) {
        e.stopPropagation = function() {this.cancelBubble = true;};
        e.preventDefault = function() {this.returnValue = false;};
    }
    e.stopPropagation();
    e.preventDefault();
}
/*
    Get a css property for a DOM object
    prop1 - the JS like naming (backgroundImage) 
    prop2 - the CSS like naming (background-image) 
*/
function getStyleProp(obj, prop1, prop2) {
	if (obj.currentStyle)
		return obj.currentStyle[prop1];
	if (window.getComputedStyle)
		return document.defaultView.getComputedStyle(obj,null).getPropertyValue(prop2);
	return obj.style[prop1];
}
/*
    Get the CSS rules for a anchor element
    references to the rules are saved in the object for further use
*/
function saveCSSRules(obj) {
    var selID = '#' + obj.id;
    var selIDHover = '#' + obj.id + ':hover';
    for (var s=0; s<document.styleSheets.length; s++) {
        var theRules = new Array();
        if (document.styleSheets[s].cssRules)
            theRules = document.styleSheets[s].cssRules;
        else if (document.styleSheets[s].rules)
            theRules = document.styleSheets[s].rules;
        for (var x=0; x<theRules.length; x++) {
            if ( theRules[x].selectorText == selID ) {
                obj.cssRuleDefault = theRules[x].style.cssText;
            }
            if ( theRules[x].selectorText == selIDHover ) {
                obj.cssRuleHover = theRules[x].style.cssText;
            }
        }
    }
}
/*
    Change the CSS class in a container to one of the childs
*/
function selectFromChilds(obj,className) {
    if (!obj || !obj.parentNode) return;
    var parent = obj.parentNode;
    // reset the previous selected
    for (var x=0; x<parent.childNodes.length; x++) {
        if (parent.childNodes[x].nodeType == 1 && parent.childNodes[x].className == className){
            parent.childNodes[x].className = '';
        }
    }
    // select the current
    obj.className = className;
}
/*
    Find the selected RADIO INPUT
    and return it's value
*/
function getSelectedRadio(parentId) {
    var parent = document.getElementById(parentId);
    return findSelectedRadio(parent);
}
function findSelectedRadio(parent) {
    if (!parent || !parent.hasChildNodes()) return null;
    for (var x=0; x<parent.childNodes.length; x++) {
        var child = parent.childNodes[x];
        if ( child.nodeType == 1 && child.nodeName == 'INPUT' ) {
            try {
                if (child.type.toUpperCase() == 'RADIO' &&
                    child.checked
                        ) {
                    return child.value;
                }
            } catch(ex) {alert(ex);}
        }
        // recursively check children
        if (child.hasChildNodes() && (child = findSelectedRadio(child)) != null)
            return child;
    }
    return null;
}
/*

*/
var buttonStack = [];
function disableButtonTemp(obj) {
    obj.onclick = function(){return false;};
    buttonStack.push( obj );
    setTimeout( 'reenableButton(' + (buttonStack.length-1) + ')', 10000 );
    return true;
}
function reenableButton(ix) {
    if (buttonStack[ix]) {
        buttonStack[ix].onclick = function(){return disableButtonTemp(this);};
        buttonStack[ix] = null;
    }
}
function applyStyleToInnerElements(/*DOM object*/parent, /*String*/tagName, /*String*/ styleProp, /*String*/ styleValue) {
    var els = parent.getElementsByTagName(tagName);
    for (var x=0; x<els.length; x++) {
        // skip elements with no display
        if (els[x].type && els[x].type == 'hidden') {
            continue;
        }
        els[x].style[styleProp] = styleValue;
    }
}