Drupal.sedes = {};

// Replace locmap with the map's id that you want

var markers = {};

function show_on_map(count,html) {
  var m = Drupal.gmap.getMap('sedes_map'); // view_gmap is my id, change it to whatever your map id is called
 
  m.vars.markers[count].marker.openInfoWindow(html);
}

function doSpan(el) {
   var obj = $.create("span", {"id":$(el).parent().attr("id")}, [$(el).text()]);
   $(el).parent().prepend($(obj));
   $(el).remove();
}

function doLink(el) {
  var obj = $.create("a", {"href":"/jse/sedes/"+$(el).parent().attr("id").replace("ccaa","")+"/1"}, [$(el).text()]);
  $(obj).click(function(){
   $("#categories span").each(function(i){
          doLink($(this));
        });
    doSpan($(this));
    $("#sedes").remove();
    requestSedes($(this));
    return false;
  });
  $(el).parent().prepend($(obj));
  $(el).remove();
}

function showSedes(idcomunidad) {
  var el = $("#ccaa"+idcomunidad+" a").eq(0);  
  requestSedes($(el));
   $("#categories span").each(function(i){
          doLink($(this));
        });
    doSpan($(el));
  
}

function requestSedes(el) {
   $("#sedes").remove();
   $.get($(el).attr("href"), {}, function(data){
          data = Drupal.parseJson(data);
          var result = $.create("div", {"id":"sedes"}, ["h2", {}, [data.ccaa]]);
          if (data.sedes.length) {
            var lista = $.create("ul", {}, []);         
            for (var i=0; i<data.sedes.length; i++) {
              var item = $.create("li", {}, [
                                  "h3", {}, [data.sedes[i].title]
              ]);
              $(item).append($.create("span",{},[data.sedes[i].street]));
              $(item).append($.create("span",{},[data.sedes[i].cp+" - "+data.sedes[i].city]));
              if (data.sedes[i].phone!="") {
                $(item).append($.create("span",{},["Tel. "+data.sedes[i].phone]));
              }
              if (data.sedes[i].fax!="") {
                $(item).append($.create("span",{},["Fax. "+data.sedes[i].fax]));
              }
              if (data.sedes[i].email!="") {
                 $(item).append($.create("span",{},["Email. ", 
                      "a",{"href":"mailto:"+data.sedes[i].email},[data.sedes[i].email]
                ]));
              }
              if (data.sedes[i].url!="") {
                 $(item).append($.create("span",{},["Web. ", 
                      "a",{"href":"http://"+data.sedes[i].url},[data.sedes[i].url]
                ]));
              }
              $(lista).append($(item));
            }
            $(result).append(lista);
          }
           else {
            $(result).append($.create("p", {}, ["No hay sedes en esta comunidad"]))
          }
          
          $("#categories").parent().append($(result));
          //$("#sedes").ScrollTo(600);
          
      });  
}

Drupal.sedes.autoAttach = function() {
  $("#categories a").each(function(i){
    
    $(this).attr("href", $(this).attr("href")+"/1");
     $(this).click(function(){
      $("#sedes").remove();
      $("#categories span").each(function(i){
          doLink($(this));
        });
        requestSedes($(this));
        doSpan($(this));
        return false;
      });
  });  
}

if (Drupal.jsEnabled) {
  $(document).ready(Drupal.sedes.autoAttach);
}
