 /**
 * aTextsize - Text resizing  with jQuery
 *   http://labs.ansa.it/textsize/
 *
 * Author: Angelo Marrara (marrara@ansa.it)
 * Copyright: free
 *
 * Built on top of the jQuery library
 * http://jquery.com
 *
 */

(function($) {
    /**
     * Creates a textsize toolbar for all matched elements.
     *
     * @example $("#mytextsize").atextsize();
     * @before 
     *
     * <ul id="mytextsize" class="mytextsize-atextsize-container">
	 * </ul>
     * 
     * @result
     * <ul id="mytextsize" class="mytextsize-atextsize-container">
     * 	<li id="mytextsize-decreaseButton-atextsize">-A</li>
     * 	<li id="mytextsize-resetButton-atextsize">A</li>
     * 	<li id="mytextsize-increaseButton-atextsize">+A</li>
	 * </ul>
     * 
     * @name atextsize
     * @type jQuery
     * @param Hash o A set of key/value pairs to set as configuration properties.
     * @cat Plugins/aTextSize
     */
	$.fn.atextsize = function(options) {
          // Build main options before element iteration
		  // Note that the first arg to extend is an empty object 
  		  // this is to keep from overriding our "defaults" object.
          var opts = $.extend({}, $.fn.atextsize.defaults, options);
          // iterate and reformat each matched element
          return this.each(function() {
          	this.guid = $(this).attr('id');
          	debug("Init() for id "+this.guid);
			$.cookie(this.guid+'atextsize', '0');
			// The main element
            this.container  = this;
            // build element specific options
            var o = $.metadata ? $.extend({}, opts, $.metadata.get(this)) : opts;
	        // The html code for the decrease button
        	this.buttonDecreaseHTML = '<li id="'+this.guid+'-atextsize-decr">'+o.buttonDecreaseLabel+'</li>';
	        // The html code for the reset button
        	this.buttonResetHTML = '<li id="'+this.guid+'-atextsize-reset">'+o.buttonResetLabel+'</li>';
	        // The html code for the increase button
        	this.buttonIncreaseHTML = '<li id="'+this.guid+'-atextsize-incr">'+o.buttonIncreaseLabel+'</li>';
			// Inject the decrease button
    	    $(this.container).append(this.buttonDecreaseHTML);
			// Inject the reset button
    	    $(this.container).append(this.buttonResetHTML);
			// Inject the increase button
    	    $(this.container).append(this.buttonIncreaseHTML);
			// The button decrease
        	this.buttonDecrease = $('#'+this.guid+'-atextsize-decr', this.container);
			// The button reset
        	this.buttonReset = $('#'+this.guid+'-atextsize-reset', this.container);        	        	                        
			// The button increase
        	this.buttonIncrease = $('#'+this.guid+'-atextsize-incr', this.container);        	        	                        
            // update element styles
    	    // Add the buttons action
			addActionDecrease(this.buttonDecrease);			
			addActionReset(this.buttonReset);
			addActionIncrease(this.buttonIncrease);			
          });
        };       

        //
        // private function for debugging
        //
        function debug(msg) {
          if (window.console && window.console.log)
            window.console.log(msg);
        };
        //
        // private function for decrease the font 
        //
        addActionDecrease = function(e) {        	        	
		  // Decrease Font Size
    	    var markup = $(e).html();
    	    markup = '<a href="#">' + markup + '</a>';
    	    e.html(markup);
    	    e.children('a').click(function(){
		    var currentFontSize = $('.article-content ').css('font-size');
		    var currentFontSizeNum = parseFloat(currentFontSize, 12);
		    var newFontSize = currentFontSizeNum*0.8;
		    if (newFontSize >=10)
			    $('.article-content ').css('font-size', newFontSize);
		    return false;
		  });    	    
        };
        //
        // private function for increase the font 
        //
        addActionIncrease = function(e) {        	        	
		  // Increase Font Size
    	    var markup = $(e).html();
    	    markup = '<a href="#">' + markup + '</a>';
    	    e.html(markup);
    	    e.children('a').click(function(){
		    var currentFontSize = $('.article-content ').css('font-size');
		    var currentFontSizeNum = parseFloat(currentFontSize, 12);
		    var newFontSize = currentFontSizeNum*1.20;
		    if (newFontSize <=32)
		    	$('.article-content ').css('font-size', newFontSize);
		    return false;
		  });        
  		};
        //
        // private function for increase the font 
        //
        addActionReset = function(e) {        	        	
		  // Reset Font Size
    	    var markup = $(e).html();
    	    markup = '<a href="#">' + markup + '</a>';
    	    e.html(markup);
    	    var originalFontSize = $('.article-content ').css('font-size');
		    e.children('a').click(function(){
		    $('.article-content ').css('font-size', originalFontSize);
		  });
  		};
        //
        // plugin defaults
        //
        $.fn.atextsize.defaults = {
//        buttonDecreaseLabel: '-A',
//        buttonResetLabel: 'A',
//        buttonIncreaseLabel: '+A',
        buttonDecreaseLabel: '<img src="/media/img/ico_toolbar_text_less.gif" alt="riduci" />',
        buttonResetLabel: '<img src="/media/img/ico_toolbar_text.gif" alt="normale" />',
        buttonIncreaseLabel: '<img src="/media/img/ico_toolbar_text_more.gif" alt="ingrandisci" />',
        // Not yet used
        start: 1,
        offset: 1,
        size: null,
        scroll: 1,
        visible: null,
        animation: 'normal',
        wrap: null,
        initCallback: null,
        reloadCallback: null,
        itemLoadCallback: null,
        itemFirstInCallback: null,
        itemFirstOutCallback: null,
        itemLastInCallback: null,
        itemLastOutCallback: null,
        itemVisibleInCallback: null,
        itemVisibleOutCallback: null,
        buttonNextEvent: 'click',
        buttonPrevEvent: 'click',
        buttonNextCallback: null,
        buttonPrevCallback: null
        };
      //
      // end of closure
      //
      })(jQuery);


