var hmh = Sammy("#main", function() {
  
  var prepare = (prepare || {});
  
  prepare.readmore = function() {
    $("a.readmore").each(function(idx, element) {
      var anchor = $(element);
      var parent = anchor.closest('.expandable');
      
      // ensure we have an ID to work with
      parent.identify();
      
      // setup the href and set the visibilities etc.
      anchor
        .attr('href', "#/more/"+parent.attr('id'))
        .show()
        .closest(".expandable")
        .find('.subsequent')
        .hide();
    });
  };
  
  prepare.location = function() {
    location_map.initialize();
  };
  
  prepare.directions = function() {
    location_directions.initialize();
  };
  
  prepare.guide = function() {
    location_guide.initialize();
  };
  
  prepare.gallery = function() {
    photo_gallery.initialize();
  };
  
  
  // entry point
  this.bind('run', function(context) { 
    for (var key in prepare) {
      if (prepare.hasOwnProperty(key)) {
        prepare[key](context);
      }
    }
  });
  
  // readmore
  this.get("#/more/:id", function(context) {
    var id = this.params['id'];
    var container = $("#"+id);
    
    // show the extra content
    container
      .find('.subsequent')
      .css({opacity: 0})
      .slideDown(function() {
        $(this).animate({opacity: 1});
      });
    
    // toggle the "Read More text
    container
      .find('a.readmore')
      .remove();
  });
  
  // location links
  this.get("#/map/", function(context) {
    location_map.reposition(location_map.lat, location_map.lng);
    location_guide.highlight(false);
  });
  
  this.get("#/map/:id", function(context) {
    var id = this.params['id'];
    location_map.show_point(id);
    location_guide.highlight(id);
    location_directions.show_point(id);
  });
  
  this.get("#/directions/:id", function(context) {
    var id = this.params['id'];
    location_directions.show_point(id);
  });
});

var location_guide = {
  initialize: function() {
    var guide = $("#location-guide");
    
    if (guide.length > 0) {
      $("#guide-sections").change(function() {
        var id = $(this).val();
        $("ul", guide).hide();
        $("ul#"+id, guide).show();
      });
    }
  },
  
  highlight: function(id) {
    
    $("#location-guide .current").removeClass("current");
    
    if (id)
      $("a[href*='/"+id+"']").addClass("current");
  }
};

var photo_gallery = {
  initialize: function() {
    $(".gallery a").colorbox();
  }
};

// the location map
var location_map = {
  map: null,
  lat: 47.50585960519183,
  lng: 19.062105417251587,
  
  marker_sprites: {
    hmh: "/images/hmh.png",
    red: "/images/marker_sprite_red.png",
    blue: "/images/marker_sprite_blue.png"
  },
  
  home_marker: null,
  markers: [],
  
  initialize: function() {
    var elem = document.getElementById("location-map");
    
    if (elem) {
      var options = {
        mapTypeControl: true,
        mapTypeControlOptions: {
          style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        },
        streetViewControl: false,
        overviewMapControl: true,
        scaleControl: true,
        zoom: 16,
        center: new google.maps.LatLng(this.lat, this.lng),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      this.map = new google.maps.Map(
        elem,
        options
      );
      
      
      // Create a div to hold the control.
      var transit_button = document.createElement('a');

      $(transit_button).addClass("map_button").html("Transit");
      this.map.controls[google.maps.ControlPosition.TOP_RIGHT].push(transit_button);
      
      $(transit_button).click(function() {
        $(this).toggleClass("enabled");
        location_map.toggle_transit();
      });
      
      
      // create the HMH marker
      var params = {
        map: this.map,
        position: new google.maps.LatLng(this.lat, this.lng),
        flat: false,
        title: "Home-Made Hostel",
        zIndex: 5
      };
      this.home_marker = new google.maps.Marker(
        $.extend(
          params, 
          this.marker_options(
            this.marker_sprites.blue)));
    }
  },
  
  setup_listeners: function() {
    // listener to remove has from URL when map is moved
    // clicking address should reset map to original zoom & position
  },
  
  reposition: function(lat, lng) {
    this.map.panTo(new google.maps.LatLng(lat, lng));
  },
  
  show_point: function(id) {
    var anchor = $("a[href*='#/map/"+id+"']");
    
    if (anchor.length == 0) return console.log("Location doesn't exist.");
    
    this.scroll_page();
    this.clear_markers();
    
    this.add_marker(
      anchor.data('lat'), 
      anchor.data('lng'), 
      { title: anchor.attr('title') });
    
    var bounds = new google.maps.LatLngBounds();
    bounds.extend(new google.maps.LatLng(this.lat, this.lng));
    bounds.extend(new google.maps.LatLng(anchor.data('lat'), anchor.data('lng')));
    this.map.fitBounds(bounds);
  },
  
  scroll_page: function() {
    if ($('body').scrollTop() < 175) 
      $.scrollTo("#primary_navigation",'slow');
  },
  
  clear_markers: function() {
    while (this.markers.length > 0) {
      var m = this.markers.pop();
      m.setMap(null);
      m.unbindAll();
    } 
  },
  
  add_marker: function(lat, lng, opts) {
    var params = {
      map: this.map,
      position: new google.maps.LatLng(lat, lng),
      flat: false,
      title: opts.title,
      zIndex: 1
    };
    
    if (opts.pin !== undefined)
      params.icon = opts.pin;
    else
      $.extend(params, this.marker_options(this.marker_sprites.red));
    
    this.markers.push(new google.maps.Marker(params));
  },
  
  marker_options: function(sprite) {
    return {
      icon: new google.maps.MarkerImage(
        sprite,
        new google.maps.Size(20,34),
        new google.maps.Point(0,0),
        new google.maps.Point(10,34)),
      shadow: new google.maps.MarkerImage(
        sprite,
        new google.maps.Size(28, 34),
        new google.maps.Point(28, 0),
        new google.maps.Point(0, 34))
    }
  },
  
  // transit layer
  
  toggle_transit: function() {
    if (this.transit_visible) this.hide_transit();
    else this.show_transit();
  },
  
  show_transit: function() {
    
    var transitOptions = {
      getTileUrl: function(coord, zoom) {
        return "http://mt1.google.com/vt/lyrs=m@155076273,transit:comp|vm:&" +
        "hl=en&opts=r&s=Galil&z=" + zoom + "&x=" + coord.x + "&y=" + coord.y;
      },
      tileSize: new google.maps.Size(256, 256),
      isPng: true
    };
    
    var transitMapType = new google.maps.ImageMapType(transitOptions);
    
    this.map.overlayMapTypes.insertAt(0, transitMapType);
    this.transit_visible = true;
  },
  
  hide_transit: function() {
    this.map.overlayMapTypes.removeAt(0);
    this.transit_visible = false;
  }
};

// directions
var location_directions = {
  initialize: function() {
    this.service = new google.maps.DirectionsService();
    this.renderer = new google.maps.DirectionsRenderer();
    
    this.info_panels().hide();
  },
  
  info_panels: function() {
    return this._info_panels || (this._info_panels = $("#location-instructions .info-panel"));
  },
  
  show_point: function(id) {
    var target = $("#"+id);
    
    // remove the old one(s)
    this.info_panels()
      .not(target)
      .hide();
    
    target.show();
  }
};


// boot 'er up
jQuery(function() {
  hmh.run(); 
});
