var isIE = document.all ? true : false;
/*
    The root URL where this script is located
    Maybe we don't have to hardcode it here ?!
*/
var rootPath = "http://atp.lu.se";

function getElementById(id)
{
    return isIE ? document.all[id] : document.getElementById(id);
}

// Returns an array of nodes matching the tagname and class
function getByClass(tagname, classname)
{
    var nodes = document.getElementsByTagName(tagname);
    var classnodes = new Array();
    for(var i = 0; i < nodes.length; i++)
    {
        var node = nodes[i];
        if(node.className==classname)
        {
            classnodes.push(node);
        }
    }
    return classnodes;
}

//  Cookie stuff
function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+"; path=" +escape("/")+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function useLanguage(toLang)
{
    var fromLang = (toLang == 'sv') ? 'en' : 'sv';

    var nodes = getByClass('span', fromLang);
    for(var i = 0; i < nodes.length; i++)
    {
        nodes[i].style.display = "none";
    }
    nodes = getByClass('span', toLang);
    for(var i = 0; i < nodes.length; i++)
    {
        nodes[i].style.display = "inline";
    }
    setCookie("lang",toLang,10);
}

function defaultLanguage()
{
    var onLang = ".en";
    var offLang = ".sv";
    var lang = getCookie("lang");
    if(lang=="sv")
    {
        onLang = ".sv";
        offLang = ".en";
    }
    // Add stylesheets
    var cssNode = document.createElement('style');
    cssNode.type = 'text/css';
    document.getElementsByTagName("head")[0].appendChild(cssNode);
    var i = document.styleSheets.length-1;
    var css = document.styleSheets[i];

    if(isIE)
    {
        css.addRule(offLang, "display: none");
        css.addRule(onLang, " display: inline");
    }
    else
    {
        if(css) // For some reason this is not here in safari when reloading page ?!
        {
            css.insertRule(offLang + " { display: none } ", 0);
            css.insertRule(onLang + " { display: inline } ", 1);
        }
    }
}

// Specific cbbp.thep.lu.se modifications
function cbbp()
{
    var title = document.getElementsByTagName("title")[0];
    var link = document.createElement("LINK");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = rootPath + "/css/lu.css";

    var html = document.getElementsByTagName("html")[0];
    var head = document.getElementsByTagName("head")[0];
    html.removeChild(head);

    // Create new head
    head = document.createElement("head");
    head.appendChild(title);
    head.appendChild(link);

    // Add h1 as it's an image today
    var h1 = document.createElement("h1");
    h1.innerHTML = "Computational Biology &amp; Biological Physics";

    // Recreate the body
    var contentMain = getByClass("div", "main")[0];
    var navbar = getElementById("navbar");
    html.insertBefore(head, html.firstChild);
    html.removeChild(document.body);
    document.body = document.createElement("body");
    document.body.appendChild(h1);
    document.body.appendChild(navbar);
    document.body.appendChild(contentMain);
}

// Specific modifications for pages within the typo3 system
function typo3()
{
    var title = document.getElementsByTagName("title")[0];
    var link = document.createElement("LINK");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = rootPath + "/css/lu.css";

    var html = document.getElementsByTagName("html")[0];
    var head = document.getElementsByTagName("head")[0];
    // Add our own stylesheet at the very end
    head.appendChild(link);

    // Recreate the body
    var contentMain = getByClass("div", "contentMain")[0];
    var iconList = getByClass("ul", "linkIconList")[0];
    contentMain.removeChild(iconList);
    html.insertBefore(head, html.firstChild);
    html.removeChild(document.body);
    document.body = document.createElement("body");
    document.body.appendChild(contentMain);
}

// Navigational crumbs class, does not support bi language labels for now
function Crumbs()
{
    this.titles = new Array();
    this.links = new Array();
    this.node = document.createElement("div");
    this.node.id = "crumbs";

    this.add = function(label, href)
    {
        this.titles[this.titles.length] = label;
        this.links[this.links.length] = href;
        this.node.innerHTML = ""; // Clear crumbs
        for(var i = 0; i < this.titles.length; i++)
        {
            var a = document.createElement("A");
            a.href = this.links[i];
            a.innerHTML = this.titles[i];
            this.node.appendChild(a);
            if(i<this.titles.length-1)
            {
                var raquo = document.createElement("b");
                raquo.innerHTML = "&raquo;";
                this.node.appendChild(raquo);
                a.className = "";
            }
            else
            {
                a.className = "current";
            }
        }
    };
}

var crumbs = new Crumbs();
window.crumbs = crumbs;
crumbs.add("Lund University", "http://www.lunduniversity.lu.se");
crumbs.add("Faculty of Science", "http://www.science.lu.se/");
crumbs.add("Astronomy &amp; Theoretical Physics", "http://atp.lu.se/");

// http://haacked.com/archive/2006/04/06/StopTheWindow.OnloadMadness.aspx
function addOnload(myfunc)
{
    if(window.addEventListener)
    {
        window.addEventListener('load', myfunc, false);
    }
    else if(window.attachEvent)
    {
        window.attachEvent('onload', myfunc);
    }
}

// Only do myonload once incase the window.onload event fires more than once
var did_already = false;
function myonload()
{
    if(did_already) return;
    did_already = true;
    // Modify sites according to location
    if(window.location.hostname.match(/www\.atp\.lu\.se/))
    {
        typo3();
    }
    else if(window.location.hostname.match(/cbbp\.thep\.lu\.se/))
    {
        cbbp();
    }

    // Create the lu header
    var div = document.createElement("div");
    div.id = "luheader";

    var logo = document.createElement("img");
    logo.src = rootPath + "/img/logo_normal.png";
    logo.onclick = function() { window.location = "http://www.lunduniversity.lu.se"; };
    logo.style.cursor = "pointer";
    div.appendChild(logo);

    var bar = document.createElement("div");
    bar.className = "bar";
    bar.innerHTML = "&nbsp;";
    div.appendChild(bar);

    var seal = document.createElement("img");
    seal.id = "seal";
    seal.src = rootPath + "/img/seal8.png";
    div.appendChild(seal);

    // Prepend breadcrumbs
    var parent = document.body;
    parent.insertBefore(crumbs.node,parent.firstChild);
    parent.insertBefore(div,parent.firstChild);

    // Translator links
    var sv = document.createElement("img");
    sv.src = rootPath + "/img/se.png";
    sv.onclick = function() { useLanguage("sv"); };
    sv.style.marginLeft = "20px";

    var en = document.createElement("img");
    en.src = rootPath + "/img/gb.png";
    en.onclick = function() { useLanguage("en"); };
    en.style.marginLeft = "10px";

    window.crumbs.node.appendChild(sv);
    window.crumbs.node.appendChild(en);

    defaultLanguage();
}

function applyDesign()
{
    // Add the general stylesheet
    var link = document.createElement("LINK");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = rootPath + "/css/lu.css";

    var head = document.getElementsByTagName("head")[0];
    head.appendChild(link);

    // Default lang
    defaultLanguage();

    addOnload(myonload);
}

applyDesign();



