$(document).ready(function () {
	app.callInProgress = false;
    
    //A page must define this js variable if they want the init method called.  This should
    //save unnecessary data services to be called on pages that include this file, but don't
    //need the init function to be used.
	if(typeof initRacingNews != 'undefined' && initRacingNews) app.action.racingNews.init();
    
});

$.extend(app.util, {

	racingNews : {
		headline : function (article) {
			return $('<p>').append($('<a>').attr('href', 'javascript:app.action.racingNews.getArticle(' + article.identity + ', \'' + article.source + '\');').append(article.title));
		}
	}

});

$.extend(app.service, {

	racingNews : {
		news : function (result, numberOfArticles, source) {
			app.call("RacingNews/Home", {n : numberOfArticles, s : source}, result);
		}
	}

});

$.extend(app.action, {

	racingNews : {
		init : function () {
			app.log("Getting Racing News");

			app.service.racingNews.news(
				function (data) {
					var news = $('#content #right .racing-news');

					news.append($('<h3>').append('Racing News'));
					news.append($('<h4>').append('Headlines'));

					app.log("Adding news to the view");

					$.each(data, function (index, value) {
						news.append(app.util.racingNews.headline(this));
					});

					news.append($('<p>').addClass('readmore').append($('<a>').attr('href', '/racingnews').append('read more &gt;')));
				},
				5,
				'T'
			);

			return false;
		},

		getArticle : function (identity, source) {
			$('form.article input[name="a"]').attr('value', identity);
			$('form.article input[name="s"]').attr('value', source);
			$('form.article').submit();
		}
	}

});
