function playerFilter() {
	document.getElementById('playerForm').submit();
}

function validateContact() {
	var validator = new formValidator();
	var validator2 = new formValidator();
	validator.checkText( 'contact_firstname', 'Your First Name', '- Your first name...' );
	validator.checkText( 'contact_lastname', 'Your Last Name', '- Your last name...' );
	validator2.checkText( 'contact_telephone', 'Your Telephone Number', '- Telephone number...' );
	validator2.validateEmailAddress( 'contact_email', 'Your Email', '- Email address...' );
	validator.checkText( 'contact_enquiry', 'Your Enquiry', '- Type your question here...' );
	if ( validator2.numberOfErrors() == 2 ) {
		validator.addError( 'Either your telephone number or email address' );
	}
	return validator.getReturn();
}

function slideTicker( i ) {
	if ( i == $('#tickerLinks span').length ) {
		i = 0;
	}
	$('#tickerLinks span').eq(i).animate( {
		left: 0,
		opacity: 1
	}, 1000, function() {
		$(this).delay( 3000 ).animate( {
			opacity: 0,
			top: 40
		}, 500, function() {
			$(this).css( {
				left: 900,
				top: 0
			} );
			slideTicker( i + 1 );
		} );
	} );
}

$(document).ready( function() {
	if ( $('#subNav a:first').is('.on') ) {
		$('#content').prepend( '<div id="blanker"></div>' );
	}
	$('#teamMembers tr, #partners tr').before( '<tr><td class="groove" colspan="2">&nbsp;</td></tr>' );
	$('.formInput').focus( function() {
		if ( $(this).val() == $(this).prop('defaultValue') ) {
			$(this).val('');
		}
	} ).blur( function() {
		if ( $(this).val() == '' ) {
			$(this).val( $(this).prop('defaultValue') );
		}
	} );
	var $anchors = $('#contentPanelText a[name]');
	if ( $anchors.length > 0 ) {
		var leftNav = '<div class="contentPanelNav"><div class="contentPanelNavTop"></div><div class="contentPanelNavContent"><div class="contentPanelNavContent2">';
		$anchors.each( function() {
			leftNav += '<a href="#' + $(this).attr('name') + '">' + $(this).closest('h2').text() + '</a>';
		} );
		leftNav += '</div></div><div class="contentPanelNavBottom"></div></div>';
		$('#contentPanelContentThin').prepend( leftNav );
		$('#contentPanelText').addClass('hasLeftNav');
		$('.contentPanelNavContent2 a:last').addClass('last');
	}
	$.get( 'rss.php', function( xml ) {
		var xmlDoc = $.parseXML( xml );
		var $xml = $( xmlDoc );
		$items = $xml.find( 'item' );
		$ticker = $('#tickerLinks');
		$items.each( function() {
			var $description = $(this).find('description');
			var $url = $(this).find('link');
			var date = new Date( $(this).find('pubDate').text() );
			var dateString = date.getDate() + '.' + ( date.getMonth() + 1 ) + '.' + date.getFullYear();
			$el = $( '<span><a href="' + $url.text() + '" target="_blank">' + dateString + '</a> - ' + $description.text() + '</span>' );
			$el.css( { opacity: 0 } );
			$ticker.append( $el );
		} );
		slideTicker( 0 );
	}, 'html' );
	
} );

/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);
			
	if ( isNaN(day_diff) )
		return time;
			
	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		Math.ceil( day_diff / 7 ) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
jQuery.fn.prettyDate = function(){
	return this.each(function(){
		var date = prettyDate(this.title);
		if ( date )
			jQuery(this).text( date );
	});
};
