String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

function checkForLogin(el) {
	if (document.getElementById('logged-in')) {
		return true;
	}
	
	RedBox.showInline('not-logged-in');
	return false;
}

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};

// var timeouts = {};

var Invite = {
	processResponse: function(request) {
  		var rtn = eval('(' + request.responseText + ')');
		if (rtn.saved == true) {
			Element.show('confirm');
			Element.hide("invite");
		} else {
			for (i = 0; i < rtn.errors.length; i++) {
				Element.update($('errors'), rtn.errors[i]);
			}
		}
  	},
  	
  	complete: function(request) {
  		Element.hide("status"); 
  		Invite.processResponse(request);
  	},
  	
  	before: function() {
  		Element.show("status");
  		Element.hide("confirm"); 
  		Element.update("errors", "");
  	},
  	
  	showInviteForm: function() {
  		Element.hide("confirm");
  		Element.show("invite");
  		Form.reset('invite-form');
  	}
};

var EmailThis = {
	processResponse: function(request) {
  		var rtn = eval('(' + request.responseText + ')');
		if (rtn.saved == true) {
			Element.show('confirm');
			Element.hide("email-this");
		} else {
			Element.show("errors"); 
		}
  	},
  	
  	complete: function(request) {
  		Element.hide("status"); 
  		EmailThis.processResponse(request);
  	},
  	
  	before: function() {
  		Element.show("status");
  		Element.hide("confirm"); 
  		Element.hide("errors");
  	}
};

var Tee = {
	processResponse: function(request) {
  		var rtn = eval('(' + request.responseText + ')');
		if (rtn.saved == true) {
			RedBox.close();
			/*
			updater = new Ajax.Updater('teeList',
					'/round/tees?course%5Bid%5D=' + rtn.course.id,
					{
						asynchrnous:true,
						onComplete: function(request){ 
							$('tee_id_' + rtn.id).checked = true; 
							new Effect.Highlight('id' + rtn.id); 
						}
					});
			*/
		} else {
			errorStr = rtn.errors.join("<br />");
			Element.update($('errors'), errorStr);
		}
  	},
  	
  	complete: function(request) {
  		Element.hide("status"); 
  		Tee.processResponse(request);
  	},
  	
  	before: function() {
  		Element.show("status");
  		Element.hide("confirm"); 
  		Element.update("errors", "");
  	},
  	
  	showInviteForm: function() {
  		Element.hide("confirm");
  		Element.show("invite");
  		Form.reset('new-tee-form');
  	}
};

var Tips = {
	processResponse: function(request) {
  		var rtn = eval('(' + request.responseText + ')');
		if (rtn.saved == true) {
			RedBox.close();
			document.location.reload();
		} else {
			for (i = 0; i < rtn.errors.length; i++) {
				Element.update($('errors'), rtn.errors[i]);
			}
		}
  	},
  	
  	complete: function(request) {
  		Element.hide("status"); 
  		Tips.processResponse(request);
  	},
  	
  	before: function() {
  		Element.show("status"); 
  		Element.update("errors", "");
  	}
};

var Reviews = {
	processResponse: function(request) {
  		var rtn = eval('(' + request.responseText + ')');
		if (rtn.saved == true) {
			RedBox.close();
			document.location.reload();
		} else {
			for (i = 0; i < rtn.errors.length; i++) {
				Element.update($('errors'), rtn.errors[i]);
			}
		}
  	},
  	
  	complete: function(request) {
  		Element.hide("status"); 
  		Reviews.processResponse(request);
  	},
  	
  	before: function() {
  		Element.show("status"); 
  		Element.update("errors", "");
  	}
};

var UserStats = {
	changeView: function(el, tableName) {
		UserStats.showHideColumns(tableName, el.options[el.selectedIndex].value);
		UserStats.showHideColumns(tableName, el.options[el.selectedIndex].value);
	},

	showHideColumns: function(tableName, showClass) {
		var tbl = $(tableName);
		var dataCells = tbl.getElementsByTagName("td");
		var headerCells = tbl.getElementsByTagName("th");

		for (var i = 0; i < headerCells.length; i++) {
			if (Element.hasClassName(headerCells[i], showClass) || 
				Element.hasClassName(headerCells[i], "all")) {
				Element.show(headerCells[i]);
			} else {
				Element.hide(headerCells[i]);
			}
		}

		for (var i = 0; i < dataCells.length; i++) {
				if (Element.hasClassName(dataCells[i], showClass) || 
					Element.hasClassName(dataCells[i], "all")) {
					Element.show(dataCells[i]);
				} else {
					Element.hide(dataCells[i]);
				}
		}

	}
};

