var local_dir = "giro-di-sardegna";
var event_badge = "/2010/giro-di-sardegna/sardegna-on-steephill-tv.jpg";
var dashboard_title = "Giro di Sardegna 2010";
var dashboard_link = "http://www.steephill.tv/"+local_dir;
var num_stages = 5;
var last_completed_stage=0;
var archived_stages = 12;
var has_prologue = false;
var start_date = new Date(2010,0,17);
var race_country = "Italy";
var race_time_utc_offset = 1;
var race_time_zone = "CET";
var distance_units = "km";
var thumbs_dir = "/2010/giro-di-sardegnan/photos/thumbs/";
var base_urls =  new Array();


base_urls["profile"] = "http://www.steephill.tv/2010/giro-di-sardegna/profile-{$1}.png";
base_urls["map"] = "http://www.ilgirodisardegna.it/frontend/ilgirodisardegna/doc/giroPlanimetriaTappa{$1}.pdf";

base_urls["timetable"] = "http://www.ilgirodisardegna.it/frontend/ilgirodisardegna/doc/giro_tabMarcia_{$1}aTappa.pdf";	
base_urls["preview"] = "/"+local_dir+"/{$1}";

base_urls["results"] = "/"+local_dir+"/{$1}";

base_urls["photos"] = "/2010/"+local_dir+"/photos/{$1}";

base_urls["video"] = "/players/youtube/?title=gds-stage-recap&id={$1}&w=560";



var base_urls_list = ["profile", "map", "timetable", "preview", "results", "photos", "video"];

weather_base_url="http://www.wunderground.com/cgi-bin/findweather/getForecast?query={$1}&wuSelect=WEATHER";


bike_map_base_url="http://www.bikemap.net/route/{$1}";

var anchor_extras = new Array();

anchor_extras["profile"] = "onclick=\"click_track_explicit(this.href);NewWindow(this.href,'photo player',800,700,'no'); return false;\"";

anchor_extras ["map"] =  " target=\"_blank\"";

anchor_extras ["video"] = "onclick=\"click_track_explicit(this.href);NewWindow(this.href, 'youtube', 575,355, 'no'); return false;\"";


var stages = new Array(); 


