var tableFilter = Class.create();
tableFilter.prototype = {
   initialize: function(selector, valuecol, myTable, defaultFilter) {
      this.selector = selector;
      this.valuecol = valuecol;
      this.myTableRows = $A(myTable.rows);
      this.generateSelector(defaultFilter);
      new Form.Element.EventObserver(this.selector,  this.hideRows.bindAsEventListener(this) );
      if(defaultFilter != '' && this.myValues.indexOf(defaultFilter) != -1){
      this.hideRows();
      }
   },
	generateSelector: function (defaultFilter){
		myTableRows = this.myTableRows;
		mySelector = this.selector;
		myValues = new Array();
		this.valuecol.each(function(col,index){
			myValuesTemp = myTableRows.collect(function(row){
				if ( row.cells[col]) {
					return row.cells[col].innerHTML.stripTags().trim();
				} else {
					return '';
				}
			});
			myValues[index] = myValuesTemp;

		})
		myValues = myValues.flatten().unique().sort();
		myValues.unshift("Choose Team");
		var sel = false;
		for (i=0; i < myValues.length; i++) {
			if (myValues[i] == defaultFilter && defaultFilter != ''){
			sel = true;
			} else {
			sel= false;
			}
			mySelector.options[i] = new Option(myValues[i], myValues[i], sel, sel);
		}
		this.myValues = myValues;
	},

	showAllRows: function (){
		this.myTableRows.each(function(row){
			Element.show(row);
		})
	},

   hideRows: function () {
	var filter = $F(this.selector).trim();
	if(document.location.href.indexOf('team') != -1){
	var link = '';
	} else {
	var link = "&team="+filter;
	}
	var up = "Link to your team schedule from your homepage!<br><a href=\""+document.location.href+link+"\">Copy this link!</a>";
    $('teamlinkinfo').style.display = 'block';
    $('teamlinkinfo').update(up);
	var valuecol = this.valuecol;
	this.myTableRows.each(function(row){
		//Here, I scope the lookin cells just to the cols we defined.
		var valuecells = [];
		for (i = 0; i < valuecol.length; i++) {
      		valuecells.push(row.cells[valuecol[i]]);
	    }
		var cells = $A(valuecells);

		var match = cells.any(function(value,index){
			if(value){
			var cellvalue = value.innerHTML.stripTags().trim();
			return (cellvalue == filter);
			}
		})

		if (!match) {
			Element.hide(row);
		} else {
			Element.show(row);
		}
	})
	}
};

var TeamLinker = Class.create({
     initialize: function(nodelist, subject){
              this.words = subject;
			  var self = this;
              nodelist.each(function(node){
                    self.highlightWords(node);
              });
    },
	highlightWords: function(node){
	      //Iterate over its children.
       if (node.childNodes != null && node.childNodes.length>0){
	  	var cnodes = $A(node.childNodes);
		var self = this;
        cnodes.each(function(node){
              self.highlightWords(node);
        });
       }
      if (node.nodeType == 3) { // text node
            var html = node.nodeValue;
            var re = new RegExp('\\b('+this.words+')\\b','ig');
            //now, test the node. see if it matches, if not, get out.
            if (re.test(html) === false){
                  return;
            }
			//look up the id;
			var match = html.match(re);
			var id = teamdata[match[0]];
            var newnodehtml = html.replace(re,'<a target="_blank" href="index.php?module=articles&func=redirect&ptid=6&aid='+id+'">$1<\/a>');
            var myText = document.createElement('span');
            myText.innerHTML = newnodehtml;
            node.parentNode.replaceChild(myText, node);
		}

	}
});

Object.extend(Array.prototype, {
  unique: function() {
    var result = [];
    for (i = 0; i < this.length; i++) {
      if (result.indexOf(this[i]) < 0 ) result.push(this[i]);
    }
    return result;
  }
});

Object.extend(String.prototype, {
   trim: function(){
    return this.replace(/^\s+|\s+$/g, '');
   }
});