var map;
//
// Create our "tiny" marker icon
var tinyIcon = new GIcon();
tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
//tinyIcon.image = "images/ar_25_pin.png";
//tinyIcon.shadow = "images/ar_25_shadow.png";
tinyIcon.iconSize = new GSize(12, 20);
tinyIcon.shadowSize = new GSize(22, 20);
tinyIcon.iconAnchor = new GPoint(6, 20);
tinyIcon.infoWindowAnchor = new GPoint(5, 1);

// Set up our GMarkerOptions object literal
markerOptions = { icon:tinyIcon };
daysArr = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
//
var places = [
		{ "area":"IL",
		"name":"2115 Taft",
		"address":"2115 Taft St",
		"address2":"Houston, TX 77006",
		"link":["2115taft.org","http://www.2115taft.org/"],
		"anch":"Art gallery, coffee shop, and a recording studio, all with free Wi-Fi.",
		"posn":[29.749978,-95.385516],
		"writein":["","","","","","",""]
		},
		{ "area":"IL",
		"name":"Inversion Coffee",
		"address":"1953 Montrose Blvd",
		"address2":"Houston, TX 77006",
		"link":["inversioncoffee.com","http://www.inversioncoffee.com/"],
		"anch":"Coffee and free Wi-Fi",
		"posn":[29.751273,-95.39179],
		"writein":["","","7:00 PM","","","",""]
		},
		{ "area":"IL",
		"name":"Te House of Tea",
		"address":"1927 Fairview St.",
		"address2":"Houston, TX 77019",
		"link":["tehouseoftea.com","http://www.tehouseoftea.com/"],
		"anch":"tea, crepes, other food, free wi-fi, lots of tables.",
		"posn":[29.744715,-95.404997],
		"writein":["","","","","7:00 PM","",""]
		},
		{ "area":"IL",
		"name":"The Daily Grind",
		"address":"4115 Washington Ave",
		"address2":"Houston, TX 77007",
		"link":["","#"],
		"anch":"Coffee, Wi-Fi",
		"posn":[29.770098,-95.402259],
		"writein":["","","","","","",""]
		},
		{ "area":"N",
		"name":"Starbucks",
		"address":"4775 W. Panther Creek",
		"address2":"The Woodlands, TX 77381",
		"link":["","#"],
		"anch":"Starbucks fare, T-Mobile HotSopt",
		"posn":[30.167113,-95.503582],
		"writein":["","","","","","",""]
		},
		{ "area":"N",
		"name":"Barnes & Noble Woodlands Mall",
		"address":"1201 Lake Woodlands Dr. #3008",
		"address2":"The Woodlands, TX 77381",
		"link":["","#"],
		"anch":"T-Mobile hotspot, books, coffee",
		"posn":[30.163101,-95.455908],
		"writein":["","","","","","",""]
		},
		{ "area":"N",
		"name":"Borders",
		"address":"9595 Six Pines Drive",
		"address2":"The Woodlands, TX 77380",
		"link":["","#"],
		"anch":"Pay Wi-Fi, books, coffee",
		"posn":[30.175994,-95.459889],
		"writein":["","","","","","",""]
		},
		{ "area":"N",
		"name":"Panera Bread",
		"address":"1201 Lake Woodlands Dr.",
		"address2":"The Woodlands, TX 77381",
		"link":["panerabread.com","http://www.panerabread.com/"],
		"anch":"All sorts of sandwiches and free Wi-Fi",
		"posn":[30.164238,-95.45553],
		"writein":["","","5:30pm-9:00pm","","","",""]
		},
		{ "area":"SE",
		"name":"Coffee Oasis",
		"address":"4650 Nasa Road 1",
		"address2":"Seabrook, TX 77586",
		"link":["coffee-oasis.com","http://www.coffee-oasis.com"],
		"anch":"Coffee, deli foods and free Wi-Fi",
		"posn":[29.566895,-95.060435],
		"writein":["","","","6:30pm-9:00pm","","",""]
		},
		{ "area":"SE",
		"name":"Panera Bread",
		"address":"19305 Gulf Fwy",
		"address2":"Webster, TX 77598",
		"link":["","#"],
		"anch":"All sorts of sandwiches and free Wi-Fi",
		"posn":[29.542765,-95.142884],
		"writein":["","","","","","",""]
		},
		{ "area":"SE",
		"name":"Houston Public Library - Clear Lake",
		"address":"16616 Diana Lane",
		"address2":"Houston, TX 77062",
		"link":["","#"],
		"anch":"Books, Wi-Fi, lots of books.",
		"posn":[29.558041,-95.119597],
		"writein":["1:00pm-5:00pm","","","","","",""]
		},
		{ "area":"E",
		"name":"Panera Bread",
		"address":"5855 Fairmont Pkwy",
		"address2":"Pasadena, TX 77505",
		"link":["panerabread.com","http://www.panerabread.com/"],
		"anch":"All sorts of sandwiches and free Wi-Fi",
		"posn":[29.650232,-95.15292],
		"writein":["","","","","","",""]
		},
		{ "area":"SE",
		"name":"Coffee Haven",
		"address":"11510 Space Center Blvd",
		"address2":"Houston, TX 77059",
		"link":["","#"],
		"anch":"coffee, finger foods, free wi-fi, computers",
		"posn":[29.624383,-95.140443],
		"writein":["","","","","","",""]
		},
		{ "area":"SE",
		"name":"Chelsea Wine Bar",
		"address":"4106 NASA Parkway",
		"address2":"Seabrook, TX 77586",
		"link":["","#"],
		"anch":"wine, beer, coffee, finger foods, free wi-fi, open late, AWESOME!",
		"posn":[29.565481,-95.053408],
		"writein":["","","","","","","4:00pm-2am"]
		},
		{ "area":"SW",
		"name":"Borders Books & Music",
		"address":"12788 Fountain Lake Cir",
		"address2":"Stafford, TX 77477",
		"link":["","#"],
		"anch":"T-Mobile hotspot, books, coffee",
		"posn":[29.634977,-95.587986],
		"writein":["","","","","","",""]
		},
		{ "area":"W",
		"name":"Panera Bread",
		"address":"2150 South Highway 6. Suite 200",
		"address2":"Houston, TX 77077",
		"link":["panerabread.com","http://www.panerabread.com/"],
		"anch":"sandwiches, soups, salads and baked goods, free wi-fi, semi-private room in the back of the seating area",
		"posn":[29.735609,-95.642668],
		"writein":["","","","6:00 PM","","",""]
		},
		{ "area":"W",
		"name":"Maud Marks Library",
		"address":"1815 Westgreen Boulevard",
		"address2":"Katy, TX 77450",
		"link":["Website","http://www.hcpl.net/branchinfo/mm/mminfo.htm"],
		"anch":"Laptops can be checked out, free wi-fi, private room reserved, non-messy food permitted",
		"posn":[29.757574,-95.734166],
		"writein":["","","","","","",""]
		},
		{ "area":"W",
		"name":"Barnes & Noble",
		"address":"7626 Westheimer",
		"address2":"Houston, TX 77063",
		"link":["","#"],
		"anch":"T-Mobile hotspot, books, Starbucks Coffee",
		"posn":[29.738185,-95.504166],
		"writein":["","","","","","7:00 PM",""]
		},
		{ "area":"W",
		"name":"Robinson-Westchase Library",
		"address":"3223 Wilcrest",
		"address2":"Houston, TX 77042",
		"link":["Library Website","http://www.houstonlibrary.org/branches/rob_home.html"],
		"anch":"wi-fi, laptop checkout, quiet meeting room, food allowed",
		"posn":[29.728868,-95.570793],
		"writein":["","","","","","",""]
		},
		{ "area":"W",
		"name":"Henington-Alief Regional Library",
		"address":"7979 South Kirkwood",
		"address2":"Houston, TX 77072",
		"link":["","#"],
		"anch":"",
		"posn":[29.692247,-95.58717],
		"writein":["","","3:30 PM","","","",""]
		},
		{ "area":"W",
		"name":"Inka's",
		"address":"12225 Westheimer Rd Suite B",
		"address2":"Houston TX 77077 ",
		"link":["","#"],
		"anch":"",
		"posn":[29.736349,-95.596805],
		"writein":["","","","","","","2:00 PM"]
		},
		{ "area":"S",
		"name":"Antigua Coffee House",
		"address":"8201 Broadway St",
		"address2":"Pearland, TX 77581",
		"link":["","#"],
		"anch":"Free Wi-Fi, bakery-type foods",
		"posn":[29.559039,-95.347188],
		"writein":["","","","","","",""]
		},
		{ "area":"S",
		"name":"Panera Bread",
		"address":"9899 Broadway St",
		"address2":"Pearland, TX 77584",
		"link":["","#"],
		"anch":"All sorts of sandwiches and free Wi-Fi",
		"posn":[29.560897,-95.27554],
		"writein":["","","","","","",""]
		},
		{ "area":"S",
		"name":"Panera Bread",
		"address":"10621 Broadway St",
		"address2":"Pearland, TX 77584",
		"link":["","#"],
		"anch":"All sorts of sandwiches and free Wi-Fi",
		"posn":[29.55609,-95.385286],
		"writein":["","","","","","",""]
		},
		{ "area":"S",
		"name":"Piccomolo Italian Ice Cream",
		"address":"9415 Broadway St",
		"address2":"Pearland, TX 77584",
		"link":["","#"],
		"anch":"Free wi-fi, yummy ice cream and more",
		"posn":[29.557462,-95.366263],
		"writein":["","","","","","",""]
		},
		{ "area":"S",
		"name":"Pizza Factory",
		"address":"1807 Broadway St",
		"address2":"Pearland, TX 77581",
		"link":["","#"],
		"anch":"Free wi-fi, variety of hot food",
		"posn":[29.547845,-95.237625],
		"writein":["","","","","","",""]
		},
		{ "area":"S",
		"name":"Sago Tapioca & Snack Bar",
		"address":"9415 Broadway St",
		"address2":"Pearland, TX 77584",
		"link":["","#"],
		"anch":"Free wi-fi, Filipino cusine, bubble tea",
		"posn":[29.557461,-95.366264],
		"writein":["","","","","","",""]
		},
		{ "area":"S",
		"name":"Starbucks",
		"address":"3310 E Broadway 102",
		"address2":"Pearland, TX 77581",
		"link":["","#"],
		"anch":"Starbucks fare, T-Mobile HotSopt",
		"posn":[29.547803,-95.237592],
		"writein":["","","","","","",""]
		},
		{ "area":"N",
		"name":"Main Street Crossing",
		"address":"",
		"address2":"Tomball, TX",
		"link":["","#"],
		"anch":"",
		"posn":[30.097242,-95.615902],
		"writein":["","","","","5:30pm-9:00pm","",""]
		},
		{ "area":"N",
		"name":"IHOP",
		"address":"",
		"address2":"Conroe, TX",
		"link":["","#"],
		"anch":"",
		"posn":[30.314802,-95.471234],
		"writein":["","","","","","","7:30pm-12am"]
		},
		{ "area":"SW",
		"name":"The Teahouse",
		"address":"4727 Sweetwater Blvd",
		"address2":"Sugar Land, TX",
		"link":["","#"],
		"anch":"",
		"posn":[29.58512,-95.631351],
		"writein":["3:00 PM","","","","","",""]
		},
		{ "area":"SW",
		"name":"Panera Bread",
		"address":"5672 Highway 6",
		"address2":"Missouri City, TX 77459",
		"link":["","#"],
		"anch":"All sorts of sandwiches and free Wi-Fi",
		"posn":[29.571292,-95.570755],
		"writein":["","","","","","",""]
		},
		{ "area":"W",
		"name":"Memorial City Mall",
		"address":"900 Gessner Rd",
		"address2":"Houston, TX 77024",
		"link":["",""],
		"anch":"Meet by the ice rink.",
		"posn":[29.781364,-95.539899],
		"writein":["","11:00am-2:00pm","","","","",""]
		},
		{ "area":"W",
		"name":"Northwest Community Unitarian Universalist Church",
		"address":"5920 Hwy 6 North ",
		"address2":"Houston, TX 77084",
		"link":["",""],
		"anch":"",
		"posn":[29.863572,-95.643936],
		"writein":["","","","4:00pm-7:00pm","","",""]
		}	
		]
