
/**
 * bind css style with table rows of automatical alternation color change
 */

var basicRules = {

  '.list-table TBODY TR' : function(element){
  	if(element.rowIndex != 0){
    	this.applyListTableStyle(element);
        Event.observe(element, 'mouseover',  this.mouseOverListTable.bindAsEventListener(this, element));
        Event.observe(element, 'mouseout',  this.mouseOutListTable.bindAsEventListener(this, element));
    }

  },

  '.list-table TBODY TH' : function(element){
  	Event.observe(element, 'click',  this.clickListTable.bindAsEventListener(this, element));
  },

  /* apply the css style for list-table */
  applyListTableStyle: function (element) {
    if(element.childNodes.length > 1) {
         if(element.rowIndex % 2 == 0){
            element.className = 'even';
          }else {
            element.className = 'odd';
          }
      }
  },

  mouseOverListTable: function(event, element){
      element.className = 'over';
  },

  mouseOutListTable: function(event, element){
     this.applyListTableStyle(element);
  },
  
  clickListTable: function(event, element) {
  	var url = element.up(2).readAttribute('url');
	if(url) {
		var orderBy = element.readAttribute('orderBy');
		if(orderBy) {
			if(element.className == 'desc') {
				window.location.href = url + '?orderBy.' + orderBy + '=asc';	
			} else {
				window.location.href = url + '?orderBy.' + orderBy + '=desc';
			}			
		}
		
	} 
  }

};

Behaviour.register(basicRules);




