(function($) {
  //$("#only_members").change( function() { alert("Hello"); } );
  var app = $.sammy(function() {
    this.element_selector = '#list';
    this.use(Sammy.Template);
    this.use(Sammy.Session);
    this.use(Sammy.NestedParams);

    this.before(function() {
      var context = this;
      $.ajax({
        url: '/professionnels_que_l_on_peut_consulter',
        dataType: 'json',
        async: false,
        success: function(items) {
          context.items = items;
        }
      });

      /* $.ajax({
        url: '/statuts.json',
        dataType: 'json',
        async: false,
        success: function(items) {
          $.each(items, function(i, item) {
            $("#index_of_statuses").append('<option value="' + item.status.id + '">' + item.status.title + '</option>');
          });
        }
      }); */

      /* $.ajax({
        url: '/arts_divinatoires.json',
        dataType: 'json',
        async: false,
        success: function(items) {
          $.each(items, function(i, item) {
            $("#index_of_arts").append('<option value="' + item.art.id + '">' + item.art.title + '</option>');
          });
          
        }
      }); */
    });

    /* this.get('#/filtre/', function(context) {
      this.log(this.params['pro']);
      
      //$.log(this.params);
      //return false;
      alert(this.params);
      //alert(this.params['mode']);
      //context.redirect('#/' + this.params['mode'], this.params['member']);
    }); */

    this.get('#/liste/', function(context) {
      $("#javascript_unabled_message").remove();
      $("#map_canvas").remove();

      context.app.swap('');

      $.each(context.items, function(i, item) {
        context.partial('templates/fiche.template', {id: i, fiche: item}, function(rendered) {
          context.$element().append(rendered);
          $('img.profile').defaultImageIfError();
        });
      });
    });

    this.get('#/carte/', function(context) {
      $("#javascript_unabled_message").remove();
      $("#list").empty();
      $("#list").after('<div id="map_canvas"></div>');

      context.app.swap('');

      var map = null;
      var geocoder = null;

      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(46.7301072, 2.9387887), 6);

        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
      }

      $.each(context.items, function(i, item) {
        showAddress(map, item);
      });
    });

    this.get('#/fiche/:id', function(context) {
      $("#javascript_unabled_message").remove();
      $("#list").empty();
      $("#map_canvas").remove();

      //context.app.swap('');
/*
      $.each(context.items, function(i, item) {
        context.partial('templates/fiche.template', {id: i, fiche: item}, function(rendered) {
          context.$element().append(rendered);
          $('img.profile').defaultImageIfError();
        });
      });
      */
      //this.log(context.items);
      
      this.fiche = this.items[this.params['id']];
      if (!this.fiche) { return this.notFound(); }
      this.partial('templates/fiche_detail.template');
    });
  });

  $(function() {
    app.run('#/liste/');
  });

})(jQuery);