FastInit.addOnLoad(function()
{
	$('text_size_1').observe('click', function(){
		set_font(false);
		this.addClassName('selected_text_size');
		$('text_size_2').removeClassName('selected_text_size');
	});
	$('text_size_2').observe('click', function(){
		set_font(true)
		this.addClassName('selected_text_size');
		$('text_size_1').removeClassName('selected_text_size');
	});
	
	$$('#body_content_intro', '#body_content_left', '#body_content_right').each(function(content){
		// it's okay if the first and last descendant are the same
		descendants = content.descendants().select(function(d){return d.tagName == "P"})
		if (descendants.length > 0)
		{
			if (descendants.first().descendants().select(function(d){
				return ["IMG", "OBJECT", "EMBED"].include(d.tagName);
			}).length > 0) {
				descendants.first().addClassName('complex_contents');
				content.addClassName('complex_contents_first');
			}
			
			descendants.first().addClassName('first_paragraph');
			
			if (descendants.last().descendants().select(function(d){
				return ["IMG", "OBJECT", "EMBED"].include(d.tagName);
			}).length > 0) {
				descendants.last().addClassName('complex_contents');
				content.addClassName('complex_contents_last');
			}
			descendants.last().addClassName('last_paragraph');
		}
		else if (content.descendants().length == 0)
		{
			content.addClassName('empty');
		}
	});
});

sfHover = function() {
	$$('#menu li').each(function(li) {
		li.onmouseover=function() {
			this.addClassName("sfhover");
		}
		li.onmouseout=function() {
			this.removeClassName("sfhover");
		}
	});
}

if (window.attachEvent) window.attachEvent("onload", sfHover);

set_font = function(upsize) {
	new Ajax.Request('/set_font?upsize=' + upsize.toString() + "&authenticity_token=" + encodeURIComponent($('form_authenticity_token').value));

		stylesheet = document.styleSheets[document.styleSheets.length-1];
		if (stylesheet != null && (stylesheet.href == null || stylesheet.href == "")) {
			rules = (stylesheet.rules || stylesheet.cssRules);
			if (upsize) {
				cssText = (rules[0] != null) ? ((rules[0].cssText != null) ? rules[0].cssText : rules[0].style.cssText) : null;
				if (rules.length == 0 || cssText[0] == "p" || cssText == "FONT-SIZE: 16px") {
					if (stylesheet.insertRule) {
						stylesheet.insertRule("p,h2,h3 { font-size:16px }", rules.length);
					}
					else if (stylesheet.addRule) {
						// approach things a wee bit differently here
						stylesheet.cssText = "p, h2, h3 { font-size: 16px }";
					};
				}							
			}
			else {
				for (i=0;i<rules.length;i++){
					if (stylesheet.removeRule) {
						stylesheet.removeRule(i);
					}
					else if (stylesheet.deleteRule) {
						stylesheet.deleteRule(i);
					}
				}
			}
		}
		else {
			if (upsize) {
				$('font_setting').innerHTML = upsize ? "p, h2, h3 {font-size: 16px}" : "";
			}
			else {
				$('font_setting').innerHTML = "";
			}
		}
}