//list of all possible css values for each item we can change (comma separated) and their corresponding control names
var possibleFontSizes = 	'smalltext,bigtext,'; 					//possible css names in CSS
var fontControlNames = 		'textsmall,textbig,textreset'; 		//corresponding control name ids in HTML
var fontControlChar = 		'tselected';								//the name of the icon to show which is selected in HTML
var possibleWidths = 		'widepage,';
var widthControlNames = 	'widthwide,widthnarrow';
var widthControlChar = 		'wselected';
//end of configuration

function setFontSize(fontSize) {
	if (replaceClasses(fontSize, possibleFontSizes) ) {
		setSelected(fontSize, possibleFontSizes, fontControlChar, fontControlNames);
		currentFontSize = fontSize;
	}
};

function changeWidth(pagewidth) {
	if (replaceClasses(pagewidth, possibleWidths)) {
		setSelected(pagewidth, possibleWidths, widthControlChar, widthControlNames);
		currentWidth = pagewidth;
	}
};


var currentFontSize = '';
var currentWidth = '';
var prefsLoaded = false;

function revertStyles(){
	setFontSize();
	changeWidth();
}

function toggleExpand( name ) {
    name.className = ( name.className == 'expanded' ) ? 'expanding':'expanded';
}


function setSelected (aVal, possibleVals, aControl, possibleControls) {
	var a = possibleVals.split(',');	
	var pc = possibleControls.split(',');
	for (i = 0; i < a.length; i++) {
		if (aVal == a[i]) {
			break;
		}
	}
	
	if (i < a.length && pc.length == a.length) {
		var b = document.getElementById(aControl).parentNode;
		if (b) {
			var oldChild = b.removeChild(document.getElementById(aControl));
			var c = document.getElementById(pc[i]);
			c.appendChild(oldChild);
		};
	}
}

function replaceClasses (newclass, possible_old_vals) {
		var b = document.body;
		var bsplit = b.className.split(' ');
		var newb = ''

		for (var i=0; i < bsplit.length; i++) {
      	if (-1 == possible_old_vals.search(bsplit[i])) {
				newb += bsplit[i] + ' ';
			}
	   }

		b.className = newb + newclass;
		
		return true;
}

function createCookie(name,value,days,path,domain,secure) {
  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 ) ? "; path=" + path : "; path=/" ) + 
		( ( domain ) ? "; domain=" + domain : "" ) +
		( ( secure ) ? "; secure" : "" );
};

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 setUserOptions() {
	if(!prefsLoaded){
		cookie = readCookie("fontSize");
		if (cookie && cookie != currentFontSize) {
			currentFontSize = cookie;
			setFontSize(currentFontSize);			
		}


		cookie = readCookie("pageWidth");
		if (cookie && cookie != currentWidth) {
			currentWidth = cookie ;
			changeWidth(currentWidth);
		}
				
		prefsLoaded = true;
	}
};

function saveSettings() {
	createCookie("fontSize", currentFontSize, 365, "/");
	createCookie("pageWidth", currentWidth, 365, "/");
};

/*
addEvent function found at http://www.scottandrew.com/weblog/articles/cbs-events
*/
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

if(document.getElementById && document.createTextNode) {
	addEvent(window, 'unload', saveSettings);
	addEvent(window, 'load', setUserOptions);
}