var stylesheetActivationsInitialised = false;
var navInitOffset;
var navigation;
var main;
var mainOffset;
var footer;

$(document).ready(function() {
	initialiseStylesheetActivations();
});

$(window).load(function() {
	initialiseStylesheetActivations();
	$("link[rel~='stylesheet']").each(function() {
		this.stylesheet = getStylesheetByHref(this.href);
		if (this.stylesheet)
			this.stylesheet.link = this;
	});

	navigation = $("#navigation");
	main = $(".main:first");

	footer = $("#footer");
	footer.bottomLayout = new Object();
	footer.bottomLayout.outerHeight = footer.outerHeight();
	footer.bottomLayout.outerWidth = footer.outerWidth();
	footer.bottomLayout.height = footer.height();
	footer.bottomLayout.width = footer.width();
	footer.bottomLayout.initOffset = footer.offset();

	if (sideMenu()){
		footer.sideLayout = new Object();
		footer.css({"width": navigation.children("ol").width() + "px", "position": "fixed", "top": navigation.outerHeight() + "px", "left": navigation.offset().left + "px"});
		footer.sideLayout.outerHeight = footer.outerHeight();
		footer.sideLayout.outerWidth = footer.outerWidth();
		footer.sideLayout.height = footer.height();
		footer.sideLayout.width = footer.width();
		footer.css({"width": "", "position": "", "top": "", "left": ""});
	}
	
	navInitOffset = navigation.offset();
	mainOffset = {top: 0, left: 0};

	$("link.menuTop[rel~='stylesheet']").each(function() {
		if (this.stylesheet && !this.stylesheet.disabled){
			mainOffset.top = navigation.outerHeight();
			return false;
		}
	});
/*
	$("link.menuSide[rel~='stylesheet']").each(function() {
		var stylesheet = getStylesheetByHref(this.href);
		if (stylesheet && !stylesheet.disabled){
			mainOffset.left = navigation.outerWidth() / 2;
			main.css({"margin-left": "auto", "left": mainOffset.left + "px"});
			return false;
		}
	});
*/
	
	if ($.support.positionFixed){
		if (sideMenu()){
			footer.css({"position": "", "bottom": "", "left": ""});
		}
		else {
			$("body").css({"margin-bottom": ($(window).height() + $(window).scrollTop() - footer.offset().top + mainOffset.top) + "px"});
		}

		$(window).resize(fixMenu);
		$(window).scroll(fixMenu);
		fixMenu();
	}
});

function sideMenu(){
	return $("link.menuSide[rel~='stylesheet']").filter(function(){
		return this.stylesheet && !this.stylesheet.disabled;
	}).length > 0;
}

var disabled = false;

