// Script for AJAX based navigation with menu.

// Note: This code is horrible.

// base path elements (slash is replaced with _) necessary for IE
var base_paths = ["itb.biologie.hu-berlin.de_", "~wilbert_"]
var base_path = "~wilbert/"
var current_hash = null;
// flag to signal if content corresponding to current_hash has been loaded
var loaded_content = false;

window.dhtmlHistory.create();

$.ajaxSetup({
    cache: false
});

$().ready(function(){
    // on page load init the timer which check if the there are anchor changes
    setInterval("checkHash()", 200);
    // modify menu links to turn them into ajac requests
    $(".mlink").each(function() {
        this.href = getHashLink(this.href, "menu");
    })
    $(".mlink").click(function(){return clickHashLink(this.href);});
    // init history functionality from rsh
    dhtmlHistory.initialize();
});


// backround loop calback to load new content if hash was changed
function checkHash() {
    var new_hash = window.location.hash.substring(1);
    if (!new_hash) {
        new_hash = "home.html";
    }
    if (current_hash != new_hash) {
        loadContent(new_hash);
    } else {
        if (loaded_content) {
            // modify new links
            $("#insert .dlink").each(function() {
                this.href = getHashLink(this.href, 0);
            })
            $("#insert .ulink").each(function() {
                this.href = getHashLink(this.href, 1);
            })
            $("#insert .dlink").click(function(){return clickHashLink(this.href);});
            $("#insert .ulink").click(function(){return clickHashLink(this.href);});
			// modify image source
			$("#insert img").each(function() {
				var hashes = new_hash.split("_");
				var n_hashes = hashes.length - 1;
				var new_src = "";
				for (i=0; i<n_hashes; i++) {
           			new_src += hashes[i] + "/";
     			}
				// remove absolute part of path
				var img_src = this.src;
				if (this.src.indexOf(base_path) > -1) {
			    	img_src = img_src.split(base_path)[1];
			    }
                this.src = new_src + img_src;
            })
            loaded_content = false;
        }
    }
}


function loadContent(hash) {
    current_hash = hash;
	// send the AJAX request
	$("#content").load(hash.replace(/_/g, "/") + " #insert", function() {
    	loaded_content = true;
	});
}


// instead off following the link (and thus reloading the whole page) only update the hash
function clickHashLink(link_url) {
    var new_hash = link_url.split('#')[1];
    if (new_hash.substring(new_hash.length - 5, new_hash.length) === ".html") {
    	window.dhtmlHistory.add(new_hash);
	} else {
		location.href = link_url;
	}
    return false;
}

// turn the link url into a version using the hash
// 'relative' can be "menu" or an integer (e.g. 1 for one up)
function getHashLink(link_url, relative) {
    var new_hash = link_url;
    // for Firefix remove relative part of link
    new_hash = new_hash.replace(new RegExp("\\.\\./", "g"), "");
    new_hash = new_hash.replace(new RegExp("\\./", "g"), "");
    new_hash = new_hash.replace(new RegExp("/", "g"), "_");
    // IE workaround to remove absolute part
    for (i=0; i<base_paths.length; i++) {
        if (new_hash.indexOf(base_paths[i]) > -1) {
            new_hash = new_hash.split(base_paths[i])[1];
        }
    }
    if (relative != "menu") {
        // append parts of the old hash
        var old_hashes = current_hash.split("_");
        var n_pre_hashes = old_hashes.length - relative - 1;
        var pre_hash = "";
        for (i=0; i<n_pre_hashes; i++) {
            pre_hash += old_hashes[i] + "_";
        }
        new_hash = pre_hash + new_hash;
    }
	// convert non-html links to normal links
	if (new_hash.substring(new_hash.length - 5, new_hash.length) === ".html") {
    	return "./#" + new_hash;
	} else {
		return "./" + new_hash.replace(/_/g, "/");
	}
}


// IE hover workaround, from http://htmldog.com/articles/suckerfish/dropdowns/
sfHover = function() {
    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
    for (var i=0; i<sfEls.length; i++) {
        sfEls[i].onmouseover=function() {
            this.className+=" sfhover";
        }
        sfEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }
}
// window.attachEvent is only available in IE
if (window.attachEvent) window.attachEvent("onload", sfHover);