var Loopr = {};

Loopr.Scorecard = Class.create();
Loopr.Scorecard.prototype = {

 initialize: function(form) {
   this.form = form;
   this.current = null;
      
   this.holes = new Array(18);
   for (var i = 0; i < this.holes.length; i++) {
   	this.holes[i] = new Loopr.Scorecard.Hole(i, this);
   }
   
   this.total_score = $('score_total_score');
   this.out_score = $('score_out_score');
   this.in_score = $('score_in_score');
   this.total_putts = $('score_total_putts');
   this.out_putts = $('score_out_putts');
   this.in_putts = $('score_in_putts');
   this.total_penalty = $('score_total_penalty_strokes');
   this.out_penalty = $('score_out_penalty_strokes');
   this.in_penalty = $('score_in_penalty_strokes');
   this.total_gir = $('score_total_gir');
   this.out_gir = $('score_out_gir');
   this.in_gir = $('score_in_gir');
   this.total_par = $('score_total_par');
   this.out_par = $('score_out_par');
   this.in_par = $('score_in_par');
   this.total_fairway = $('score_total_fairway');
   this.out_fairway = $('score_out_fairway');
   this.in_fairway = $('score_in_fairway');
                  
   new Form.EventObserver(this.form, this.updateTotals.bind(this))

	 Event.observe(document.body, "click", this.closePopup.bind(this));

 },

 closePopup: function(event) {
	 	// console.log("caught click in scorecard.closePopup()=",this);
	  var target = event.target || event.srcElement;
		if (this.current) {
			var currentPopup = this.current.tee_shot_form;
			if(!Element.descendantOf(target, currentPopup.parentNode.parentNode)) {
				this.current.hideTeeShotPopup();
			}
		}
 },
 
 next: function(field) {
 	for (var n = 0; n < this.form.elements.length; n++) {
 		if (field == this.form.elements[n]) {
 			for (var j = n + 1; j < this.form.elements.length; j++) {
 				if (this.form.elements[j].disabled == false) {
 					this.form.elements[j].focus();
 					this.form.elements[j].select();
 					return;
 				}	
 			}
 		}
 	}
 },

 
 updateTotals: function() {
    var score_total_score = 0;
    var score_out_score = 0;
    var score_in_score = 0;
    var score_total_putts = 0;
    var score_out_putts = 0;
    var score_in_putts = 0;
    var score_total_penalty_strokes = 0;
    var score_out_penalty_strokes = 0;
    var score_in_penalty_strokes = 0;
    var score_total_gir = 0;
    var score_out_gir = 0;
    var score_in_gir = 0;
    var score_total_par = 0;
    var score_out_par = 0;
    var score_in_par = 0;
        
    this.holes.each(function(h, i) {
			var score = parseInt(h.score.value);
			var putts = parseInt(h.putts.value);
			var penalty_strokes = parseInt(h.penalty.value);
			var par = parseInt(h.par.value);

      if (par && !isNaN(par)) { 
        score_total_par += par;

	      if (h.num < 9) {
	      	score_out_par += par;
	      } else {
	        score_in_par += par;
	      }
      }
            
      if (penalty_strokes && !isNaN(penalty_strokes)) { 
          score_total_penalty_strokes += penalty_strokes; 
	      if (h.num < 9) {
	      	score_out_penalty_strokes += penalty_strokes;
	      } else {
	        score_in_penalty_strokes += penalty_strokes;
	      }
      }
           
      if (putts && !isNaN(putts)) { 
          score_total_putts += putts; 
	      if (h.num < 9) {
	      	score_out_putts += putts;
	      } else {
	        score_in_putts += putts;
	      }
      }

      if (score && !isNaN(score)) { 
	      score_total_score += score;         
	      if (h.num < 9) {
	      	score_out_score += score;
	      } else {
	        score_in_score += score;
	      }
      }
      
      if (h.is_gir()) {
	    	score_total_gir += 1;         
	    	if (h.num < 9) {
	      	score_out_gir += 1;
	    	} else {
	      	score_in_gir += 1;
	    	}
    	}
   	
    	h.scorecard.total_score.update(score_total_score == 0 ? "" : score_total_score.toString());
 			h.scorecard.out_score.update(score_out_score == 0 ? "" : score_out_score.toString());
 			h.scorecard.in_score.update(score_in_score == 0 ? "" : score_in_score.toString());
 			h.scorecard.total_putts.update(score_total_putts == 0 ? "" : score_total_putts.toString());
 			h.scorecard.out_putts.update(score_out_putts == 0 ? "" : score_out_putts.toString());
 			h.scorecard.in_putts.update(score_in_putts == 0 ? "" : score_in_putts.toString());
 			h.scorecard.out_penalty.update(score_out_penalty_strokes.toString());
 			h.scorecard.in_penalty.update(score_in_penalty_strokes.toString());
 			h.scorecard.total_penalty.update(score_total_penalty_strokes.toString());
 			h.scorecard.total_gir.update(score_total_gir == 0 ? "" : score_total_gir.toString());
 			h.scorecard.out_gir.update(score_out_gir == 0 ? "" : score_out_gir.toString());
 			h.scorecard.in_gir.update(score_in_gir == 0 ? "" : score_in_gir.toString());
		 	h.scorecard.out_par.update(score_out_par.toString());
		 	h.scorecard.in_par.update(score_in_par.toString());
		 	h.scorecard.total_par.update(score_total_par.toString());
 		});
 }
}

