﻿/**
 * @fileoverview Pozwala powiekszać i zmniejszać rozmiar fontow w ramach
 * okrelonego elementu html (this.elementId)
 * <div id="mainContent">content</div>
 *
 * @used IE, NN, FireFox, Opera
 *
 * @version 1.2 2005/01/06
 */

/**
 * @constructor
 * @param        Mixed        Element strony lub id elementu.
 */
FontSize = function(element){ //System.subClass();
        /**
         * Id elementu w którym będzie zmieniany rozmiar czcionki.
         * @type        String
         * @public
         */
        this.id = 'mainContent';

        /**
         * Domylny rozmiar czcionki.
         * @type        Number
         * @public
         */
        this.size = 8;

        /**
         * Stan klawisz shift.
         * @type        Number
         * @private
         */
        this.shift = 0;

        /**
         * Zawiera element w którym będzie zmieniany rozmiar czcionki.
         * @type        Object
         * @private
         */
        this.element = null;

        if(typeof(element) == 'object'){
                this.element = element;
        }
        else if(typeof(element) == 'string'){
                this.element = document.getElementById(element);
        }
        else{
                this.element = document.getElementById(this.id);
        }
};

FontSize.size = 8;

/**
 * Przechowuje aktualny obiekt klasy FontSize.
 * @type        Object
 * @private
 */
FontSize.self = null;

/**
 * Dodaje zdarzenia umożliwiajšce powiększanie tekstu (klaiwsz +) oraz
 * zmniejszanie tekst (klawisz -).
 * @public
 * @return        void
 */
/*
FontSize.prototype.addEvents = function(){
        FontSize.self = this;
        new CreatorEvents(window).add('keydown', function(e){ FontSize.self.resize(e); });
        new CreatorEvents(window).add('keyup', function(e){ FontSize.self.clearShift(e); });
};
*/

/**
 * Resetuje zmiennš this.shift jeżeli został wcinięty klawisz szift.
 * @public
 * @return        void
 */
FontSize.prototype.clearShift = function(e){
        if(window.event)
                var key = window.event.keyCode;
        else
                var key = e.which;

        if(key == 16){
                this.shift = 0;
        }
};

/**
 * @public
 * @param        Object        Zdarzenie wywołujšce funkcję.
 * @return        void
 */
FontSize.prototype.resize = function(e) { // for keys
        var key, keyMinus, keyPlus;

        if(window.event){
                key = window.event.keyCode;
                keyMinus = 189 // inny kod klawisza + dla IE
                keyPlus = 187; // inny kod klawisza + dla IE
        }
        else{
                key = e.which;
                keyMinus = 109 // w gecko ten sam kod dla klawisza
                keyPlus = 61;
        }

        //alert(key);

        if(key == 16)
                this.shift = key;
        if(key == 109 || (this.shift == 16 && key == keyMinus))
                this.resizeFont(false);
        if(key == 107 || (this.shift == 16 && key == keyPlus))
                this.resizeFont(true);
};

/**
 * Zmienia rozmiar czcionki w zależnoci od przekazanej zmiennej (true +, false -).
 * @public
 * @param        Boolean        Okrela czy powiększyć, czy zmniejszyć czcionkę.
 * @return        void
 */
FontSize.prototype.resizeFont = function(quantity){ // for mouse
        var tmp = parseInt(FontSize.size);

        if(!quantity && tmp > 8)
                tmp -= 1;
        if(quantity && tmp < 12)
                tmp += 1;
        FontSize.size = tmp + 'pt';

        this.changeSize(this.element);
};

/**
 * Zmienia rozmiar czcionki dla podanego elementu oraz
 * dla wszystkich podrzędnych elementów.
 * @private
 * @param        Object        Element strony w którym ma być zmieniony rozmiar czcionki.
 * @return        void
 */
FontSize.prototype.changeSize = function(element){
        if(element.nodeName != '#text'){
                element.style.fontSize = FontSize.size;
        }

        for(var i = 0; i < element.childNodes.length; i++){
                //alert(element.childNodes[i].nodeName + '=' + typeof(element.childNodes[i]));
                this.changeSize(element.childNodes[i]);
        }
};

