function Browser() {
    var ua, s, i;

    this.isIE = false;  // Internet Explorer
    this.isNS = false;  // Netscape
    this.version = null;

    ua = navigator.userAgent;

    s = "MSIE";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }

    s = "Netscape6/";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }

    // Treat any other "Gecko" browser as NS 6.1.

    s = "Gecko";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = 6.1;
        return;
    }
}

var browser = new Browser();

// Global variable for tracking the currently active button.
var activeButton = null;

var sideMenuButtonActive = null;

// Capture mouse clicks on the page so any active button can be
// deactivated.
if (browser.isNS) {
    document.addEventListener("mousedown", pageMousedown, true);
}
else {
    document.onmousedown = pageMousedown;
}


function pageMousedown(event) {
    var el;

    // If there is no active menu, exit.
    if (!activeButton) {
        return;
    }

    // Find the element that was clicked on.
    if (browser.isNS) {
        el = (event.target.className ? event.target : event.target.parentNode);
    }
    else {
        el = window.event.srcElement;
    }

    // If the active button was clicked on, exit.
    if (el == activeButton) {
        return;
    }

    // If the element clicked on was not a menu button or item, close the active menu.
    if (el.className != "menuButton"  && el.className != "menuItem" &&
        el.className != "menuItemSep" && el.className != "menu") {
        resetButton(activeButton);
    }
}

function buttonMouseover(button, menuName) {
    // Associate the named menu to this button if not already done.
    if (!button.menu) {
        button.menu = document.getElementById(menuName);
    }

    // Reset the currently active button, if any.
    if (activeButton && activeButton != button) {
        resetButton(activeButton);
    }

    // Toggle the button's state.
    if (!button.isDepressed) {
        depressButton(button);
    }

    // If any other button menu is active, deactivate it and activate this one.
    // Note: if this button has no menu, leave the active menu alone.
    if (activeButton && activeButton != button) {
        resetButton(activeButton);
    }
}

function depressButton(button) {
    var width, x, y;

    // Change the button's style class to make it look like it's depressed.
    button.className = "menuButtonActive";


     // Position the associated drop down menu under the button and
    // show it. Note that the position must be adjusted according to
    // browser, styling and positioning.
    x = getPageOffsetLeft(button);
    y = getPageOffsetTop(button) + button.offsetHeight;
    if (browser.isIE) {
        x += 2;
        y += 2;
    }
    if (browser.isNS && browser.version < 6.1) {
        y--;
    }

    //If the menu has not yet a width.
    if(!button.menu.style.width) {
        width = 0;
        for(var i = 0; i < button.menu.childNodes.length; i++) {
            width = width < (button.menu.childNodes[i].offsetWidth + 12) ? (button.menu.childNodes[i].offsetWidth + 12) : width;
        }
        button.menu.style.width = width + "px";
    }

    // Position and show the menu.
    button.menu.style.left = (x + 7) + "px";
    button.menu.style.top  = y + "px";
    button.menu.style.visibility = "visible";

    // Set button state and let the world know which button is active.
    button.isDepressed = true;
    activeButton = button;
}


function resetButton(button) {
    // Restore the button's style class.
    button.className = "menuButton";

    // Hide the button's menu.
    if (button.menu) {
        button.menu.style.visibility = "hidden";
    }

    //Set button state and clear active menu global.
    button.isDepressed = false;
    activeButton = null;
}


function getPageOffsetLeft(el) {
    // Return the true x coordinate of an element relative to the page.
    return el.offsetLeft + (el.offsetParent ? getPageOffsetLeft(el.offsetParent) : 0);
}


function getPageOffsetTop(el) {
    // Return the true y coordinate of an element relative to the page.
    return el.offsetTop + (el.offsetParent ? getPageOffsetTop(el.offsetParent) : 0);
}

function head(lng, depth) {
    var path = getPath(depth);

    var htmlText = '<a href="'  + path + 'index_' + lng + '.html"><img src="' + path + '../imgs/logo_banner.jpg" border="0" alt="LOCALITE"></a>';
    document.writeln(htmlText);
}

