/* Helix Athletics JS by Josh Lizarraga @ FreshCutSD.com */
/* Copyright 2009 Helix Charter High School */

(function(){

YAHOO.namespace('HA');
var HA = YAHOO.HA;

// ScheduleStar Widget:

HA.ScheduleStarWidget = function(){
	
	// Properties:
	
	this.xml = false;
	this.results = [];
	this.matches = [];
	
	this.selectGender = false;
	this.selectSport = false;
	this.selectLevel = false;
	
	this.table = false;
	
	var that = this;
	
	// Objects:
	
	this.resultObject = function(pResult){
		this.Gender = pResult.getElementsByTagName('Gender')[0].firstChild.nodeValue;
		this.Level = pResult.getElementsByTagName('Level')[0].firstChild.nodeValue;
		this.Sport = pResult.getElementsByTagName('Sport')[0].firstChild.nodeValue;
		this.Place = pResult.getElementsByTagName('Place')[0].firstChild.nodeValue;
		this.Opponent = pResult.getElementsByTagName('Opponent')[0].firstChild.nodeValue;
		this.GameDate = pResult.getElementsByTagName('GameDate')[0].firstChild.nodeValue;
		this.StartTime = pResult.getElementsByTagName('StartTime')[0].firstChild.nodeValue;
	}; // resultObject()
	
	// Methods:
	
	this.checkMatch = function(pResult){
		if(this.selectGender.value != pResult['Gender'] && this.selectGender.value != 'All'){
			return false;
		}
		if(this.selectSport.value != pResult['Sport'] && this.selectSport.value != 'All'){
			return false;
		}
		if(this.selectLevel.value != pResult['Level'] && this.selectLevel.value != 'All'){
			return false;
		}
		return true;
	}; // checkMatch()
	
	this.update = function(e){
		
		// Filter matches:
		this.matches = [];
		for(var i=0; i<this.results.length; i++){
			if(this.checkMatch(this.results[i])){
				this.matches.push(this.results[i]);
			}
		}
		
		// Update table:
		var oTBODY = document.createElement('tbody');
		if(this.matches.length > 0){
			for(var i=0; i<this.matches.length; i++){
				var oTR = document.createElement('tr');
				oTBODY.appendChild(oTR);
				var oGender = document.createElement('td');
				var oLevel = document.createElement('td');
				var oSport = document.createElement('td');
				var oPlace = document.createElement('td');
				var oOpponent = document.createElement('td');
				var oGameDate = document.createElement('td');
				var oStartTime = document.createElement('td');
				oTR.appendChild(oGender);
				oTR.appendChild(oLevel);
				oTR.appendChild(oSport);
				oTR.appendChild(oPlace);
				oTR.appendChild(oOpponent);
				oTR.appendChild(oGameDate);
				oTR.appendChild(oStartTime);
				oGender.appendChild(document.createTextNode(this.matches[i]['Gender']));
				oLevel.appendChild(document.createTextNode(this.matches[i]['Level']));
				oSport.appendChild(document.createTextNode(this.matches[i]['Sport']));
				oPlace.appendChild(document.createTextNode((this.matches[i]['Place'] === 'Home') ? 'VS' : '@'));
				oOpponent.appendChild(document.createTextNode(this.matches[i]['Opponent']));
				oGameDate.appendChild(document.createTextNode(this.matches[i]['GameDate']));
				oStartTime.appendChild(document.createTextNode(this.matches[i]['StartTime']));
			}
		} else {
			var oTR = document.createElement('tr');
			oTBODY.appendChild(oTR);
			var oNoMatches = document.createElement('td');
			oTR.appendChild(oNoMatches);
			oNoMatches.appendChild(document.createTextNode('There are no upcoming games for this sport.'));
		}
		this.table.removeChild(this.table.getElementsByTagName('tbody')[0]);
		this.table.appendChild(oTBODY);
		
	}; // update()
		
	// Init:
	
	this.init = function(){
		// Get Schedule XML:
		var oRequest = YAHOO.util.Connect.asyncRequest('GET', '/ssw.php', {
			success: function(o){
				that.xml = o.responseXML;
				that.selectGender = document.getElementById('ssw-select-gender');
				that.selectSport = document.getElementById('ssw-select-sport');
				that.selectLevel = document.getElementById('ssw-select-level');
				that.table = document.getElementById('ssw-results');
				var oResults = that.xml.getElementsByTagName('result');
				for(var i=0; i<oResults.length; i++){
					that.results.push(new that.resultObject(oResults[i]));
				}
				YAHOO.util.Event.addListener('ssw-go', 'click', that.update, that, true);
			},
			failure: function(){}
		}); // asyncRequest()
	}; // init()
	
}; // ScheduleStarWidget()

// Search:

HA.initSearch = function(){
	YAHOO.util.Event.addListener('s', 'focus', function(e){
		var oTarget = YAHOO.util.Event.getTarget(e);
		if(oTarget.value == 'Search...'){
			oTarget.value = '';
		}
	});
	YAHOO.util.Event.addListener('s', 'blur', function(e){
		var oTarget = YAHOO.util.Event.getTarget(e);
		if(oTarget.value == ''){
			oTarget.value = 'Search...';
		}
	});
}; // initSearch()

YAHOO.util.Event.onDOMReady(function(){
	
	// Search:
	HA.initSearch();
	
	// ScheduleStar Widget:
	if(YAHOO.util.Dom.inDocument('ssw-header')){
		HA.ssw = new HA.ScheduleStarWidget();
		HA.ssw.init();
	}
	
});

})();