/**
* jQuery.colorize
* Copyright (c) 2008 Eric Karimov - ekarim57(at)gmail(dot)com | http://franca.exofire.net/jq/
* Dual licensed under MIT and GPL.
* Date: 10/27/2008
*
* @projectDescription Table colorize using jQuery.
* http://franca.exofire.net/jq/colorize
*
* @author Eric Karimov, contributor Aymeric Augustin
* @version 1.3.1
*/

jQuery.fn.colorize = function(params) {
    options = {
        altColor: '#EAEAEA',
        bgColor: '#fff',
        hoverColor: 'url(' + ServidorUrl + 'Content/img/hiliteRow.gif)',
        hiliteColor: '',
        oneClick: false,
        columns: false,
        banColumns: [],
        ListaHover: 'ListaResultadosHover'
    };
    if (AreaSitioColor == 3) {
        options = {
            altColor: '#EAEAEA',
            bgColor: '#fff',
            hoverColor: 'url(' + ServidorUrl + 'Content/img/hiliteRowVer.gif)',
            hiliteColor: '',
            oneClick: false,
            columns: false,
            banColumns: [],
            ListaHover: 'ListaResultadosHover'
        };
    }

    jQuery.extend(options, params);

    var colorHandler = {
        checkHover: function() {
            if (window.event)
                window.event.cancelBubble = true;
            var row = this;
            setTimeout(function() {
                if (!row.onfire && !$(row).hasClass('hiliteRow')) {
                    if (options.hoverColor != '')
                        row.style.backgroundImage = options.hoverColor;
                    if (options.ListaHover != '')
                        $(row).addClass(options.ListaHover);
                }
            }, 0);
        },
        checkHoverOut: function() {
            if (window.event)
                window.event.cancelBubble = true;
            var row = this;
            setTimeout(function() {
                if (!row.onfire && !$(row).hasClass('hiliteRow')) {
                    row.style.backgroundImage = "none";
                    $(row).removeClass(options.ListaHover);
                }
            }, 0);
        },
        highlight: function() {
            if (options.hiliteColor != 'none') {
                this.style.backgroundColor = options.hiliteColor;
                this.onfire = true;
            }
        },
        stopHighlight: function() {
            this.onfire = false;
            this.style.backgroundColor = this.origColor;
        }
    }

    function getColCells(cells, idx) {
        var arr = [];
        for (var i = 0; i < cells.length; i++) {
            if (cells[i].cellIndex == idx)
                arr.push(cells[i]);
        }
        return arr;
    }

    function processCells(cells, idx, func) {
        var colCells = getColCells(cells, idx);
        jQuery.each(colCells, function(index, cell2) {
            func.call(cell2);
        });
    }

    function processAdapter(cells, cell, func) {
        processCells(cells, cell.cellIndex, func);
    }

    function toggleColumnClick(cells) {
        var func = (!this.onfire) ? colorHandler.highlight : colorHandler.stopHighlight;
        processAdapter(cells, this, func);
    }

    function toggleRowClick(cells) {
        row = jQuery(this).parent().get(0);
        if (!row.onfire)
            colorHandler.highlight.call(row);
        else
            colorHandler.stopHighlight.call(row);
    }

    function oneColumnClick(cells) {
        processAdapter(cells, this, colorHandler.highlight);
        if (cells.clicked > -1) {
            processCells(cells, cells.clicked, colorHandler.stopHighlight);
        }
        cells.clicked = this.cellIndex;
    }

    function oneRowClick(cells) {
        row = jQuery(this).parent().get(0);
        colorHandler.highlight.call(row);
        if (cells.clicked) {
            colorHandler.stopHighlight.call(jQuery(cells.clicked).parent().get(0));
        }
        cells.clicked = this;
    }

    function checkBan() {
        return (jQuery.inArray(this.cellIndex, options.banColumns) != -1);
    }

    return this.each(function() {

        jQuery(this).find('tr:odd').css('background', options.bgColor);
        jQuery(this).find('tr:even').css('background', options.altColor);

        var cells = jQuery(this).find('td,th');
        cells.clicked = null;

        if (options.columns) {
            jQuery.each(cells, function(i, cell) {
                cell.onmouseover = function() {
                    processAdapter(cells, this, colorHandler.checkHover);
                }
                cell.onmouseout = function() {
                    processAdapter(cells, this, colorHandler.checkHoverOut);
                }
                cell.onclick = function() {
                    if (checkBan.call(this))
                        return;
                    if (options.oneClick)
                        oneColumnClick.call(this, cells);
                    else
                        toggleColumnClick.call(this, cells);
                }
            });
        }
        else {
            var rows = jQuery(this).find('tr');
            rows.clicked = null;

            jQuery.each(rows, function(i, row) {
                row.onmouseover = colorHandler.checkHover;
                row.onmouseout = colorHandler.checkHoverOut;
                row.onclick = function() {
                    if (rows.clicked != null) {
                        var trClicked = rows.clicked;
                        if ($(trClicked).hasClass('hiliteRow')) {
                            trClicked.style.backgroundImage = "none";
                            $(trClicked).removeClass('hiliteRow');
                            $(trClicked).removeClass(options.ListaHover);
                        }
                    }
                    $(this).addClass('hiliteRow');
                    if (options.hoverColor != '')
                        this.style.backgroundImage = options.hoverColor;
                    if (options.ListaHover != '')
                        $(this).addClass(options.ListaHover);
                    rows.clicked = this;
                }
            });
        }
    });
}
