// globals
var cookie = readCookie("style");
var width = readCookie("width");
width = width ? width : 'fixed';
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
var image_count = -1;

// detection
var noOverwrite=false;
var alertText;

var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));
var is_safari = ((clientPC.indexOf('AppleWebKit')!=-1) && (clientPC.indexOf('spoofer')==-1));
var is_khtml = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));
var is_IE = (navigator.appName == 'Microsoft Internet Explorer');

if (clientPC.indexOf('opera')!=-1) {
    var is_opera = true;
    var is_opera_preseven = (window.opera && !document.childNodes);
    var is_opera_seven = (window.opera && document.childNodes);
}

function toggleFixedWidth() {
    var c;
    c = document.getElementById("container");
    width = c.className == 'floating' ? 'fixed' : 'floating';
    c.className = width;
    // #toc is a bit much for gecko to resize on the fly
    setActiveStyleSheet(title);
    if (!is_gecko) insertToggleArrow(width);
    if (is_IE) window.location.reload();
}

function insertToggleArrow(w) {
    var div = document.getElementById('toggle_fixed');
    if (w == 'fixed') div.firstChild.innerHTML = '&gt;';
    if (w == 'floating') div.firstChild.innerHTML = '&lt;';
}

function setActiveStyleSheet(t) {
    var i, a, main;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if(a.getAttribute("title") == t) a.disabled = false;
            title = t;
        }
    }
}

function getActiveStyleSheet() {
    var i, a;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
    }
    return null;
}

function getPreferredStyleSheet() {
    var i, a;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
         && a.getAttribute("rel").indexOf("alt") == -1
         && a.getAttribute("title")
         ) return a.getAttribute("title");
    }
    return null;
}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function setBrowser(browser) {
    var divs = [ 
        'toggle_fixed',
        'toc',
        'content_actions',
        'personal_tools'
    ];
    for ( var i = 0; i < divs.length; i++ ) {
        var e = document.getElementById(divs[i]);
        e.className = browser;
    }
    body = document.body;
    body.className = browser;
}

window.onload = function(e) {
    if (is_khtml) setBrowser('khtml');
    if (is_IE && !is_gecko) setBrowser('msie');
    if (is_IE && !is_gecko) setImageWidths();
    if (is_IE && !is_gecko) startList();
    if (!is_gecko) insertToggleArrow(width);
    var cookie = readCookie("style");
    var title = cookie ? cookie : getPreferredStyleSheet();
    var container = document.getElementById("container");
    container.className = width;
    setActiveStyleSheet(title);
    var elem = document.getElementById("toolbox");
}

window.onunload = function(e) {
    var title = getActiveStyleSheet();
    createCookie("style", title, 365);
    createCookie("width", width, 365);
}

function setImageWidths() {
    var div = document.body.getElementsByTagName("div");
    for (var i = 0; i < div.length; i++) {
        if (div(i).className.indexOf('thumb ') != -1) {
            img = div(i).getElementsByTagName("img")(0);
            if (img) div(i + 1).style.width 
                = img.width < document.body.clientWidth 
                    ? img.width + 4 : document.body.clientWidth - 20;
        }
    }
};

function startList() {
    if ( document.all && document.getElementById ) {
        var divs = [
            'personal_tools',
            'main_nav',
            'styles',
            'interwiki_links',
            'toolbox'
        ];
        for (var i = 0; i < divs.length; i++) {
            var e = document.getElementById(divs[i]);
            if (e != null) {
                e.onmouseover = function() {
                    this.className += " over";
                };
                e.onmouseout = function() {
                    this.className = this.className.replace (" over", "");
                };
            }
        }
    }
}

// vim:encoding=utf8:

