/*
Copyright 2006 Heath Johns

This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.
*/


// Constants
var MAP_SMALL_HEIGHT = "190px";
var MAP_LARGE_HEIGHT = "400px";
var DEFAULT_SCIDDY_REFRESH_TIME = 14500;
var FOLLOW_ZONE_PERCENTAGE = 0.7; // 1 == follow only when sciddy leaves map
var DEFAULT_ZOOM = 13;
var NYC = new GLatLng(40.758688, -73.985158);
var DONE_LOC = new GLatLng(38.548165,-111.953125);
var DEFAULT_MAP_TYPE = G_HYBRID_MAP;
var FRESH      = "http://hitch50.com/gps/fresh";
var ADDY       = "http://hitch50.com/gps/adat/addy";
var DISTY       = "http://hitch50.com/gps/ldat/dist.js";
var TIME_JS = "http://hitch50.com/gps/ldat/time.js";
var TIME_SMALL_JS = "http://hitch50.com/gps/ldat/time_small.js";
var LINE_RED = "#ff0000";
var LINE_OPACITY = 0.7;
var LINE_REFRESH_TIME = 60 * 120 * 1000;
var SMALL_LINE_REFRESH_TIME = 60 * 120 * 1000;
var FOLLOW_REFRESH_TIME = 60 * 120 * 1000;
var COUNTER_REFRESH_TIME = 60 * 120 * 1000;
var ADDY_REFRESH_TIME = 60 * 120 * 1000;

//FRESH = FRESH_FAKE
//FOLLOW_REFRESH_TIME = 0.5 * 1000;

// Globals
var map = null;
var small_map = null;
var map_controls = new Array();
var map_maxed = true;
var sciddy_icon = null;
var wee_sciddy_icon = null;
var sciddy_refresh_time = DEFAULT_SCIDDY_REFRESH_TIME;
var follow_timer = null;
var sciddy_move_waiting = false;
var force_follow = false;
var dragging = false;

// Line Globals
var polylines = Array();
var drawn_patches = Array();
var fake_zoom = DEFAULT_ZOOM;
var line_inited = false;
var line_interval = null;
var live_patch = null;
var live_bounds = null;
var live_polyline = null;
var line_on = false;
var small_polyline = null;


// Zoot!
function load() {
	munge_page();
	init_map();
}



// Get the google map on the screen; init the basics
function init_map() {
	header_div = document.getElementById("header");

	// Set up map
    if (!GBrowserIsCompatible()) {
		header_div.innerHTML = "<p>Your browser is not supported by " +
			"Google Maps.  Log on with IE6+, FireFox, or Safari to " +
			"see a really cool real-time map here...</p>" +
			"Alternately, you are viewing this from a cache or proxy, in which " +
			"case, click <a href='http://hitch50.com'>here</a> to " +
			"go to the authentic page.</p>";
		return;
    }
    map = new GMap2(header_div);
	map.setCenter(DONE_LOC, 2);
	init_small_map();
	init_controls();
	init_sciddy();
	init_wee_sciddy();
    map_smallify();
	map.setMapType(DEFAULT_MAP_TYPE);
	init_timers();

	line_on = true;
	init_line();
}


// Init small map
function init_small_map() {
	var small_map_div = document.getElementById("small_map");
	small_map = new GMap2(small_map_div);
	small_map.setCenter(new GLatLng(38.410558,-94.294922), 2);
	small_map.setMapType(G_HYBRID_MAP);
}



