/**
 * class font resizer
 * 
 * @copyright Zig Websoftware (2009)
 */
var FontSize = Class.create();
FontSize.prototype = {
	
	Version: "1.0",
	/**
	 * holds the last step of the font resize
	 */
	iLastStep: null,
	/**
	 * current step of resizing
	 */
	iCurrentSizeStep: null,
	
	/**
	 * initialize the font resizer
	 * 
	 * @param iStep
	 * @param aOptions
	 * @return void
	 */
	initialize: function(iStep, aOptions) {
	
		//first set the given step as the current step
		this.iCurrentSizeStep = iStep;
		
		this.setOptions(aOptions);
		
		this.setStep(iStep);

		Event.observe(window, 'load', this.resize.bind(this));
	},
	
	/**
	 * set the options
	 * 
	 * @param aOptions
	 * @return void
	 */
	setOptions: function(aOptions) {
	    this.options = {
	    	iTotalSizeSteps	: 	3,
	    	iStepSize		: 	2,
	    	sDiv			: 	'container'
	    }
	    
	    Object.extend(this.options, aOptions || {});
	  },
	  
	/**
	 * set the resize step
	 * 
	 * @param iStep
	 * @return void
	 */	  
	  setStep: function(iStep)
	  {	 
		  if(iStep != undefined)
		  {
			  if(iStep > this.options.iTotalSizeSteps || iStep < 1)
			  {
				  iStep = 1;
			  }

			  this.iLastStep = this.iCurrentSizeStep;
			  
			  this.iCurrentSizeStep 	= iStep;

			  new Ajax.Request('/styles/set/'+this.iCurrentSizeStep);
		  }
	  },
	  
	/**
	 * get the children of the element given in options array to resize all those children
	 * 
	 * @param iStep
	 * @return void
	 */	 
	  resize: function()
	  {
		  if(this.iCurrentSizeStep == 1 && this.iLastStep == 1)
		  {
		  }
		  else
		  {
			  $(this.options.sDiv).descendants().each(this.changeFontSize.bind(this));  
		  }
	  },
	  
	  /**
	   * increase the font
	   * @return void
	   */
	  increase: function()
	  {
		  var iStep = this.iCurrentSizeStep + 1;
		  this.setStep(iStep);
		  
		  this.resize();
	  },
	  
	  /**
	   * decrease the font
	   * @return void
	   */	  
	  decrease: function()
	  {
		  var iStep = this.iCurrentSizeStep - 1;
		  this.setStep(iStep);
		  
		  this.resize();
	  },
	  
	  /**
	   * change the fontsize of an element
	   * 
	   * @param oElement
	   * @return void
	   */
	  changeFontSize: function(oElement)
	  {
		  
	  	 if(	(	oElement.tagName == 'P' 
	  		 		|| oElement.tagName == 'SPAN' 
	  		 		|| oElement.tagName == 'LI' 
	  		 		|| oElement.tagName == 'LABEL'
	  		 		|| oElement.tagName == 'H1'
	  		 		|| oElement.tagName == 'H2'
	  		 		|| oElement.tagName == 'H3'
	  		 	)  
		  		 && oElement.getStyle('fontSize') 
		  		 && !oElement.hasClassName('noresize') 
		  		 && oElement.getStyle('fontSize').match('px')
	  		 ) 
	  	 {
	  		 var iSize = parseInt(oElement.getStyle('fontSize').replace("px",""));
	  	     
	  	     /**
	  	      * resize the the size to default with help of the laststep 
	  	      * to count how many times we need to get back
	  	      */
	  	     if((this.iCurrentSizeStep == 1) && this.iCurrentSizeStep != this.iLastStep)
	  	     {
	  	    	 iSize -= ((this.iLastStep - 1) * this.options.iStepSize); 
	  	     }
	  	     /**
	  	      * iCurrentSizeStep is not one, so we are resizing
	  	      */
	  	     else if(this.iCurrentSizeStep > 1)
	  	     {
	  	    	 /**
	  	    	  * in case the the page is refreshed you can inmediately start with a higher step, 
	  	    	  * so count the total resizing instead of just adding the stepsize
	  	    	  */
	  	    	 if(this.iLastStep == this.iCurrentSizeStep)
	  	    	 {
	  	    		 iSize += ((this.iCurrentSizeStep - 1) * this.options.iStepSize);
	  	    	 }
	  	    	 else
	  	    	 {
	  	    		 iSize += this.options.iStepSize;
	  	    	 }
	  	     }
	  	   
	  	     oElement.style.fontSize = iSize+"px"
	  	 } 
	  }
};
