wwm = { };

wwm.minimap = null;
wwm.minimapinfo = {
  lat:    false,
  lng:    false,
  address:false,
  cityState: false
};
wwm.minimappoint = null;

wwm.loadMiniMap = function(c) {
  if (GBrowserIsCompatible()) {
    this.minimappoint = new GLatLng(c.lat,c.lng);
    
    this.minimap = new GMap2($('commmap'));
    this.minimap.addControl( new GSmallZoomControl());
    this.minimap.setCenter(this.minimappoint,14);
    var marker = new GMarker(this.minimappoint);
    GEvent.addListener(marker, 'click', function() {
      marker.openInfoWindowHtml(c.address+'<br />'+c.cityState);
    });
    this.minimap.addOverlay(marker);
    this.minimap.checkResize();
  }
}

wwm.refreshMiniMap = function(tab) {
  if (this.minimap && tab == 'propertymap') {
    this.minimap.checkResize();
    this.minimap.setCenter(wwm.minimappoint, 14);
  } 
}

wwm.showTab = function(tab) {
  var tabs = ['propertyinfo', 'propertymap'];
  $A(tabs).each(function(id) {
    if (tab == id) {
      $(id).show();
    } else {
      $(id).hide();
    }
  });
  wwm.refreshMiniMap(tab);
}

wwm.photolinks = [];

wwm.applyPhotoBehavior = function(photodiv, options) {
  options = Object.extend({
    className: 'photolink',
    propertyPhoto: 'propertyphoto',
    caption:  'caption_description'
	}, options || {});
	
  wwm.photolinks = $(photodiv).getElementsByClassName(options.className);
  $A(wwm.photolinks).each( function(photolink) {
    Element.observe(photolink, 'click', function(e) {wwm.loadUnitPhoto(e, options) });
    photolink.onclick = function() { return false; };
  });
}

wwm.loadUnitPhoto = function(e, options) {
  var link = Event.element(e);
  $A(wwm.photolinks).each( function(photolink) { photolink.removeClassName('selected') });
  var idMatch = link.id.match(/photo_([\d]+)/);
  var id = idMatch[1];
  
  link.addClassName('selected');
  if (wwm.photos[id]) {
    var photoInfo = wwm.photos[id];
    $(options.propertyPhoto).src = photoInfo.src;
    if ($(options.caption)) {
      $(options.caption).update(photoInfo.caption);
    }
  }
  return false;
}

wwm.applyUnitRowBehavior = function(div) {
  var unitsdiv = $(div);
  var unitrows = unitsdiv.getElementsBySelector('div.unittype');
  $A(unitrows).each(function(row) { 
    Event.observe(row, 'mouseover', wwm.hiliteUnit);
    Event.observe(row, 'mouseout', wwm.unliteUnit);
  });
  var unitcells = unitsdiv.getElementsBySelector('div.cell');
  $A(unitcells).each(function(cell) {
    if (!cell.hasClassName('noclick')) {
      Event.observe(cell, 'click', wwm.clickUnit);
    }
  })
}

wwm.hiliteUnit = function(e) {
  var el = Event.findElement(e, '.unittype');
  if (el) el.addClassName('rowhilite');
}

wwm.unliteUnit = function(e) {
  var el = Event.findElement(e, '.unittype');
  if (el) el.removeClassName('rowhilite');
}

wwm.clickUnit = function(e) {
  var el = Event.findElement(e, '.unittype');
  if (!el) return;
  var idMatch = el.id.match(/unit_([\d]+)/);
  if (idMatch == null) return;
  var id = idMatch[1];
  var url = $F('url_'+id);
  var titleElement = el.down('span.unittitle');
  var unitTitle = (titleElement ? titleElement.innerHTML : 'Unit Details');

  if (url) {
    el.href = url;
    $('unittitle').update(unitTitle);
    return hs.htmlExpand(el, { objectType: 'iframe', contentId: 'unitdetailwrap'} );
  }
}
