// register function navi_create to be called after page is loaded
window.addOnLoadListener(navi_create, null);

// displayes the navigation
function navi_create() {

    // navigation is already built => return (obsolete ?)
    if (NAVIGATION.created) {
        return;
    }

    // mark navigation as built (obsolete ?)
    NAVIGATION.created = true;

    // put first item to display into work queue
    var queue = ["id_0"];

    // div for item
    var item;

    // current navigation data to display
    var current;

    // we start at level 0
    var level = 0;

    // activ item is not known yet
    var active = null;

    // dummy loop
    while (true) {

        // queue is empty => we are done
        if (queue.length == 0) {
	    break;
	}

        // get next child of queue[0] to b displayed
        current = navi_get_next(queue[0]);

        // there is no child any more  to be displayed
	if (current == null) {

	    // remove parent from queu
	    queue.shift();

	    // continue with parent item
	    level--;

	    continue;
	}

	// create item
        item = document.createElement("div");

	// store html tag in configuration
	NAVIGATION.data[current.id].tag = item;

	// store item configuration in html tag
	item.data = current;

	// assign level to html tag
	item.data.level = level;

	// set text
        item.innerHTML = current.label;

	// display item from level 0 only
	if (level != 0) {
	    item.style.display = "none";
	}

	// we found current item => keep it
	if (navi_is_active(current)) {
	    active = item;
	} 

	// set class
        item.className = current.style+"_item_"+level;

	// bind click function
	item.onclick = function () { document.location.href = this.data.link; };

	// append item into dom
        document.getElementById("nav_container").appendChild(item);

        // put current item into queue to display its children
        queue.unshift(current.id);

        // increase level
	level++;
    }

    // mark current item
    navi_activate(active);
}

// mark current item
function navi_activate(item) {

    if (item == null) {
        return;
    }

    // assign active style to active item
    item.className = item.data.style+"_item_"+item.data.level+"_active";

    // display its anchestors and direct children
    navi_display(item);
}

// display anchestors and direct children of item
function navi_display(item) {

    if (item == null) {
        return;
    }

    // do for all items
    for (var index in NAVIGATION.data) {

        // we found a direct child of item => display it
        if (NAVIGATION.data[index].parent == item.data.id) {
	    NAVIGATION.data[index].tag.style.display = "block";
	}

        // we found an anchester of item => display it
        if (NAVIGATION.data[index].parent == item.data.parent) {
	    NAVIGATION.data[index].tag.style.display = "block";
	}
    }

    navi_activate(NAVIGATION.data[item.data.parent].tag);
}

// check if item is active item
function navi_is_active(item) {

    // get list of alternative links from ro
    var links = item.other.split(" ");

    // add current url without paramters
    links.push(item.link.split('?')[0]);

    // this is where we ar now
    // cut off parameters
    var here = document.location.href.split('?')[0].split('/');

    // cut off protocol
    here.shift();
    here.shift();
    here.shift();
    here = '/'+here.join('/');

    // look if we can find "here" in "list"
    for (index in links) {

        // found it => return true
        if (here == links[index]) {
            return true;
        }
    }

    // current url is not in list => return fals
    return false;
}

// return next child of parent pid
// pid = parent id
function navi_get_next(pid) {

    // do for all items
    for (key in NAVIGATION.data) {

        // skip already deployed items 
        if (NAVIGATION.data[key].deployed == true) {
            continue;
        }

	// found an undeployed child of pid
        if (NAVIGATION.data[key].parent == pid) {

	    // mark it as deployed
	    NAVIGATION.data[key].deployed = true;

	    // return it
            return NAVIGATION.data[key];
        }
    }

    // no child of pid found => return null
    return null;
}

// clone js object
function _clone(obj){
    if(obj == null || typeof(obj) != 'object') {
        return obj;
    }

    if (obj.length != null) {
        var temp = [];
        for (var i=0; i<obj.length; i++) {
            temp.push(_clone(obj[i]));
        }
        return temp;
    }

    var temp = {};
    for(var key in obj)
        temp[key] = _clone(obj[key]);
    return temp;
}