var stage = {"stage": 1, "date": new Date(2010,1,23,12,0,0), "distance": 144, "type": "", "start_host": "Olbia", "start_country": "", "start_url": "", "finish_host": "Bonorva", "finish_country": "", "finish_url": "", "start_time": "11:30", "video_time": "none", "finish_time": "15:06", "start_weather": "http://www.wunderground.com/global/stations/16531.html", "finish_weather": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=Bonorva&wuSelect=WEATHER", "bike_map": 0, "profile": "series", "finish": "", "map": "series", "timetable": "series", "preview": "#01-preview-2010", "results": "#01-results-2010", "photos": "stage-01/", "video": "", "headline": "", "thumb": ""};
stages.push(stage);

var stage = {"stage": 2, "date": new Date(2010,1,24,12,0,0), "distance": 173, "type": "Mountain", "start_host": "Macomer", "start_country": "", "start_url": "", "finish_host": "Monte Ortobene", "finish_country": "Nuoro", "finish_url": "http://it.wikipedia.org/wiki/Monte_Ortobene", "start_time": "11:00", "video_time": "none", "finish_time": "15:19", "start_weather": "http://www.italy-weather-and-maps.com/weather/NU/", "finish_weather": "http://www.italy-weather-and-maps.com/weather/NU/", "bike_map": 0, "profile": "series", "finish": "", "map": "series", "timetable": "series", "preview": "#02-preview-2010", "results": "#02-results-2010", "photos": "stage-02/", "video": "", "headline": "", "thumb": ""};
stages.push(stage);

var stage = {"stage": 3, "date": new Date(2010,1,25,12,0,0), "distance": 206, "type": "", "start_host": "Nuoro", "start_country": "", "start_url": "", "finish_host": "Oristano", "finish_country": "", "finish_url": "", "start_time": "10:30", "video_time": "none", "finish_time": "15:55", "start_weather": "http://www.italy-weather-and-maps.com/weather/NU/", "finish_weather": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=Oristano&wuSelect=WEATHER", "bike_map": 0, "profile": "series", "finish": "", "map": "series", "timetable": "series", "preview": "#03-preview-2010", "results": "#03-results-2010", "photos": "stage-03/", "video": "", "headline": "", "thumb": ""};
stages.push(stage);

var stage = {"stage": 4, "date": new Date(2010,1,26,12,0,0), "distance": 182, "type": "", "start_host": "Carbonia", "start_country": "", "start_url": "", "finish_host": "Iglesias", "finish_country": "", "finish_url": "", "start_time": "11:00", "video_time": "none", "finish_time": "15:32", "start_weather": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=Carbonia&wuSelect=WEATHER", "finish_weather": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=Iglesias&wuSelect=WEATHER", "bike_map": 0, "profile": "series", "finish": "", "map": "series", "timetable": "series", "preview": "#04-preview-2010", "results": "#04-results-2010", "photos": "stage-04/", "video": "uy3cWz6ACnE", "headline": "", "thumb": ""};
stages.push(stage);

var stage = {"stage": 5, "date": new Date(2010,1,27,12,0,0), "distance": 141, "type": "", "start_host": "Sant'Antioco", "start_country": "", "start_url": "", "finish_host": "Cagliari", "finish_country": "", "finish_url": "", "start_time": "11:30", "video_time": "none", "finish_time": "14:51", "start_weather": "http://www.wunderground.com/global/stations/16560.html", "finish_weather": "http://www.wunderground.com/global/stations/16560.html", "bike_map": 0, "profile": "series", "finish": "", "map": "series", "timetable": "series", "preview": "#05-preview-2010", "results": "#05-results-2010", "photos": "stage-05/", "video": "", "headline": "", "thumb": ""};
stages.push(stage);

function start_finish_details(stage_num) {
	var i = lookup_stage_offet(stage_num);
	//document.write('<table  border="0" cellspacing="0" cellpadding="0" align="right" style="margin-left:20px" width="340">');
}


function formatRaceSummaryTable() {
	//document.write('<table  border="0" cellspacing="0" cellpadding="0" align="right" style="margin-left:20px" width="340">');
	document.write('<tr><td colspan=3><a name="summary"><img src="');
	document.write(event_badge);
	document.write('" align="left" width="340" xheight="100" style="margin-right:0px;border-bottom:1px solid white"/><\/td><\/tr>');
	
	
	var total_distance = 0;
	for (var i=0; i < stages.length; i++) {
		// host start and host finish row
		document.write('<tr class="stage-date"><td class="num">'); 
		document.write(stages[i].stage);
		document.write('<\/td><td class="date">');
		document.write(days[stages[i].date.getDay()]);
		document.write(', ');
		document.write(months[stages[i].date.getMonth()]);
		document.write(' ');
		document.write(stages[i].date.getDate());
		document.write('<\/td><td class="distance" style="text-align:right" width="70">');
		if (stages[i].stage != "R") {
			document.write(stages[i].distance);
			document.write(' '+distance_units);
			total_distance += stages[i].distance;
		}
		document.write('&nbsp;<\/td><\/tr>');
		
		// host start and finish
			document.write('<tr><td><\/td><td colspan=2>');
		if (stages[i].stage == "R") {
			document.write("Rest day");
			if (stages[i].start_host != ""){
				document.write(' in <a href="');
				if (stages[i].start_url == '') {
	  				document.write('http://en.wikipedia.org/wiki/'+stages[i].start_host);
					
				}
				else
					document.write(stages[i].start_url);	
				document.write('">');
				document.write(unescape(stages[i].start_host));
				document.write('<\/a>');
				if (stages[i].start_country != "")
					document.write(' (' + stages[i].start_country + ')');
			}		
		}
		else {
		
			if (stages[i].type == "Mountain") {
				document.write('<img src="/img/mountain.gif" align="right" width="16" height="16"/>');
			}
			else if (stages[i].type == "TT") {
			document.write('<img src="/img/tt-symbol-16.jpg" align="right" width="27" height="16"/>');
			}
			else  if (stages[i].type == "TTT") {
			document.write('<img src="/img/ttt-symbol-16.jpg" align="right" width="39" height="16"/>');
			}
			document.write('<a href="');
			if (stages[i].start_url == '') {
				document.write('http://en.wikipedia.org/wiki/'+stages[i].start_host);
				
			}
			else
				document.write(stages[i].start_url);
				
			document.write('">');
			document.write(unescape(stages[i].start_host));
			document.write('<\/a>');
			if (stages[i].start_country != "")
				document.write(' (' + stages[i].start_country + ')');	
			
			if (stages[i].finish_host != "") {
				document.write(' &#8594; ');
				 
				document.write('<a href="');
				if (stages[i].finish_url == '') {
					document.write('http://en.wikipedia.org/wiki/'+stages[i].finish_host);		
				}
				else
					document.write(stages[i].finish_url);
					
					
				document.write('">');
				document.write(unescape(stages[i].finish_host));
				document.write('<\/a>');
				if (stages[i].finish_country != "")
					document.write(' (' + stages[i].finish_country + ')');	
			}
		}
		document.write('<\/td><\/tr>');
		
		// links row
		if (stages[i].stage != "R") {
			document.write('<tr class="stage-details"><td><\/td> <td colspan="2">'); 
			formatStageLinks(i);
		}
		
		
		// end of links row
		document.write('<\/td><\/tr>');
	}	
	document.write('<tr class="stage-date" style="color:#E7573B; background-color:black"><td colspan="2"  >&nbsp;&nbsp;<b>Total Distance<\/b><\/td><td  align="right"><b>' + Math.round(total_distance) + ' ' + distance_units + '&nbsp;<\/b><\/td><\/tr>');	
}

function lookup_stage_offet(stage_num) {
	for (var j = 0; j < stages.length; j++) {
		if (stages[j].stage == stage_num)
			return j;
	}
}

function formatStageLinks(i) {
	for (var j = 0; j < base_urls_list.length; j++) {
		if (j > 0)
			document.write(' | ');
		document.write('<a ');
		(stages[i][base_urls_list[j]] == "series") ? summary_value = new String(stages[i].stage) : summary_value = stages[i][base_urls_list[j]];
		
		if (summary_value == "") {				
			if (base_urls_list[j] == "map"  && stages[i].bike_map > 0) { // use bikemap if no official map	
				document.write('href=');
				document.write(bike_map_base_url.replace("{$1}", stages[i].bike_map));
			}
			else
				document.write('name=""');
		}
		else {
			document.write('href=');
			//if (base_urls_list[j] == "map" && summary_value == "" && stages[i].bike_map > 0) // use bikemap if no official map		
				//document.write(bike_map_base_url.replace("{$1}", stages[i].bike_map));	
			if (summary_value.indexOf('http://') < 0) { // relative/substitute
				document.write(base_urls[base_urls_list[j]].replace("{$1}", summary_value).replace("{$stage_num}", stages[i].stage));
			}
			else document.write(summary_value);
		
		}
		if (anchor_extras[base_urls_list[j]]) {
			document.write(' ' + anchor_extras[base_urls_list[j]]);	
		}		
		document.write('>'+base_urls_list[j]+'<\/a>');
	}
}

function getBikeMapRoute(stage_num) {
	return stages[lookup_stage_offet(stage_num)].bike_map;
}

function start_finish_summary(stage_num) {
	var i = lookup_stage_offet(stage_num);
	document.write("<br clear=left/>");
	var components = stages[i].start_time.split(":");
	var time_ms = stages[i].date.setUTCHours(components[0], components[1], 0);
	var utcRaceDate0 = new Date(time_ms - (race_time_utc_offset * 3600000));
	
	document.write('<b>');
	document.write(stage_num == 0 ? "Prologue" : "Stage " + stage_num);
	document.write(" start time:<\/b> " + days[utcRaceDate0.getDay()] + " " + stages[i].start_time + " " + race_time_zone + " ");
	document.write('(' + utcRaceDate0.format("longTime") + ')');
	
	

	var host_town_weather = stages[i].start_host;	
	
	if (host_town_weather.indexOf(',') < 0)
		host_town_weather = host_town_weather + ', ' + (stages[i].start_country != "" ? stages[i].start_country : race_country);
	
	document.write('; <a href=' + stages[i].start_weather + '>Weather for ' + unescape(host_town_weather) + '<\/a><br/>');
	
	document.write('<b><font color=red>Earliest live video:<\/font><\/b> ')
	
	
	if (stages[i].video_time == "")
		document.write('TBA');
	if (stages[i].video_time == "none")
		document.write('None');
	else {
		var components = stages[i].video_time.split(":");
		time_ms = stages[i].date.setUTCHours(components[0], components[1], 0);
		var utcRaceDateV = new Date(time_ms - (race_time_utc_offset * 3600000));
		document.write(stages[i].video_time + " " + race_time_zone + ' (' + utcRaceDateV.format("longTime") + ')');
	}
	
	document.write('; ' + '<a href=' + dashboard_link + '/#live>Live video, audio and text options</a>');
	
	components = stages[i].finish_time.split(":");
	var time_ms = stages[i].date.setUTCHours(components[0], components[1], 0);
	var utcRaceDate1 = new Date(time_ms - (race_time_utc_offset * 3600000));
	
	document.write('<br/><b>');
	document.write("Approximate finish:<\/b> " + stages[i].finish_time + " " + race_time_zone + " ");
	document.write('(' + utcRaceDate1.format("longTime") + ')');
	
		
	if (stages[i].finish_host != "")
		host_town_weather = stages[i].finish_host;
	else
		host_town_weather = stages[i].start_host;
	
	if (host_town_weather.indexOf(',') < 0)
		host_town_weather = host_town_weather + ', ' + (stages[i].finish_country != "" ? stages[i].finish_country : race_country);
	
	document.write('; <a href=' + stages[i].finish_weather + '>Weather for ' + unescape(host_town_weather) + '<\/a>');	
	
	
	
}

function stage_text_summary(stagen) {
	var i = lookup_stage_offet(stagen);;
	document.write("<b>Stage " + stagen + ":<\/b> " + unescape(stages[i].start_host));
	if (stages[i].finish_host != "")
		document.write(' &#8594; ' + unescape(stages[i].finish_host));
	document.write('<br/>');
	
	document.write("<b>Date:<\/b> ");
	document.write(days[stages[i].date.getDay()]);
	document.write(', ');
	document.write(months[stages[i].date.getMonth()]);
	document.write(' ');
	document.write(stages[i].date.getDate()+'<br/>');
	document.write("<b>Distance:<\/b> ");
	document.write(stages[i].distance);
	document.write(" " + distance_units + '<br/>');
}

function stage_stubs() {	
	for (var i = archived_stages; i > 0; i--) {
		var stage = stages[lookup_stage_offet(i)];
		var stage_text = (i < 10) ? "0"+i : i;
		document.write("<a name=\"" + stage_text + "-results-2009\"/>");
		document.write("<img src=\"" + thumbs_dir + stage.thumb + ".jpg\" align=left width=200 style=\"margin-right:10px\" />");
		document.write("<h3>Stage " + i + " results: " + stage.headline + "</h3>");
		
		document.write('<img src="/img/pages.gif" width="12" height="14" style="margin-left:2px;margin-right:3px"/><img src="/camera.gif" width="16" height="13" style="margin-right:2px" /><img src="/video-red.gif" style="margin-bottom:1px" /> ');
		
		document.write("<a href=" +base_urls["preview"].replace("{$1}", ("stage-"+stage_text+"/")) + ">Stage " + i + " Results, Reports, Photos, Highlights and  Video Clips &raquo;</a>");
		document.write("<br clear=left /><hr align=left />")
	}
}

