function clean_time(time_clean, open_close) {
	if (time_clean.search(/\./) != -1) { //if there is period in time
		//alert (time_clean+" becomes "+open_close);
		time_clean = time_clean.replace(".", ":");
	if (open_close == 'open') {
		time_clean = time_clean + " am";
	}
	else if (open_close == 'close') {
		time_clean = time_clean + " pm";	
	}
	}
	time_clean = time_clean.replace(/AM/g, "am");
	time_clean = time_clean.replace(/PM/g, "pm");
	return time_clean;
}

function createSidebarInvalid (){
		var div = document.createElement('div');
		var html = "Sorry, the address you entered is invalid.";
		div.innerHTML = html;
		return div;
	}

function initialize_map(from, radius) {
	geocoder = new google.maps.Geocoder();
	var latlng = new google.maps.LatLng(-34.397, 150.644);
	
	codeAddress();
	function codeAddress() {
		myOptions = {
			zoom: 20,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false
		}
		var sidebar = document.getElementById('sidebar');
		sidebar.innerHTML = ''; 

		var address = document.getElementById("from").value;
		if (geocoder) {
			geocoder.geocode( { 'address': address, 'country': 'us'}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
						map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
						map.setCenter(results[0].geometry.location);
							x = map.getCenter();
	lat = x.lat();
	lng = x.lng();
	searchUrl = 'getvenue.php?lat='+lat+'&lng='+lng+'&radius='+radius;
	downloadUrl(searchUrl, function(data) {
		var stores = data.documentElement.getElementsByTagName("store");
		var bounds = new google.maps.LatLngBounds(map.getCenter(), (map.getCenter()));
		marker_home = new google.maps.Marker({
			map: map, 
			position: map.getCenter(),
			icon: "http://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=home|FFFF00"
			//shadow: "http://chart.apis.google.com/chart?chst=d_map_pin_shadow"
		});
		var marker = new Array();
		/*if (stores.length == 0) sidebar.innerHTML = 'No results found.';*/
		
		var sidebarEntry = createSidebarNumResults(stores.length);
		sidebar.appendChild(sidebarEntry);
		for (var i = 0; i < stores.length; i++) {
			store_name		= stores[i].getAttribute("store_name");
			city			= stores[i].getAttribute("city");
			state			= stores[i].getAttribute("state");
			zip				= stores[i].getAttribute("zip");
			phone			= stores[i].getAttribute("phone");
			url				= stores[i].getAttribute("url");
			store_number	= parseInt(stores[i].getAttribute("store_number"));
			store_address	= stores[i].getAttribute("address");
			var latlng = new google.maps.LatLng(parseFloat(stores[i].getAttribute("lat")),
																					parseFloat(stores[i].getAttribute("lng")));
			distance = stores[i].getAttribute("distance");
			j = i + 1;
			marker[i] = new google.maps.Marker({
				icon: "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld="+j+"|FF0000|000000",
				//shadow: "http://chart.apis.google.com/chart?chst=d_map_pin_shadow",
				position: latlng, 
				map: map,
				zIndex: store_number
			});				
			infowindow = new google.maps.InfoWindow({});
			google.maps.event.addListener(marker[i], 'click', function() {
			foo = this;
			store_number = this.getZIndex();
			searchUrl = 'getinfo.php?store_number='+store_number;
			downloadUrl(searchUrl, function(data2) {
			var infos = data2.documentElement.getElementsByTagName("info");
			content = '';
			mon_open = clean_time(infos[0].getAttribute("mon_open"), 'open');
			mon_close = clean_time(infos[0].getAttribute("mon_close"), 'close');
			tue_open = clean_time(infos[0].getAttribute("tue_open"), 'open');
			tue_close = clean_time(infos[0].getAttribute("tue_close"), 'close');
			wed_open = clean_time(infos[0].getAttribute("wed_open"), 'open');
			wed_close = clean_time(infos[0].getAttribute("wed_close"), 'close');
			thu_open = clean_time(infos[0].getAttribute("thu_open"), 'open');
			thu_close = clean_time(infos[0].getAttribute("thu_close"), 'close');
			fri_open = clean_time(infos[0].getAttribute("fri_open"), 'open');
			fri_close = clean_time(infos[0].getAttribute("fri_close"), 'close');
			sat_open = clean_time(infos[0].getAttribute("sat_open"), 'open');
			sat_close = clean_time(infos[0].getAttribute("sat_close"), 'close');
			sun_open = clean_time(infos[0].getAttribute("sun_open"), 'open');
			sun_close = clean_time(infos[0].getAttribute("sun_close"), 'close');
			var alike = 1;
			var alike_weekend = 1;
			//content = content + "<div class = info_window style='border: gray medium solid; padding-bottom 5px; '>";
			if (infos[0].getAttribute("wifi") == 'yes') content = content + "<div style='clear: left; float:left; padding-right: 5px;'><img alt='Free Wifi' src='cosi.png' height='37' width='19' /></div>"; //end WiFi
			content = content + "<div style = 'float: left; padding-bottom: 3px; font-family: arial,helvetica,sans-serif; font-size:12px; '>";

			content = content+ "<span style = 'color: #A13F19; font-weight: bold;'>" + infos[0].getAttribute("store_name") + "</span><br>";
			content = content+ infos[0].getAttribute("store_address") + "<br>";
			content = content+ infos[0].getAttribute("city");
			if(infos[0].getAttribute("city") != "" && infos[0].getAttribute("state") !=""){
				content = content+ ', ';
			}
			content = content+ infos[0].getAttribute("state");
			if(infos[0].getAttribute("state") != "" && infos[0].getAttribute("zip") !=""){
				content = content+ ' ';
			}
			content = content+ infos[0].getAttribute("zip") + "<br>";
			if (infos[0].getAttribute("phone") != "") content = content+ "Phone: "+infos[0].getAttribute("phone")+"<br>";
			if (infos[0].getAttribute("url") != "") content += "<a href=\"" + infos[0].getAttribute("url") + "\">Visit this restaurant's page</a><br />";
			content = content + "</div></div>";
						content = content + "<div style='border-top: gray medium solid; padding-top: 3px; padding-bottom: 5px; clear:left; font-family: arial,helvetica,sans-serif;font-size:12px;'>";
				
			if ( ((mon_open == tue_open) && (tue_open == wed_open) && (wed_open == thu_open)) && ((mon_close == tue_close) && (tue_close == wed_close) && (wed_close == thu_close)) ) alike = 4;
			if ( (alike == 4) && (thu_open == fri_open) && (thu_close == fri_close) ) alike = 5;
			if ( (alike == 5) && (fri_open == sat_open) && (fri_close == sat_close) ) alike = 6;
			if ( (alike == 6) && (sat_open == sun_open) && (sat_close == sun_close) ) alike = 7;
			if ( (alike < 6) && (sat_open == sun_open) && (sat_close == sun_close) ) alike_weekend = 2;
			
			international = 'international';
			if (mon_open.toLowerCase() != 'international') {
			content = content+ "<b>HOURS:</b><br>"
			if (alike == 4) content = content+ "<b>Monday-Thursday: </b>"+mon_open+" - "+mon_close+"<br>";
			if (alike == 5) content = content+ "<b>Monday-Friday: </b>"+mon_open+" - "+mon_close+"<br>";
			if (alike == 6) content = content+ "<b>Monday-Saturday: </b>"+mon_open+" - "+mon_close+"<br>";
			if (alike == 7) content = content+ "<b>Every Day: </b>"+mon_open+" - "+mon_close+"<br>";
			if (alike == 1) {
				content = content+ "<b>Monday: </b>"+mon_open+"</b><br>";
				content = content+ " - "+mon_close+"</b><br>";
				content = content+ "<b>Tuesday: </b>"+tue_open+"</b><br>";
				content = content+ " - "+tue_close+"</b><br>";
				content = content+ "<b>Wednesday: </b>"+wed_open+"</b><br>";
				content = content+ " - "+wed_close+"</b><br>";
				content = content+ "<b>Thursday: </b>"+thu_open+"</b><br>";
				content = content+ " - "+thu_close+"</b><br>";
			}
			if (alike < 5) content = content+ "<b>Friday: </b>"+fri_open+" - "+fri_close+"<br>";
			if (alike < 7) {
				if (alike_weekend == 1) {
					if (alike < 6) content = content+ "<b>Saturday: </b>"+sat_open+" - "+sat_close+"</b><br>";
					content = content+ "<b>Sunday: </b>"+sun_open+" - "+sun_close+"</b><br>";
				}
				else if (alike_weekend == 2) content = content + "<b>Saturday-Sunday: </b>"+sat_open+" - "+sat_close+"<br>";
			}
			content = content.replace(/closed - closed/g, "Closed");
			}
			dir_address = infos[0].getAttribute("store_address").replace(/ /g, "%20");
			dir_city = infos[0].getAttribute("city").replace(/ /g, "%20");
			dir_state = infos[0].getAttribute("state").replace(/ /g, "%20");
			dir_origin = address.replace (/ /g, "%20"); 
			content = content+ "<a style='font-family: arial,helvetica,sans-serif;font-size:12px;' href='http://maps.google.com/maps?f=q&hl=en&saddr="+dir_origin+"%2C%20US&daddr="+dir_address+","+dir_city+","+dir_state+","+infos[0].getAttribute("zip")+"' target='_blank'>Get Directions</a></div>";
			//content = content + "</div><br>";
			//content = content+ "<div onClick='map.setZoom(18);'>Zoom Here</div>";
			store_name		= infos[0].getAttribute("store_name");
			city			= infos[0].getAttribute("city");
			state			= infos[0].getAttribute("state");
			zip				= infos[0].getAttribute("zip");
			phone			= infos[0].getAttribute("phone");
			store_number	= parseInt(infos[0].getAttribute("store_number"));
			store_address	= infos[0].getAttribute("address");
			infowindow.setContent(content);
			infowindow.open(map, foo);
			});
			}); //end listener 
			bounds.extend(marker[i].position);
			j = i + 1;
			
			var sidebarEntry = createSidebarEntry(marker[i], j, store_name, store_address, city, state, zip, distance, phone, url);
			sidebar.appendChild(sidebarEntry);
			//}
			map.setCenter(bounds.getCenter());
			bounds.extend(map.getCenter());
			map.fitBounds(bounds);
		} //end for i
	}); //end data

	function createSidebarNumResults (n_results){
		var div = document.createElement('div');
		div.style.width = '200px';
		if (n_results == 1) plural = "";
		else plural = "s";
		var html = "<b>" + n_results + " Cosi Restaurant"+plural+" Found in Your Area</b><hr style = 'color: Gray;'>";
		div.innerHTML = html;
		return div;
	}
	
	function createSidebarEntry(marker, index, name, address, city, state, zip, distance, phone, url) {
		
		var my_div = document.createElement ('div');
		var html = '<div style = "width:25px;float: left; clear: left;"><img alt="'+j+'" src="http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld='+j+'|FF0000|000000" /></div>';
		html = html + '<div style ="float:left; width: 155px; ">' + '<div style= "color: #A13F19; text-decoration: underline;"><b>' + name + '</b></div>';
		html = html + address	 + '<br/>' + city;
		if(city != "" && state != ""){
			html = html + ', ';
		}
		html = html + state;
		if(state != "" && zip != ""){
			html = html + ' ';
		}
		html = html + zip + '<br>' + distance + ' miles<br>';
		if (phone != '') html = html + 'Phone: ' + phone+'<br>';
		if (url != '') html += '<a href="' + url +'">Visit this restaurant\'s page</a><br>';
		html = html + '<hr style = "color: Gray;"></div>';
		my_div.innerHTML = html;
			
		my_div.style.cursor = 'pointer';
		my_div.style.marginBottom = '5px';
		//my_div.style.backgroundColor = 'tan';
		my_div.style.width = '200px';
		//alert (my_div.style.backgroundColor);
		my_div.style.fontFamily = 'arial, helvetica, sans-serif'; 
		google.maps.event.addDomListener(my_div, 'click', function() {
			google.maps.event.trigger(marker, 'click');
		});
		/*google.maps.event.addDomListener(my_div, 'mouseover', function() {
			my_div.style.backgroundColor = 'white';
		});
		google.maps.event.addDomListener(my_div, 'mouseout', function() {
			my_div.style.backgroundColor = 'tan';
		});*/
		return my_div;
	}
	
	//function createSidebarText (marker, index, name, address, city, state, zip, distance, phone) {
	//}
					} 
					//else alert("No results found");
				} 
				else {				
					var sidebarEntry = createSidebarInvalid();
					sidebar.appendChild(sidebarEntry);
				}
			});
		}
	} 

} //end initialize
