var Global = {
  map_options: {},
  
  init: function()
  {
    Global.init_first_link_on_click();
    Global.init_auto_focus();
    Global.init_auto_clear();
    
    // needs to be initialised before map
    
    if ($$('body.user_locations')[0]) {
      YouEye.init();
    }
    
    if ($('map')) {
      Global.init_map();
    }
    
    if ($('results')) {
      Results.init();
    }
    
    if ($$('body.mortgage_settings')[0]) {
      Mortgage.init();
    }
    
    if ($$('body.listings')[0]) {
      YouBuy.init();
    }
    
    if ($('pages_index')) {
      Homepage.init();
    }
    
    if ($('historical_sales_index') || $('historical_sales_search_new')) {
      YouKnow.init();
    }
  },
  
  
  init_auto_focus: function()
  {
    $$('.auto_focus').each( function(el) {
      el.activate()
    });
  },
  
  
  init_auto_clear: function()
  {
    $$('.auto_clear').each(function(el) {
      el.observe('focus', function(event) { 
        if ($F(el) == el.defaultValue) {
          el.clear()
        }
      });
    });
  },
  
  
  // elements that change location to first link within them when clicked
  
  init_first_link_on_click: function()
  {
    $$('.first_link_on_click').each( function(element) {
      var link = element.select('a')[0];
      element.observe('click', function(event) { 
        if(!Event.element(event).hasClassName('overides_first_link_on_click')) {
          location = link.href;
        }
      });
    });
  },
  
  
  init_map: function()
  {
    // initialise YouMap with options already determined
    Global.map = new YouMap(Global.map_options);
    
    if (! $$('body.user_locations')[0]) {
      // Initialise markers for properties
			// 'properties' should be defined in the calling page (eg. listings/index.html.haml)
      Global.add_markers(properties);
      // zoom the map to show all markers
      if ($('map_container')) {
        Global.map.zoom_for_markers();
      }
      
      if (typeof(window['comparables']) != "undefined") {
        Global.add_markers(comparables);
      }
    }
  },
  
  
  // make YouMarkers for each entry
  //
  add_markers: function(data)
  {
    data.each( function(property) {
      if (property.lat && property.lng) {
        existing_marker = Global.map.markers().find( function(m) { 
          return m.lat() == property.lat && m.lng() == property.lng;
        });
          
        if (existing_marker) { // existing marker at coordinates, so let's make a new tab
          existing_marker.add_tab(property);  
        }
        else { // make new marker
          m = new YouMarker(Global.map, property.type, property);
        }
      }
      else {
        $('property_' + property.id).addClassName('no_marker');
      }
    });
  }
};


// object to encapsulate property list functionality
//
var Results = {
  init: function()
  {
    $$('.property_list>li').each( function(element) {
      // warning! change this nasty substring if you change the name of the id
      // of each li
      var id = element.id.substring(9);
      if (id) {
        element.observe('mouseover', function(event) {
          if (Global.map.no_info_window_open()) {
            marker = Results.find_marker(id);  
            if (marker) {
              marker.highlight_in_table(id);
              marker.highlight();
            }
          }
        });
          
        if (! $$('body.listings')[0]) {
          element.observe('click', function(event) {
            marker = Results.find_marker(id);
            if (marker) {
              marker.open_info_window(id);
            }
          });
        }
      }
    });
  },
  
  
  find_marker: function(id)
  {
    return Global.map.markers().find(function(m) {
      marker_ids = m.objects.collect(function(obj) {
                                       return obj.id;
                                     });
      marker_is_found = marker_ids.include(id);
      return marker_is_found;
    });
  }
};

document.observe('dom:loaded', Global.init);
