/**
 * MooCode
 *
 * @version		0.2.1 (2008/11/25) Fixed display bug when line was containing more than 1 keyword
 * @author		Geoffray Warnants - http://www.geoffray.be
 */
var MooCode = new Class({
    Implements:[Events,Options],
    options:{
        showTitle:true,
        className:'mooCode',
        start:1,
        ignoreEmptyLines:false,
        scroll:true
    },
    initialize: function(options){
        var C=Array.link(arguments,{options:Object.type});
        this.setOptions(C.options||null);
    },
    applyTo: function(el) {
        if ($type(el) == 'array') {
            el.each(this.build, this);
        } else if ($type(el) == 'element') {
            this.build(el);
        }
    },
    build: function(el){
        var container = new Element('div', {'class': this.options.className});
        if (el.get('id') != null) {
            container.set('id', el.get('id'));
        }
        if (el.get('title') != null) {
            container.set('title', el.get('title'));
        }
        if (this.options.showTitle && el.get('title') != null) {
            new Element('div', {'class': 'title', 'text':el.get('title')}).inject(container);
        }
        
        if (this.options.scroll==true) {
            
            var c = new Element('div', {'class':'scroll'}).inject(container);
        } else {
            var c = container;
        }
        
        if (this.options.start != 1) {
            var liste = new Element('ol', {'start': this.options.start}).inject(c);
        } else {
            var liste = new Element('ol', {}).inject(c);
        }
                
        var lines = el.get('text').trim().split(/\r\n|\r|\n/);

        var keywords = 'abstract and __FILE__ __LINE__ array as break case ' +
					'cfunction class const continue declare default die do else ' +
					'elseif empty enddeclare endfor endforeach endif endswitch endwhile ' +
					'extends for foreach function implements include include_once interface global if ' +
					'new or public private protected return static switch use throw require require_once ' +
					'var while xor __FUNCTION__ __CLASS__ ' + 
					'__METHOD__ _CURSOR_';
                
        var regex = new RegExp('\\b'+keywords.replace(/ /g, '\\b|\\b')+'\\b', 'gmi');
        //var quotedStringRegEx = new RegExp('"(?:\\.|(\\\\\\")|[^\\""\\n])*"','g');
                
        lines.each(function(item, i) {
            if ((!this.options.ignoreEmptyLines || item.length > 0)) {
 
                var li = new Element('li', {'class': ((i%2) ? 'alt' : '')}).inject(liste);
                while ((match = regex.exec(item)) != null) {
/*
                    if (match.index == 0) {
                        
                        
                        var text = this.htmlspecialchars(item.substr(0, match[0].length));
                        if (item.substr(0, match[0].length)=='mooCursor') {
                            new Element('span', {'class':'keyword','text':'|'}).inject(li);
                        } else {
                            new Element('span', {'class':'cursor','text': text}).inject(li);
                        }
                        
                    } else {
*/
                        new Element('span', {'text': this.htmlspecialchars(item.substr(0,match.index))}).inject(li);
                        if (item.substr(match.index, match[0].length)=='_CURSOR_') {
                            new Element('span', {'class':'cursor','text': '|'}).inject(li);
                        } else {
                            new Element('span', {'class':'keyword','text': this.htmlspecialchars(item.substr(match.index, match[0].length))}).inject(li);
                        }
//                    }
                    item=item.substr(match.index+match[0].length);
                    
                }
                if (item.length > 0) {
                    new Element('span', {'text': this.htmlspecialchars(item)}).inject(li);
                }
            }
        }, this);

        container.inject(el, 'before');
        el.destroy();
    },
    
    htmlspecialchars: function(str) {
        
        //str=str.replace(/</g, "&lt;");
        //str=str.replace(/&gt;/g, ">");
        
        return str;
    }
});
