var notifierMessages=Array();
var notifierOpacities=Array();
var notifierFading=false;
var notifierRemoving=false;

var messageDuration=2000;

function addMessage(message,classname) {
    var o=document.getElementById('messageNotifierDiv');
    notifierMessages[notifierMessages.length]=10;
    notifierOpacities[notifierOpacities.length]=100;

    var newDiv=document.createElement('div');
    newDiv.className=classname;
    newDiv.innerHTML=message;
    newDiv.style.height='20px';
    o.appendChild(newDiv);
    if(o.childNodes.length==1)
        setTimeout('fadeoutMessages()',messageDuration);
}


/*
function checkMessages() {
    var o=document.getElementById('messageNotifierDiv');
    if(notifierMessages.length>0) {
        var x=0;
    //for(var x=notifierMessages.length-1;x>=0;x--) {
        if(notifierMessages[x]>0) notifierMessages[x]--;
        else {
            if(!notifierFading)
                fadeoutMessages();
//            alert('removing '+x);
           // o.removeChild(o.childNodes[x]);
            //notifierMessages.splice(x,1);
            //notifierOpacities.splice(x,1);
        }
    }
    if(notifierMessages.length>0)
        setTimeout('checkMessages()',500);
}

*/

function fadeoutMessages() {
    notifierFading=true;
    var o=document.getElementById('messageNotifierDiv');
    if(notifierMessages.length>0) {
      var x=0;
 //       if(notifierMessages[x]<=0 && notifierOpacities[x]>0) {
            notifierOpacities[x]=notifierOpacities[x]-2;
            if(notifierOpacities[x]<=0) {
                removeMessages()
            }
            else {
                o.childNodes[x].style.MozOpacity=notifierOpacities[x]/100;
                o.childNodes[x].style.filter="alpha(opacity="+notifierOpacities[x]+")";
                setTimeout('fadeoutMessages()',20);
            }
//        }
    }
}
function removeMessages() {
    notifierRemoving=true;
    var o=document.getElementById('messageNotifierDiv');
    if(notifierMessages.length>0) {
        var x=0;
//        if(notifierMessages[x]<=0 && notifierOpacities[x]<=0) {
            o.childNodes[x].innerHTML='';
            var ch=parseInt(o.childNodes[x].style.height);
            ch--;
            if(ch<=0) {
                o.removeChild(o.childNodes[x]);
                notifierMessages.splice(x,1);
                notifierOpacities.splice(x,1);

                if(o.childNodes.length>0)
                    setTimeout('fadeoutMessages()',messageDuration);

            }
            else {
                o.childNodes[x].style.height=ch+'px';
                setTimeout('removeMessages()',50);
            }
 //       }
    }
}

var divOpacities=Array();
var divsFading=false;
var divFadeStep=10;
var divFadeTick=50;

//dir is out ir in
function fadeDiv(d,dir) {
    var start=(dir=='out'?100:0);
    divOpacities[divOpacities.length]=[start,d,dir];

    var d=document.getElementById(d)
    d.style.MozOpacity=start/100;
    d.style.filter="alpha(opacity="+start+")";

    if(dir=="in") d.style.display='';

    if(!divsFading) {
        divsFading=true;
        setTimeout('fadeDivStep()',divFadeTick);
    }
    return 100/divFadeStep*divFadeTick;
}

function fadeDivStep() {
    for(var i=divOpacities.length;i>0;i--) {
        var ar=divOpacities[i-1];
        var d=document.getElementById(ar[1]);
        if(ar[2]=='out')
            ar[0]-=divFadeStep;
        else
            ar[0]+=divFadeStep;

        if( (ar[2]=='out' && ar[0]>0) || (ar[2]=='in' && ar[0]<100)) {
            //firefox
            d.style.MozOpacity=ar[0]/100;
            //konqueror/safari
            d.style.KhtmlOpacity=ar[0]/100;
            //internet exploder
            d.style.filter="alpha(opacity="+ar[0]+")";
            setTimeout('fadeDivStep()',divFadeTick);
        }
        else {
            if(ar[2]=="out") d.style.display='none';
            divOpacities.splice(i-1,1);
        }
    }
    if(divOpacities.length==0)
        divsFading=false;
}

