//--------------------------------------------------------------
// UbiFontSwitch class
// created 07/09/05 : hubby@ubisan.com
// copyright ubisan ltd 2006
// utilises sciptaculous effect library by thomas fuchs 
// (http://script.aculo.us, http://mir.aculo.us)
//--------------------------------------------------------------

var UbiFontSwitch = {

	ea : new Array(),
	originalFont : null,
	fontChangeOpenText : 'Choose a font',
	fontChangeCloseText : 'Close font options',
	otherPlaceholderText : 'pick your own',

// ------------------------------------------------------

	handleFontChange : function(e) {
		
		var target = UbiEvent.getTarget(UbiEvent.getEvent(e));
		
		UbiFontSwitch.setActiveFont(UbiFontSwitch.findLink(target).className);
		
		UbiFontSwitch.refreshAbsoluteElements();
		
		if(window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if(e && e.stopPropagation && e.preventDefault) {
			e.stopPropagation();
			e.preventDefault();
		}
	},

// ------------------------------------------------------

	handleOtherFontChange : function(e) {
		var title = document.getElementById('font_family_other_input').value;
		UbiFontSwitch.setActiveFont(title);
		
		UbiFontSwitch.refreshAbsoluteElements();
		
		if(window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if(e && e.stopPropagation && e.preventDefault) {
			e.stopPropagation();
			e.preventDefault();
		}
	},

// ------------------------------------------------------
	
	findLink : function(t) {
		while(t != document.body && t.nodeName.toLowerCase() != 'a') t = t.parentNode;
		return t;
	},

// ------------------------------------------------------
	
	setActiveFont : function(title) {
		if(title) {
			if(title == 'reset') {
				document.body.style.fontFamily = UbiFontSwitch.originalFont;
				UbiCookie.eraseCookie('font');
			}
			else {
				title = title.replace(/_/g, ' ');
				document.body.style.fontFamily = title;
	  		UbiCookie.createCookie("font", title, 365);
  		}
  	}
  },

// ------------------------------------------------------

	findAbsoluteElements : function() {
		var bo = document.getElementsByTagName('body');
		var s = document.styleSheets;
		for(var x = 0; x < s.length; x++) {
			var rule = null;
			if(document.styleSheets[x].rules) rule = s[x].rules;
			else if(document.styleSheets[x].cssRules) rule = s[x].cssRules;
			if(rule) {
				for(var y = 0; y < rule.length; y++){
					if(rule.item(y).style.getPropertyValue && rule.item(y).style.getPropertyValue('position') == 'absolute') UbiFontSwitch.ea.push(rule.item(y).selectorText);
					else if(rule[y].style.position && rule[y].style.position == 'absolute') UbiFontSwitch.ea.push(rule[y].selectorText);
				}
			}
		}
	},

// ------------------------------------------------------

	refreshAbsoluteElements : function() {
		if(UbiFontSwitch.ea.length > 0) {
			for(var q = 0; q < UbiFontSwitch.ea.length; q++) {
			
				if(UbiFontSwitch.ea[q][0] == '#') {
					if(elem = document.getElementById(UbiFontSwitch.ea[q].substr(1))) {
						elem.style.display = 'none';
						elem.style.display = '';
					}
				}
			
				// safari defines ids as *[ID"id"]
				else if(UbiFontSwitch.ea[q].substring(0,4) == '*[ID') {
					var e = UbiFontSwitch.ea[q].slice(5,-2);
					elem = document.getElementById(e);
					elem.style.display = 'none';
					elem.style.display = '';
				}
			
				else if(UbiFontSwitch.ea[q][0] == '.') {
					var cid = UbiFontSwitch.ea[q].substr(1);
					for(w = 0; w < bo.length; w++) {
						if(bo[w].className == cid) {
							bo[w].style.display = 'none';
							bo[w].style.display = '';
						}
					}
				}
			
				else {
					elem = document.getElementsByTagName(UbiFontSwitch.ea[q]);
					for(var e = 0; e < elem.length; e++) {
						elem[e].style.display = 'none';
						elem[e].style.display = '';
					}
				}
			}
		}
	},

// ------------------------------------------------------

	getActiveFont : function() {
  	return document.body.style.fontFamily;
	},

// ------------------------------------------------------

// legacy method for browsers that don't understand stopPropagation()

	cancelClick : function() {
		return false;
	},

// ------------------------------------------------------

	handleOtherToggleClick : function(e) {
		UbiFontSwitch.handleOtherToggle(true);
	},

// ------------------------------------------------------

	handleOtherToggle : function(doEffect) {
		var ul = document.getElementById('font_family_ul');
		if(ul.style.display == 'none') {
			if(doEffect) Effect.SlideDown(ul.id);
			else ul.style.display = 'inline';
			if(!UbiCookie.readCookie('show_font_options')) {
				UbiCookie.createCookie('show_font_options', 'true', 365);
			}
		}
		else {
			if(doEffect) Effect.SlideUp(ul.id);
			else ul.style.display = 'none';
			UbiCookie.eraseCookie('show_font_options');
		}
		UbiFontSwitch.refreshAbsoluteElements();
	},

// ------------------------------------------------------

	handleInputClear : function(e) {
		var t = UbiEvent.getTarget(UbiEvent.getEvent(e));
		if(t.value == UbiFontSwitch.otherPlaceholderText) t.value = '';
	},

// ------------------------------------------------------

	addListeners : function() {
		var links = document.getElementById('font_family').getElementsByTagName('a');
		for(var i = 0; i < links.length; i++) {
			if(links[i].id == 'font_family_toggle') UbiEvent.addEvent(links[i], 'click', UbiFontSwitch.handleOtherToggleClick, false);
			else UbiEvent.addEvent(links[i], 'click', UbiFontSwitch.handleFontChange, false);
			links[i].onclick = UbiFontSwitch.cancelClick;
		}
		UbiEvent.addEvent(document.getElementById('font_family_other_submit'), 'click', UbiFontSwitch.handleOtherFontChange, false);
		document.getElementById('font_family_other_submit').onclick = UbiFontSwitch.cancelClick;
		
		UbiEvent.addEvent(document.getElementById('font_family_other_input'), 'click', UbiFontSwitch.handleInputClear, false);
		UbiEvent.addEvent(document.getElementById('other_font_form'), 'submit', UbiFontSwitch.handleOtherFontChange, false);
		document.getElementById('other_font_form').onsubmit = UbiFontSwitch.cancelClick;
	
		UbiEvent.addEvent(document.getElementById('font_family_toggle'), 'click', UbiFontSwitch.swapLinkText, false);
		
	},

// ------------------------------------------------------

	getOriginalFont : function() {
		var s = document.styleSheets;
		for(var x = 0; x < s.length; x++) {
			var rule = null;
			if(document.styleSheets[x].rules) rule = s[x].rules;
			else if(document.styleSheets[x].cssRules) rule = s[x].cssRules;
			if(rule) {
				for(var y = 0; y < rule.length; y++) {
					if(rule.item(y).selectorText && rule.item(y).selectorText.toLowerCase() == 'body') return UbiFontSwitch.cleanCode(rule.item(y).style.fontFamily);
					else if(rule[y].cssText && rule[y].cssText.toLowerCase() == 'body') return UbiFontSwitch.cleanCode(rule[y].style.fontFamily);
				}
			}
		}
		return false;
	},

// ------------------------------------------------------

	cleanCode : function(code) {
		var c = code.replace(/\'/g, '\"');
		return c;
	},

// ------------------------------------------------------

	swapLinkText : function() {
		var t = document.getElementById('font_family_toggle');
		if(t.firstChild.nodeValue == UbiFontSwitch.fontChangeOpenText) t.firstChild.nodeValue = UbiFontSwitch.fontChangeCloseText;
		else t.firstChild.nodeValue = UbiFontSwitch.fontChangeOpenText;
	},

// ------------------------------------------------------

	init : function() {
		if(!document.getElementById &&
		!document.getElementsByTagName) return false;
		
		var t;
		
		UbiFontSwitch.findAbsoluteElements();
		
		if(t = document.getElementById('font_family')) {
			t.innerHTML += '<h3>Font options</h3><h4><a href="#" id="font_family_toggle">'+UbiFontSwitch.fontChangeOpenText+'</a></h4><div id="font_family_ul" style="display: none;"><ul><li><a href="#" class="arial" style="font-family: arial;">Arial</a></li><li><a href="#" class="verdana" style="font-family: verdana;">Verdana</a></li><li><a href="#" class="georgia" style="font-family: georgia;">Georgia</a></li><li><a href="#" class="times_new_roman" style="font-family: \'times new roman\';">Times New Roman</a></li><li><a href="#" class="courier_new" style="font-family: \'courier new\';">Courier New</a></li><li class="reset_li"><form id="other_font_form" action="#"><input type="text" id="font_family_other_input" value="'+UbiFontSwitch.otherPlaceholderText+'" /><input type="submit" value="go" id="font_family_other_submit" /></form></li><li class="reset_li"><a href="#" class="reset" style=\'font-family: '+UbiFontSwitch.getOriginalFont()+'\';">Reset the original font</a></li></ul></div>';
			UbiFontSwitch.addListeners();
			var c = UbiCookie.readCookie("font");
  		var title = c ? c : null;
  		UbiFontSwitch.setActiveFont(title);
  		var s = UbiCookie.readCookie("show_font_options");
  		var show = s ? s : null;
  		if(show == 'true') {
  			UbiFontSwitch.swapLinkText();
  			UbiFontSwitch.handleOtherToggle(false);
  		}
  		UbiFontSwitch.refreshAbsoluteElements();
		}
	}

};