function fixMenu() {
if (disabled)
	return;
	if (navInitOffset.top + navigation.outerHeight() > footer.bottomLayout.initOffset.top)
		$("body").css({"min-height": (navigation.outerHeight() + navInitOffset.top + footer.bottomLayout.outerHeight) + "px"});
	else
		$("body").css({"min-height": (footer.bottomLayout.initOffset.top + footer.bottomLayout.outerHeight) + "px"});

	var hiddenMenus = $("#navigation ol").filter(function() {
			return $(this).css("position") == "absolute";
		});
	var windowHeight = $(window).height();
	var windowScrollTop =  + $(window).scrollTop();

	var tallHiddenMenus = hiddenMenus.filter(function() {
			return $(this).outerHeight() > windowHeight;
		});
	var shortHiddenMenus = hiddenMenus.not(tallHiddenMenus);

	var sideFooter = sideMenu() && $("body").height() > navigation.outerHeight() + footer.sideLayout.outerHeight;
	var freeScrolling = navigation.outerHeight() + (sideFooter ? footer.sideLayout.outerHeight : footer.bottomLayout.outerHeight) > windowHeight || tallHiddenMenus.length > 0;

	if (freeScrolling || navInitOffset.top > $(window).scrollTop()){
		navigation.css({"position": "", "top": "", "left": ""});
		main.css({"top": ""});
	}
	else {
		navigation.css({"position": "fixed", "top": ((navigation.innerHeight() - navigation.outerHeight()) / 2) +"px", "left": navigation.offset().left + "px"});
		main.css({"top": mainOffset.top + "px", "margin-bottom": "0"});
	}

	if (sideFooter){
		footer.css({"width": navigation.children("ol").width(), "bottom": ""});
		footer.find("legend").css({"width": "100%", "margin-right": "0"});
		if (freeScrolling || navInitOffset.top > $(window).scrollTop())
			footer.css({"position": "absolute", "top": (navigation.offset().top + navigation.outerHeight()) + "px", "left": ""});
		else
			footer.css({"position": "fixed", "top": navigation.outerHeight() + "px", "left": navigation.offset().left + "px"});
	}
	else {
		footer.css({"width": footer.bottomLayout.width + "px", "position": "fixed", "top": "", "bottom": "0", "left": ""});
		footer.find("legend").css({"width": "", "margin-right": ""});
		//if (sideMenu() && $("#main").outerHeight() < navigation.outerHeight())
		//	footer.css({"width": $("#main").innerWidth() + "px", "position": "absolute", "top": "", "bottom": "", "left": navigation.outerWidth() + "px"});
	}
	
	tallHiddenMenus.css({"top": "", "bottom": ""});
	shortHiddenMenus.each(function() {
			$(this).css({"top": "", "bottom": ""});
			if ($(this).outerHeight() + $(this).offset().top > windowHeight + windowScrollTop){
				var parent = $(this).offsetParent();
				$(this).css({"top": "auto", "bottom": (parent.offset().top + parent.height() - windowHeight - windowScrollTop) + "px"});
			}
		});

	navigation.trigger("positioned");
}

function initialiseStylesheetActivations() {
	if (!stylesheetActivationsInitialised)
		setStylesheetActivations();
	stylesheetActivationsInitialised = true;
}

function setStylesheetActivations() {
	var Links = document.getElementsByTagName("link");
	var stylesheet;

	var re;
	var m;
	var link;

	// Safari doesn't load alternate stylesheets properly, so load only the ones that should be shown here instead.
	for (var index = 0; index < Links.length; index++) {
		link = Links[index];
		if (link.rel == "alternate stylesheet" && !getStylesheetByHref(link.href)) {
			uri = parseUri(link.href);
			re = new RegExp("^(?:.*;)?[\s ]*stylesheet[[]" + escape(uri.file) + "]\.disabled[\s ]*=[\s ]*([^;]*)[\s ]*(?:;.*)?$");
			m = re.exec(document.cookie);
			if (m && m[1] == "false") {
				link.rel = "stylesheet";
				link.title = "";
			}
		}
	}

	var specified = false;
	for (var index = 0; index < document.styleSheets.length; index++) {
		stylesheet = document.styleSheets[index];
		if (stylesheet.href && stylesheet.type == "text/css") {
			uri = parseUri(stylesheet.href);
			re = new RegExp("^(?:.*;)?[\s ]*stylesheet[[]" + escape(uri.file) + "]\.disabled[\s ]*=[\s ]*([^;]*)[\s ]*(?:;.*)?$");
			m = re.exec(document.cookie);
			if (m) {
				specified = true;
				stylesheet.disabled = m[1] == "true";
			}
		}
	}
}

function getStylesheetByHref(href) {
	for (var index = 0; index < document.styleSheets.length; index++)
		if (document.styleSheets[index].href == href)
		return document.styleSheets[index];
}

function getStylesheetByFilename(fileName) {
	var href = "styles/" + fileName;
	var stylesheet = getStylesheetByHref(href);

	if (!stylesheet) {
		href = "2011/styles/" + fileName;
		stylesheet = getStylesheetByHref(href);
	}

	if (!stylesheet) {
		href = location.protocol + "//" + location.host + "/2011/styles/" + fileName;
		stylesheet = getStylesheetByHref(href);
	}

	if (!stylesheet) {
		href = location.protocol + "//" + location.host + "/styles/" + fileName;
		stylesheet = getStylesheetByHref(href);
	}

	return stylesheet;
}

function parseUri(str) {
	var o = parseUri.options,
		m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
	q: {
		name: "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};