// Make some controls...
function init_controls() {
	// Logo
	var logo_link = document.createElement("a");
	logo_link.href="http://hitch50.com";
	logo_link.style.border = "0";
	logo_link.style.cursor = "pointer";
	var logo_img = create_transparent_img("/img/logo_map.png", 150, 159);
	logo_link.appendChild(logo_img);
	map_controls["logo"] = create_control(logo_link, G_ANCHOR_TOP_LEFT, 0, 0);

	// Left Bottom buttons
	var se_buttons = document.createElement("div");
	var find_sciddy = create_google_button("Find Scotty & Fiddy");
	find_sciddy.id = "find_sciddy_button";
	activate_button_div(find_sciddy, function() {
		center_map();
	});
	se_buttons.appendChild(find_sciddy);
	var show_history = create_google_button("Hide History");
	activate_button_div(show_history, function () {
		line_on = !line_on;
		if (line_on)
			init_line();
		else
			erase_line();
		show_history.innerHTML = (line_on) ? "Hide History" : "Show History";
	});
	se_buttons.appendChild(show_history);
	map_controls["se_buttons"] = create_control(se_buttons, G_ANCHOR_BOTTOM_RIGHT, 7, 20);

	// Addy
    var addy = document.createElement("div");
	addy.id = "addy_div";
	addy.style.color = "white";
	addy.style.backgroundColor = "#0b5535";
	addy.style.textAlign = "left";
	addy.style.padding = "3px";
	addy.style.fontFamily = "Arial, verdana, arial, helvetica";
	map_controls["addy"] = create_control(addy, G_ANCHOR_BOTTOM_LEFT, 0, 37);

	// Disty
    var disty = document.createElement("div");
    var dist = document.createElement("div");
    var speed = document.createElement("div");
	dist.id = "dist_div";
	speed.id = "speed_div";
	disty.id = "disty_div";
	dist.style.color = speed.style.color = "black";
	dist.style.backgroundColor = speed.style.backgroundColor = "#98ed40";
	dist.style.textAlign = speed.style.textAlign = "right";
	dist.style.padding = speed.style.padding = "3px";
	dist.style.fontFamily = speed.style.fontFamily = "Arial, verdana, arial, helvetica";
	dist.style.borderStyle = speed.style.borderStyle = "solid";
	dist.style.borderWidth = speed.style.borderWidth = "1px";
	dist.style.borderColor = speed.style.borderColor = "#4daaf2";
	speed.style.marginLeft = "50px";
	speed.style.display = "inline";
	dist.style.display = "inline";
	disty.style.padding = "5px";
	disty.appendChild(dist);
	disty.appendChild(speed);
	map_controls["disty"] = create_control(disty, G_ANCHOR_TOP_LEFT, 120, 7);


	// Set up some events
    GEvent.addListener(map, "click", map_largify);
	GEvent.addListener(map, "dragstart", map_largify);
	GEvent.addListener(map, "dragstart", drag_start);
	GEvent.addListener(map, "dragend", drag_end);
	GEvent.addListener(map, "dragend", view_changed);
	GEvent.addListener(map, "zoomend", view_changed);
	GEvent.addListener(map, "zoomend", change_zoom_line);
	GEvent.addListener(map, "moveend", draw_line);
	GEvent.addDomListener(document.getElementById("giant_red_arrow"),
					 "click", map_largify);
}


// Set up the idealized representation of the amalgamated hitchhikers
function init_sciddy() {
    var icon = new GIcon();
    icon.image = "/img/dot.png";
    icon.shadow = "/img/dot_shadow.png";
    icon.iconSize = new GSize(18, 31);
    icon.shadowSize = new GSize(28, 31);
    icon.iconAnchor = new GPoint(9, 31);
    icon.infoWindowAnchor = new GPoint(18, 1);
	sciddy_icon = new GMarker(NYC, icon);
	map.addOverlay(sciddy_icon);
	GEvent.addListener(sciddy_icon, "click", function() {
		info = document.createElement("div");
		info.innerHTML = 
			"<p>Contact Scotty and Fiddy:</p>" +
			"<p>Phone: 310-494-8058<br/>" +
			"   Email: <a href='mailto:hitch50@gmail.com'>hitch50@gmail.com</a></p>";
		info.style.textAlign = "left";
		sciddy_icon.openInfoWindow(info);
	});
}


// Set up the wee idealized representation of the amalgamated hitchhikers
function init_wee_sciddy() {
	var icon = new GIcon();
	icon.image = "/img/wee_dot.png";
	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon.iconSize = new GSize(12, 20);
	icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	wee_sciddy_icon = new GMarker(NYC, icon);
	small_map.addOverlay(wee_sciddy_icon);
}



