var map;
var ref;
var cs;

$(document).ready(function() {
  map = new GMap2(document.getElementById("storeMap"));
  defaultCenter();
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
});

window.onunload = function() {
  GUnload();
}

function defaultCenter(c) {
  map.setCenter(new GLatLng(45.305803, -63.017578), 7);
  if (c) {
    map.clearOverlays();
  }
}

function ss(msg) { $("#status").slideUp("fast", function() { $(this).html(msg).slideDown("fast"); }); }
function hs() { $("#status").slideUp("fast"); }
function ls() { $("fieldset input, fieldset select").attr("disabled","true"); }
function as() { $("fieldset input, fieldset select").attr("disabled",""); }

function clearSearch() {
  hs();
  $("fieldset input.input, fieldset select").each(function() { $(this).val(''); }); 
}

function findStores() {
  if ( !$("#s_name").val().length && !$("#s_city").val().length && !$("#s_product").val().length && !$("#s_category").val().length ) {
    ss("You must enter some search criteria.");
    return false;
  }
  /*cook("s_name", $("#s_name").val());
  cook("s_city", $("#s_city").val());
  cook("s_product", $("#s_product").val());
  cook("s_category", $("#s_category").val());*/
  ls();
  $("#listOptions").css("display", "none");
  $("#searchResults").slideUp("slow", function() {
    $("#searchResults").html("");
    $("#dbox").slideUp("slow", function() { 
      $("#dpan").html("");
      $("#dbox a").css("display", "none");
    });
    ss("Loading...", true);
    $.getJSON("dr.php", {
        a: "FindStores",
        name: $("#s_name").val(), 
        city: $("#s_city").val(),
        product: $("#s_product").val(),
        category: $("#s_category").val()
      }, 
      populateStores
    );
  });
}

function createMarker(point, i) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "mouseover", function() {
    marker.openInfoWindowHtml("<strong>" + ref[i].name + "</strong><br /><a href=\"#s" + ref[i].id + "\" onclick=\"displayStore(" + ref[i].id + "," + i + ",true);\">View Store Information</a>");
    //displayStore(ref[i].id,i,true);
  });
  return marker;
}

function populateStores(list) {
  ref = list;
  defaultCenter(true);

  if ( list.length == 0 ) {
    ss("No producers found for the given search criteria.");
    as();
    return false;
  }

  var ul = document.getElementById("searchResults");
  
  for( i = 0; i < list.length; i++ ) {
    var li = document.createElement("li");
    li.id = "s" + list[i].id;
    li.innerHTML = '<a name="s' + list[i].id + '" href="javascript:void(0);" onclick="displayStore(' + list[i].id + ',' + i + ');">' + list[i].name + '</a><div class="storeBlock"></div>';
    ul.appendChild(li);
    var point  = new GLatLng(list[i].lat, list[i].lng);
    var marker = createMarker(point, i);
    map.addOverlay(marker);
  }
  
  $("#listOptions").css("display", "block");
  $("#searchResults").slideDown("slow", function() { hs(); as(); });
}

function displayStore(id,ps,s,da) {
  cs = ps;
  $("#dbox").slideUp("slow", function() {
    $("#dpan").html("");
    $("#dbox a").css("display", "none");
    if (!s)
      ss("Loading...");
  });
  var speed = s ? 1 : "slow";
  $("ul#searchResults li div").slideUp(1);
  $.getJSON("dr.php?a=StoreData&id=" + id, function(json) {
    var div = $("li#s" + id + " div");
    var output  = "<div class=\"storeInfo\">";
        output += json.address + "<br />";
        output += json.city + ", NS<br /><br />";
        output += "<strong>Telephone:</strong> " + json.phone + "<br />";
        if ( json.website.length ) {
          output += "<strong>Website:</strong> <a href=\"http://" + json.website + "\" target=\"_blank\">" + json.website + "</a>";
        }
        if ( json.email.length ) {
          output += "<br /><strong>Email:</strong> " + json.email + "</a>";
        }
        output += "</div>";
        output += "<div class=\"storeProducts\">";
        output += "<h5>Products Available</h5>";
        output += "<ul>";
        for( x = 0; x < json.products.length; x++ ) {
			if(x == (json.products.length) - 1){
				output += json.products[x]; 
			}else{	
          		output += json.products[x] + ", ";
			}
        }
        output += "</ul></div>";

        if ( json.hours )
        {
          output += "<div class=\"storeHours\">";
          output += "<h5>Store Hours</h5>";
          output += "<div class=\"wrapper\">" + json.hours + "</div>";
          output += "</div>";
        }

        output += "<div class=\"storeOptions\"><br /><a href=\"#map\" onclick=\"zoomTo(" + ps + ");\">Zoom To &amp; Find Directions</a></div>";
        div.html(output);
        div.slideDown(speed, function() { if (!s) hs(); });
  });
}

function zoomTo(ps) {
  var store  = ref[ps];
  $("#dbox").slideDown("slow");
  var point  = new GLatLng(store.lat, store.lng);
  var marker = new GMarker(point);
  map.clearOverlays();
  map.setCenter(point, 16);
  map.addOverlay(marker);
  marker.openInfoWindowHtml("<strong>" + store.name + "</strong>");
}

function getDirections() {
  var path = $("#postal").val() + " to " + ref[cs].lat + "," + ref[cs].lng;
  $("#dbox a").css("display", "block");
  directions = new GDirections(map, document.getElementById("dpan"));
  directions.load(path);
  $("#dpan").slideDown("slow");
}

function expandAll() {
  $("ul#searchResults li div").slideUp(1);
  for( i = 0; i < ref.length; i++ ) {
    displayStore(ref[i].id, i, false);
  }
}