// Returns menu in spezified language
function menu(lng, depth) {
    var path = getPath(depth);

    var htmlText;
    // german
    if (lng == "de") {
        htmlText = '<!-- Tags for the menu bar. -->';
        htmlText += '<div id="menuBar">';
            htmlText += '<a class="menuButton" href="' + path + 'index_de.html">| Home </a>';
            htmlText += '<a class="menuButton" href="' + path + 'company.html">| Unternehmen </a>';
            htmlText += '<a class="menuButton" href="' + path + 'products.html" onmouseover="buttonMouseover(this, ' + "'productMenu'" + ');">| Produkte </a>';
            htmlText += '<a class="menuButton" href="' + path + 'partners.html">| Partner </a>';
            htmlText += '<a class="menuButton" href="' + path + 'news.html">| News </a>';
            htmlText += '<a class="menuButton" href="' + path + 'contact.html">| Kontakt </a>';
            //htmlText += '<a class="menuButton" href="' + path + 'jobs.html">| Jobs </a>';
        htmlText += '</div>';
    }
    // english
    else if (lng == "en") {
        htmlText = '<!-- Tags for the menu bar. -->';
        htmlText += '<div id="menuBar">';
            htmlText += '<a class="menuButton" href="' + path + 'index_en.html">| Home </a>';
            htmlText += '<a class="menuButton" href="' + path + 'company.html">| Company </a>';
            htmlText += '<a class="menuButton" href="' + path + 'products.html" onmouseover="buttonMouseover(this, ' + "'productMenu'" + ');">| Products </a>';
            htmlText += '<a class="menuButton" href="' + path + 'partners.html">| Partners </a>';
            htmlText += '<a class="menuButton" href="' + path + 'news.html">| News </a>';
            htmlText += '<a class="menuButton" href="' + path + 'contact.html">| Contact </a>';
        htmlText += '</div>';
    }
    else {}

    htmlText += '<br>';
    htmlText += '<!-- Tags for the drop down menus. -->';
    htmlText += '<div id="productMenu" class="menu">';
    htmlText += '<a class="menuItem" href="' + path + 'orp/index_orp.html">OrthoPlanner</a>';
    htmlText += '<a class="menuItem" href="' + path + 'orp/vet_orp.html">OrthoPlanner VET</a>';
    htmlText += '<a class="menuItem" href="' + path + 'knee/index_knee.html">KneeNavigator</a>';
    htmlText += '<a class="menuItem" href="' + path + 'hip/index_hip.html">HipNavigator</a>';
    htmlText += '<div class="menuItemSep"></div>';
    htmlText += '<a class="menuItem" href="' + path + 'tms/index_tms.html">TMS Navigator</a>';
    htmlText += '<a class="menuItem" href="' + path + 'brain/index_brain.html">BrainNavigator</a>';
    htmlText += '<a class="menuItem" href="' + path + 'imri/index_imri.html">iMRI Navigator</a>';
//    htmlText += '<a class="menuItem" href="' + path + 'sono/index_sono.html">SonoNavigator</a>';
    htmlText += '<div class="menuItemSep"></div>';
    if(lng == "de") {
        htmlText += '<a class="menuItem" href="' + path + 'rapid/index_rapid.html">RapidPrototyping</a>';
    }
    htmlText += '<a class="menuItem" href="' + path + 'cons/index_cons.html">Consulting</a>';
    htmlText += '</div>';

    document.writeln(htmlText);
}

