/*
 * @author     Andrei Eftimie
 * @email      k3liutZu@gmail.com
 * @copyright  (c) Andrei Eftimie 
 * @web        http://www.eftimie.ro
 * 
 * 
 * If you want persistance, you should use the jquery.cookie.js plugin
 * 
 * You should call this plugin on the element you have defined the lowest font-size denominator.
 * Usually the body or the html, sometimes on a #content or #wrapper.
 * 
 * You site must be built using em's, or this plugin wont work. It only changes the font-size of 1 element.
 * If the other elements are not related to that element (by using em's), no font-resize magic will happen
 * 
 * Calling
 * -------
 * $('body').textResize({
 * 		increase: '.increase-text-size',		//Increase text size button
 * 		descrease '.decrease-text-size',		//Decrease text size button
 * 		reset: '.default-text-size',			//Reset text size button {optionally}
 * 		pace: '1'								//How big the textsize jump will be (default 1px)
 * });         
 * 
 */

(function($){
	$.fn.textResize = function(options) {
	
		var defaults = {
			plus:  '.increase-text-size',
 			minus: '.decrease-text-size',
 			reset: '.default-text-size',	
	 		pace:  '1'	
		};
		
		var options = $.extend(defaults, options);
		
	    return this.each(function() {
	
			var body 			 = $(this);
			var plus 			 = options.plus;
			var minus 			 = options.minus;
			var reset 			 = options.reset;
			var pace 			 = eval(options.pace);
			var textSize		 = 16;
			var originalTextSize = 16;
			
			//we read the cookie
			var cookie = $.cookie('text-size');
			
			originalTextSize = body.css('font-size');
			originalTextSize = parseInt(originalTextSize.substring(0,originalTextSize.length-2)); 
			
			if (cookie) {
				update(cookie); //we update the document with the cookie value (persistance)
			} 
			
			//Initialize the size variable
			init();
			
			//Apply events to the buttons
			$(plus).click(function(){
				if (textSize < 14 ) {
					update(textSize+pace);	
				}
			init();
			});
			
			$(minus).click(function(){
				if (textSize > 10 ) {
					update(textSize-pace);
				}
				init();
			});
			
			$(reset).click(function(){
				update(original);
				init();
			});
			
			function init(){
				//Current font-size
				textSize = body.css('font-size');
				textSize = parseInt(textSize.substring(0,textSize.length-2));
				
				//Write cookie
				$.cookie('text-size', textSize);
			}
			
			function update(value){
				body.css('font-size',value+'px');
			}			
		})
		
	};
})(jQuery);