function map_largify() {
	if (map_maxed == true)
		return;

	// Resize map
    var center = map.getCenter()
    var header = document.getElementById("header");
	header.style.height = MAP_LARGE_HEIGHT;
    map.checkResize();
    map.setCenter(center);
    map_maxed = true;

	// Get rid of the blurb
	element_appear(document.getElementById("blurb2"));
	element_disappear(document.getElementById("blurb"));

	// Change behaviour/apperance
	map.enableDragging();
	element_appear(map_controls["se_buttons"].parent_div);
	element_appear(map_controls["addy"].parent_div);
	element_appear(map_controls["disty"].parent_div);
	element_disappear(map_controls["logo"].parent_div);
	map_controls["gmap0"] = new GLargeMapControl();
	map_controls["gmap1"] = new GMapTypeControl();
	map.addControl(map_controls["gmap0"]);
	map.addControl(map_controls["gmap1"]);
	poll_addy();
	poll_disty();
}


function map_smallify() {
	if (map_maxed == false)
		return;
	
	// Resize map
    var header = document.getElementById("header");
	header.style.height = MAP_SMALL_HEIGHT;
    map.checkResize();
	center_map();
    map_maxed = false;

	// Get make the blurb appear
	element_appear(document.getElementById("blurb"));
	element_disappear(document.getElementById("blurb2"));

	// Change behaviour/apperance
   	map.enableDragging();
	element_disappear(map_controls["se_buttons"].parent_div);
	element_disappear(map_controls["addy"].parent_div);
	element_disappear(map_controls["disty"].parent_div);
	element_appear(map_controls["logo"].parent_div);
	if (map_controls["gmap0"]) 
		map.removeControl(map_controls["gmap0"]);
	if (map_controls["gmap1"]) 
		map.removeControl(map_controls["gmap1"]);
}

// Called when the view changes
function view_changed() {
	sciddy_coords = sciddy_icon.getPoint();
	if (map.getBounds().contains(sciddy_coords))
		element_disappear(document.getElementById("find_sciddy_button"));
	else
		element_appear(document.getElementById("find_sciddy_button"));
}


// Make sure we know when the user is dragging the map
function drag_start() {
	dragging = true;
}
function drag_end() {
	dragging = false;
}


// Follow the guys
function follow_sciddy(old_pos, new_pos) {
	if (dragging)
		return;
	if (old_pos.equals(new_pos))
		return;
	if (map.getBounds().contains(old_pos) == false)
		return;
	if (map.getBounds().contains(new_pos) == false) {
		center_map();
		return;
	}

	var map_bounds = map.getBounds();
	var map_center = map.getCenter();

	var c_lat = map_center.lat();
	var c_lng = map_center.lng();
	var n_lat = new_pos.lat();
	var n_lng = new_pos.lng();
	var o_lat = old_pos.lat();
	var o_lng = old_pos.lng();
	var bounds  = map_bounds.getNorthEast();
	var mx_lat = bounds.lat();
	var mx_lng = bounds.lng();

   	var pd_lat = percent_distance(c_lat, mx_lat, n_lat);
	var pd_lng = percent_distance(c_lng, mx_lng, n_lng);

	if ((pd_lat > FOLLOW_ZONE_PERCENTAGE) ||
		(pd_lng > FOLLOW_ZONE_PERCENTAGE))
	{
		var new_c_lat = c_lat;
		var new_c_lng = c_lng;
		if (pd_lat > FOLLOW_ZONE_PERCENTAGE &&
			same_direction(n_lat - c_lat, n_lat - o_lat))
		{
			new_c_lat = c_lat + ((n_lat - c_lat) * 1.7);
		}
		if (pd_lng > FOLLOW_ZONE_PERCENTAGE &&
			same_direction(n_lng - c_lng, n_lng - o_lng))
		{
			new_c_lng = c_lng + ((n_lng - c_lng) * 1.7);
		}
		map.panTo(new GLatLng(new_c_lat, new_c_lng), map.getZoom());
	} else {
		if (logo_overlap_hack(pd_lng, n_lng, c_lng)) {
			map.panTo(new GLatLng(c_lat,
				c_lng + ((n_lng - c_lng) * 1.7)), map.getZoom());
		}
	}
}
function logo_overlap_hack(pd_lng, n_lng, c_lng) {
	if (map_maxed) 
		return false;
	return (pd_lng > 0.5 && n_lng < c_lng);
}
function same_direction(s_dist, v_dist) {
	if (Math.abs(s_dist) < 0.00001) s_dist = 0;
	if (Math.abs(v_dist) < 0.00001) 
		return false;
	return (nxor((s_dist > 0), (v_dist > 0)));
}