// Returns the side menu in the specified language
function sidemenu(lng, depth, link) {
    var path = getPath(depth);

    var htmlText;
    // german
    if (lng == "de") {
        htmlText = '<!-- Tags for the side menu bar. -->';
        htmlText += '<div id="sideMenuBar" style="text-align:left;">';

            htmlText += link != "index" ?  '<a class="sideMenuButton" href="' + path + 'orp/index_orp.html" > Digitale Prothesenplanung </a>'
            : '<a class="actualLink" href="' + path + 'orp/index_orp.html" > Digitale Prothesenplanung </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

             htmlText += link != "a2d" ?  '<a class="sideMenuButton" href="' + path + 'orp/a2d.html"> Analog wird Digital </a>'
            : '<a class="actualLink" href="' + path + 'orp/a2d.html"> Analog wird Digital </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

             htmlText += link != "advantages" ?  '<a class="sideMenuButton" href="' + path + 'orp/advantages.html"> Vorteile und Funktionalität </a>'
            : '<a class="actualLink" href="' + path + 'orp/advantages.html"> Vorteile und Funktionalität </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

             htmlText += link != "prosthesis" ?  '<a class="sideMenuButton" href="' + path + 'orp/prosthesis.html"> Integrierte Prothesen </a>'
            : '<a class="actualLink" href="' + path + 'orp/prosthesis.html"> Integrierte Prothesen </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

             htmlText += link != "pacs" ?  '<a class="sideMenuButton" href="' + path + 'orp/pacs.html"> PACS-Anbindung </a>'
            : '<a class="actualLink" href="' + path + 'orp/pacs.html"> PACS-Anbindung </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

             htmlText += link != "calibration" ?  '<a class="sideMenuButton" href="' + path + 'orp/calibration.html"> Größenkalibrierung </a>'
            : '<a class="actualLink" href="' + path + 'orp/calibration.html"> Größenkalibrierung </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

            /* Site for the ORP reference customers. Can be activated when all of the reference customers agree.
             htmlText += link != "reference" ?  '<a class="sideMenuButton" href="' + path + 'orp/reference.html"> Referenzen </a>'
            : '<a class="actualLink" href="' + path + 'orp/reference.html"> Referenzen </a>';
            htmlText += '<hr>'
            */
            htmlText += link != "system" ?  '<a class="sideMenuButton" href="' + path + 'orp/system.html"> Systemvoraussetzungen </a>'
            : '<a class="actualLink" href="' + path + 'orp/system.html"> Systemvoraussetzungen </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

            htmlText += link != "demo" ?  '<a class="sideMenuButton" href="' + path + 'orp/demo.html"> Demoversion </a>'
            : '<a class="actualLink" href="' + path + 'orp/demo.html"> Demoversion </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

            htmlText += link != "dpartners" ?  '<a class="sideMenuButton" href="' + path + 'orp/dpartners.html"> Vertriebspartner </a>'
            : '<a class="actualLink" href="' + path + 'orp/dpartners.html"> Vertriebspartner </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

            htmlText += link != "vet_orp" ?  '<a class="sideMenuButton" href="' + path + 'orp/vet_orp.html"> <p style="font-style:italic">Neu</p> LOCALITE OrthoPlanner VET </a>'
            : '<a class="actualLink" href="' + path + 'orp/vet_orp.html"><p style="font-style:italic">Neu</p> LOCALITE OrthoPlanner VET </a>';

        htmlText += '</div>'
    }
    // english
    else if (lng == "en") {
        htmlText = '<!-- Tags for the side menu bar. -->';
        htmlText += '<div id="sideMenuBar">';

            htmlText += link != "index" ?  '<a class="sideMenuButton" href="' + path + 'orp/index_orp.html" > Digital prosthesis planning </a>'
            : '<a class="actualLink" href="' + path + 'orp/index_orp.html" > Digital prosthesis planning </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

             htmlText += link != "a2d" ?  '<a class="sideMenuButton" href="' + path + 'orp/a2d.html"> From analogue to digital </a>'
            : '<a class="actualLink" href="' + path + 'orp/a2d.html"> From analogue to digital </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

             htmlText += link != "advantages" ?  '<a class="sideMenuButton" href="' + path + 'orp/advantages.html"> Advantages and functionality </a>'
            : '<a class="actualLink" href="' + path + 'orp/advantages.html"> Advantages and functionality </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

             htmlText += link != "prosthesis" ?  '<a class="sideMenuButton" href="' + path + 'orp/prosthesis.html"> Integrated prostheses </a>'
            : '<a class="actualLink" href="' + path + 'orp/prosthesis.html"> Integrated prostheses </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

             htmlText += link != "pacs" ?  '<a class="sideMenuButton" href="' + path + 'orp/pacs.html"> PACS connectivity </a>'
            : '<a class="actualLink" href="' + path + 'orp/pacs.html"> PACS connectivity </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

             htmlText += link != "calibration" ?  '<a class="sideMenuButton" href="' + path + 'orp/calibration.html"> Size calibration </a>'
            : '<a class="actualLink" href="' + path + 'orp/calibration.html"> Size Calibration </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

            /* Site for the ORP reference customers. Can be activated when all of the reference customers agree.
             htmlText += link != "reference" ?  '<a class="sideMenuButton" href="' + path + 'orp/reference.html"> References </a>'
            : '<a class="actualLink" href="' + path + 'orp/reference.html"> References </a>';
            htmlText += '<hr>'
            */
            htmlText += link != "system" ?  '<a class="sideMenuButton" href="' + path + 'orp/system.html"> System requirements </a>'
            : '<a class="actualLink" href="' + path + 'orp/system.html"> System requirements </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

            htmlText += link != "demo" ?  '<a class="sideMenuButton" href="' + path + 'orp/demo.html"> Demo version </a>'
            : '<a class="actualLink" href="' + path + 'orp/demo.html"> Demo version </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

            htmlText += link != "dpartners" ?  '<a class="sideMenuButton" href="' + path + 'orp/dpartners.html"> Distribution partners </a>'
            : '<a class="actualLink" href="' + path + 'orp/dpartners.html"> Distribution partners </a>';
            htmlText += '<br/>';
            htmlText += '<img class="sidemenu_line" src="imgs/sidemenu_line.png">';
            htmlText += '<br/>';

            htmlText += link != "vet_orp" ?  '<a class="sideMenuButton" href="' + path + 'orp/vet_orp.html"> <p style="font-style:italic">New</p> LOCALITE OrthoPlanner VET </a>'
            : '<a class="actualLink" href="' + path + 'orp/vet_orp.html"> <p style="font-style:italic">New</p> LOCALITE OrthoPlanner VET </a>';

        htmlText += '</div>'
    }
    else {}

    document.writeln(htmlText);
}