Loopr.Scorecard.Hole = Class.create();
Loopr.Scorecard.Hole.prototype = {

 initialize: function(num, scorecard) {
   this.num = num;
 	 this.disabled = false;
 	 this.scorecard = scorecard;
 	 
   this.par = $('score_' + num + '_par');
   this.par.observe("keyup", this.parKeyUp.bind(this), false);
   
   this.score = $('score_' + num + '_score');
   this.score.observe("keyup", this.scoreKeyUp.bind(this), false);

   this.putts = $('score_' + num + '_putts');
   this.putts.observe("keyup", this.puttsKeyUp.bind(this), false);
   
   this.penalty = $('score_' + num + '_penalty_strokes');
   
   this.tee_shot = $('score_tee_shot_' + num);
   this.tee_shot.observe("click", this.teeShotClick.bind(this));
   // this.tee_shot_click_observer = this.teeShotClick.bind(this);
   
   this.tee_shot_club = $('score_' + num + '_tee_shot_club');
   this.tee_shot_club.observe("change", this.updateTeeShot.bind(this));

   this.fairway = $('score_' + num + '_fairway');
   this.fairway.observe("change", this.updateTeeShot.bind(this));

   this.tee_shot_distance = $('score_' + num + '_tee_shot_distance');
   this.tee_shot_distance.observe("change", this.updateTeeShot.bind(this));
   
   this.tee_shot_form = $('tee_shot_form_' + num);
   
   $('tee_shot_form_' + num + '_close').observe("click", this.hideTeeShotPopup.bind(this));
 },
 
 updateTeeShot: function(evt) {
   // console.log("in updateTeeShot");
	 var club = this.tee_shot_club.getValue();
	 var direction = this.fairway.getValue();
	 var distance = this.tee_shot_distance.getValue();
	
	 var updateText = "";
	 updateText += (club || "-") + '<br />';
	 updateText += '<img src="/images/fairway-arrow-' + (direction || 'none') + '.gif" /><br />';
	 updateText += (distance || '-');
	
	 this.tee_shot.update(updateText);
 },

 teeShotMouseOver: function(evt) {
	this.tee_shot.addClassName("tee-shot-display-hover")
 },

 teeShotMouseOut: function(evt) {
	this.tee_shot.removeClassName("tee-shot-display-hover")
 },
   
 teeShotClick: function(evt) {
	 // console.log("in showTeeShotForm hole=", this.num);
	 if (this.scorecard.current && this.scorecard.current.num != this.num) {
	 	this.scorecard.current.hideTeeShotPopup();
	 }
	 
	 if (this.disabled) {
	 	return;
	 }
	 
	 this.scorecard.current = this;
	 this.tee_shot_form.show();
	 this.tee_shot.addClassName("tee-shot-display-active");
 },
 
 hideTeeShotPopup: function() {
	 this.tee_shot_form.hide();	
	 this.tee_shot.removeClassName("tee-shot-display-active");
 },
 
 parKeyUp: function(evt) {
 	var charCode = (evt.charCode) ? 
    evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    
  if (charCode > 31 && this.par.value.match(/^[345]$/)) {
  	this.scorecard.next(this.par);
  }
  this.redraw();
 },

 scoreKeyUp: function(evt) {
 	var charCode = (evt.charCode) ? 
    evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    
  if (charCode > 31 && this.score.value.match(/^(1\d|[2-9])$/)) {
  	this.scorecard.next(this.score);
  }
  this.redraw();
 },

 puttsKeyUp: function(evt) {
 	var charCode = (evt.charCode) ? 
    evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    
  if (charCode > 31 && this.putts.value.match(/^[0-9]$/)) {
  	this.scorecard.next(this.putts);
  }
  this.redraw();
 },
 
 activate: function() {
 	this.disabled = false;
 	this.redraw();
 },
 
 redraw: function() {
 	 // console.log("redrawing hole ", this.num);
   this.par.disabled = this.disabled;   
   this.score.disabled = this.disabled;
   this.putts.disabled = this.disabled;
   this.penalty.disabled = this.disabled;
   
   if (this.disabled == false) {
	  
    if (this.is_gir()) {
      	$('score_' + this.num + '_gir').update('<img src="/images/checkmark.gif" />');
    } else {
      	$('score_' + this.num + '_gir').update('');
    }  
 	 }
 },
 
 is_gir: function() {
 	return (this.score.value && this.putts.value && ((this.par.value - 2) >= this.score.value - this.putts.value));
 },

 is_fairway: function() {
 	return (this.fairway.value == "0");
 },
  
 disable: function() {
 	this.disabled = true;
 	this.redraw();
 }

}