////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\
// Update stuff
////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\


// Bind the icon (and our hearts) to the fate of our hitchhikers
function poll_sciddy() {
	if (sciddy_move_waiting)
		return;

	get_new_script(FRESH);
	sciddy_move_waiting = true;
}
function move_sciddy(time, lat, lng) {
	sciddy_move_waiting = false;

	if (small_polyline == null)
		init_small_line();

	var old_sciddy_coords = sciddy_icon.getPoint();
	var sciddy_coords = new GLatLng(lat, lng);
	if (old_sciddy_coords.equals(sciddy_coords))
		return;

	sciddy_icon.setPoint(sciddy_coords);
	wee_sciddy_icon.setPoint(sciddy_coords);
	if (line_inited)
		add_to_live_line(sciddy_coords);
	
	poll_disty();

	/*
	if (force_follow) {
		center_map();
		force_follow = false;
	} else {
		follow_sciddy(old_sciddy_coords, sciddy_coords);
	}
	*/
}

// Put up the address
function poll_addy() {
	if (map_maxed)
		get_new_script(ADDY);
}
function update_addy(html) {
	if (map_maxed) {
		var div = document.getElementById('addy_div')
		div.innerHTML = html;
	}
}

// Put up the distance/speed
function poll_disty() {
	if (map_maxed)
		get_new_script(DISTY);
}
function update_dist(dist, speed) {
	if (map_maxed) {
		document.getElementById('dist_div').innerHTML = dist + " miles";
		document.getElementById('speed_div').innerHTML = speed + " mph";
	}
}

// Set states/days counters
function update_counter() {
	var counter = document.getElementById("counters");
	counter.src = "/img/counters.png?ref" + random_string();
}

// Redraw line from new data
function reinit_line() {
	if (line_inited)
		init_line();
}


// Set up all the timers
function init_timers() {
	force_follow = true;
	set_timer(poll_sciddy, FOLLOW_REFRESH_TIME);
	set_timer(update_counter, COUNTER_REFRESH_TIME);
	set_timer(poll_addy, ADDY_REFRESH_TIME);
	set_timer(reinit_line, LINE_REFRESH_TIME);
	setInterval(init_small_line, SMALL_LINE_REFRESH_TIME);
}



////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\
// Util functions
////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\


// Make a control to go on the map
function create_control(element, anchor_pos, offset_x, offset_y) {
	control = function() {}
	control.prototype = new GControl();
	control.prototype.initialize = function(map_ref) {
		var parent = document.createElement("div");
		parent.appendChild(element);
		map_ref.getContainer().appendChild(parent);
		this.parent_div = parent;
		return parent;
	}
	control.prototype.getDefaultPosition = function() {
		return new GControlPosition(anchor_pos, new GSize(offset_x, offset_y));
	}
	var control_inst = new control();
	map.addControl(control_inst);
	return control_inst;
}


// Make a transparent png img that works in IE
function create_transparent_img(name, width, height) {
	var img = null;
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if ((navigator.appName == "Microsoft Internet Explorer") &&
		(version >= 5.5) && (version < 7) && 
		(document.body.filters)) 
	{
		img = document.createElement("span");
		img.style.width = width;
		img.style.height = height;
		img.style.display = "inline-block";
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src='" + name + "', sizingMethod='scale')";
	} else {
		img = document.createElement("img");
		img.src = name;
	}
	return img;
}


// Make a button that looks like the google ones...
function create_google_button(text) {
	var button = document.createElement("div");
	button.innerHTML = text;
	button.style.textDecoration = "none";
	button.style.color = "#0000cc";
	button.style.backgroundColor = "white";
	button.style.font = "small Arial";
	button.style.fontFamily = "Arial, verdana, arial, helvetica";
	button.style.border = "1px solid black";
	button.style.padding = "2px";
	button.style.marginBottom = "3px";
	button.style.textAlign = "center";
	button.style.width = "6em";
	button.style.cursor = "pointer";
	button.style.color = "black";
	return button;
}