// Returns copyright footer in spezified language
function copyright(lng, depth) {
    var path = getPath(depth);
     var htmlText = '<td>';
    htmlText  += '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
        htmlText  += '<tr>';
            htmlText  += '<td class="copyright">';
            htmlText  += '<div align="center">'
            htmlText  += '&copy; 1999-2011 LOCALITE GmbH. ';

            // german
            if (lng == "de") {

            htmlText  += 'Bei Fragen oder Kommentaren wenden Sie sich bitte an den ';
            htmlText  += '<a class="norm" href="mailto:' + decodeMail("jogpAmpdbmjuf/ef") + '">Webmaster</a>. ';
            htmlText  += 'Alle Rechte vorbehalten.';
            htmlText += '</div>'
            htmlText  += '</td>';
        htmlText  += '</tr>';
        htmlText  += '<tr>';
            htmlText += '<td class="copyright">';
            htmlText += '<div align="center"><a class="norm" href="' + path + 'impress.html">Impressum</a></div>';

            }

            // english
            else if (lng == "en") {

            htmlText  += 'For questions or comments please contact our ';
            htmlText  += '<a class="norm" href="mailto:' + decodeMail("jogpAmpdbmjuf/ef") + '">webmaster</a>. ';
            htmlText  += 'All rights reserved.';
            htmlText += '</div>'
            htmlText  += '</td>';
        htmlText  += '</tr>';
        htmlText  += '<tr>';
            htmlText += '<td class="copyright">';
            htmlText += '<div align="center"><a class="norm" href="' + path + 'impress.html">legal notice</a></div>';

            }

            else {}

            htmlText  += '</td>';
        htmlText  += '</tr>';
    htmlText  += '</table>';
    htmlText += '</td>';

    document.writeln(htmlText);

    piwikText = ' <script type="text/javascript">';
    piwikText += 'var pkBaseURL = "http://www.localite.de/piwik/";'
    piwikText += 'document.write(unescape("%3Cscript src=\'" + pkBaseURL + "piwik.js\' type=\'text/javascript\'%3E%3C/script%3E"));';
    piwikText += '</script>';
    piwikText += '<script type="text/javascript">';
    piwikText += 'try {';
    piwikText += 'var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);';
    piwikText += '      piwikTracker.trackPageView();';
    piwikText += '      piwikTracker.enableLinkTracking();';
    piwikText += '      } catch( err ) {}';
    piwikText += '</script>';
    document.writeln(piwikText);
}



function getPath(depth) {
    var path = "";
    for(var i = 0; i < depth; i++) {
        path = "../" + path;
    }

    return path;
}

//Generates the spam save email adress to write on the webpage.
function encodeMail(s) {
    var erg = "";

    for(var i = 0; i < s.length; i++) {
        var n = s.charCodeAt(i);
        erg += String.fromCharCode(n + 1);
    }
    return erg;
}

//get the right email adress from the given code.
function decodeMail(s) {
    var erg = "";

    for(var i = 0; i < s.length; i++) {
        var n = s.charCodeAt(i);
        erg += String.fromCharCode(n - 1);
    }
    return erg;
}

//mailfunktion: decodes the given adress and sends a mail.
function sendMail(s) {

    location.href="mailto:" + decodeMail(s);
}

//mailfunktion with subject: decodes the given adress and sends a mail with the given subject.
function sendMailS(s, subject) {

    location.href="mailto:" + decodeMail(s) + "?subject=" + subject;
}

//Function for the opening the larger image version in the sidemenu
function popUp( URL ) {

         day = new Date();
         id = day.getTime();
         eval( "page" + id + " = window.open( URL, '" + id + "');" );
}
