function ProgressbarMapControl(map, width) { 
  this.map_ = map; 
}

ProgressbarMapControl.prototype = new GControl(true, false);

ProgressbarMapControl.prototype.initialize = function () {
  var container_ = document.createElement('div');
  container_.innerHTML = '<div style="position:absolute;width:100%;border:5px;text-align:center;" id="geo_progress_text"></div>'
    + '<div style="background-color:#5e6a71;height:100%;" id="geo_progress"></div>';
  container_.id = "geo_progress_container";
  container_.style.display = "none";
  container_.style.width = "237px";
  container_.style.fontSize = "1.65em";
  container_.style.fontWeight = "bold";
  container_.style.height = "1.2em";
  container_.style.backgroundColor = "#003c69";
  container_.style.textAlign = "left";
  this.map_.getContainer().appendChild(container_);
  return container_;
};

ProgressbarMapControl.prototype.getDefaultPosition = function () {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(269, 222));
};

function ProgressbarControl(map, opt_opts) {
  this.options_ = opt_opts || {};
  this.loadstring_ = this.options_.loadstring || 'Loading...';
  this.control_ = new ProgressbarMapControl(map);
  this.map_ = map;
  this.map_.addControl(this.control_);
  this.div_ = document.getElementById('geo_progress');
  this.text_ = document.getElementById('geo_progress_text');
  this.container_ = document.getElementById('geo_progress_container');
  this.operations_ = 0;
  this.current_ = 0;
}

ProgressbarControl.prototype.start = function (operations) {
  this.div_.style.width = '0%'; 
  this.operations_ = operations || 1;
  this.current_ = 0;
  this.text_.style.color = "#fff";
  this.text_.innerHTML = this.loadstring_;
  this.container_.style.display = "block";
};

ProgressbarControl.prototype.updateLoader = function (step) {
  this.current_ += step;
  if (this.current_ > 0) {
    var percentage_ = Math.ceil((this.current_ / this.operations_) * 100);
    if (percentage_ > 100) { 
      percentage_ = 100; 
    }
    this.div_.style.width = percentage_ + '%'; 
    this.text_.innerHTML = this.current_ + ' / ' + this.operations_;
  }
};

ProgressbarControl.prototype.remove = function () {
  this.container_.style.display = 'none';
};

var map;
var progressBar;
var mapMarkers = [];
var mapHTMLS = [];
var maxNum = 0;
var num = 0;

var campusIcon = new GIcon();
campusIcon.image = 'http://cahnrs.wsu.edu/images/map/campus.png';
campusIcon.shadow = 'http://cahnrs.wsu.edu/images/map/campus-shadow.png';
campusIcon.iconSize = new GSize(20, 22);
campusIcon.shadowSize = new GSize(32, 22);
campusIcon.iconAnchor = new GPoint(10, 22);
campusIcon.infoWindowAnchor = new GPoint(10, 11);

var energyIcon = new GIcon(); 
energyIcon.image = 'http://cahnrs.wsu.edu/images/map/energy.png';
energyIcon.shadow = 'http://cahnrs.wsu.edu/images/map/energy-shadow.png';
energyIcon.iconSize = new GSize(17, 17);
energyIcon.shadowSize = new GSize(26, 17);
energyIcon.iconAnchor = new GPoint(8, 17);
energyIcon.infoWindowAnchor = new GPoint(8, 8);

var ewuIcon = new GIcon();
ewuIcon.image = 'http://cahnrs.wsu.edu/images/map/ewu.png';
ewuIcon.shadow = 'http://cahnrs.wsu.edu/images/map/ewu-shadow.png';
ewuIcon.iconSize = new GSize(23, 22);
ewuIcon.shadowSize = new GSize(35, 22);
ewuIcon.iconAnchor = new GPoint(11, 22);
ewuIcon.infoWindowAnchor = new GPoint(11, 11);

var extcoIcon = new GIcon(); 
extcoIcon.image = 'http://cahnrs.wsu.edu/images/map/ext.png';
extcoIcon.shadow = 'http://cahnrs.wsu.edu/images/map/ext-shadow.png';
extcoIcon.iconSize = new GSize(15, 17);
extcoIcon.shadowSize = new GSize(24, 17);
extcoIcon.iconAnchor = new GPoint(7, 17);
extcoIcon.infoWindowAnchor = new GPoint(7, 8);

