/**
 ** Resizes font-size in stylesheets, as long as they are set in px
 ** tSizes is an array with the text size changes, in px
 ** Uses Prototype
 **/
function cssTextResize(tSizes,cookieName, updateFunction) {
	this.cookieName = cookieName;
	this.chosenSize=readCookie(this.cookieName);
	//object to run on update, runs this object's 'onupdate' function, passing this 
	//as only parameter
	this.updateFunction = updateFunction;
	if(this.chosenSize==null ||this.chosenSize>=tSizes.size()) {
		createCookie(this.cookieName,0,3);
		this.chosenSize = 0;
	}
	this.sizes=tSizes;


	//Array of styleSheets
	//Note, before first resize, this will be empty.
	this.sSheets = new Array();
	
	this.recheckForStyleSheets = function() {
		if(document.styleSheets) {
			this.sSheets = styleSheetListToArray(document.styleSheets);
		} 
	};
	
	function styleSheetListToArray(sSheetList) {
		var nArray = new Array();
		for(var i=0;i<sSheetList.length;i++) {
			nArray.push(sSheetList[i]);
		}
		return nArray;
	}
	
	function sheetExistsIn(sheetList, childSheet) {
		
		for(var i=0; i<sheetList.length; i++) {
				if(sheetList[i]===childSheet) {
				return true;
				}
		}
		return false;
	}

	this.updateSize = function(changeSize){
		//
		if(typeof(changeSize)=='undefined'){
			changeSize=true;
		}
		var curSheets =  this.sSheets.clone();
		this.recheckForStyleSheets();
		prevSize=0;
		curSize=0;
		prevSize=this.sizes[this.chosenSize];
		if(changeSize) {
		//only change the Size if changeSize set to true 
		this.chosenSize++;
		}
		if(this.chosenSize>=this.sizes.length) {
			this.chosenSize=0;
		}
		curSize=this.sizes[this.chosenSize];
		//	alert("STARTchange:"+curSheets.length+"::"+curSize);
		for(var i=0;i<this.sSheets.length;i++)
		{
		var oRules = this.sSheets[i].rules ? this.sSheets[i].rules : this.sSheets[i].cssRules;
			for(var j=0;j<oRules.length ;j++) {
				curRule=oRules[j];
					//only change if set with px
					if(curRule.style.fontSize && curRule.style.fontSize.endsWith("px")) {
						if(sheetExistsIn(curSheets,this.sSheets[i])) {
						//if this sheet was changed before, 
							curRule.style.fontSize=(parseInt(curRule.style.fontSize)-prevSize+curSize)+'px';
						} else {
							curRule.style.fontSize=(parseInt(curRule.style.fontSize)+curSize)+'px';
						}
					}
			}
		}
	

	this.updateFunction.onUpdate(this);
	//alert(iconEl.getStyle('backgroundImage'));
	//update cookie.
	createCookie(this.cookieName,this.chosenSize,3);
	};

	//On creation, update texts to current state.
	this.updateSize(false);
};

//subfunction for cssTextResize.  
//Subbed into cssTextResize as param, then on update, this.onUpdate is called, subbing in
//parent cssTextResize as parameter
function updateIconPicture(iconId,imageArray ){
	this.iconId = iconId;
	this.imageArray = imageArray; // array of images for background

	//pass The 
	this.onUpdate=	function(resizer) {
		iconEl=$(this.iconId);
		iconEl.setStyle({backgroundImage:'url("'+this.imageArray[resizer.chosenSize]+'")'});
	}
}

//Taken from www.quirksmode.org/js/cookies.html
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 var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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 eraseCookie(name) {
	createCookie(name,"",-1);
}

