Zr.GlitterText = (function(){

    var me = {};
    
    /**
     * Create and display the glitter text
     */
    function _displayText(){
        var out = '';
        var BBout = '';
        
        var inputText = $('#inputText').val().toLowerCase();
        
        var Gfont = $('#Gfont').val();
        var GfontSize = $('#GfontSize').val();
        var GwordSpace = $('#GwordSpace').val();
        
        for (var ctr = 0; ctr < inputText.length; ctr++) {
            var currentChar = inputText.charAt(ctr);
            
            if (currentChar == '\r') 
                out = out;
            else 
                if (currentChar == '\n') 
                    out += '<br />';
                else 
                    if (currentChar == ' ') {
                    
                        BBout += '[img]' + Zr.config.WWW_STATIC + 'char/view/' + GwordSpace + '.gif[/img] \n';
                        out += '<img src="' + Zr.config.WWW_STATIC + 'char/view/' + GwordSpace + '.gif" border="0" alt="' + currentChar + '" /></span> <span>';
                    }
                    else 
                        if (currentChar >= 'A' && currentChar <= 'z') {
                            out += '<img src="' + Zr.config.WWW_STATIC + 'char/' + GfontSize + '/' + currentChar + '_' + Gfont + '.gif" border="0"  alt="' + currentChar + '" />';
                            BBout += '[img]' + Zr.config.WWW_STATIC + 'char/' + GfontSize + '/' + currentChar + '_' + Gfont + '.gif[/img] \n';
                        }
        }
        out = '<a href="' + Zr.config.WWW + '" target="_blank"><span>' + out + '</span></a>';
        BBout = '[url=' + Zr.config.WWW + '> ' + BBout + '[/url]';
        $('#displyText').html(out);
        $('#htmlCode').val(out);
        $('#bbCode').val(BBout);
        
    }
    
    /**
     * Update fornt and change the giltter codes
     * @param {Object} Gfont
     */
    function _setGfont(Gfont){
        $('#Gfont').val(Gfont);
        _displayText();
    }
    
    /**
     * Register events
     */
    function _register(){
        $('#generateText').click(_displayText);
        $('#GfontSize,#GwordSpace').change(_displayText);
        $('#inputText').keyup(_displayText);
        $('#charList a').click(function(e){
            _setGfont($(this).attr('title'));
        });
        $('.select_text').focus(function(){
            this.select();
        });
    }
    /**
     * Inialize the plugin
     */
    me.init = function(){
        $().ready(function(){
        
            _register();
            _displayText();
            
        });
        
    }
    
    return me;
    
})();

