/*
FONT SIZE CONTROL CLASS

NEED PROTOYPE 1.6 to work

oBtn => id of the button
oCtn => id of the container that child element will have the font size change applied (default: body)
iMaxStep => maximum number of time to increment font before reset (default: 2)
iSizeJump => number of pixel to add to font size each time  (default: 2)
sClassIgnore => the name of the class to ignore (default: ignoreFont)
sCookieName => the name of the cookie (default: CGFontSize)
aReset => an array with the id of all element that are position absolute bottom that need to be fix for ie6 (default: empty array)

Usage exemple :

	var config = {
		oBtn: 'myBtn', 
		oCtn: 'myCtn',
		iMaxStep: 1,
		iSizeJump: 5,
		sClassIgnore: 'myClassToIgnore',
		sCookieName: 'myCookieName',
		aReset: new Array("myIdThatNeedTheIE6Fix", "myIdThatNeedTheIE6Fix_2"),
	}
	var oFontSize = new FontSizeControl(config);
	oFontSize.start();
*/
var FontSizeControl = Class.create({
	
	/********************************/
	// default properties 
	aBtnPlus: [],
	aCtn: [],
	aRteCtn: [],
	iCurStep: 0, 				// reference to the current step 0 = normal state
	iCookieJump: null,			// will be use to increment the font size after page load if there's a cookie
	bObjectIsReady: false,		// the class is ready or not
	
	/********************************/
	// constructor
	initialize: function(options){
		
		// method used to extend an object passed as parameter with default values
		var l_options = Object.extend({
			
			// required
			sCssBtnPlus: null,
			sCssCtn: null, 					// css selectors that will get class aplly to them
			aFontClass: [],					// list of class to be apply
			
			// optional
			sCssRteCtn: null, 				// css selectors for the rte
			iMaxStep: 2, 					// maximum number of time to increment font before reset
			iSizeJump: 2, 					// number of pixel to add each time for the RTE
			//sClassIgnore: 'ignoreFont', 	// class name that will ignore the font size change
			sCookieName: 'CGFontSize', 		// cookie name
			aReset: [] 						// array of element that need to reset their bottom pos fix for ie6 (ID of element)
		}, options || {});
		
		// extend object with options
		Object.extend(this, l_options);
		
		this.checkControl();
		
		if(this.bObjectIsReady){	
			// set current step to the cookie value
			if( (sCookieValue = this.readCookie()) != null){
				
				var iCookVal = parseInt(sCookieValue);
				// check that the cookie value is in the expected step range
				if(iCookVal > 0 && iCookVal <= this.iMaxStep){
	
					this.iCurStep = iCookVal;
					this.iCookieJump = this.iCurStep * this.iSizeJump;
					this.changeFontSize();
					this.iCookieJump = null;
				}
			}
		}
		
	},
	
	checkControl: function(){
		
		this.aBtnPlus = this.sCssBtnPlus ? $$(this.sCssBtnPlus) : [];
		this.aCtn = this.sCssCtn ? $$(this.sCssCtn) : [];
		this.aRteCtn = this.sCssRteCtn ? $$(this.sCssRteCtn) : [];
		
		var bFontClassNumber = false;
		if(this.aFontClass.length == this.iMaxStep){
			bFontClassNumber = true;
		}
		
		if(this.aBtnPlus.length > 0 && this.aCtn.length > 0 && bFontClassNumber){
			this.bObjectIsReady = true;
		}
	},

	
	/********************************/
	// start: click event on the button(s)
	start: function(){
		
		if(this.bObjectIsReady){
			
			this.aBtnPlus.invoke('observe', 'click', function(e){
				
				Event.stop(e);
				
				this.iCurStep++;
				// reset step if we reach max step 
				this.iCurStep > this.iMaxStep ? this.iCurStep = 0 : null;

				this.changeFontSize();
				this.createCookie();
				
			}.bind(this));	
			
		}
	},
	
	// change font size to all elements in the container
	changeFontSize: function(){
		
		// change font size of the RTE elements
		if(this.aRteCtn.length > 0){
			this.changeRteFontSize();	
		}	
		
		// remove class name
		this.aFontClass.each(function(sClass){
			this.aCtn.invoke('removeClassName', sClass);
		}.bind(this));
		
		if(this.iCurStep != 0){
			this.aCtn.invoke('addClassName', this.aFontClass[this.iCurStep-1]);
		}
		
		
		this.aReset.length > 0 ? this.resetAbsolutePos() : null;
	},
	
	// change font size to all elements in the RTE container
	changeRteFontSize: function(){
		
		this.aRteCtn.each(function(elem){
			
			var cpt = 0;
			var aProperty = new Array();
			
			elem.descendants().each(function(node){
				
				var iFontSize = parseInt(node.getStyle('fontSize').replace('px',''));
				
				// reset size if the current step is 0
				if(this.iCurStep == 0){
					iFontSize -= (this.iSizeJump * this.iMaxStep);
				}
				// set font size to the value of iCookieJump if it's not null
				else if(this.iCookieJump != null){
					iFontSize += this.iCookieJump;
				}
				// default behavior
				else{
					iFontSize += this.iSizeJump;
				}

				// create reference to each element and new font-size
				aProperty[cpt] = {element: node, fs: iFontSize};
				cpt++;
				
				
			}.bind(this));
			// change the font-size of each element
			aProperty.each(function(obj){
				obj.element.setStyle( {fontSize: obj.fs + 'px'} );
			});
			
		}.bind(this));
		
	},
	
	
	// create a cookie with the current step that will last for 365 days
	createCookie: function(){
		var date = new Date();
		date.setTime(date.getTime()+(365*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = this.sCookieName + "=" + this.iCurStep + expires + "; path=/";
	},
	
	// read cookie with the current step
	readCookie: function(){
		var sName = this.sCookieName + "=";
		var aCookie = document.cookie.split(';');
		for(var i=0, j=aCookie.length; i < j; i++) {
			var cookie = aCookie[i];
			while (cookie.charAt(0)==' '){ cookie = cookie.substring(1,cookie.length) };
			if (cookie.indexOf(sName) == 0){ 
				return cookie.substring(sName.length, cookie.length); 
			};
		}
		return null; 
	},
	
	// Fix Ie bottom Position of given elements
	resetAbsolutePos: function(){

		this.aReset.each(function(name){
			if($(name)){
				
				var iPos = $(name).getStyle('bottom');
				$(name).setStyle( {bottom: ''});
				$(name).setStyle( {bottom: iPos});
			}
		});	
	}
});