//Original code by Anders Fagerjord
//enhanced by J. Nathan Matias

function stretch( targetId ) {
	if (document.getElementById) { 
		target = document.getElementById( targetId); 
		targetlink = document.getElementById ("t_" + targetId).firstChild.firstChild     
		 fade=new fx.Opacity(target , {duration: 300, onComplete: function()
		  {
		    if(this.faded==false){this.el.style.display="none";}
		    var pos=findPos(target);
		    updateAllAnnotations(pos[1]+2);
		        }});
		if (target.style.display == "none") {
			fade.faded=true;
			fade.setOpacity(0.3);
			target.style.display = ""; //Hvis target er skjul
			fade.custom(0.3,1);
		} else {
			fade.faded=false;
			fade.custom(1,0);
		}
	}
}

function quoteStretch(quoteID,quotePre, quoteSuff ) {
        if (document.getElementById) {
 
                targetPre = document.getElementById( quotePre); //la target v�re det skjulte elementet
		targetSuff = document.getElementById( quoteSuff);
                targetlink = document.getElementById ("t_" + quoteID).firstChild.firstChild
		targetQuote = document.getElementById(quoteID);
                if (targetPre.style.display == "none") {
			targetPre.style.display = "";
			targetSuff.style.display= "";
			targetQuote.style.backgroundColor="#ddddbd";
                } else {
                        targetPre.style.display = "none"; //Og omvendt.
			targetSuff.style.display= "none";
			targetQuote.style.backgroundColor="";
                }
        }
	updateAllAnnotations();				
}
	

function popitup(url)
{
        newwindow=window.open(url,'name','height=600,width=800,top=100,left=200,resizable=yes');
        if (window.focus) {newwindow.focus()}
        return false;
}

var idcounter=0;

//ANNOTATION CODE BY J. Nathan Matias
var annotationList=new Array();

//enable annotation
function flipAnnotation(name, linkLoc){
	
        if(document.getElementById){
        notationItem=document.getElementById(name);
        line = document.getElementById(name+"line");
        posArray=findPos(linkLoc);
        notationItem.style.top=posArray[1];
	annotationPos=findPos(notationItem);
	//add the item to the annotation list if it doesn't exist	
        if(!annotationList||!annotationList[name]){
                annotationList[name]=new Object();
                annotationList[name].name=name;
                annotationList[name].notationItem=notationItem;
                annotationList[name].line=line;
                annotationList[name].linkLoc=linkLoc
                annotationList[name].annotationPos=annotationPos;
                annotationList[name].posArray=posArray;
                annotationList[name].visibility=false;
		annotationList[name].fade=new fx.Opacity(notationItem , {duration: 500});
		annotationList[name].lineFade=new fx.Opacity(line , {duration: 500});
        }
	line.style.left=posArray[0];
	line.style.top=posArray[1];
	line.style.width=annotationPos[0]-posArray[0];

        if(annotationList[name].visibility==true){
		annotationList[name].fade.custom(1,0);
    //            notationItem.style.visibility="hidden";
                //line.style.visibility="hidden";
		annotationList[name].lineFade.custom(1,0);
		annotationList[name].visibility=false;
		linkLoc.style.backgroundColor="";
        }else{  
       //         notationItem.style.visibility="visible";
       		annotationList[name].fade.setOpacity(0);
       		annotationList[name].fade.custom(0,1);
                annotationList[name].lineFade.custom(0,1); 
                //line.style.visibility="visible";
		annotationList[name].visibility=true;
		linkLoc.style.background="#ece9c0";
        }

        }

}

function updateAllAnnotations(){
	if(annotationList){
	for(name in annotationList){
		if(typeof(annotationList[name])!="function"){
			if(arguments.length>0){
				updatePos(annotationList[name], arguments[0]);
			}else{
				updatePos(annotationList[name]);
			}
		}
	}
	}
}

//encapsulated function
function updatePos(noteObj){
	var comparison=1;
	if(arguments.length>1){comparison=arguments[1];}
	if(document.getElementById){
       		notationItem=noteObj.notationItem
	        line = noteObj.line;
		linkLoc = noteObj.linkLoc;
	        posArray = findPos(linkLoc);
		if(posArray[1]<comparison&&(comparison-posArray[1])<8){//ie, the paragraph is hidden
			if(annotationList[notationItem.id].visibility){
				annotationList[notationItem.id].line.style.visibility="hidden";
				annotationList[notationItem.id].visibility=false;
				annotationList[notationItem.id].fade.custom(1,0);
				linkLoc.style.background="none";
			}
		}else{
	        	notationItem.style.top = posArray[1];
			annotationPos = findPos(notationItem);
			line.style.left=posArray[0];
			line.style.top=posArray[1];
			line.style.width=annotationPos[0]-posArray[0];
		}
	}
}

//this is definitely the better of the two
function findPos(obj) {
        var curleft = curtop = 0;
        if (obj.offsetParent) {
                curleft = obj.offsetLeft
                curtop = obj.offsetTop
                while (obj = obj.offsetParent) {
                        if(obj.id=="body")break;
                        curleft += obj.offsetLeft
                        curtop += obj.offsetTop
                }
        }
        return [curleft,curtop];
}