// Generate a random string to avoid caching
function random_string() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
	var string_length = 8;
	var random_string = new Array(string_length);
	for (var i = 0; i < string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		random_string[i] = chars.substring(rnum, rnum+1);
	}
	return random_string.join("");
}

// Mmmm, ajaxy!
function get_new_script(url) {
	var request = new XssHttpRequest();
	request.open("GET", url + "?" + random_string(), true);
	request.send(null);
}

// Center map on Sciddy
function center_map() {
	map.panTo(sciddy_icon.getPoint(), map.getZoom());
	element_disappear(document.getElementById("find_sciddy_button"));
}

// Throwaways
function percent_distance(center, edge, point) {
	return Math.abs(point - center) / Math.abs(edge - center);
}

function set_timer(func, interval) {
	func();
	setInterval(func, interval);
}

function element_disappear(ele) {
	ele.style.display = "none";
}

function element_appear(ele) {
	ele.style.display = "block";
}

function activate_button_div(button, func) {
    GEvent.addDomListener(button, "click", func);
}

function nxor(lh, rh) {
	return (lh == rh);
}




////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\
// Line functions
////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\


function diff_num_arrays(lh, rh) {
	var rc = 0;
	var lc = 0;
	var add = Array();
	var del = Array();

	while (1) {
		if (rc == rh.length) {
			if (lc != lh.length)
				del = del.concat(lh.slice(lc));
			break;
		}
		if (lc == lh.length) {
			if (rc != rh.length)
				add = add.concat(rh.slice(rc));
			break;
		}
		if (lh[lc] == rh[rc]) {
			lc++;
			rc++;
			continue;
		}
		if (lh[lc] > rh[rc]) {
			add.push(rh[rc]);
			rc++;
			continue;
		}
		if (lh[lc] < rh[rc]) {
			del.push(lh[lc]);
			lc++;
			continue;
		}
	}
	return Array(del, add);
}


function sortNumber(a, b) { return a - b }

function draw_line() {
	if (!line_inited)
		return;

	var map_bounds = map.getBounds();
	var to_draw_patches = Array();
	for (x = 0; x < patch_bounds.length; x++) {
		if (map_bounds.intersects(patch_bounds[x])) {
			to_draw_patches.push(x);
		}
	}
	var diffs = diff_num_arrays(drawn_patches.sort(sortNumber), to_draw_patches.sort(sortNumber));
	var del = diffs[0];
	var add = diffs[1];
	
	for (x in del)
		erase_polyline(del[x]);

	for (x in add)
		draw_polyline(add[x]);

	drawn_patches = to_draw_patches;
	draw_live_line(false);
}

function draw_polyline(x) {
	var patch = patches[x]
	if (patch == null) {
		get_new_script("http://hitch50.com/gps/ldat/" + 
	                    line_time + "/" + fake_zoom + "/" + x + ".js");
		
	} else {
		var line = new GPolyline(patch, LINE_RED, 5, LINE_OPACITY);
		map.addOverlay(line);
		polylines[x] = line;	
	}
}

function loaded_patch(x) {
	for (y in drawn_patches) {
		if (drawn_patches[y] == x)
			draw_polyline(x);
	}
}

function erase_polyline(x) {
	var line = polylines[x];
	if (line != null)
		map.removeOverlay(line);
	polylines[x] = null;
}

function change_zoom_line() {
	if (line_inited)
		init_line();
	draw_line();
}

function erase_line() {
	line_inited = false;
	for (x in drawn_patches) 
		erase_polyline(drawn_patches[x]);
	polylines = Array();
	drawn_patches = Array();
	patches = Array();
	patch_bounds = Array();
	erase_live_line();
	live_patch = null;
	live_bounds = null;
	live_polyline = null;
}

function init_line() { 
	erase_line();
	get_new_script(TIME_JS);
}

