/* Based on code from http://davidwalsh.name/mootools-zebra-table-plugin 
 * Requires mootools core 1.2
 *
 * Removed colourisation on intitalise, zebraTables.zebraize() must be called 
 * when the DOM is ready. Added code to remove css classes and events in
 * zebraize() so that it can safely be called repeatedly.
 */
var ZebraTable = new Class(
{
    //implements
    Implements: [Options,Events],
    
    // options
    options: 
    {
        cssEven: 'alt1',
        cssOdd: 'alt2',
        cssMouseEnter: 'hover'
    },
    
    // initialization
    initialize: function(options) 
    {
        //set options
        this.setOptions(options);
    },
    
    // colourise table rows with alternating colours.
    zebraize: function(table) {
        var pos = 0;

        // for every row in this table...
        table.getElements('tr').each(function(tr,i) {
            // check to see if the row has th's, or it's invisible, if so, leave it alone
            if(tr.getFirst().get('tag') != 'th' && tr.style.display != 'none' && tr.hasClass('zebra')) {
                var options = this.options;

                // We might not know which class we have (if any), so check for either
                if(tr.hasClass(options.cssEven)) {
                    tr.removeClass(options.cssEven);
                } else if(tr.hasClass(options.cssOdd)) {
                    tr.removeClass(options.cssOdd);
                }

                // This is rather harsh, but provided the only events get added in zebraize()
                // it should be safe enough…
                tr.removeEvents();

                // set the class for this based on odd/even
                var klass = ++pos % 2 ? options.cssEven : options.cssOdd;
                
                // start the events!
                tr.addClass(klass).addEvents({
                    //mouseenter
                    mouseenter: function () {
                         if(!tr.hasClass(options.cssHighlight)) tr.addClass(options.cssMouseEnter).removeClass(klass);
                    },
                    //mouseleave
                    mouseleave: function () {
                        if(!tr.hasClass(options.cssHighlight)) tr.removeClass(options.cssMouseEnter).addClass(klass);
                    },
                });
            }
        },this);
    }
});