var farmsIcon = new GIcon(); 
farmsIcon.image = 'http://cahnrs.wsu.edu/images/map/farms.png';
farmsIcon.shadow = 'http://cahnrs.wsu.edu/images/map/farms-shadow.png';
farmsIcon.iconSize = new GSize(20, 13);
farmsIcon.shadowSize = new GSize(27, 13);
farmsIcon.iconAnchor = new GPoint(10, 13);
farmsIcon.infoWindowAnchor = new GPoint(10, 6);

var recIcon = new GIcon(); 
recIcon.image = 'http://cahnrs.wsu.edu/images/map/rec.png';
recIcon.shadow = 'http://cahnrs.wsu.edu/images/map/rec-shadow.png';
recIcon.iconSize = new GSize(20, 18);
recIcon.shadowSize = new GSize(30, 18);
recIcon.iconAnchor = new GPoint(10, 18);
recIcon.infoWindowAnchor = new GPoint(10, 9);

var customIcons = [];
customIcons["campus"] = campusIcon;
customIcons["energy"] = energyIcon;
customIcons["ext"] = extcoIcon;
customIcons["farms"] = farmsIcon;
customIcons["rec"] = recIcon;

var markerGroups = { "campus": [], "energy": [], "ext": [], "farms": [], "rec": []};

var param_nameColumn = "name";
var param_addressColumn = "address";
var param_latColumn = "latitude";
var param_lngColumn = "longitude";
var param_websiteColumn = "website";
var param_phoneColumn = "phone";
var param_contactColumn = "contact";
var param_typeColumn = "type";

function load() {  
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(47.318100, -120.988980), 7, G_PHYSICAL_MAP);
	progressBar = new ProgressbarControl(map);
	map.enableScrollWheelZoom();
	getJSON();
  } else {
    alert("Sorry, the Google Maps API is not compatible with this browser");
  }
}

function loadMarkers(){
  progressBar.start(maxNum);
  setTimeout('loadMapJSON', 10);
}

function markerClicked(markerNum) {
  mapMarkers[markerNum].openInfoWindowHtml(mapHTMLS[markerNum]);
}

function loadMapJSON(json) {
  num++;
  progressBar.updateLoader(1);

  for (var i = 0; i < json.feed.entry.length; i++) {
    var entry = json.feed.entry[i];
    if(entry["gsx$" + param_latColumn]) {
      var lat = parseFloat(entry["gsx$" + param_latColumn].$t);
      var lng = parseFloat(entry["gsx$" + param_lngColumn].$t);
      var point = new GLatLng(lat,lng);    
	  var type = entry["gsx$"+param_typeColumn].$t;
	  var html = "<div style='font-size:12px'>";
      html += "<a href=" + entry["gsx$"+param_websiteColumn].$t + ">" + "<strong>" + entry["gsx$"+param_nameColumn].$t + "</strong></a>";
	  if(entry["gsx$" + param_addressColumn]) {
        html += "<br/>" + entry["gsx$"+param_addressColumn].$t;
      }  
	  if(entry["gsx$" + param_phoneColumn]) {
        html += "<br/>" + entry["gsx$"+param_phoneColumn].$t;
      }
	  if(entry["gsx$" + param_contactColumn]) {
        html += "<br/><a href=mailto:" + entry["gsx$"+param_contactColumn].$t + ">Email</a>";
      }	  
      html += "</div>";

      var marker = createMarker(point,html,type);
      map.addOverlay(marker);
      mapHTMLS.push(html);
    } 
  }
  if (num < maxNum) {
    setTimeout('loadMapJSON(json)', 10);
  } else {
    progressBar.remove();
    num = 0;
  }
  hide("energy");
  hide("ext");
  hide("farms");
  hide("rec");
}

function createMarker(point, html, type) {
  var marker = new GMarker(point, customIcons[type]);
    markerGroups[type].push(marker);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}

function show(type) {
  for (var i = 0; i < markerGroups[type].length; i++) {
	  markerGroups[type][i].show();
  }
}

function hide(type) {
  for (var i = 0; i < markerGroups[type].length; i++) {
	  markerGroups[type][i].hide();
  }
  map.closeInfoWindow();
}

function boxclick(box,type) {
  if (box.checked) {
	show(type);
  } else {
	hide(type);
  }
}

function getJSON() {
  var script = document.createElement('script');
  script.setAttribute('src', 'http://spreadsheets.google.com/feeds/list/rQwkh4Cfw3i9GaBCWocAnKA/od6/public/values?alt=json-in-script&callback=loadMapJSON');
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

function mailpage() {mail_str = "mailto:?subject=Check out " + document.title; mail_str += "&body=I thought you might be interested in " + document.title; mail_str += ". You can view it at " + location.href; location.href = mail_str;}