/*==============================================================================================
	fontControl
	
	Requires: jquery 1.2.6
	Optional: jquery.cookies.2.0.1.js
	
	Changes the existing size of text by a fixed amount (fontControl.size)
	For use with font resizing buttons. Buttons should call fontControl.up() and fontControl.down()
	Saves changed font size to cookies so it can be applied between pages.
	
==============================================================================================*/
$(document).ready( function(){ fontControl.init(); } );

var fontControl = {
    defaultSize: 12,
    increment: 2, 	// increment font size is changed by
    size: 12, 		// initial font size change
    minimum: 12, 	// minimum font size change
    maximum: 18,

    blockSelectors: ['.resizeText'], 						// list of block selectors containing text to be resized
    textSelectors: ['h1', 'h2', 'p', 'td', 'span', 'div', 'a'], // list of sub-selectors containing text to be resized

    blockExcluders: ['.breadcrumb'], // list of block selectors to exclude
    textExcluders: [''], 		// list of sub-selectors to exclude

    selector: [],
    excluder: [],

    init: function () {
        var fontBase;

        // Build list of block and text selectors
        for (i = 0; i < this.blockSelectors.length; i++) {
            for (j = 0; j < this.textSelectors.length; j++) {
                this.selector.push(this.blockSelectors[i] + ' ' + this.textSelectors[j]);
            }
        }

        // Build list of excluders
        for (i = 0; i < this.blockExcluders.length; i++) {
            for (j = 0; j < this.textExcluders.length; j++) {
                this.excluder.push(this.blockExcluders[i] + ' ' + this.textExcluders[j]);
            }
        }
    },

    up: function () {
        if (this.size + this.increment <= this.maximum) {
            this.size += this.increment;
            this.resize(this.size - this.defaultSize);
        }
    },

    down: function () {
        if (this.size - this.increment >= this.minimum) {
            this.size -= this.increment;
            this.resize(this.size - this.defaultSize);
        }
    },

    resize: function (sizeChange) {
        var fontSize;
        var fontSize = 0;
        var lineHeight = 0;
        var tags = new Array();
        var copyRepeater = this.noneRepeater;
        // Change size of each element by the size change.

        //create hidden array
        if ($('#hdFontSizes').val() == null || $('#hdFontSizes').val() == "") {
            var fSizes = "";
            $(this.selector.join(', ')).not(this.excluder.join(', ')).each(function () {
                fSizes += parseInt($(this).css('font-size')) + ",";
            });
            fSizes = fSizes.substring(0, fSizes.length - 1);
            var $hiddenInput = $('<input/>', { type: 'hidden', id: 'hdFontSizes', value: fSizes });
            $hiddenInput.appendTo('.resizeText');
        }

        var singleFontSize = $('#hdFontSizes').val().split(',');

        //begin to resize text
        $(this.selector.join(', ')).not(this.excluder.join(', ')).each(function (index) {
            fontSize = parseInt(singleFontSize[index]) + sizeChange;
            lineHeight = parseInt(singleFontSize[index]) + 6 + sizeChange * 3;
            $(this).css('font-size', fontSize + 'px');
            $(this).css('line-height', lineHeight + 'px');
        });
    },

    setsize: function (newSize) {
        var sizeChange;
        sizeChange = parseInt($(this.selector.join(', ')).not(this.excluder.join(', ')).find('p').css('font-size')) - newSize;
        this.resize(sizeChange);
    }

};


