﻿/**
* Purpose: Font sizer class, handles increasing and decreasing font size
of a page.
* It increases the font in 10% increments. By getting the level / 10 + 1.
i.e. level 2 is .2 + 1 so 1.2 or 120%.
*
* Requires: JQuery and the JQuery cookies plugin.
*
* Use: Setup the fontsizer $.FontSizer.Init(options); the two options are
min and max, for the min level and max level.
* Defaults are min: -3 and max: 5.
*
* Author: Stefan Sedich ([EMAIL PROTECTED]
* Edited: Dion Foster 2009-04-17 to include quick print functionality and
*         also onclick event bindings.  Extended the options to also include
*         the "divid" of the div you want to increase/decrease/print.
*/
$.FontSizer = {

    level: 0,

    options: {
        min: -3,
        max: 5,
        elementid: '#Content'
    },

    Init: function(options) {
        if (options)
            $.FontSizer.options = $.extend($.FontSizer.options, options);

        //Get the current level from cookies.
        var level = ($.cookie('font_level') != null) ? $.cookie('font_level') : 0;

        //Set the font size to the current level.
        $.FontSizer.SetFontSize(level);

        //Bind up the onclick events for resizing and printing
        $('#decreasefont').bind('click', function() {
            $.FontSizer.DecreaseFontSize();
        });
        $('#increasefont').bind('click', function() {
            $.FontSizer.IncreaseFontSize();
        });
        $('#quickprint').bind('click', function() {
            $.FontSizer.QuickPrint();
        });
    },

    IncreaseFontSize: function() {
        if (parseInt($.FontSizer.level) + 1 <= $.FontSizer.options.max) {
            //If we have not exceded the max level,
            //Get the next level and the set the size to this level.
            var next = (parseInt($.FontSizer.level) + 1);
            $.FontSizer.SetFontSize(next);
        }
    },

    DecreaseFontSize: function() {
        if (parseInt($.FontSizer.level) - 1 >= $.FontSizer.options.min) {
            //If we have not exceded the min level,
            //Get the next level and the set the size to this level.
            var next = (parseInt($.FontSizer.level) - 1);
            $.FontSizer.SetFontSize(next);
        }
    },

    GetFontSize: function() {
        var level = $.FontSizer.level;
        return (level / 10) + 1;
    },

    SetFontSize: function(level) {

        //Set the current level in the member variable and the cookie.
        $.FontSizer.level = level;
        $.cookie('font_level', level);

        //Work out the new em value and set it.
        var level = $.FontSizer.GetFontSize();

        $($.FontSizer.options.elementid).css('fontSize', level + 'em');
    },

    QuickPrint: function() {

        //get the content and put it in a new window
        //written in jquery cause i can.
        //we don't want to forget the text size on the print either.
        var level = $.FontSizer.GetFontSize();        

        // get the html
        var html = $($.FontSizer.options.elementid).html();

        // spawn a new window
        var printWindow = window.open();

        // write the html to a nice div with stuff in it
        printWindow.document.write("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"/includes/StyleSheet.css\" />");
        printWindow.document.write("</head><body><div style=\"background-color: white; font-size: " + level + "em\">");
        printWindow.document.write(html);
        printWindow.document.write("</div></body></html>");
        printWindow.document.close();
        printWindow.focus();
    }
};
