/**
* Font Controller
* For creating a font size changer interface with minimum effort
* Copyright (c) 2009 Hafees (http://cool-javascripts.com)
* License: Free to use, modify, distribute as long as this header is kept :)
*
*/

/**
* Required: jQuery 1.x library, 
* Optional: jQuery Cookie Plugin (if used, the last used font size will be saved)
* Usage: (For more details visit 
* This function can be called inside a $(document).ready()
* Eg: fontSize("#controls", "#content", 9, 12, 20); where,
* #controls - where control is the element id, where the controllers will be created.
* #content - for which element the font size changes to apply. In this case font size of content div will be changed
* 9 - minimum font size
* 12 - default font size
* 20 - maximum font size
* 
*/

function fontSize(container, target ) {
    /*Editable settings*/
    //var minCaption = "a"; //title for smallFont button
    //var defCaption = "A"; //title for defaultFont button
    //var maxCaption = "AAA"; //title for largefont button

    //Now we'll add the font size changer interface in container
  //  smallFontHtml = "<a href='javascript:void(0);' class='smallFont' title='" + minCaption + "'>" + minCaption + "</a> ";
  //  defFontHtml = "<a href='javascript:void(0);' class='defaultFont' title='" + defCaption + "'>" + defCaption + "</a> ";
   // largeFontHtml = "<a href='javascript:void(0);' class='largeFont' title='" + maxCaption + "'>" + maxCaption + "</a> ";
    //$(container).html(smallFontHtml + defFontHtml + largeFontHtml);
    var defSizepx = $('#UberNav div').css('font-size');
    var defSize = parseFloat(defSizepx, 10);

    var defLineHeightpx = $('#scroller-body div').css('line-height');
    var defLineHeight = parseFloat(defLineHeightpx, 10);

    if ($.browser.msie) {
        defSizepx = "13";
        defLineHeightpx = "15";
    }
    
    //Read cookie & sets the fontsize
    if ($.cookie != undefined) {
        var cookie = target.replace(/[#. ]/g, '');
        var value = $.cookie(cookie);
        if (value != null) {
            $(target).css('font-size', parseInt(value));
        }
    }

    //on clicking small font button, font size is decreased by 1px
    $(container + " .largerFont").click(function() {
        var newSize = defSize * 1.2;
        var newLineHeight = defLineHeight * 1.2;

        if ($.browser.msie) {
            var newSize = "16";
            var newLineHeight = "18";
        }
        
        $(target).css('font-size', newSize + "px");
        $(target).css('line-height', newLineHeight + "px");

        $(container + " .largeFont").css('color', "#ffec92");
        $(container + " .defaultFont").css('color', "#fff");
        $(container + " .largestFont").css('color', "#fff");

        updatefontCookie(target, newSize); //sets the cookie 

    });

    //on clicking default font size button, font size is reset
    $(container + " .defaultFont").click(function() {

        $(target).css('font-size', defSize + "px");
        $(target).css('line-height', defLineHeight + "px");
        
        $(container + " .largestFont").css('color', "#fff");
        $(container + " .defaultFont").css('color', "#ffec92");
        $(container + " .largeFont").css('color', "#fff");
        
        updatefontCookie(target, defSize);
    });

    //on clicking large font size button, font size is incremented by 1 to the maximum limit
    $(container + " .largestFont").click(function() {
        var newSize = defSize * 1.4;
        var newLineHeight = defLineHeight * 1.4;

        if ($.browser.msie ) {
            var newSize = "18";
            var newLineHeight = "22";
        }
        
        $(target).css('font-size', newSize + "px");
        $(target).css('line-height', newLineHeight + "px");

        $(container + " .largeFont").css('color', "#fff");
        $(container + " .defaultFont").css('color', "#fff");
        $(container + " .largestFont").css('color', "#ffec92");
        
        $('#mask').css('height', '650px');        
        updatefontCookie(target, newSize);
    });

    function updatefontCookie(target, size) {
        if ($.cookie != undefined) { //If cookie plugin available, set a cookie
            var cookie = target.replace(/[#. ]/g, '');
            $.cookie(cookie, size);
        }
    }
}
