// show or hide text input value

$.fn.search = function() {
	return this.focus(function() { if( this.value == this.defaultValue ) { this.value = ""; } }).blur(function() { if( !this.value.length ) { this.value = this.defaultValue; } });
};


/* news cycling on home page - start */

current_news_item = 1;
manual = false;
clickedItem = 0;
clicked = 0;

function updateNewsNav() {
	if (manual == false) {
		if (current_news_item < 6) {
			// page 1
			$("#news_nav_p1").css({"display" : "block"});
			$("#news_nav_p2").css({"display" : "none"});
			$(".news_nav_link_p1").addClass("active_page_number");
			$(".news_nav_link_p2").removeClass("active_page_number");
			updateNavState();
		}
		else {
			// page 2
			$("#news_nav_p1").css({"display" : "none"});
			$("#news_nav_p2").css({"display" : "block"});
			$(".news_nav_link_p1").removeClass("active_page_number");
			$(".news_nav_link_p2").addClass("active_page_number");
			updateNavState();
		}
	}
	else {
		// manual click
		previous_news_item = parseInt(current_news_item) - 1;
		if (clicked < 1) {
			$(".news_nav_item_" + previous_news_item).find(".news_nav_item").removeClass("news_nav_item_active");
		}
		else {
			$(".news_nav_item_" + previous_clicked_item).find(".news_nav_item").removeClass("news_nav_item_active");
		}
		clicked++;
		$(".news_nav_item_" + clickedItem).find(".news_nav_item").addClass("news_nav_item_active");
		previous_clicked_item = parseInt(clickedItem);
		current_news_item = parseInt(clickedItem) + 1;
		// reset the counter
		if (current_news_item > 10) {
			current_news_item = 1;
		}
	}
}

function updateNavState() {
	previous_news_item = parseInt(current_news_item) - 1;
	if (previous_news_item == 0) {
		previous_news_item = 10;
	}
	$(".news_nav_item_" + previous_news_item).find(".news_nav_item").removeClass("news_nav_item_active");
	$(".news_nav_item_" + current_news_item).find(".news_nav_item").addClass("news_nav_item_active");
	current_news_item++;
	// reset the counter
	if (current_news_item > 10) {
		current_news_item = 1;
	}
}

function showPause() {
	$(".pause_news").css({"display" : "block"});
	$(".play_news").css({"display" : "none"});
}

function showPlay() {
	$(".pause_news").css({"display" : "none"});
	$(".play_news").css({"display" : "block"});
}

/* news cycling on home page - end */


/* slideshow - start */

function showPauseShow() {
	$(".pause_show").css({"display" : "block"});
	$(".play_show").css({"display" : "none"});
}

function showPlayShow() {
	$(".pause_show").css({"display" : "none"});
	$(".play_show").css({"display" : "block"});
}

function pauseShow() {
	$(".slideshow").cycle('pause');
	showPlayShow();
}

/* slideshow - end */

/* google maps start */