function init_line2() { 
	fake_zoom = map.getZoom();
	fake_zoom = (fake_zoom < 5) ? 5 : fake_zoom;
	fake_zoom = (fake_zoom > 13) ? 13 : fake_zoom;
	get_new_script("http://hitch50.com/gps/ldat/" + 
					line_time + "/" + fake_zoom + "/init.js"); 
} 

function init_live_line(first_point) {
	live_patch = Array();
	live_bounds = new GLatLngBounds(first_point, first_point);
	add_to_live_line(first_point);
	add_to_live_line(sciddy_icon.getPoint());
}

function add_to_live_line(point) {
	if (!live_patch) 
		return;
	if (!live_bounds.contains(point)) {
		var s = Math.min(point.lat(), live_bounds.getSouthWest().lat());
		var w = Math.min(point.lng(), live_bounds.getSouthWest().lng());
		var n = Math.max(point.lat(), live_bounds.getNorthEast().lat());
		var e = Math.max(point.lng(), live_bounds.getNorthEast().lng());
		live_bounds = new GLatLngBounds(new GLatLng(s, w), new GLatLng(n, e));
	}
	live_patch.push(point);
	draw_live_line(true);
}

function draw_live_line(update) {
	if (update)
		erase_live_line();
	if (map.getBounds().intersects(live_bounds)) {
		if (!live_polyline) {
			live_polyline = new GPolyline(live_patch, LINE_RED, 5, LINE_OPACITY);
			map.addOverlay(live_polyline);
		}
	} else {
		if (live_polyline)
			erase_live_line();
	}
}

function erase_live_line() {
	if (!live_polyline)
		return;
	map.removeOverlay(live_polyline);
	live_polyline = null;
}

// Bandwidth savers
function h_a(s, w, n, e) { 
	return new GLatLngBounds(new GLatLng(s, w), new GLatLng(n, e));
}
function h_c(lat, lng) { 
	return new GLatLng(lat, lng);
}


// Small map line
function init_small_line() { 
	get_new_script(TIME_SMALL_JS);
}

function draw_small_line() {
	get_new_script("http://hitch50.com/gps/ldat/" + 
					small_line_time + "/0/0.js"); 
}

function loaded_small_patch(patch_num) {
	if (small_polyline != null) {
		small_map.removeOverlay(small_polyline);
		small_polyline = null;
	}
	small_patch.push(sciddy_icon.getPoint());
	small_polyline = new GPolyline(small_patch, "#b00000", 2, 0.75);
	small_map.addOverlay(small_polyline);
}




////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\
// Page munging functions
////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\

var archives = Array("http://hitch50.com/2006_09_24_hitch50_archive.html",
					 "http://hitch50.com/2006_10_01_hitch50_archive.html",
					 "http://hitch50.com/2006_10_08_hitch50_archive.html",
					 "http://hitch50.com/2006_10_15_hitch50_archive.html",
					 "http://hitch50.com/2006_10_22_hitch50_archive.html",
					 "http://hitch50.com/2006_10_29_hitch50_archive.html",
					 "http://hitch50.com/2006_11_05_hitch50_archive.html",
					 "http://hitch50.com/2006_11_12_hitch50_archive.html",
					 "http://hitch50.com/2006_11_19_hitch50_archive.html",
					 "http://hitch50.com/2006_11_26_hitch50_archive.html");

// Replaces elements on the page with more dynamic ones
function munge_page() {
	// Put forward/next buttons in the weekly archives
	for (page in archives) {
		if (location.href == archives[page]) {
			var week_nav = document.getElementById("week_nav");
			if (week_nav == null)
				return;
			var text = "";
			if (page > 0) {
				text += "<a href=\"" + archives[Number(page) - 1] + 
						"\"><img src=\"http://hitch50.com/img/prev_week.png\" alt=\"Prev Week\"/></a>" +
						"&nbsp;&nbsp;&nbsp;&nbsp;";
			}
			if ((page - archives.length) < 0) {
				text += "<a href=\"" + archives[Number(page) + 1] + 
						"\"><img src=\"http://hitch50.com/img/next_week.png\" alt=\"Next Week\"/></a>";
			}
			week_nav.innerHTML = text;
			return;
		}
	}
}

