/*
---
script: TextResizer.js
version: 1.0
description: A Class that provide quick Text resizing tool for elements.
license: MIT-style
authors: 
- Nickolas Simard

requires: 
  core:1.2.4: '*'

provides: [TextResizer]
...
*/
var TextResizer = new Class ({
    // Implements
    Implements: [Options, Events],
	
    // Options
    options: {
        sizes: null,
        container: null,
        // Set the default Size status to normal for the moment
        size_status: $H({
            normal: "12px"
        })
    },
	
    // Initialization
    initialize: function(options){

        // Set Options
        this.setOptions(options);
        // Get the container OR the default Body
        this.container = document.id(this.options.container) || document.body;
        // Get the default size settings        
        if(Browser.ie6){
            this.sizes = this.options.ie6_sizes; 
        }else{
            this.sizes = this.options.sizes; 
        }
        if(this.sizes == null){
            this.sizes = $H({
                normal: "12px",
                big: "14px",
                bigger: "16px",
                huge: "18px",
                humongous: "22px"
            });
        }        

        // Get text resize class
        this.text_size_class = this.options.text_size_class;
		
        // Grab the Elements with the class "text-resize-me"
        if ($$(this.text_size_class).length != 0)
        {
            this.text_zones = $$(this.text_size_class);
        }
        else
        {
            this.text_zones = $$(document.body);
        }
        // Set the sizes for the text_zones
        this.text_zones.each(function(text_zone){
            text_zone.store('sizes', this.sizes);
        }, this);
		
        // Grab the cookie OR the default size
        // Check if the user defined his own sizes and if there is a "normal" state
        if (this.sizes.get("normal"))
        {
            var normal_size_hash = new Hash();
            this.options.size_status = normal_size_hash.set('normal', this.sizes.get("normal"));
        }
        var cookie_hash = new Hash();
        cookie_hash.set(Cookie.read('size_status_key'), Cookie.read('size_status_value'));
        this.size_status =  cookie_hash || this.options.size_status;
		
        // Set the starting size
        
        this.setSize(this.size_status);
		
        // Inject the controllers in the container
        this.buildController();
    },
	
    // buildController
    // Create the div, the label and the size switching ul/li
    buildController: function(){
        // Generate the Size list
        var font_sizes = new Element('span');
        this.sizes.each(function(size, index){
            this.addSizeButton(size, index, font_sizes);
        }, this);
		
        // Create the controller and add the newly create size list to it
        var controller = new Element('span', {
            'id': 'TextResizerController'
        }).adopt(
            new Element('span', {
                'html': 'Text size : '/*,
				'style': 'float: left;'*/
            }).adopt(font_sizes)
            );
		
        // Inject the HTML into the container
        controller.inject(this.container, 'top');
    },
	
    // addSizeButton
    addSizeButton: function(size, index, holder){
        var param_hash = new Hash();
        param_hash.set(index, size);
        holder.adopt(new Element('span', {
            'html': 'A',
            'style': 'cursor: pointer; padding: 0; margin: 3px; font-size:' + size,
            'title': "Change font size to " + size +" (" + index + ")"
        }).addEvent('click', this.onClick.pass(param_hash, this)));
    },
	
    onClick: function(param, evt) {
        this.setSize(param);
        return false;
    },
	
    // setSize
    setSize: function(size){
        // If the "size" format is an hash, keep going
        if (typeOf(size) == 'hash')
        {
            var selected_size_key = size.getKeys();
            var selected_size_value = size.getValues();
            this.text_zones.each(function(text_zone){
				
                var size_hash = text_zone.retrieve('sizes');
                //alert(size_hash);
                size_hash.each(function(value, key){
                    // If the element has a corresponding size value for the specified size name
                    if (key == selected_size_key)
                    {
                        // Redefined the size setting
                        selected_size_value = value;
                        selected_size_key = key;
                    }
                }, this);
				
                // Set the size using either the default or the redefined size
                text_zone.setStyle('font-size', selected_size_value);
            }, this);
        }
        this.setCookie(selected_size_key, selected_size_value);
    },
	
    // setCookie to save the current settings
    setCookie: function(key, value){
        Cookie.write('size_status_key', key, {/*domain : 'test.scholar.hw.ac.uk',*/ path : '/' });
        Cookie.write('size_status_value', value, {/*domain : 'test.scholar.hw.ac.uk',*/ path : '/' });
    }
});