function loadClubOffice(){

	if (!($.browser.msie && $.browser.version.substr(0,1) < 7)) {

	/* club office map */
	var point = new google.maps.LatLng(51.311625,-2.29091);

	var myMapOptions = {
		zoom: 13,
		center: point,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	var map = new google.maps.Map(document.getElementById("map1"),myMapOptions);

	var image = new google.maps.MarkerImage(
		'_assets/img/common/gmap_bath_rugby_logo.png',
		new google.maps.Size(46,46),
		new google.maps.Point(0,0),
		new google.maps.Point(23,46)
	);

	var shadow = new google.maps.MarkerImage(
		'_assets/img/common/gmap_bath_rugby_shadow.png',
		new google.maps.Size(72,46),
		new google.maps.Point(0,0),
		new google.maps.Point(23,46)
	);

	var shape = {
		coord: [29,0,32,1,34,2,35,3,37,4,38,5,39,6,40,7,41,8,41,9,42,10,43,11,43,12,44,13,44,14,44,15,45,16,45,17,45,18,45,19,45,20,45,21,45,22,45,23,45,24,45,25,45,26,45,27,45,28,45,29,44,30,44,31,44,32,43,33,43,34,42,35,41,36,41,37,40,38,39,39,38,40,37,41,35,42,34,43,32,44,29,45,16,45,13,44,11,43,10,42,8,41,7,40,6,39,5,38,4,37,4,36,3,35,2,34,2,33,1,32,1,31,1,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,1,15,1,14,1,13,2,12,2,11,3,10,4,9,4,8,5,7,6,6,7,5,8,4,10,3,11,2,13,1,16,0,29,0],
		type: 'poly'
	};

	var marker = new google.maps.Marker({
		draggable: false,
		raiseOnDrag: false,
		icon: image,
		shadow: shadow,
		shape: shape,
		map: map,
		position: point
	});

	}

}

function loadRec(){

	if (!($.browser.msie && $.browser.version.substr(0,1) < 7)) {

	/* the rec map */
	var point = new google.maps.LatLng(51.381803,-2.354885);

	var myMapOptions = {
		zoom: 16,
		center: point,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	var map = new google.maps.Map(document.getElementById("map2"),myMapOptions);

	var image = new google.maps.MarkerImage(
		'_assets/img/common/gmap_bath_rugby_logo.png',
		new google.maps.Size(46,46),
		new google.maps.Point(0,0),
		new google.maps.Point(23,46)
	);

	var shadow = new google.maps.MarkerImage(
		'_assets/img/common/gmap_bath_rugby_shadow.png',
		new google.maps.Size(72,46),
		new google.maps.Point(0,0),
		new google.maps.Point(23,46)
	);

	var shape = {
		coord: [29,0,32,1,34,2,35,3,37,4,38,5,39,6,40,7,41,8,41,9,42,10,43,11,43,12,44,13,44,14,44,15,45,16,45,17,45,18,45,19,45,20,45,21,45,22,45,23,45,24,45,25,45,26,45,27,45,28,45,29,44,30,44,31,44,32,43,33,43,34,42,35,41,36,41,37,40,38,39,39,38,40,37,41,35,42,34,43,32,44,29,45,16,45,13,44,11,43,10,42,8,41,7,40,6,39,5,38,4,37,4,36,3,35,2,34,2,33,1,32,1,31,1,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,1,15,1,14,1,13,2,12,2,11,3,10,4,9,4,8,5,7,6,6,7,5,8,4,10,3,11,2,13,1,16,0,29,0],
		type: 'poly'
	};

	var marker = new google.maps.Marker({
		draggable: false,
		raiseOnDrag: false,
		icon: image,
		shadow: shadow,
		shape: shape,
		map: map,
		position: point
	});

	}

}

function loadShop(){

	if (!($.browser.msie && $.browser.version.substr(0,1) < 7)) {

	/* shop map */
	var point = new google.maps.LatLng(51.383393,-2.357404);

	var myMapOptions = {
		zoom: 16,
		center: point,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	var map = new google.maps.Map(document.getElementById("map3"),myMapOptions);

	var image = new google.maps.MarkerImage(
		'_assets/img/common/gmap_bath_rugby_logo.png',
		new google.maps.Size(46,46),
		new google.maps.Point(0,0),
		new google.maps.Point(23,46)
	);

	var shadow = new google.maps.MarkerImage(
		'_assets/img/common/gmap_bath_rugby_shadow.png',
		new google.maps.Size(72,46),
		new google.maps.Point(0,0),
		new google.maps.Point(23,46)
	);

	var shape = {
		coord: [29,0,32,1,34,2,35,3,37,4,38,5,39,6,40,7,41,8,41,9,42,10,43,11,43,12,44,13,44,14,44,15,45,16,45,17,45,18,45,19,45,20,45,21,45,22,45,23,45,24,45,25,45,26,45,27,45,28,45,29,44,30,44,31,44,32,43,33,43,34,42,35,41,36,41,37,40,38,39,39,38,40,37,41,35,42,34,43,32,44,29,45,16,45,13,44,11,43,10,42,8,41,7,40,6,39,5,38,4,37,4,36,3,35,2,34,2,33,1,32,1,31,1,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,1,15,1,14,1,13,2,12,2,11,3,10,4,9,4,8,5,7,6,6,7,5,8,4,10,3,11,2,13,1,16,0,29,0],
		type: 'poly'
	};

	var marker = new google.maps.Marker({
		draggable: false,
		raiseOnDrag: false,
		icon: image,
		shadow: shadow,
		shape: shape,
		map: map,
		position: point
	});

	}

}

function loadTicketOffice(){

	if (!($.browser.msie && $.browser.version.substr(0,1) < 7)) {

	/* ticket office map */
	var point = new google.maps.LatLng(51.381803,-2.354885);

	var myMapOptions = {
		zoom: 16,
		center: point,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	var map = new google.maps.Map(document.getElementById("map4"),myMapOptions);

	var image = new google.maps.MarkerImage(
		'_assets/img/common/gmap_bath_rugby_logo.png',
		new google.maps.Size(46,46),
		new google.maps.Point(0,0),
		new google.maps.Point(23,46)
	);

	var shadow = new google.maps.MarkerImage(
		'_assets/img/common/gmap_bath_rugby_shadow.png',
		new google.maps.Size(72,46),
		new google.maps.Point(0,0),
		new google.maps.Point(23,46)
	);

	var shape = {
		coord: [29,0,32,1,34,2,35,3,37,4,38,5,39,6,40,7,41,8,41,9,42,10,43,11,43,12,44,13,44,14,44,15,45,16,45,17,45,18,45,19,45,20,45,21,45,22,45,23,45,24,45,25,45,26,45,27,45,28,45,29,44,30,44,31,44,32,43,33,43,34,42,35,41,36,41,37,40,38,39,39,38,40,37,41,35,42,34,43,32,44,29,45,16,45,13,44,11,43,10,42,8,41,7,40,6,39,5,38,4,37,4,36,3,35,2,34,2,33,1,32,1,31,1,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,1,15,1,14,1,13,2,12,2,11,3,10,4,9,4,8,5,7,6,6,7,5,8,4,10,3,11,2,13,1,16,0,29,0],
		type: 'poly'
	};

	var marker = new google.maps.Marker({
		draggable: false,
		raiseOnDrag: false,
		icon: image,
		shadow: shadow,
		shape: shape,
		map: map,
		position: point
	});

	}

}

/* google maps end */


$(document).ready(function() {

	// search submit button hover
	$("#search_submit_btn").hover(function() {
		$(this).attr("src","_assets/img/common/search_submit_btn_over.gif");
	}, function() {
		$(this).attr("src","_assets/img/common/search_submit_btn.gif");
	});


	// search field value show/hide
	$("#searchfield").search();


	// news cycling
	if ($('#news_content_home').length != 0) {

		$('#news_content_home').cycle({
			fx:     	'fade',
			speed:		500,
			timeout: 	4000,
			pause:		1,
			autostop:	0,
			before:		updateNewsNav
		});

	}

	// news nav items
    // Removed as of Nov 2011 - Now the nav links straight to the news item
//	$(".news_nav_item").click(function(event) {
//		manual = true;
//		clickedItem = $(this).parent().parent().attr("class");
//		clickedItem = clickedItem.substring(14,16);
//		// pause the cycle
//		$("#news_content_home").cycle('pause');
//		showPlay();
//		// show the clicked item
//		$('#news_content_home').cycle((parseInt(clickedItem) - 1));
//    	return false;
//		event.preventDefault();
//
//	});

	// pause news
	$(".pause_news").click(function(event) {
		$("#news_content_home").cycle('pause');
		showPlay();
		manual = true;
		event.preventDefault();
	});

	// play news
	$(".play_news").click(function(event) {
		$("#news_content_home").cycle('resume');
		showPause();
		manual = false;
		clicked = 0;
		event.preventDefault();
	});

	// page 1 news
	$(".news_nav_link_p1").click(function(event) {
		$("#news_content_home").cycle('pause');
		showPlay();
		manual = true;
		$("#news_nav_p1").css({"display" : "block"});
		$("#news_nav_p2").css({"display" : "none"});
		$(".news_nav_link_p1").addClass("active_page_number");
		$(".news_nav_link_p2").removeClass("active_page_number");
		event.preventDefault();
	});

	// page 2 news
	$(".news_nav_link_p2").click(function(event) {
		$("#news_content_home").cycle('pause');
		showPlay();
		manual = true;
		$("#news_nav_p1").css({"display" : "none"});
		$("#news_nav_p2").css({"display" : "block"});
		$(".news_nav_link_p1").removeClass("active_page_number");
		$(".news_nav_link_p2").addClass("active_page_number");
		event.preventDefault();
	});


	// prevent bg image flickering on anchor links in ie6
	if($.browser.msie) {
	  try { document.execCommand("BackgroundImageCache", false, true);
	  }catch(err){}
	}


	// tooltips
	if ($(".hoverbox").length != 0){

		$(".hoverbox").each(function(){

			var tt = $(this).attr("id");

			$(this).click(function(event){
				event.preventDefault();
			});

			$(this).mouseover(function(){
				$("." + tt).css({opacity:1,display:"none"}).fadeIn(200);
			}).mousemove(function(kmouse){
				var left_pos;
				var top_pos;
				left_pos = kmouse.pageX + 25;
				top_pos = kmouse.pageY - 25;
				$("." + tt).css({left:left_pos, top:top_pos});
			}).mouseout(function(){
				$("." + tt).hide();
			});

		});
	}


	// subscribe button on newsletter signup
	if ($(".subscribe_btn").length != 0) {
		$(".subscribe_btn").hover(function() {
			  $(this).attr("src","_assets/img/common/subscribe_btn_over.gif");
		},function(){
			  $(this).attr("src","_assets/img/common/subscribe_btn.gif");
		});
	}


	// tabs
	if ($("ul.tabs").length != 0) {
		$("ul.tabs").tabs("div.panes > div.pane");
	}

	// tabs on contact page
	$("#contact_club_office").click(function(event) {
		window.setTimeout(loadClubOffice, 10, true);
		event.preventDefault();
	});
	$("#contact_the_rec").click(function(event) {
		window.setTimeout(loadRec, 10, true);
		event.preventDefault();
	});
	$("#contact_shop").click(function(event) {
		window.setTimeout(loadShop, 10, true);
		event.preventDefault();
	});
	$("#contact_ticket_office").click(function(event) {
		window.setTimeout(loadTicketOffice, 10, true);
		event.preventDefault();
	});


	// tabs (promos)
	if ($("ul.tabs_promo").length != 0) {
		$("ul.tabs_promo").tabs("div.panes > div.pane");
	}


	// slideshows
	if ($(".slideshow").length != 0) {
		$('.slideshow')
		.after('<div class=".slideshow_nav">')
		.cycle({
			fx:  			'fade',
			speed:  		500,
			timeout: 		4000,
			pager:  		'.slideshow_nav',
			onPagerEvent:	pauseShow
		});
	}

	// pause slideshow
	$(".pause_show").click(function(event) {
		$(".slideshow").cycle('pause');
		showPlayShow();
		event.preventDefault();
	});

	// play slideshow
	$(".play_show").click(function(event) {
		$(".slideshow").cycle('resume');
		showPauseShow();
		event.preventDefault();
	});


	//lineups
	if (!($.browser.msie && $.browser.version.substr(0,1) < 7)) {

		if ($(".player_hover").length != 0) {

			$(".player_profile").hide();
			$(".player_profile_bg").hide();

			profile_image_folder_non = "_assets/img/players/120x120/"; //If the image is set to '0.jpg', use this path
            profile_image_folder = "assets/images/";

			$(".player_hover").hover(function() {
				// get the players number
				player_number = $(this).attr("id");
				player_number = player_number.substr(2,2);
				player_number = parseInt(player_number);
				// populate the hover box
                if( playersArray[player_number - 1][1] == "0.jpg" )
    				$(".player_profile").find("img").attr("src", profile_image_folder_non + playersArray[player_number - 1][1]);
                else
                    $(".player_profile").find("img").attr("src", profile_image_folder + playersArray[player_number - 1][1]);
				$(".player_profile").find("h3.player_number").html(playersArray[player_number - 1][2]);
				$(".player_profile").find("h3.player_name").html(playersArray[player_number - 1][0]);
				$(".player_profile").find("h3.player_position").html(positionsArray[player_number - 1]);
				box_height = $(".player_profile").height();
				$(".player_profile_bg").css({opacity: 0.85});
				$(".player_profile_bg").css({height: box_height + 20});
				$(".player_profile").fadeIn(200);
				$(".player_profile_bg").fadeIn(200);

			}, function() {
				$(".player_profile").hide();
				$(".player_profile_bg").hide();
			});

		}

	}


	// events table (png fix)
	if ($("#events").length != 0) {
		$('body').supersleight({shim: '_assets/img/transparent.gif'}); // path to transparent gif
		$('#events').supersleight();
	}


	// ticket prices (lightbox)
	if ($("#ticket_prices").length != 0) {
		$("#ticket_prices").fancybox({
			'overlayColor'		: '#000',
			'overlayOpacity'	: 0.5,
			'width'				: 462,
			'height'			: 700,
			'padding'			: 0,
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'
		});
	}

    // virtual tour (lightbox)
	if ($("#virtual_tour").length != 0) {
		$("#virtual_tour").fancybox({
			'overlayColor'		: '#000',
			'overlayOpacity'	: 0.62,
			'width'				: 855,
			'height'			: 592,
			'padding'			: 0,
			'autoScale'			: false,
            'scrolling'         : 'no',
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'

		});
	}


	// gallery
	if ($("#gallery").length != 0) {
		$(function() {
			var galleries = $('.ad-gallery').adGallery({
				"loader_image"				:	"_assets/img/gallery/classic-loader.gif",
				"effect"					:	"slide-hori",
				"display_next_and_prev"		:	true,
				"display_back_and_forward"	:	true
			});
		});
	}


});


function loadTravelMap(){

	var park = new Array();
	var park_ride = new Array();
	var bus_stop = new Array();
	var train_station = new Array();

	// parking locations
	park.push(new google.maps.LatLng(51.381632, -2.354037));
	park.push(new google.maps.LatLng(51.380473, -2.352580));
	park.push(new google.maps.LatLng(51.379469, -2.356591));
	park.push(new google.maps.LatLng(51.377794, -2.357985));
	park.push(new google.maps.LatLng(51.378243, -2.359369));
	park.push(new google.maps.LatLng(51.378732, -2.362300));
	park.push(new google.maps.LatLng(51.381638, -2.364489));
	park.push(new google.maps.LatLng(51.382060, -2.362148));
	park.push(new google.maps.LatLng(51.384618, -2.360829));
	park.push(new google.maps.LatLng(51.384149, -2.359155));
	park.push(new google.maps.LatLng(51.383901, -2.367107));

	// park & ride locations
	park_ride.push(new google.maps.LatLng(51.378672, -2.359949));
	park_ride.push(new google.maps.LatLng(51.381159, -2.357550));
	park_ride.push(new google.maps.LatLng(51.381089, -2.362708));
	park_ride.push(new google.maps.LatLng(51.384397, -2.361399));
	park_ride.push(new google.maps.LatLng(51.382669, -2.349683));

	// bus stop location
	bus_stop.push(new google.maps.LatLng(51.377701, -2.359284));

	// train stop location
	train_station.push(new google.maps.LatLng(51.377680,-2.357030));

	// stadium location
	var point = new google.maps.LatLng(51.381926, -2.355593);

	// new centre point
	var new_centre = new google.maps.LatLng(51.380353, -2.359339);

	var myMapOptions = {
		zoom: 16,
		center: new_centre,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};

	var map = new google.maps.Map(document.getElementById("map"),myMapOptions);

	// icon for bath rugby stadium
	var image = new google.maps.MarkerImage(
		'_assets/img/common/gmap_bath_rugby_logo.png',
		new google.maps.Size(46,46),
		new google.maps.Point(0,0),
		new google.maps.Point(23,46)
	);

	var shadow = new google.maps.MarkerImage(
		'_assets/img/common/gmap_bath_rugby_shadow.png',
		new google.maps.Size(72,46),
		new google.maps.Point(0,0),
		new google.maps.Point(23,46)
	);

	var shape = {
		coord: [29,0,32,1,34,2,35,3,37,4,38,5,39,6,40,7,41,8,41,9,42,10,43,11,43,12,44,13,44,14,44,15,45,16,45,17,45,18,45,19,45,20,45,21,45,22,45,23,45,24,45,25,45,26,45,27,45,28,45,29,44,30,44,31,44,32,43,33,43,34,42,35,41,36,41,37,40,38,39,39,38,40,37,41,35,42,34,43,32,44,29,45,16,45,13,44,11,43,10,42,8,41,7,40,6,39,5,38,4,37,4,36,3,35,2,34,2,33,1,32,1,31,1,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,1,15,1,14,1,13,2,12,2,11,3,10,4,9,4,8,5,7,6,6,7,5,8,4,10,3,11,2,13,1,16,0,29,0],
		type: 'poly'
	};

	// icon for parking
	var park_image = new google.maps.MarkerImage(
		'_assets/img/common/park_image.png',
		new google.maps.Size(28,28),
		new google.maps.Point(0,0),
		new google.maps.Point(14,14)
	);

	var park_shadow = new google.maps.MarkerImage(
		'_assets/img/common/park_shadow.png',
		new google.maps.Size(46,28),
		new google.maps.Point(0,0),
		new google.maps.Point(14,14)
	);

	var park_shape = {
		coord: [26,0,27,1,27,2,27,3,27,4,27,5,27,6,27,7,27,8,27,9,27,10,27,11,27,12,27,13,27,14,27,15,27,16,27,17,27,18,27,19,27,20,27,21,27,22,27,23,27,24,27,25,27,26,26,27,1,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,1,0,26,0],
		type: 'poly'
	};

	// icon for park & ride
	var park_ride_image = new google.maps.MarkerImage(
		'_assets/img/common/park_ride_image.png',
		new google.maps.Size(28,28),
		new google.maps.Point(0,0),
		new google.maps.Point(14,14)
	);

	var park_ride_shadow = new google.maps.MarkerImage(
		'_assets/img/common/park_ride_shadow.png',
		new google.maps.Size(46,28),
		new google.maps.Point(0,0),
		new google.maps.Point(14,14)
	);

	var park_ride_shape = {
		coord: [26,0,27,1,27,2,27,3,27,4,27,5,27,6,27,7,27,8,27,9,27,10,27,11,27,12,27,13,27,14,27,15,27,16,27,17,27,18,27,19,27,20,27,21,27,22,27,23,27,24,27,25,27,26,26,27,1,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,1,0,26,0],
		type: 'poly'
	};

	// icon for bus stop
	var bus_stop_image = new google.maps.MarkerImage(
		'_assets/img/common/bus_stop_image.png',
		new google.maps.Size(51,28),
    	new google.maps.Point(0,0),
    	new google.maps.Point(26,14)
	);

	var bus_stop_shadow = new google.maps.MarkerImage(
		'_assets/img/common/bus_stop_shadow.png',
		new google.maps.Size(69,28),
    	new google.maps.Point(0,0),
    	new google.maps.Point(26,14)
	);

	var bus_stop_shape = {
		coord: [48,0,49,1,50,2,50,3,50,4,50,5,50,6,50,7,50,8,50,9,50,10,50,11,50,12,50,13,50,14,50,15,50,16,50,17,50,18,50,19,50,20,50,21,50,22,50,23,50,24,50,25,49,26,48,27,2,27,1,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,1,1,2,0,48,0],
		type: 'poly'
	};

	// icon for train station
	var train_station_image = new google.maps.MarkerImage(
		'_assets/img/common/train_station_image.png',
		new google.maps.Size(40,28),
    	new google.maps.Point(0,0),
    	new google.maps.Point(20,14)
	);

	var train_station_shadow = new google.maps.MarkerImage(
		'_assets/img/common/train_station_shadow.png',
		new google.maps.Size(58,28),
    	new google.maps.Point(0,0),
    	new google.maps.Point(20,14)
	);

	var train_station_shape = {
		coord: [38,0,39,1,39,2,39,3,39,4,39,5,39,6,39,7,39,8,39,9,39,10,39,11,39,12,39,13,39,14,39,15,39,16,39,17,39,18,39,19,39,20,39,21,39,22,39,23,39,24,39,25,39,26,38,27,1,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,1,0,38,0],
		type: 'poly'
	};


	// add the main marker
	var marker = new google.maps.Marker({
		draggable: false,
		raiseOnDrag: false,
		icon: image,
		shadow: shadow,
		shape: shape,
		map: map,
		title: 'Bath Rugby Stadium Grounds',
		position: point
	});

	// add parking icons
	for (var i = 0; i < park.length; i++) {
		var marker = new google.maps.Marker({
			position: park[i],
			icon: park_image,
			shadow: park_shadow,
			shape: park_shape,
			map: map,
			title: 'Parking'
		});
	}

	// add park & ride icons
	for (var i = 0; i < park_ride.length; i++) {
		var marker = new google.maps.Marker({
			position: park_ride[i],
			icon: park_ride_image,
			shadow: park_ride_shadow,
			shape: park_ride_shape,
			map: map,
			title: 'Park and Ride'
		});
	}

	// add bus stop icons
	for (var i = 0; i < bus_stop.length; i++) {
		var marker = new google.maps.Marker({
			position: bus_stop[i],
			icon: bus_stop_image,
			shadow: bus_stop_shadow,
			shape: bus_stop_shape,
			map: map,
			title: 'Bus stop'
		});
	}

	// add train station icons
	for (var i = 0; i < train_station.length; i++) {
		var marker = new google.maps.Marker({
			position: train_station[i],
			icon: train_station_image,
			shadow: train_station_shadow,
			shape: train_station_shape,
			map: map,
			title: 'Train station'
		});
	}

}



$(window).load(function() {

	/* match timeline start */

	td_count = 0;
	scroll_amount = 370;
	clicked_forward = 0;
	at_end = false;

	// count the number of td's in the top row
	$(".icons").children("td").each(function(){
		td_count++;
	});

	match_length = td_count;
	x_remaining = (match_length - 16) * 37;	// 16 mins are shown initially
	total_scroll_clicks = Math.floor(x_remaining / scroll_amount);
	last_scroll_amount = (x_remaining % scroll_amount) + 37;

	// set the height of the buttons & outer wrapper
	if ($(".timeline").length != 0) {
		timeline_height = $(".timeline").height();
		$(".match_events").css({"height" : timeline_height});
		$(".timeline_back > a").css({"height" : timeline_height});
		$(".timeline_forward > a").css({"height" : timeline_height});
	}

	// forward button
	$(".tl_forward").click(function(event) {

		if (clicked_forward < total_scroll_clicks) {
			clicked_forward++;
			at_end = false;
			$('.timeline').animate({"left" : "-=" + scroll_amount}, 200);
		}
		else if (clicked_forward == total_scroll_clicks) {
			at_end = true;
			clicked_forward++;
			$('.timeline').animate({"left" : "-=" + last_scroll_amount}, 200);
		}
		$(".timeline_hoverbox").hide();
		event.preventDefault();
	});

	// back button
	$(".tl_back").click(function(event) {

		// if at the end of the timeline
		if (at_end == true) {
			clicked_forward--;
			at_end = false;
			$('.timeline').animate({"left" : "+=" + last_scroll_amount}, 200);
		}
		else if (clicked_forward <= total_scroll_clicks && clicked_forward > 0) {
			clicked_forward--;
			at_end = false;
			$('.timeline').animate({"left" : "+=" + scroll_amount}, 200);
		}
		$(".timeline_hoverbox").hide();
		event.preventDefault();
	});

	// tooltips
	if ($(".timeline").length != 0){

		// hide hoverbox failsafes
		$(".timeline").hover(function() {
			$(".timeline_hoverbox").hide();
		});
		$(".tl_back").hover(function() {
			$(".timeline_hoverbox").hide();
		});
		$(".tl_forward").hover(function() {
			$(".timeline_hoverbox").hide();
		});

		$(".tl_hover").each(function(){

			var tt = "timeline_hoverbox"

			$(this).click(function(event){
				event.preventDefault();
			});

			$(this).mouseover(function(){

				var hb_text = $(this).children("img").attr("alt");
				var hb_text_split = hb_text.split("|", 2);
				hb_text_top = hb_text_split[0];
				hb_text_bottom = hb_text_split[1];
				$("." + tt).find(".hb_top").html(hb_text_top);
				$("." + tt).find(".hb_bottom").html(hb_text_bottom);
				var hb_width = $("." + tt).width();
				hb_centre = hb_width / 2;

				$("." + tt).css({opacity:1,display:"none"}).fadeIn(200);

			}).mousemove(function(kmouse){
				var left_pos;
				var top_pos;
				left_pos = kmouse.pageX - hb_centre;
				if ($.browser.msie && $.browser.version.substr(0,1) == 7) {
					top_pos = kmouse.pageY + 10; // ie7 doesn't play nicely
				}
				else {
					top_pos = kmouse.pageY + 25;
				}
				$("." + tt).css({left:left_pos, top:top_pos});
			}).mouseout(function(){
				$("." + tt).hide();
			});

		});
	}

	/* match timeline end */

    // start of added by Al
    if ($(".box_entry").length != 0) {
        if ($(".box_entry_sponsors").length == 0) {
            $(".box_entry:last").addClass("box_entry_last");
        } else {
            $(".box_entry_sponsors").each(function() {
                $(this).find(".box_entry:last").addClass("box_entry_last");
            });
        }
    }

    if ($(".promos_row").length != 0) {
        $('.promos_row').each(function() {
            var each_row = $(this);
            tallest = 0;
            each_row.find('h2').each(function() {
                if ($(this).height() > tallest) {
                tallest = $(this).height();
                }
            });
            each_row.find('h2').each(function() {
               $(this).height(tallest);
            });
        });
    }

    $(".player_hover").click(function(event) {
		event.preventDefault();
	});

    if ($("#signup_form").length != 0) {
        $(".subscribe_btn").click(function(event) {
            event.preventDefault();
            var error_msg_text = "";
            var errors = 0;
            $("#global_error_messages").hide();

            if ($("#nl_title").attr("selectedIndex") == 0) {
                $("#err_nl_title").html("<p>Please select your title</p>");
                errors++;
            } else {
                $("#err_nl_title").html("");
            }

            if ($("#nl_firstname").val() == "") {
                $("#err_nl_firstname").html("<p>Please enter your first name</p>");
                errors++;
            } else {
                $("#err_nl_firstname").html("");
            }

            if ($("#nl_surname").val() == "") {
                $("#err_nl_surname").html("<p>Please enter your last name</p>");
                errors++;
            } else {
                $("#err_nl_surname").html("");
            }

            var email_val = $("#nl_email").val();
            var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            if (email_val == '') {
                $("#err_nl_email").html("<p>Please enter your email address</p>");
                errors++;
            } else if (!email_reg.test(email_val)) {
                $("#err_nl_email").html("<p>Please enter a valid email address</p>");
                errors++;
            } else {
                $("#err_nl_email").html("");
            }

            if ($("#nl_postcode").val() == "") {
                $("#err_nl_postcode").html("<p>Please enter your postcode</p>");
                errors++;
            } else {
                $("#err_nl_postcode").html("");
            }
            
            if ($('#signup_form input[name=nl_ticket_promos]:checked').val() == undefined &&
                $('#signup_form input[name=nl_club_news]:checked').val() == undefined &&
                $('#signup_form input[name=nl_shop_promos]:checked').val() == undefined
            ) {
                $("#err_nl_newsoptions").html("<p>Please select a newsletter to subscribe to</p>");
                errors++;
            } else {
                $("#err_nl_newsoptions").html("");
            }

            if (errors == 0) {
                $("#signup_form").submit();
            }

        });
    }

    // end of added by Al
    
    /* added by James - 07/11/2011 */

	//if we are in the "tickets and travel" section, add event tracking to the "buy tickets" subnav item
	
	if ($("body").attr("id") == "tickets") {

		$("ul.primary_subnav li a:contains('Buy tickets')").bind('click', function() {
		  //get current URI - remove starting slash
		  currPage = window.location.pathname
		  currPage = currPage.substr(1);
		  _gaq.push(['_trackEvent', currPage, 'Subnav', 'Buy tickets']);
		});


	}
	
	/* end added by James - 07/11/2011 */


});