var _clickMarkerIndex = 0;
var _directfrto = 0;
//
function showDirections(inaddr) {
	url = "http://maps.google.com/maps";  
	var comp = places[_clickMarkerIndex];
	var addr = comp["address"] + ", " + comp["address2"];
	if ( _directfrto == 0 ) {	// From the clicked marker
		from = addr.replace(/ /g, "+");
		to = inaddr.replace(/ /g, "+");
	} else {					// To the clicked marker
		from = inaddr.replace(/ /g, "+");
		to = addr.replace(/ /g, "+");
	}
	url += "?saddr=" + from + "&daddr=" + to;
	window.open(url, "googlemaps");
}

//
function showDirectDiv(frto) {
	_directfrto = frto;
	if ( frto == 0 ) {
		html = "End Address:";
	} else {
		html = "Start Address:";
	}
	document.getElementById("directext").innerHTML = html;
	document.getElementById("directdiv").style.visibility = "visible";

}
//
function createMarker(point, number, arr) {
  var marker = new GMarker(point, markerOptions);
  marker.value = number;
  GEvent.addListener(marker, "click", function() {
	 _clickMarkerIndex = number
    var myHtml = "<span class='gmStyle'><b>" + arr["name"] + "</b><br/>";
	myHtml += arr["address"]+'<br/>'+arr["address2"]+'<br/>';
	myHtml += 'Features: '+arr["anch"]+'<br/>';
	if (arr["link"][1] != "#") {
		myHtml += '<a href="'+arr["link"][1]+'">'+arr["link"][0]+'</a><br clear="all" />';
	}
	var wiStr = "Write-Ins: ";
	var showWI = false;
	for (i = 0; i < 7; i ++) {
		if (arr["writein"][i] != "") {
			wiStr += daysArr[i] + ": " +arr["writein"][i] +" ";
			showWI = true;
		}
	}
	if (showWI) {
		myHtml += wiStr + '<br/>';
	}	
	myHtml += '<strong>Directions:</strong>&nbsp;';
	myHtml += '	<a href="javascript: showDirectDiv(0)">From Here</a> | ';
	myHtml += '	<a href="javascript: showDirectDiv(1)">To Here</a>';
	myHtml += '	<div id="directdiv">';
	myHtml += '		<div id="directext">&nbsp;</div>';
	myHtml += '		<form class="simpleform" name="directions" onsubmit="return false;">';
	myHtml += '		<input type="text" name="directions" size="20"/>';
	myHtml += '		<input type="button" value="show" onclick="showDirections(this.form.directions.value)"/>';
	myHtml += '		<small>(opens new window)</small>';
	myHtml += '		</form>';
	myHtml += '	</div></span>';
	map.openInfoWindowHtml(point, myHtml);
  });
  return marker;
}
//
function setupMap() {
	// nothing
  if (GBrowserIsCompatible()) {
  	// does it still move?
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
    //map.setCenter(new GLatLng(29.761993,-95.361328), 10);
	map.setCenter(new GLatLng(29.764377,-95.369568),9);
	//alert(places.length);
	for (j = 0; j < places.length; j ++) {
		var place = places[j];
		var point = new GLatLng(place["posn"][0], place["posn"][1]);
		map.addOverlay(createMarker(point, j, place));

	}
	//window.setTimeout(setupOfficeMarkers, 0);
  }
};
//
//
function load() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		//map.addControl(new GLargeMapControl());
		//map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(29.751263,-95.567322),11);
		//window.setTimeout(setupOfficeMarkers, 0);
	}
};
//
var weekArr = new Array();
weekArr.Sunday = new Array();
weekArr.Monday = new Array();
weekArr.Tuesday = new Array();
weekArr.Wednesday = new Array();
weekArr.Thursday = new Array();
weekArr.Friday = new Array();
weekArr.Saturday = new Array();
showWeek = function() {
	for (i = 0; i < places.length; i ++) {
		var arr= places[i]["writein"];
		for (j = 0; j < 7; j ++) {
			if (arr[j] != "") {
				weekArr[daysArr[j]].push([places[i]["area"],places[i]["name"],places[i]["address2"],arr[j],places[i]["posn"]]);
			}
		}
	}
	//
	var wks = "";
	for (i = 0; i < 7; i ++) {
		var arr = weekArr[daysArr[i]];
		wks += '<div class="weekdayname" onClick="exapandDay('+i+');" >'+daysArr[i]+' ('+arr.length+')</div>';
		wks += '<div class="weekdayitems" ID="wd'+i+'">';
		if (arr.length > 0) {
			for (j = 0; j < arr.length; j ++) {
				wks += '<p>'+arr[j][3]+'<br/><strong>';
				wks += arr[j][0]+' <a href="#" onclick="map.setCenter(new GLatLng('+arr[j][4][0]+','+arr[j][4][1]+'),15);" >';
				wks += arr[j][1]+'</a></strong><br/>'+arr[j][2]+'</p>';
			}
		}
		wks += '</div>';
	}
	document.write(wks);
}
//
var expArr = new Array(false,false,false,false,false,false,false);
exapandDay = function(num) {
	var conID = 'wd'+num;
	if (expArr[num]) {
		document.getElementById(conID).style.display = 'none';
		document.getElementById(conID).style.visibility = 'hidden';
		expArr[num] = false;
	} else {
		document.getElementById(conID).style.display = 'block';
		document.getElementById(conID).style.visibility = 'visible';
		expArr[num] = true;
	}
}