var event_badge = "/2009/tour-de-suisse/suisse-on-steephill-tv.jpg";
var dashboard_title = "Tour de Suisse 2009";
var dashboard_link = "http://www.steephill.tv/tour-de-suisse/"
var num_stages = 8;
var has_prologue = false;
var start_date = new Date(2009,5,13);
var race_country = "Switzerland";
var race_time_utc_offset = 2;
var race_time_zone = "CEST";
var distance_units = "km";
var base_urls =  new Array();
base_urls["profile"] = 
"http://www.steephill.tv/2009/tour-de-suisse/profile-0{$1}.png";
base_urls["map"] = "http://www.tds.ch/sportch/upload/09_karte{$1}.pdf"; 
base_urls["timetable"] = 
"http://www.tds.ch/sportch/upload/2009_marschtabelle_{$1}detail.pdf";
base_urls["preview"] = "/2009/tour-de-suisse/previews-results/{$1}";
base_urls["results"] = "/tour-de-suisse/{$1}";

base_urls["photos"] = "/2009/tour-de-suisse/photos/{$1}";

base_urls["video"] = "/tour-de-suisse/{$1}";

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',900,440,'no'); return false;\"";

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

var stages = new Array(); 

var stage = {"stage": 1, "date": new Date(2009,5,13), "distance": 7.8, "type": "TT", "start_host": "Mauren", "start_url": "http://www.mauren.li/", "finish_host": "Ruggell", "finish_url": "", "start_time": "15:08", "video_time": "17:00", "finish_time": "18:15", "start_weather": "Mauren, Liechtenstein", "finish_weather": "Ruggell, Liechtenstein", "bike_map": 152758, "profile": "stage_num", "finish": "", "map": "stage_num", "timetable": "stage_num", "preview": "stage-01/", "results": "#01-results-2009", "photos": "stage-01/", "video": "#01-results-2009"};
stages.push(stage);

var stage = {"stage": 2, "date": new Date(2009,5,14), "distance": 150, "type": "", "start_host": "Davos", "start_url": "", "finish_host": "Davos", "finish_url": "", "start_time": "13:29", "video_time": "16:25", "finish_time": "17:30", "start_weather": "", "finish_weather": "", "bike_map": 152491, "profile": "stage_num", "finish": "", "map": "stage_num", "timetable": "stage_num", "preview": "stage-02/", "results": "#02-results-2009", "photos": "stage-02/", "video": "#02-results-2009"};
stages.push(stage);

var stage = {"stage": 3, "date": new Date(2009,5,15), "distance": 198, "type": "Mountain", "start_host": "Davos", "start_url": "", "finish_host": "Lumino", "finish_url": "", "start_time": "13:12", "video_time": "17:00", "finish_time": "18:10", "start_weather": "", "finish_weather": "", "bike_map": 152502, "profile": "stage_num", "finish": "", "map": "stage_num", "timetable": "stage_num", "preview": "stage-03/", "results":"#03-results-2009", "photos": "http://www.grahamwatson.com/gw/imagedocs.nsf/updateframesetcall?openform&09tdsSt3", "video": "#03-results-2009"};
stages.push(stage);

var stage = {"stage": 4, "date": new Date(2009,5,16), "distance": 195, "type": "Mountain", "start_host": "Biasca", "start_url": "", "finish_host": "St%E4fa", "finish_url": "St%C3%A4fa,_Switzerland", "start_time": "12:48", "video_time": "17:00", "finish_time": "18:09", "start_weather": "", "finish_weather": "Zurich", "bike_map": 152770, "profile": "stage_num", "finish": "", "map": "stage_num", "timetable": "stage_num", "preview": "stage-04/", "results": "#04-results-2009", "photos": "stage-04/", "video": "#04-results-2009"};
stages.push(stage);

var stage = {"stage": 5, "date": new Date(2009,5,17), "distance": 202, "type": "Mountain", "start_host": "St%E4fa", "start_url": "St%C3%A4fa,_Switzerland", "finish_host": "Serfaus", "finish_url": "", "start_time": "12:37", "video_time": "17:00", "finish_time": "18:10", "start_weather": "Zurich", "finish_weather": "Imst, Austria", "bike_map": 216800, "profile": "stage_num", "finish": "", "map": "stage_num", "timetable": "stage_num", "preview": "stage-05/", "results": "#05-results-2009", "photos": "stage-05/", "video": "#05-results-2009"};
stages.push(stage);

