$(document).ready(function(){
  showCountry();
});
function showCountry(){
  $('#WrapperStoresCountry').show();
  outputCountry();
}
function showCity(id){
  $('#WrapperStoresStores').hide();
  $('#WrapperStoresCity').show();
  outputCity(id);
}
function showStore(id,city){
  $('#WrapperStoresStores').show();
  outputStore(id,city);
}
function outputCountry(){
  $.ajax({
    type: "POST",
    url: "countryList.php",
    data: "",
    dataType: "json",
    beforeSend: function(){
      $('#OutputStoresCountry').html("");
    },
    success: function(data){
      var o = "<i>Europe:</i><br />";
      $.each(data, function(i){
        if(data[i].loc == "europe"){
          o += "<a onclick='showCity("+data[i].id+");return false;' href='#' alt="+data[i].country+" title="+data[i].country+">"+data[i].country+"</a><br />";
        }
      });
      o += "<br /><i>World:</i><br />";
      $.each(data, function(i){
        if(data[i].loc == "world"){
          o += "<a onclick='showCity("+data[i].id+");return false;' href='#' alt="+data[i].country+" title="+data[i].country+">"+data[i].country+"</a><br />";
        }
      });
      $('#OutputStoresCountry').html(o);
    },
    error: function(xhr,textStatus,x){
      alert("Error: "+textStatus+" - "+xhr.responseText);
    }
  }); 
}
function outputCity(id){
  $.ajax({
    type: "POST",
    url: "cityList.php",
    data: "id="+id,
    dataType: "json",
    beforeSend: function(){
      $('#OutputStoresCity').html("Loading...");
    },
    success: function(data){
      var o = "";var title = true;
      $.each(data, function(i){
        if(data[i].type == "web"){
          if(title){o +="<i>Online Stores:</i><br />";title=false;}
          //o += "<a onclick='showStore("+data[i].id+");return false;' href='#' alt="+data[i].city+" title="+data[i].city+">"+data[i].city+"</a><br />";
          o += "<a href='http://www."+data[i].city+"' alt="+data[i].city+" title="+data[i].city+"  target='_blank'>"+data[i].city+"</a><br />";
        }
      });
      if(o != ""){o += "<br />";}
      $.each(data, function(i){
        if(data[i].type == "store"){
          o += "<a onclick='showStore("+data[i].id+",\""+data[i].city+"\");return false;' href='#' alt="+data[i].city+" title="+data[i].city+">"+data[i].city+"</a><br />";
        }
      });
      $('#OutputStoresCity').html(o);
    },
    error: function(xhr,textStatus,x){
      alert("Error: "+textStatus+" - "+xhr.responseText);
    }
  }); 
}
function outputStore(id,city){
  $.ajax({
    type: "POST",
    url: "storeList.php",
    data: "id="+id,
    dataType: "json",
    beforeSend: function(){
      $('#OutputStoresStores').html("Loading...");
    },
    success: function(data){
      var o = "";
      $.each(data, function(i){
        o += "<div class='content_stores_store'><b>"+data[i].store+"</b><div class='store_name'>"+data[i].street+"</div>"+city;
        if(data[i].phone != ""){ o += "<div>"+data[i].phone+"</a></div>";};
        if(data[i].url != ""){ o += "<div><a href='"+data[i].url+"'>"+data[i].url+"</a></div>";};
        o += "</div>";
      });
      $('#OutputStoresStores').html(o);
    },
    error: function(xhr,textStatus,x){
      alert("Error: "+textStatus+" - "+xhr.responseText);
    }
  }); 
}