Loopr.TextboxDefaultText = Class.create()
Loopr.TextboxDefaultText.prototype = {
  
  initialize: function(textbox, default_text, doValidation, form, alert_text) {
    this.default_text = default_text;
    this.alert_text = alert_text;
    this.search_box = $(textbox);
    this.search_form = $(form);
    if (this.search_box.value == '') {
      this.search_box.value = default_text;
    }
    
    Event.observe(this.search_box, "focus", this.textboxFocus.bind(this), false);
    Event.observe(this.search_box, "blur", this.textBoxBlur.bind(this), false);
    if(doValidation)
      Event.observe(this.search_form, "submit", this.validateSearchForm.bind(this), false);
  },
  
  textboxFocus: function(ev) {
    if (this.search_box.value == this.default_text) {
      this.search_box.s=this.search_box.className;
      this.search_box.value='';
      this.search_box.className='search-regular-text';
    }
  },
  
  textBoxBlur: function(ev) {
    if (this.search_box.value == '') {
      var c=this.search_box.className;    
      if (c != (this.search_box.s?this.search_box.s:"search-dimmed-text")) {
        this.search_box.className=this.search_box.s?this.search_box.s:"search-dimmed-text";
      }      
      this.search_box.value=this.default_text;
    }
  },
  
  validateSearchForm: function(ev) {
    if (this.search_box.value == this.default_text) {
      this.search_box.value = '';
    }
  }
}

Loopr.RoundSummarizer = Class.create();
Loopr.RoundSummarizer.prototype = {
  
  initialize: function(filterForm, rounds, summary) {
    this.filterForm = filterForm || 'filter';
    this.rounds = rounds || [];
    this.summary = summary || {};
    // this.onLoadingHandler = this.handleSubmit.bind(this);
  },

  load_summary: function(onLoading, onComplete) {
  	this.remote('/rounds/summary/', onLoading, onComplete);
  },
    
  load_rounds_with_summary: function(onLoading, onComplete) {
  	this.remote('/rounds/rounds_with_summary/', onLoading, onComplete);
  },
  
  remote: function(path, onLoading, onComplete) {
	  new Ajax.Request(
	  		path, {
	  		asynchronous:true, 
	  		evalScripts:true, 
	  		onComplete: this.handleSubmitResponse.bind(this, onComplete), 
	  		onCreate: this.handleSubmit.bind(this, onLoading), 
	  		parameters:Form.serialize(this.filterForm)
	  	}
	  );
  },
  
  handleSubmitResponse: function(onComplete, request) {
  	var response = eval('(' + request.responseText + ')');
  	this.rounds = response.rounds;
  	this.summary = response.summary;
  	onComplete(this)
  },

  handleSubmit: function(onLoading, request) {
  	onLoading(this);
  }
}