var stage = {"stage": 6, "date": new Date(2009,5,18), "distance": 177, "type": "", "start_host": "Oberriet", "start_url": "Oberriet,_Switzerland", "finish_host": "Bad Zurzach", "finish_url": "Bad_Zurzach,_Switzerland", "start_time": "13:37", "video_time": "17:00", "finish_time": "18:10", "start_weather": "", "finish_weather": "Zurich", "bike_map": 152789, "profile": "stage_num", "finish": "", "map": "stage_num", "timetable": "stage_num", "preview": "stage-06/", "results": "#06-results-2009", "photos": "http://www.grahamwatson.com/gw/imagedocs.nsf/updateframesetcall?openform&09tdsSt6", "video": "#06-results-2009"};
stages.push(stage);

var stage = {"stage": 7, "date": new Date(2009,5,19), "distance": 204, "type": "", "start_host": "Bad Zurzach", "start_url": "Bad_Zurzach,_Switzerland", "finish_host": "Juraparc/Vallorbe", "finish_url": "http://www.juraparc.ch/", "start_time": "13:07", "video_time": "17:00", "finish_time": "18:10", "start_weather": "Zurich", "finish_weather": "Vallorbe", "bike_map": 152794, "profile": "stage_num", "finish": "", "map": "", "timetable": "stage_num", "preview": "stage-07/", "results": "#07-results-2009", "photos": "stage-07/", "video": "#07-results-2009"};
stages.push(stage);

var stage = {"stage": 8, "date": new Date(2009,5,20), "distance": 182, "type": "Mountain", "start_host": "Le Sentier", "start_url": "http://www.myswitzerland.com/en.cfm/destinations/resorts/offer-Destinations_Resorts-General-83585.html", "finish_host": "Crans-Montana", "finish_url": "http://en.wikipedia.org/wiki/Crans_Montana", "start_time": "13:20", "video_time": "17:00", "finish_time": "18:10", "start_weather": "Vallorbe", "finish_weather": "http://www.qwikcast.com/cgi-bin/forecast.cgi?zip=Crans%20Montana,CH", "bike_map": 152803, "profile": "stage_num", "finish": "", "map": "stage_num", "timetable": "stage_num", "preview": "stage-08/", "results": "#08-results-2009", "photos": "stage-08/", "video": "#08-results-2009"};
stages.push(stage);

var stage = {"stage": 9, "date": new Date(2009,5,21), "distance": 39, "type": "TT", "start_host": "Berne", "start_url": "", "finish_host": "Berne", "finish_url": "", "start_time": "13:01", "video_time": "16:25", "finish_time": "17:30", "start_weather": "", "finish_weather": "", "bike_map": 152799, "profile": "stage_num", "finish": "", "map": "stage_num", "timetable": "stage_num", "preview": "stage-09/", "results": "#09-results-2009", "photos": "stage-09/", "video": "#09-results-2009"};
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" 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");
			// todo: add in start_host		
		}
		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 {
				if (stages[i].start_url.indexOf('http://') < 0)			
					document.write('http://en.wikipedia.org/wiki/');	
				document.write(stages[i].start_url);	
			}
				
			document.write('">');
			document.write(unescape(stages[i].start_host));
			document.write('<\/a>');
			
			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 {
					if (stages[i].finish_url.indexOf('http://') < 0)			
						document.write('http://en.wikipedia.org/wiki/');	
					document.write(stages[i].finish_url);	
				}
					
					
				document.write('">');
				document.write(unescape(stages[i].finish_host));
				document.write('<\/a>');
			}
		}
		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:#cc002b; background-color:#b7b7b7"><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]] == "stage_num") ? 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));
			}
			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=all/>");
	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") + ')');
	
	
	if (stages[i].start_weather != "" && (stages[i].start_weather.indexOf('http') < 0))
		host_town_weather = stages[i].start_weather;	
	else
		host_town_weather = stages[i].start_host;	
	
	if (host_town_weather.indexOf(',') < 0)
		host_town_weather = host_town_weather + ', ' + race_country;
	
	var sw_url = stages[i].start_weather.indexOf('http') < 0 ? weather_base_url.replace("{$1}", escape(host_town_weather)) : stages[i].start_weather;
	
	document.write('; <a href=' +  sw_url + '>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');
	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_weather != "" && (stages[i].finish_weather.indexOf('http') < 0))
		host_town_weather = stages[i].finish_weather;	
	else
		host_town_weather = stages[i].finish_host;	
	
	if (host_town_weather.indexOf(',') < 0)
		host_town_weather = host_town_weather + ', ' + race_country;
	
	var w_url = stages[i].finish_weather.indexOf('http') < 0 ? weather_base_url.replace("{$1}", escape(host_town_weather)) : stages[i].finish_weather;
	
	document.write('; <a href=' +  w_url + '>Weather for ' + unescape(host_town_weather) + '<\/a><br/>');	
}