/*
 * 
 *  General JavaScript for the NWRCGi
 *  
 *  @author Rob Boyle 
 *  @date 13/09/2009
 *  
 * 	@author ZebraTable class David Walsh
 *	http://davidwalsh.name/
 * 	@alterations rob boyle
 * 	@date 17/11/2009
 *
 *
 *  		SECTION 1 : Mootool Scripts
 *  		
*/










/*------------------------------------------------------------------------------------------------------------
 * 
 * 												SECTION 1 : Mootools 
 * 
 *----------------------------------------------------------------------------------------------------------*/

 /*--------------------------------------------------------------------------------------
 * Zebra table Class
 * 
 *  @author ZebraTable class David Walsh
 *	http://davidwalsh.name/
 * 	@alterations rob boyle
 * 	@date 17/11/2009
 * 
*/
var ZebraTable = new Class({
	//implements
	Implements: [Options,Events],
	
	//options
	options: {
		elements: 'table.list-table',
		cssEven: 'even',
		cssOdd: 'odd',
		cssHighlight: 'highlight',
		cssMouseEnter: 'mo'
	},
	
	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options);
		//zebra-ize!
		$$(this.options.elements).each(function(table) {
			this.zebraize(table);
		},this);
	},
	
	//a method that does whatever you want
	zebraize: function(table) {
		//for every row in this table...
		table.getElements('tr').each(function(tr,i) {
			//check to see if the row has th's
			//if so, leave it alone
			//if not, move on
			if(tr.getFirst().get('tag') != 'th') {
				//set the class for this based on odd/even
				var options = this.options, klass = i % 2 ? options.cssEven : options.cssOdd;
				//start the events!
				tr.addClass(klass).addEvents({
					//mouseenter
					mouseenter: function () {
						if(!tr.hasClass(options.cssHighlight)) tr.addClass(options.cssMouseEnter).removeClass(klass);
					},
					//mouseleave
					mouseleave: function () {
						if(!tr.hasClass(options.cssHighlight)) tr.removeClass(options.cssMouseEnter).addClass(klass);
					},
					//click 
					click: function() {
						//if it is currently not highlighted
						if(!tr.hasClass(options.cssHighlight))
							tr.removeClass(options.cssMouseEnter).addClass(options.cssHighlight);
						else
							tr.addClass(options.cssMouseEnter).removeClass(options.cssHighlight);
					}
				});
			}
		},this);
	}
});











/*--------------------------------------------------------------------------------------
 * countDown ()
 * 
 * Calculates how many days before the next ride and inserts a div displaying this value
*/

function countDown () {
	
		//Find the difference in the dates
		var now = new Date();
		var actualDate = new Date();
		actualDate = ($('usDate').get('html'));
		actualDate = Date.parse(actualDate);	 			
		actualDate.increment('hour', 24);				
		dayDifference = now.diff(actualDate); 

		
		switch(dayDifference)
		{
			case 0:
  				countDownMessage = 'There\'s a ride today!'
  				break;
			case 1:
  			 	countDownMessage = 'Our next ride is tomorrow'
				break;
			default:
  				countDownMessage = 'Our next ride is in '+dayDifference+' days...'
		}
		
		
		//Set up the div
		var rideCountdown = new Element('div', {
				html:countDownMessage,
				id:'rideCountdown'
		});

	
		//Detect screen width and set the rideCountdown's div position accordingly
		if (screen.width > 1400){
				rideCountdown.set('class', 'rideCountdown_highres');
				rideCountdown.inject($('footer'), 'before');//insert div into the DOM	
		}else{
				if ($('content_container_titlepage')) {
						rideCountdown.set('class', 'rideCountdown_lowres');
						rideCountdown.inject($('footer'), 'before');//insert div into the DOM	
				}
		}
}










/*--------------------------------------------------------------------------------------
 * resetSearchForm()
 * 
 * Resets the search form on each page when the page is refreshed
*/

function resetSearchForm(){
		$('searchFor').set('value','');
} 
 









/*--------------------------------------------------------------------------------------
 * sendForm()
 * 
 * submits form onload, if the form exists on the current page
*/

function sendForm() {
	
		if ($('emailForm')){
				document.emailForm.submit();
		} 
}

 
 
 
 
 
 
 
 
 
 /*--------------------------------------------------------------------------------------
 * domready
 * Does what the Domready should
*/

window.addEvent('domready', function() {
		
		var zebraTables = new ZebraTable();	
		countDown();
  		resetSearchForm();
		sendForm();

});	
