// Javascript for ireland.com form centre hosted component
$(function() {

//
// GLOBAL
//

	// Reset all inline styles TABLE
	$('table').attr({ 
		cellpadding: "0",
		cellspacing: "0",
		width: "100%",
		border: "0"
	});

	// Insert location DIV's to pages
	var urlString = window.location.toString();
	var contDiv = $('body');
		// ../racing/meetings/..
		if (urlString.match(/racing\/meetings/g)){
			contDiv.wrapInner('<div id="pageContainer" class="meetings"></div>');
		}
		// ../racing/racecard/..
		if (urlString.match(/racing\/racecard/g)){
			contDiv.wrapInner('<div id="pageContainer" class="racecard"></div>');
		}
		// ../racing/fastresults/..
		if (urlString.match(/racing\/fastresults/g)){
			contDiv.wrapInner('<div id="pageContainer" class="results fast"></div>');
		}
		// ../racing/results/..
		if (urlString.match(/racing\/results\//g)){
			contDiv.wrapInner('<div id="pageContainer" class="results home"></div>');
		}
		// ../racing/result/..
		if (urlString.match(/racing\/result\//g)){
			contDiv.wrapInner('<div id="pageContainer" class="results item"></div>');
		}
		// ../racing/horse/..
		if (urlString.match(/racing\/horse/g)){
			contDiv.wrapInner('<div id="pageContainer" class="profile horse"></div>');
		}
		// ../racing/jockey/..
		if (urlString.match(/racing\/jockey/g)){
			contDiv.wrapInner('<div id="pageContainer" class="profile jockey"></div>');	
		}
		// ../racing/trainer/..
		if (urlString.match(/racing\/trainer/g)){
			contDiv.wrapInner('<div id="pageContainer" class="profile trainer"></div>');
		}

//
// HOMEPAGE
//

	// Change URL target
	$('#home a').attr('target','_top');
	
	// Remove extra content
	$('div#home_latest ul').find('li:gt(2)').css('display','none');
	
	// Change list-style-image to background-image
	$('div#home_latest ul li').each(function(){
		// read out inline style
		var listImage = $(this).attr('style');
		// get the url
		var listImageUrl = listImage.indexOf('url');
		// replace the list style with background image
		$(this).css('background-image', listImage.substr(listImageUrl)).css('list-style-image','');
	});
	
	// Change URL paths
	var urlA = "http://www.ireland.com/sport/horseracing/";
	var urlB = "/landing.ie?location=";
		// "Cards" links
		$('#home #home_cards a').each(function(){
			var encodedLink = encodeURIComponent(this.href);
			var getLink = (urlA + "racecards" + urlB + encodedLink);
			this.href = getLink;
		});
		// "Result" link
		$('#home #home_latest a').each(function(){
			var encodedLink = encodeURIComponent(this.href);
			var getLink = (urlA + "raceresults" + urlB + encodedLink);
			this.href = getLink;
		});
		// "All Racecards" link
		$('#home #home_footer a:eq(0)').each(function(){
			var encodedLink = encodeURIComponent(this.href);
			var getLink = (urlA + "racecards" + urlB + encodedLink);
			this.href = getLink;
		});
		// "All Fast Results" link
		$('#home #home_footer a:eq(1)').each(function(){
			var encodedLink = encodeURIComponent(this.href);
			var getLink = (urlA + "fastresults" + urlB + encodedLink);
			this.href = getLink;
		});

//
// SUBPAGES
//

	// meetings images
	$('img[src=/Images/Icons/i_blue_bullet.gif]').attr('src','/Images/ireland/racing/i_bullet.gif');
	$('img[src=/Images/Icons/i_mresult.gif]').attr('src','/Images/ireland/racing/i_mresult.gif');

	// consistent date styles
	$('.meetings .raceIdxSectionHdr:first').removeClass('raceIdxSectionHdr').addClass('date');
	$('.racecard .race_name:first').removeClass('race_name').addClass('date');
	$('.results .raceIdxSectionHdr:first').removeClass('raceIdxSectionHdr').addClass('date');
	$('.item .race_name:first').removeClass('race_name').addClass('date');

	// fill empty table cells for consistent borders
	$('table.racecard_table td:empty').append('&nbsp;');
	
	// group like rows for result
	$('.item table.racecard_table tr:odd td[class!="nr_entry"]').css('border-bottom','none');
	
	// table row highlighting for result
	$('.item table.racecard_table tr[name^="rc"]').hover(function(){
			$(this).addClass('rowHover').next().addClass('rowHover');
		},function(){
			$(this).removeClass('rowHover').next().removeClass('rowHover');
	});
	$('.item table.racecard_table tr[name^="nr"]').hover(function(){
			$(this).addClass('rowHover');
		},function(){
			$(this).removeClass('rowHover');
	});
	
	// table row highlighting for racecard
	$('.racecard table.racecard_table').find('tbody tr').hover(function(){
			$(this).addClass('rowHover');
		},function(){
			$(this).removeClass('rowHover');
	});
	
	// bold certain elements
	$('table.raceQuickResult td:first').css('font-weight','bold');
	
	// fix non-runner colspan
	$('.item table.racecard_table tr td.racecard_nr').parent().prev().find('td').attr('colspan','9').css('border-bottom','none');
	
	// align fast result table cells
	$('.fast table.fr_left tr').find('td[colspan!="3"]:eq(0),td[colspan!="3"]:eq(2)').css('width','50px');
	
	

	// Fix item result navigation
	$('.item ul#tools_right li span').each(function(){
		// read out span title
		var spanTitle = $(this).attr('title');
		// replace with link
		$(this).replaceWith('<a title="' + spanTitle + '">' + $(this).text() + '</a>');
	});
	
	// destroy all line breaks used for layout
	$('br').remove();
	
//
// PROFILE PAGES
//

	// table row hovers
	$('.trainer .recentRaces, .jockey .recentRaces, .horse .recentRaces').find('tbody tr').hover(function(){
			$(this).addClass('rowHover');
		},function(){
			$(this).removeClass('rowHover');
	});
	
	// padding on left most cells
	$('');

});