﻿// Change these parameters to customize map
var param_wsId = "default";
//var param_ssKey = "teZsC9skisyLyoTajvEmjJw";
var param_titleColumn = "title";
var param_descriptionColumn = "description";
var param_xstreet = "xstreet";
var param_address = "address";
var param_zipcode = "zipcode";
var param_phone = "phone";
var param_link = "link";
var param_closed = "closed";
var param_category = "category";
var param_iconType = "green";
var param_iconOverType = "orange";

var categories = new Array();
var categoryTitles = new Array();

function cm_sortRows(rowA, rowB) {
    var x = rowA["gsx$" + param_titleColumn].$t.toLowerCase();
    var y = rowB["gsx$" + param_titleColumn].$t.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function cm_getJSON() {
  // Retrieve the JSON feed.
  var script = document.createElement('script');

  script.setAttribute('src', 'http://spreadsheets.google.com/feeds/list'
                         + '/' + param_ssKey + '/' + param_wsId + '/public/values' +
                        '?alt=json-in-script&callback=renderList');
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

cm_getJSON();

function renderList(json) {
	var listHTML = "";
	json.feed.entry.sort(cm_sortRows);
	for (var i = 0; i < json.feed.entry.length; i++) {
		var entry = json.feed.entry[i];
		if (entry["gsx$" + param_titleColumn] && (entry["gsx$" + param_titleColumn].$t != "title")) {
			
			// add to categories array
			var cat = entry["gsx$" + param_category].$t.split(",");
			for (j=0; j < cat.length; j++) {
				if (typeof categories[cat[j]] == "undefined") {
					categories[cat[j]] = [];
					if (cat[j]) {
						categoryTitles[categoryTitles.length] = cat[j];
					}
				}
				categories[cat[j]].push(i);
			}
		}
	}
	categoryTitles.sort();
	for (i=0; i<categoryTitles.length; i++) {
		if (categoryTitles[i] != "Luncher") {
			var newTit = "";
			for (j=0; j<categoryTitles[i].length; j++) {
				if (categoryTitles[i].charAt(j) == "_") {
					newTit += " ";
				} else {
					newTit += categoryTitles[i].charAt(j);
				}
			}
			listHTML += "<b style='margin-bottom:10px;'>" + newTit + "</b><ul>";
			for (j=0; j < categories[categoryTitles[i]].length; j++) {
				tmp = categories[categoryTitles[i]][j];
				entry = json.feed.entry[tmp];
				var closedStyle = ""
				if (entry["gsx$" + param_closed].$t == "TRUE") {
					closedStyle = "<span style='text-decoration: line-through;'>";
				}
				var restlink = ""
				if (entry["gsx$" + param_link].$t != "") {
					restlink = "<a href='" + json.feed.entry[tmp]["gsx$" + param_link].$t + "'>";
				} 
					
					
				if (entry["gsx$" + param_titleColumn].$t != "") {
					listHTML += "<li>" + closedStyle + restlink + json.feed.entry[tmp]["gsx$" + param_titleColumn].$t;
					
					if (entry["gsx$" + param_link].$t != "") {
						listHTML += "</a>";
					} 
					if (entry["gsx$" + param_closed].$t == "TRUE") {
						listHTML += "</span>";
					}
					if (entry["gsx$" + param_xstreet].$t != "") {
						listHTML += " (" + json.feed.entry[tmp]["gsx$" + param_xstreet].$t + ") ";
					}
	
					
					if (entry["gsx$" + param_link].$t == "") {
						listHTML += " **review coming soon** ";
					}
					if (entry["gsx$" + param_closed].$t == "TRUE") {
						listHTML += " **closed**";
					}
					
					listHTML += "</li>";
				}
			}
			listHTML += "</ul><br>";
		}
	}
	document.getElementById("listCont").innerHTML = listHTML;
}


