function findPos(obj) {
    log("find pos");
    var left=0;
	var top=0;
    while (obj) {

        log("FP "+obj+" "+obj.offsetLeft+" "+obj.offsetTop);
        left += obj.offsetLeft;
		top += obj.offsetTop;
		obj = obj.offsetParent;
	}
    log("->"+left+" "+top);
    return [left,top];
}

//Thanks to quirksmode for this code:
function findMousePos(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}
//	document.getElementById('monitor').appendChild(document.createTextNode(posx+','+posy));
//	document.getElementById('monitor').appendChild(document.createElement("br");

    return [posx,posy];
}



function showBelow(aboveEl,belowEl) {
    return showPopup(aboveEl,belowEl,null,false);
}

function showLeft(aboveEl,belowEl) {
    return showPopup(aboveEl,belowEl,true,false);
}

function showPopup(anchor,popup,left,above) {
    var position = findPos(anchor);
    var containerPos=findPos(popup.offsetParent);


    
    var clientWidth=Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);
    var clientHeight=Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);

    if (left==null) {
        log("auto left "+position[0]+" "+clientWidth+" "+popup.offsetWidth);
        position[0]=Math.max(0,Math.min(position[0],clientWidth-popup.offsetWidth));
    } else if (left) {
        position[0]=position[0]+anchor.offsetWidth-popup.offsetWidth;
    }


    if (above==null) {

        above=(popup.offsetHeight+anchor.offsetHeight+position[1])>clientHeight;
        if (position[1]-popup.offsetHeight<0) above=false;
        log("auto above "+popup.offsetHeight+" "+anchor.offsetHeight+" "+position[1]+" "+clientHeight+" "+(popup.offsetHeight+anchor.offsetHeight+position[1])+" "+above);
    }

    if (above)
        position[1]=position[1]-popup.offsetHeight;
    else
        position[1]=position[1]+anchor.offsetHeight;

    popup.style.left=(position[0]-containerPos[0])+"px";
    popup.style.top=(position[1]-containerPos[1])+"px";

    log("popup "+popup.style.left+" "+popup.style.top)

    popup.style.visibility='visible';

    var ifs=popup.getElementsByTagName('iframe');

    for (var i=0;i<ifs.length;i++) {
            if (ifs[i].getAttribute("xsrc")!=null && (ifs[i].getAttribute("src")==null || ifs[i].getAttribute("src")==""))
                ifs[i].setAttribute("src",ifs[i].getAttribute("xsrc"));
    }

    return popup;
}

function showAbove(aboveEl,belowEl) {
    return showPopup(aboveEl,belowEl,false,true);
}


var mouseoverPopupElement,clickPopupElement;

function storeMouseover(elt) {
    if (mouseoverPopupElement!=elt) hidePopup(mouseoverPopupElement);
    mouseoverPopupElement=elt;
    return false;
}

var hideTimeout;

function storeClick(elt) {
    log("storeClick "+elt);
    if (clickPopupElement!=elt) hidePopup(clickPopupElement);
    clickPopupElement=elt;
    if (hideTimeout) {window.clearTimeout(hideTimeout);hideTimeout=null;}
    return false;
}

function elt(belowID) {
    var belowEl=document.getElementById(belowID);
    return belowEl;
}

function autopopup(aboveEl,event) {
    var belowEl = findpopup(aboveEl);
    if (belowEl==null) return;
    showPopup(aboveEl,belowEl,null,null);
    cancelEvent(event);

    return belowEl;
}

function findpopup(aboveEl) {
    var el=aboveEl;
    while (true) {
        if (el.nextSibling==null) el=el.parentNode;
        else el=el.nextSibling;        
        if (el==null) return null;
        if (el.tagName && el.tagName.toUpperCase()=='DIV' && el.className=='popup') return el;
    }    
}

function popup(aboveEl,belowID,event) {
    if (!document.getElementById) return;
    var belowEl = document.getElementById(belowID);
    showBelow(aboveEl,belowEl);
    cancelEvent(event);
    return belowEl;
}

function popupClan(aboveID,belowID,event) {
    var aboveEl = document.getElementById(aboveID);
    var belowEl = document.getElementById(belowID);
    showBelow(aboveEl,belowEl);
    cancelEvent(event);
    return belowEl;
}

function popupWidth(aboveID,belowID,event) {
    var aboveEl = document.getElementById(aboveID);
    var belowEl = document.getElementById(belowID);
    belowEl.style.width=(aboveEl.offsetWidth)+"px";
    showBelow(aboveEl,belowEl);

    return belowEl;
}


function popupOffset(aboveID,belowID,event,x,y) {
    var aboveEl = document.getElementById(aboveID);
    var belowEl = document.getElementById(belowID);
    var position = findPos(aboveEl);

    var containerPos=findPos(belowEl.offsetParent);

    position[0]=position[0]-containerPos[0];
    position[1]=position[1]-containerPos[1];


    belowEl.style.left=x+"px";
    belowEl.style.top=position[1]+y+"px";
    cancelEvent(event);
    belowEl.style.visibility='visible';
    return belowEl;
}

function hide(elt) {
    log("hide "+elt);
    elt.style.visibility='hidden';
}
function cancelEvent(evt) {
    if (!evt) evt=window.event;
    unpropagate(evt);
    if (evt.preventDefault) evt.preventDefault();
}

function unpropagate(evt) {
    if (!evt) evt=window.event;
    if (evt.stopPropagation) evt.stopPropagation(); else evt.cancelBubble = true;    
}

function hidePopup(elt) {
    if (elt!=null) hide(elt);
}
function setselect(selectName,value,labelName,inputName,labelValue) {
    if (document.getElementById(inputName))
        document.getElementById(inputName).name=value;
    if (document.getElementById(selectName))
        document.getElementById(selectName).value=value;
    document.getElementById(labelName).value=labelValue;
    document.getElementById(labelName).focus();
    log("setselect");
    storeClick(null);

    return false;
}



function delayedHideClick() {
    hideTimeout=window.setTimeout('hideNOW()',200);
}


function hideNOW() {
    log("hideNOW");
    storeClick(null);
}

var consoleDocument,consoleElement;

function log(text) {
    if (consoleDocument==null) return;
    consoleElement.appendChild(consoleDocument.createTextNode(text));
    consoleElement.appendChild(consoleDocument.createElement("br"));
}

function clearLog() {
    if (consoleDocument==null) {
        consoleDocument=document;
        consoleElement=document.getElementById('console');
        consoleElement.style.display='block';
    }
    while (consoleElement.hasChildNodes()) {consoleElement.removeChild(consoleElement.firstChild);}
    log("Logging");
}

function removeSelect(form) {
    var s=form.getElementsByTagName("select");
    for (var i=0;i<s.length;i++) {
        var inp=document.getElementById("input_"+s[i].id);
        if (inp!=null) {
            s[i].disabled="disabled";
            inp.name=s[i].options[s[i].selectedIndex].value;
        }
    }
    var inp=form.getElementsByTagName("input");
    for (var i=0;i<inp.length;i++) {
        if (inp[i].value=='') inp[i].disabled="disabled";
        if (inp[i].name=='width') inp[i].value=self.screen.availWidth-400;
    }

}

function idclick(event,jumpto) {
    var targetElement = event.target;
    if (!targetElement) targetElement=event.srcElement;
    window.location=jumpto+targetElement.innerHTML;
}
