
function FontResizer(){
    /* How many increments to apply */
    this.factor = 0;

    /* Max/Min global factor (updated on init) */
    this.max_factor = 1;
    this.min_factor = 1;

    /* Selectors to apply the resize:
        increment: percentage to increase original font size;
        max_factor: max number of increases (respect to original size) to apply;
        min_factor: max number of decreases (respect to original size) to apply;
    */
    this.selectors = {
        '#container': {
            increment: 0.2,
            max_factor: 4,
            min_factor: -1
        },
        '.section': {
            increment: 0.2,
            max_factor: 4,
            min_factor: -1
        },
        'p.intro-t': {
            increment: 0.2,
            max_factor: 4,
            min_factor: -1
        },
        '.box-wrapper p, .box p': {
            increment: 0.2,
            max_factor: 4,
            min_factor: -1
        },
        '#sidebar .widget p': {
            increment: 0.2,
            max_factor: 4,
            min_factor: -1
        },
//         '.footer p': {
//             increment: 0.2,
//             max_factor: 4,
//             min_factor: -1
//         },
        '.content-dhold': {
            increment: 0.2,
            max_factor: 4,
            min_factor: -1
        }
//         '.breadcrumbs': {
//             increment: 0.2,
//             max_factor: 4,
//             min_factor: -1
//         },
//         '.menu-resizable': {
//             increment: 0.2,
//             max_factor: 4,
//             min_factor: -1
//         }
    };

    this.initialize = function() {
        /* Read cookie for factor initialization */
        if ($.cookie('font_resize_factor')) {
            this.factor = parseInt($.cookie('font_resize_factor'), 0);
        }
        if (isNaN(this.factor)){
            this.factor = 0;
        }

        /* Initialize max/min general factor and original sizes */
        var max_f = this.max_factor;
        var min_f = this.min_factor;

        $.each(this.selectors, function(key, val) {
            var ourText = $(key);
            max_f = (max_f < val.max_factor) ? val.max_factor : max_f;
            min_f = (min_f > val.min_factor) ? val.min_factor : min_f;
            if (ourText.css('fontSize')){
                val['original_size'] = ourText.css('fontSize');
            }else{
                /* selector not available in current page, set a default */
                val['original_size'] = '12px';
            }
        });
        this.max_factor = max_f;
        this.min_factor = min_f;

        /* Update text size */
        if (this.factor != 0)
            this.update_size();
    };

    this.increase = function() {
        this.factor += 1;
        this.update_size();
    };

    this.decrease = function() {
        this.factor -= 1;
        this.update_size();
    };

    this.save_cookie = function() {
        $.cookie('font_resize_factor', this.factor, {path: '/', expires: 100});
    };

    this.update_size = function() {
        this.factor = (this.factor > this.min_factor) ? this.factor: this.min_factor;
        this.factor = (this.factor > this.max_factor) ? this.max_factor: this.factor;

        var the_factor = this.factor;
        // Loop through each set of selectors and resize them
        $.each(this.selectors, function(key, val) {
            var ourText = $(key);
            var increment = val.increment;
            var max_factor = val.max_factor;
            var min_factor = val.min_factor;
            var original_size = val.original_size;
            var finalNum = parseFloat(original_size, 10);
            var stringEnding = original_size.slice(-2);

            var factor_to_apply = (the_factor > min_factor) ? the_factor: min_factor;
            factor_to_apply = (factor_to_apply > max_factor) ? max_factor: factor_to_apply;

            if (factor_to_apply == 0){
                ourText.css('fontSize', '');
            }else{
                finalNum = finalNum + (factor_to_apply * increment * finalNum);
                ourText.css('fontSize', finalNum + stringEnding);
            }
        });
        this.save_cookie();
    };

}

$(document).ready(function() {
    var font_resizer = new FontResizer();
    font_resizer.initialize();

    $('.resizer').click(function(){
            var resizer_id = this.id
            if(resizer_id == 'increase') {
                font_resizer.increase();
            } else if (resizer_id == 'decrease') {
                font_resizer.decrease();
            }
            return false;
    });
});