Loopr.Metric = Class.create();
Loopr.Metric.prototype = {
  initialize: function(props) {
  	this.props = props
  },
  
  drill: function() {
    if (this.props.drill == false) {
      return null;
    } else if (this.props.drill == null) {
      return this;
    } else {
      return Loopr.Metric[this.props.drill.metric]
    }
  },
  
  label: function() {
    return this.props.label;
  },
  
  name: function() {
    return this.props.name;
  },

  unit: function() {
    return this.props.unit;
  },  

  min: function() {
    return this.props.min;
  }  
}

Loopr.LineChart = Class.create();
Loopr.LineChart.prototype = {
  
  initialize: function(chartDiv, dataTable, xGraph, yLeftGraph, roundUrl, metric) {
		this.chartDiv = chartDiv;
		this.dataTable = dataTable;
		this.xGraph = xGraph;
		this.yLeftGraph = yLeftGraph;
		this.roundUrl = roundUrl;
		this.metric = metric;
		
		var flashvars = {
      path: "/amline/",
      settings_file: encodeURIComponent("/amline/amline_settings.xml"),
      additional_chart_settings: encodeURIComponent(this.settingsAsXml()),
      chart_data: encodeURIComponent(this.dataAsXml())
		};
		
		swfobject.embedSWF("/amline/amline.swf", this.chartDiv, "600", "250", "8", "expressInstall.swf", flashvars, {}, { id: "amline" });
  },

  draw: function() {
  	// console.log("in draw");
  	
  	var cx = $('amline');
  	cx.setSettings(this.settingsAsXml());		
		cx.setData(this.dataAsXml());
				
		// console.log("done in draw");
  },
	
	blur: function() {
		var cx = $('amline');
		cx.setParam("vertical_lines.alpha", 20);
		cx.setParam("graphs.graph[1].line_alpha", 20);
		cx.setParam("graphs.graph[1].bullet_alpha", 20);
	},
	
  settingsAsXml: function() {
  	// console.log("in settingsAsXml");
		var yLeftGraph = this.yLeftGraph;
   	var yLeftGraphHasValues = this.dataTable.any(); // this.hasValues(yLeftGraph.name());

		// console.log("in settingsAsXml: yLeftGraph=", yLeftGraph, "yLeftGraphHasValues=", yLeftGraphHasValues);
			
   	var settings_xml = "<settings><values>";
		if (yLeftGraphHasValues) {
			settings_xml += "<y_left><enabled>true</enabled></y_left>"; 
    	settings_xml += "<y_left><unit>" + yLeftGraph.unit() + "</unit></y_left>";
    } else {
      settings_xml += "<y_left><enabled>false</enabled></y_left>";
    }
      
    settings_xml += "</values>";    	
    	
    if (!yLeftGraphHasValues) {
    	settings_xml += "<labels><label lid='0'><text><![CDATA[Sorry, no data to graph for " + yLeftGraph.label() + "]]></text></label></labels>";
    } else {
    	settings_xml += "<labels><label lid='0'><text><![CDATA[]]></text></label></labels>";
    }
    
    if (yLeftGraphHasValues) {
    	settings_xml += "<graphs>";
    	if (yLeftGraphHasValues) {
    		settings_xml += "<graph gid='1'><title>" + yLeftGraph.label() + "</title></graph>";
    	} 	
    	settings_xml += "</graphs>";
    }
    
    settings_xml += "</settings>";
    	
    // console.log("done in settingsAsXml");
    	 	
		return settings_xml;
	},

	dataAsXml: function() {
		var xml = "<chart>";
		var series = [];		  
		var graph_1 = [];
		var yLeftGraph = this.yLeftGraph;
		
		// console.log("in dataAsXml() yLeftGraph=", yLeftGraph);
		
		if (this.dataTable.length > 0) {

			for (var i = 0; i < this.dataTable.length; i++) {
				var yLeftGraphData = this.dataTable[i].stats[yLeftGraph.name()].value;
				var yLeftGraphDataFormatted = this.dataTable[i].stats[yLeftGraph.name()].value_formatted;
				var yLeftGraphDataUrl = this.roundUrl +"/" + this.dataTable[i].id;
				if (yLeftGraphData != null) {
				  series.push(this.dataTable[i][this.xGraph]);
				  graph_1.push({ url: yLeftGraphDataUrl, description: this.dataTable[i]["course"] + "\n" + yLeftGraphDataFormatted, data: yLeftGraphData });
				}
	    }
	  } else {
	  	return "<chart><series><value xid='0'> </value></series><graphs><graph gid='1'></graph></chart>";
	  }

		xml += "<series>";
		for (var i = 0; i < series.length; i++) {
			xml += "<value xid='" + i + "'>" + series[i] + "</value>";
		}
		xml += "</series>";

		xml += "<graphs>";
		xml += "<graph gid='1'>";
		for (var i = 0; i < graph_1.length; i++) {
			xml += "<value xid='" + i + "' description='" + graph_1[i].description.replace(/\'/, '&#39;') + "' url='" + graph_1[i].url + "'>" + graph_1[i].data + "</value>";
		}
		xml += "</graph>";
		xml += "</graphs>";
		xml += "</chart>";
		
    return xml;
  }  
}

Loopr.ReportPager = Class.create();
Loopr.ReportPager.prototype = {  
  initialize: function() {
    this.page = 0;
  },
  
  redraw: function() {
  	// $('scroller').update("");
		if (this.pages > 1) {
			var prev;
      if (this.page > 0) {
      	prev = document.createElement('a');
				Event.observe(prev, "click", this.setPage.bindAsEventListener(this, this.page - 1));
			  prev.setAttribute("href", "#");
			  prev.appendChild(document.createTextNode("\u00ab Previous"));
      } else {
				prev = document.createElement('span')
				prev.appendChild(document.createTextNode("\u00ab Previous"));
      }

			var nexxt;
      if (this.page < (this.pages -1 )) {
      	nexxt = document.createElement('a');
				Event.observe(nexxt, "click", this.setPage.bindAsEventListener(this, this.page + 1));
			  nexxt.setAttribute("href", "#");
			  nexxt.appendChild(document.createTextNode("Next \u00bb"));
      } else {
				nexxt = document.createElement('span')
				nexxt.appendChild(document.createTextNode("Next \u00bb"));
      }      
      
      $('scroller').update("");
      $('scroller').appendChild(prev);
      $('scroller').appendChild(nexxt);
		} else {
			$('scroller').update("")
		}
  },

	setPage: function(e, page) {
    this.showPage(page);
		if (e) { Event.stop(e); }
	},
	
	showPage: function(page) {
		// console.log("showPage() page=", page, " this.pages=", this.pages);
		this.page = page;
		for (var i = 0; i < this.pages; i++) {
		  if (i == page) {
		  	$('page-' + i).show();
		  } else {
		  	$('page-' + i).hide();
		  }
		}

		this.redraw();

	}
}

Loopr.RoundList = Class.create();
Loopr.RoundList.prototype = {  
  initialize: function(updateUrl, buttonContainer, progressContainer, roundsContainer) {
  	this.buttonContainer = buttonContainer ? $(buttonContainer) : $('round-filter-links');
  	this.progressContainer = progressContainer ? $(progressContainer) : $('progress');
  	this.roundsContainer = roundsContainer ? $(roundsContainer) : $('rounds');
  	this.updateUrl = updateUrl || '/rounds/list';
  	
  	this.filterLinks = this.buttonContainer.select('a');
  },
  
  addButton: function(id, params) {
		Event.observe(id, "click", this.updateRounds.bindAsEventListener(this, $(id), params));
  },
    
  updateRounds: function(ev, btn, params) {
  	var filterLinks = this.filterLinks;
  	var progressContainer = this.progressContainer;
  	var roundsContainer = this.roundsContainer;
  	
  	ev.stop();
  	
	  new Ajax.Updater(roundsContainer, this.updateUrl, {
	  	parameters: params,
	    onLoading: function(transport) {
	    	progressContainer.show();
	    	roundsContainer.addClassName("disabled");
	      filterLinks.each(function(i) {
	      	if (btn.id == i.id) {
	      		i.addClassName('current');
	      	} else {
	      		i.removeClassName('current');
	      	}
	      });
	    },
	    
	    onComplete: function(transport) {
	    	roundsContainer.removeClassName("disabled");
	    	progressContainer.hide(); 
	    }
	  });
  
  }
}

Event.observe(window, 'load', function(e) {
    if ($('navbar-search')) {
      new Loopr.TextboxDefaultText('navbar-search-q', 'course name', true, 'navbar-search');
    }
}, false);

	  			  