var resizeTimeout=null;
var resizeObj=null;
var resizeMin=80;
var resizeMax=400;

function resizeTextareaKeypress(obj,min,max) {
    resizeObj=obj;
    resizeMin=min;
    resizeMax=max;
    if(resizeTimeout)
        window.clearTimeout(resizeTimeout);

    resizeTimeout=window.setTimeout(resizeTextareaHeight,400);
    return true;
}

function resizeTextarea(obj,min,max) {
    resizeObj=obj;
    resizeMin=min;
    resizeMax=max;
    resizeTextareaHeight();
}

function resizeTextareaHeight() {
    if(!resizeObj) return;
    var txt=resizeObj.value;

    var lines=txt.split('\n');
    var numlines=lines.length;

    var lineHeight=resizeObj.style.lineHeight;
    if(!lineHeight) lineHeight='1.5em';
    lineHeight=lineHeight.substring(0,lineHeight.length-2);

    var height=lineHeight*(numlines+1);
    var resizeHeight=0;

    if(height<resizeMin) resizeHeight=resizeMin;
    else if(height>resizeMax) resizeHeight=resizeMax;
    else resizeHeight=height;

    resizeObj.style.height=resizeHeight+'em';
    return true;
}

var popuptitlevar="";
var popupbodyvar="";
var popupcache=Array();
var popuptmpurl="";

function showPopup(e,url,titlevar,bodyvar) {
    if(!e) var e=window.event;
//	$("#popupdiv").mouseout(function() { hidePopup() });

	popuptmpurl=url;
    popuptitlevar=titlevar;
    popupbodyvar=bodyvar;

    var obj=document.getElementById('popupdiv');
    if(obj === null) {
        var obj=dom.create('div',{id:'popupdiv'});
        document.getElementsByTagName('body')[0].appendChild(obj);
    }

	if(popupcache[url]) {
		handleShowPopup(popupcache[url]);
	}
	else {
		var request=ajax.createRequest(url);
		request.setResponseHandler(handleShowPopup);
		request.setResponseType(ajax.JSON);
		request.send();
	}

    movePopup(e);
   
}

function handleShowPopup(response) {
    var obj=document.getElementById('popupdiv');
    obj.innerHTML="<div class=\"popupdivtitle\"><span style=\"float: right; \"><a href=\"#\" onclick=\"return hidePopup()\" style=\"color: #AAAAAA;\">[X]</a></span>"+response[0][popuptitlevar]+"</div><div class=\"popupdivbody\">"+response[0][popupbodyvar]+"</div>";
	popupcache[popuptmpurl]=response;

	obj.style.visibility='visible';
    obj.style.display='';
}

function hidePopup() {
    var obj=document.getElementById('popupdiv');
    obj.style.display='none';
    obj.style.visibility='hidden';
	return false;
}

function movePopup(e) {
    var obj=document.getElementById('popupdiv');
    if(!e) var e=window.event;

    var posx=0;
    var posy=0;

    //FIXME: this _should_ work for IE, needs to be tested
	if(e.clientX || e.clientY) {
        //this works for FF
        posx=e.clientX+document.body.scrollLeft;//-e.target.x;
        posy=e.clientY+document.body.scrollTop;//-e.target.y;
    }
	else if(e.clientY && e.clientX) {
        posy=e.clientY+(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop);//-e.target.y;
        posx=e.clientX+(document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft);//-e.target.y;
	
	}
	else {
	}
    posx+=5;
    posy+=5;
    obj.style.top=posy+'px';
    obj.style.left=posx+'px';
}
