var CoinShow = {
    init: function (el, sel, cat) {
      if ($(el)) {
        $(el).observe('click', this.cellClick.bind(this));
        $(el).observe('mouseover', this.cellHover.bind(this));
        $(el).observe('mouseout', this.cellMouseout.bind(this));
        this.table = $(el);
        this.sel = sel;
        this.cat = cat;
      }
    },
    cellHover:      function (evt)
    {
        clearTimeout(this.hover);
        this.hover = setTimeout(function () {
        var td = Event.findElement(evt, 'TD');
        if (td && !td.className.match(/hover/)) {
            this.colGroups = this.getColGroups();
            if (td.cellIndex > 0) $(this.colGroups[td.cellIndex]).addClassName('hover');
            $(td).addClassName('hover');

            var row = td.up('TR');
            if (row.rowIndex > 0) $(row).addClassName('hover');
        }
        }.bind(this), 75);
    },
    cellMouseout:   function (evt)
    {
        var td = Event.findElement(evt, 'TD');
        if (td) {
            if ($(this.colGroups[td.cellIndex])) { $(this.colGroups[td.cellIndex]).removeClassName('hover'); }
            $(td).removeClassName('hover');
            var row = td.up('TR');
            if (row.rowIndex > 0) {
                $(row).removeClassName('hover');
            }
        }
    },
    cellClick:      function (evt)
    {
        var el = Event.element(evt);
        if (el.nodeName == 'TD' && el.cellIndex > 0 && el.parentNode.rowIndex >= 0 && !el.className.match(/selected/))
        {
            var parentRow = el.parentNode.id;
            var colGroups = this.getColGroups();
            
            var keyDex = [ parentRow.replace(/year_/,''), colGroups[el.cellIndex].id.replace(/colgroup_/,'')];
            if (el.innerHTML != '-' && el.innerHTML != '$0.00')
                window.location = '/item-details/s/' + this.sel + '/c/' + this.cat + '/y/' + keyDex[0] + '/x/' + keyDex[1] ;
            //else
                //alert ('cellIdx: ' + el.cellIndex + ' rowIdx: ' + el.parentNode.rowIndex + ' className: ' + el.className);
        } else {
            //alert ('cellIdx: ' + el.cellIndex + ' rowIdx: ' + el.parentNode.rowIndex + ' className: ' + el.className);
        }
    },
    getColGroups:   function ()
    {
      var colGroups = [];
      for(i=0,j=this.table.childNodes.length;i<j;i++)
      { 
        var node = this.table.childNodes[i];
        if (node.nodeType != document.TEXT_NODE && node.nodeName == 'COLGROUP')
        {
          colGroups[colGroups.length] = node;
        }
      }
      return colGroups;
    }
};

