//--------------------------------------------------------------
// UbiStyleSwitch class
// created 20/07/05 : hubby
// setActiveStyleSheet, getActiveStyleSheet and getPreferredStyleSheet 
// methods originally written by paul sowdon (http://idontsmoke.co.uk/)
//--------------------------------------------------------------

var UbiStyleSwitch = {

	ea : new Array(),

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

	handleStyleChange : function(e) {
		var target = UbiEvent.getTarget(UbiEvent.getEvent(e));
		
		UbiStyleSwitch.setActiveStyleSheet(UbiStyleSwitch.findLink(target).className);
		
		UbiStyleSwitch.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;
	},

// ------------------------------------------------------
	
	setActiveStyleSheet : function(title) {
  	var i, a;
  	for(i = 0; (a = document.getElementsByTagName('head')[0].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;
      		UbiCookie.createCookie("style", 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') UbiStyleSwitch.ea.push(rule.item(y).selectorText);
					else if(rule[y].style.position && rule[y].style.position == 'absolute') UbiStyleSwitch.ea.push(rule[y].selectorText);
				}
			}
		}
	},

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

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

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

	getActiveStyleSheet : function() {
  	var i, a;
  	for(i = 0; (a = document.getElementsByTagName('head')[0].getElementsByTagName("link")[i]); i++) {
    	if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  	}
  	return null;
	},

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

	getPreferredStyleSheet : function() {
  	var i, a;
  	for(i = 0; (a = document.getElementsByTagName('head')[0].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;
  },

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

	cancelClick : function() {
		return false;
	},

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

	addListeners : function() {
		var links = document.getElementById('text_size').getElementsByTagName('a');
		for(var i = 0; i < links.length; i++) {
			UbiEvent.addEvent(links[i], 'click', UbiStyleSwitch.handleStyleChange, false);
			links[i].onclick = UbiStyleSwitch.cancelClick;
		}
	},

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

	init : function() {
		if(!document.getElementById &&
		!document.getElementsByTagName) return false;
		
		var t;
		
		UbiStyleSwitch.findAbsoluteElements();
		
		if(t = document.getElementById('text_size')) {
			t.innerHTML += '<h3>Text size</h3><ul><li><a href="#" class="standard_size">A</a></li><li><a href="#" class="big_size">A</a></li><li><a href="#" class="biggest_size">A</a></li></ul>';
			UbiStyleSwitch.addListeners();
			var c = UbiCookie.readCookie("style");
  		var title = c ? c : UbiStyleSwitch.getPreferredStyleSheet();
  		UbiStyleSwitch.setActiveStyleSheet(title);
  		UbiStyleSwitch.refreshAbsoluteElements();
		}
	}

};