﻿// ----------------------------------------------
// StyleSwitcher functions written by Paul Sowden
// http://www.idontsmoke.co.uk/ss/
// - - - - - - - - - - - - - - - - - - - - - - -
// For the details, visit ALA:
// http://www.alistapart.com/stories/alternate/

function setActiveStyleSheet(title) {

    var i, a, main;
	var countstylesheets = 0
	for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) 
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
		{
		    countstylesheets++;
		    a.disabled = true;

		    if (a.getAttribute("title") == title) 
		    {
		        a.disabled = false;
		    }
		    
		    if (document.getElementById('nav')) 
		    { 
		        menuHeight(); 
		    }
			if (document.getElementById('issues')) 
			{ 
			    issuesHeight() 
			}
			if (document.getElementById('bsa_dashboard')) 
			{ 
			    dashboardHeight(); 
			}
			if (document.getElementById('statistics_trends')) 
			{ 
			    antiPiracyHeight(); 
			}
			if (document.getElementById('left_col')) 
			{ 
			    setHeight(); 
			}
		
		}
	}
}

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 stInit() {
	if(document.getElementById) {
//		document.getElementById('small_text').onclick = function() {
//			setActiveStyleSheet('', 1);
//			if(dehyphenateSubnav) dehyphenateSubnav()
//			return false;
//		}
//		document.getElementById('medium_text').onclick = function() {
//			setActiveStyleSheet('Medium Text', 1);
//			if(dehyphenateSubnav) dehyphenateSubnav()
//			return false;
//		}
//		document.getElementById('large_text').onclick = function() {
//			setActiveStyleSheet('Large Text', 1);
//			if(hyphenateSubnav) hyphenateSubnav();
//			return false;
//		}
		
		var cookie = readCookie("style");
		var title = cookie ? cookie : getPreferredStyleSheet();
		setActiveStyleSheet(title);
	}

}
window.onload = function(e) {
	stInit();
}

window.onunload = function(e) {
	var title = getActiveStyleSheet();
	createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


function hyphenateSubnav() {
	if(navigator.userAgent.indexOf('Firefox') != -1 && document.getElementById('subnav')) {
		links = document.getElementById('subnav').getElementsByTagName('a');
		for (i=0; i<links.length; i++) {
			if(links[i].innerHTML.indexOf('&shy;') == -1) {
				words = links[i].innerHTML.split(' ');
				for (var j=0; j<words.length; j++) {
					if (words[j].length > 16) {
						for (var k=10; k<words[j].length; k++) {
							chr = words[j].charAt(k);
							if(chr == 'a' || chr == 'e' || chr == 'i' || chr == 'o' || chr == 'u') {
								words[j] = words[j].substring(0, k+1) + '- ' + words[j].substring(k+1, words[j].length);
								break;
							}
						}
						links[i].innerHTML = words.join(' ');
}}}}}}

function dehyphenateSubnav() {
	if(document.getElementById('subnav')) {
		links = document.getElementById('subnav').getElementsByTagName('a');
		for (i=0; i<links.length; i++) {
			if(links[i].innerHTML.indexOf('- ') != -1)
				links[i].innerHTML = links[i].innerHTML.replace(/- /g, '')
		}
	}
}

function $(v) { return (document.getElementById(v)); }

function menuHeight() {

    var els = $('nav').getElementsByTagName('a');
    var maxH = 0;
    var h = new Array()
    for (var i = 0; i < els.length; i++) {
        els[i].style.padding = '0';
        h[i] = els[i].offsetHeight;
        maxH = maxH > h[i] ? maxH : h[i];
    }
    for (var i = 0; i < els.length; i++) {
        pt = (maxH - h[i]) / 2 + 10;
        ptf = Math.floor(pt);
        pb = ptf + (pt - ptf) * 2;
        els[i].style.padding = ptf + 'px 3px ' + pb + 'px';

        els[i].onmouseover = function() { this.parentNode.style.backgroundImage = 'url(/images/french/nav_li_on_bgr.gif)' }
        els[i].onfocus = function() { this.parentNode.style.backgroundImage = 'url(/images/french/nav_li_on_bgr.gif)' }
        els[i].onmouseout = function() { p = this.parentNode; if (p.className != 'on') p.style.backgroundImage = 'url(/images/french/nav_li_bgr.gif)' }
        els[i].onblur = function() { p = this.parentNode; if (p.className != 'on') p.style.backgroundImage = 'url(/images/french/nav_li_bgr.gif)' }
    }
}

function mkLoad() { var v = new Date().getTime(); if (!$('nav')) { if (v - loadTime <= 5000) setTimeout("mkLoad()", 50); else window.onload = init1; } else menuHeight(); }
var loadTime = new Date().getTime(); setTimeout("mkLoad()", 10);

function setHeight() {
    if (!$('left_col') || !$('mid_col')) return;
    var padding = 0; /* this is equal to top-padding of #left_col - defined in leveldown.css */
    var left_col = $('left_col');
    var mid_col = $('mid_col');
    left_col.style.height = 'auto';
    var h1 = left_col.offsetHeight;
    var h2 = mid_col.offsetHeight;
    if (h1 < h2)
        left_col.style.height = h2 - padding + 'px';
}

function issuesHeight() {
    if (!$('issues')) return;
    els = $('issues').getElementsByTagName('div');
    for (var i = 0; i < els.length; i += 2) {
        els[i].style.height = 'auto';
        els[i + 1].style.height = 'auto';
        if (els[i + 1]) {
            var h1 = els[i].offsetHeight;
            var h2 = els[i + 1].offsetHeight;
            if (h1 > h2) {
                els[i + 1].style.height = h1 + 'px';
            } else if (h2 > h1) {
                els[i].style.height = h2 + 'px';
            }
        }
    }
}
function dashboardHeight() {
    if (!$('bsa_dashboard')) return;
    els1 = $('bsa_dashboard').getElementsByTagName('div');
    var els = new Array();
    for (var i = 0; i < els1.length; i++) {
        if (els1[i].className == 'inner_box') els.push(els1[i]);
    }
    var h = 0;
    for (var i = 0; i < els.length; i++) {
        els[i].style.height = 'auto';
        if (h < els[i].offsetHeight) h = els[i].offsetHeight;
    }
    for (var i = 0; i < els.length; i++) {
        els[i].style.height = h - 22 + 'px';
    }
}
function antiPiracyHeight() {
    if (!$('statistics_trends')) return;
    els1 = $('statistics_trends').getElementsByTagName('div');
    var els = new Array();
    for (var i = 0; i < els1.length; i++) {
        if (els1[i].className == 'inner') els.push(els1[i]);
    }
    var h = 0;
    for (var i = 0; i < els.length; i++) {
        els[i].style.height = 'auto';
        if (h < els[i].offsetHeight) h = els[i].offsetHeight;
    }
    for (var i = 0; i < els.length; i++) {
        els[i].style.height = h - 22 + 'px';
    }
}

function init() {
    stInit();
    issuesHeight();
    dashboardHeight();
    antiPiracyHeight();
    setHeight();
}
function init1() {
    stInit();
    issuesHeight();
    dashboardHeight();
    antiPiracyHeight();
    setHeight();
    menuHeight
}
window.onload = init;

