﻿// ----------------------------------------------
// 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;
	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") == 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, '')
		}
	}
}

