var current;
var lastId;
var prefix;
var itemList = new Array();  // itemList is filled via addItem(..)

function focusSymbolDeco() {
	if(document.getElementById('jumpForm') != null) document.getElementById('jumpForm').symbolDeco.focus();
}

function markClicked(id, newClass, oldClass) {
    // alert("==>" + id + "<==" );
    var element = document.getElementById(id);
	if (lastId != null && id != lastId) {	
		// alert("-->" + lastId + "<--" );
		var lastElement = document.getElementById(lastId);
		//reset the last element
		if(lastElement != null) lastElement.className = oldClass;
	}
	if(element != null) element.className = newClass;
	// save the current object id for the next call
	lastId = id;
	
	current = id.substring(prefix.length, id.length) - 0;
    buttonEnabler();
}

function first() {
	jumpto(0, 1);
}

function last() {
	jumpto(itemList.length-1, 2); 
}

function prev() {
	jumpto(current-1, 2);
}

function next() {
	jumpto(current+1, 1);
}

/**
 * comingfrom: 1=going up, 2=going down, 3=initial
 */
function jumpto(index, comingfrom) {
	if (index < 0) {
		index=0;
	} else if (index > itemList.length-1) {
		index=itemList.length-1;
	}
	if (itemList[index] == 0) {  // no entry - find next entry
		if (comingfrom == 2) {  		// one step back
			jumpto(index-1, 3);
		} else if (comingfrom == 1) {  	// one step forward
			jumpto(index+1, 2);
		} else {
		    jumpto(0,1);				// goto beginning
		}
		return;
	}
   	parent.content.location.href=decodeURIComponent(itemList[index]);
    
    current=index;

    try {
	    markClicked(prefix+current, 'clicked', 'notclicked');
    } catch (e) {
	    buttonEnabler();
    }
}


// function is called in IpcConcordanceLinkDecorator.java (via JavaScript)
function addItem(value) {
	itemList.push(value);
}

function buttonEnabler(){
    if(document.getElementById("first") != null) (document.getElementById("first")).disabled=false;
   	if(document.getElementById("prev") != null) (document.getElementById("prev")).disabled=false;
    if(document.getElementById("last") != null) (document.getElementById("last")).disabled=false;
    if(document.getElementById("next") != null)(document.getElementById("next")).disabled=false;
    
    if(current != null) {
	    if (current == itemList.length-1) {
		    if(document.getElementById("last") != null) (document.getElementById("last")).disabled=true;
		    if(document.getElementById("next") != null) (document.getElementById("next")).disabled=true;
		}
	   	if (current < 1) {
	    	if(document.getElementById("first") != null) (document.getElementById("first")).disabled=true;
	    	if(document.getElementById("prev") != null) (document.getElementById("prev")).disabled=true;
	    }
    }
	if(document.getElementById("jumpstatus") != null) (document.getElementById("jumpstatus")).value=current+1;
}

