// JavaScript Document

document.writeln('<script language="javascript" type="text/javascript" src="/betting/js/templates.js"></script>');
document.writeln('<script language="javascript" type="text/javascript" src="/betting/js/home_boxes.js"></script>');
document.writeln('<script language="javascript" type="text/javascript" src="/betting/js/home_tabs.js"></script>');

Home = Class.create({
		initialize: function ()
		{
			// vars
			this.racesUrl = "/betting/ajax_services/next_races.asp";
			this.meetingsUrl = "/betting/ajax_services/next_meetings.asp";
			this.newsUrl = "/betting/ajax_services/latest_news.asp";
			this.latestFinishUrl = "/betting/ajax_services/latest_finish.asp";
			
			this.loading = null;
			this.content = null;
			this.latestFinish = { ctnr: null, title: null, time: null, photo: null, info: null, order: null, race: null, first: null, second: null, third: null, fourth: null }
			this.data = null;
			
			this.homeObj = null;
			
			this.templates = null;
			this.templateList = [
				{name: "loading", url: "/betting/templates/loading.asp"}
				];
			
			document.observe("dom:loaded", this.handleDomLoaded.bind(this));
		},
		handleDomLoaded: function (evt)
		{
			this.content = $('Content');
			this.latestFinish.ctnr = $('LatestFinish');
			this.latestFinish.title = $('LatestFinishTitle');
			this.latestFinish.photo = $('LatestFinishPhoto');
			this.latestFinish.info = $('LatestFinishInfo');
			this.latestFinish.order = $('LatestFinishOrder');
			this.latestFinish.time = $('LatestFinishTime');
			this.latestFinish.race = $('LatestFinishRace');
			this.latestFinish.first = $('LatestFinishFirst');
			this.latestFinish.second = $('LatestFinishSecond');
			this.latestFinish.third = $('LatestFinishThird');
			this.latestFinish.fourth = $('LatestFinishFourth');
			
			this.homeObj = (this.content.hasClassName("home-1"))? new HomeBoxes(this): new HomeTabs(this);
			
			this.templates = new Templates(this.templateList);
			this.templates.loadedCallback = this.templatesLoaded.bind(this);
			
			this.addEventListeners();
			
			this.latestFinish.title.style.display = "none";
			this.latestFinish.photo.style.display = "none";
			this.latestFinish.info.style.display = "none";
			this.latestFinish.order.style.display = "none";
		},
		addEventListeners: function ()
		{
			/*Element.observe(this.meetings.title, "click", this.toggleMeetings.bind(this));
			Element.observe(this.races.title, "click", this.toggleRaces.bind(this));
			Element.observe(this.betTypes.title, "click", this.toggleBetTypes.bind(this));
			//Element.observe(this.runners.title, "click", this.toggleRunners.bind(this));
			Element.observe(this.meetings.buttons.today, "click", this.showTodayMeetings.bind(this));
			Element.observe(this.meetings.buttons.tomorrow, "click", this.showTomorrowMeetings.bind(this));
			Element.observe(this.btnBet, "click", this.handleBtnBetClick.bind(this));*/
		},
		templatesLoaded: function ()
		{
			this.loading = document.createElement("div");
			this.loading.innerHTML = this.templates.getTemplate("loading");
			this.latestFinish.ctnr.appendChild(this.loading);
			
			this.loading.style.width = Number.toCssString(100);
			this.loading.style.marginTop = Number.toCssString((this.latestFinish.ctnr.offsetHeight - this.loading.offsetHeight) / 2);
			this.loading.style.marginLeft = Number.toCssString((this.latestFinish.ctnr.offsetWidth - this.loading.offsetWidth) / 2);
			
			var requestOptions = {}
			requestOptions.onSuccess = this.handleLatestFinishRequestSuccess.bind(this);
			requestOptions.onFailure = this.handleLatestFinishRequestFailure.bind(this);
			requestOptions.method = "post"

			new Ajax.Request(this.latestFinishUrl, requestOptions);
		},
		handleLatestFinishRequestSuccess: function (response)
		{
                    
			this.data = response.responseText.evalJSON(false);
			
			this.latestFinish.ctnr.removeChild(this.loading);
			this.latestFinish.title.style.display = "block";
			this.latestFinish.photo.style.display = "block";
			this.latestFinish.info.style.display = "block";
			this.latestFinish.order.style.display = "block";

			var timeParts = /([0-9]{2}):([0-9]{2}):[0-9]{2}/.exec(this.data.race.start);
			
			var photoId = 0;
			try
			{
				photoId = this.data.details.photos[0].url;
			}
			catch(e) {}
				

			this.latestFinish.title.innerHTML = this.data.meeting.hippodrome + " - " + this.data.race.name;
			// Question: photos or photos[0].photoId??? check if data have arrived
			this.latestFinish.photo.innerHTML = (this.data.details.photos != "")? "<img src='http://www.zeturf.com/photo/" + photoId + ".jpg' border='0' class='photo-finish-image-small' />": "";

			this.latestFinish.time.innerHTML = timeParts[1] + "h" + timeParts[2];
			this.latestFinish.race.innerHTML = "RACE " + this.data.race.number;

			var limit = 20;
			var text;

			text = this.data.runnersFinish[0].number + " - " + this.data.runnersFinish[0].name.toLowerCase();
			text = (text.length < limit)? text: "<a title=\"" + text + "\" class='tooltip'>" + text.substring(0, limit) + "...</a>";
			this.latestFinish.first.innerHTML = "1 &nbsp; " + text;
			
			text = this.data.runnersFinish[1].number + " - " + this.data.runnersFinish[1].name.toLowerCase();
			text = (text.length < limit)? text: "<a title=\"" + text + "\" class='tooltip'>" + text.substring(0, limit) + "...</a>";
			this.latestFinish.second.innerHTML = "2 &nbsp; " + text;
			
			text = this.data.runnersFinish[2].number + " - " + this.data.runnersFinish[2].name.toLowerCase();
			text = (text.length < limit)? text: "<a title=\"" + text + "\" class='tooltip'>" + text.substring(0, limit) + "...</a>";
			this.latestFinish.third.innerHTML = "3 &nbsp; " + text;
			
			text = this.data.runnersFinish[3].number + " - " + this.data.runnersFinish[3].name.toLowerCase();
			text = (text.length < limit)? text: "<a title=\"" + text + "\" class='tooltip'>" + text.substring(0, limit) + "...</a>";
			this.latestFinish.fourth.innerHTML = "4 &nbsp; " + text;
			
		},
		handleLatestFinishRequestFailure: function (response)
		{
			
		},
		findMeeting: function (meetings, id)
		{
			returnVal = null;
			for(var i = 0; i < meetings.length; i++) {
				if(meetings[i].id == id) {
					returnVal = meetings[i];
					break;
				}
			}
			return returnVal;
		}
	});

bethome = new Home();
