/**
 * User Friendly js 
 * @author      Frank van Noorden <fnoorden@gmail.com> 
 * @license     will be released under the same LICENSE as symfony
 * @version     0.0.1
 */

var prefsLoaded = false;
var defaultFontSize = 100;
var currentFontSize = defaultFontSize;

/**
 * addLoadEvent: Add event handler to body when window loads 
 */
// Multiple onload function created by: Simon Willison
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent

function addLoadEvent(func) {
    var oldonload = window.onload;

    if (typeof window.onload != "function") {
        window.onload = func;
    } else {
        window.onload = function () {
            oldonload();
            func();
        }
    }
}

/**
 * changeFontSize: Change font size
 */
function changeFontSize(rulename, newSize){
	if(newSize == 'plus') currentFontSize = parseInt(currentFontSize) + 10;
	if(newSize == 'min') currentFontSize = parseInt(currentFontSize) - 10;
	if(newSize == 'std') currentFontSize = 100;
	
	if(currentFontSize > 150){
	currentFontSize = 150;
	}else if(currentFontSize < 50){
	currentFontSize = 50;
	}
	
	createCookie("fontSize", currentFontSize, 365);
	if(newSize == 'std'){
		window.location = document.location.href;
	}else{
		setFontSize(rulename, currentFontSize);
	}
}

/**
 * setFontSize: Set font size 
 */
function setFontSize(ruleName, newFontSize){
var rule = getCSSRule(ruleName);
rule.style.fontSize= newFontSize + '%';
}

/**
 * setUserOptions: Get cookie and set stored font size
 */
function setUserOptions(rulename){
if(!prefsLoaded){

cookie = readCookie("fontSize");
currentFontSize = cookie ? cookie : defaultFontSize;
setFontSize(rulename, currentFontSize);

prefsLoaded = true;
}

}

/**
 * createCookie: Create cookie, takes number of days as input
 */
function createCookie(name,value,days) {
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=/";
}

/**
 * readCookie: Read cookie
 */
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;
}

/**
 * getCSSRule: Get the CSS rule
 */
function getCSSRule(ruleName) { 
	ruleName=ruleName.toLowerCase(); 
	if (document.styleSheets) { 
		for (var i=0; i<document.styleSheets.length; i++) { 
			var styleSheet=document.styleSheets[i]; 
			var ii=0; 
			var cssRule=false; 
			do { // For each rule in the stylesheet
				if (styleSheet.cssRules) { 
					cssRule = styleSheet.cssRules[ii]; // Mozilla
				} else { 
					cssRule = styleSheet.rules[ii]; // IE style 
				} 
				if (cssRule) { // Rule found
					if (cssRule.selectorText.toLowerCase()==ruleName) { 
							return cssRule; // Return the style object
					} // End found rule name
				} // end found cssRule
				ii++; 
			} while (cssRule) // end Do While loop
		} // end For loop
	} // end styleSheet ability check
	return false; // Nothing found
} // end getCSSRule 

/**
 * setPrintMethod: Set print method
 */
function setPrintMethod() { // Set print method

var x = readCookie("userfriendly_print")
if (x == 0) {
	x =1;
} else {
	x=0;
}
createCookie("userfriendly_print",x,365,"/");

window.location = document.location.href;

return false;
}