// highlights past fixtures
function highlightFixtures(){

	if(!document.getElementById('fixtures-list')){return;}
	
	var list =  document.getElementById('fixtures-list');
	// get all the h3 items (assuming the structure is constant and h3s always contain the date)	
	var headings = list.getElementsByTagName('h3');
	
 	var i;
 	var d = new Date();
 	
 	var scrollPointSet = false;
 	
 	// loop through the headings and check if the date is in the past
   	for(i = 0 ; i < headings.length ; i++) {
   			
		// trim the date
		var cleandate = headings[i].childNodes[0].nodeValue.replace(/^\s+|\s+$/g,"");
		
		// get the date in IETF standard
		var ietf_std = getDateFormat(cleandate);
		
		// if the date is in the past add the class "highlight"
		headings[i].parentNode.className = (Date.parse(ietf_std) < d.getTime()) ? "highlight" : "" ;
		
		if ( (Date.parse(ietf_std) > d.getTime()) && !scrollPointSet){
		    
		    headings[i].id = "latest-fixture";
		    
		    scrollPointSet = true;
		}

 	};	
 	
 


}

// function returns a string e.g. Sunday, 2nd December 2007 
// and converts it to IETF standard format for parsing
function getDateFormat(datestring){
 		
	var ds = datestring;
	// trim the string of spaces and split into an array
	
	var dateComponents = [];
	var dateComponents = ds.split(',')[1].replace(/^\s+|\s+$/g,"").split(" ");
	
	// clean up the elements of the array
	dateComponents[0] = dateComponents[0].replace(/th|nd|st|rd/g,"");
	dateComponents[1] = dateComponents[1].substr(0,3);
 	
	// return the formatted date
	var formatted = dateComponents[1]+" "+dateComponents[0]+", "+dateComponents[2];
 	return formatted;
	
}


	
addDOMLoadEvent(highlightFixtures);