var realtor_regionPrepared = false;
var realtor_regionInitiated = false;
var realtor_showMapBtn;
var realtor_showMapBtnText = '';
var realtor_mapLargeStatus = 'hidden';

function initRegion()
{
  if (realtor_regionInitiated) return;
  realtor_regionInitiated = true;
  $('mapLarge').setOpacity(0.5);
  var map = readAndCreateMap('mapLarge', {controls:true});
  $('mapLarge').down('.mapControls').setOpacity(0);
  Event.observe( document.onresize ? document : window, "resize", onWindowResize);
  if ($('aside')) $('pre_page').style.height = $('aside').getHeight() + 'px';
  if ($('mapOptions'))
  {
    realtor_showMapBtn = $('mapOptions').down('a[href=#showMap]');
    realtor_showMapBtn.observe('click',toggleMap);
  }
  var properties = $('properties').select('li');
  if (typeof realtor_adminMode == 'undefined') properties.each( function (p) { p.observe('click',onClickProperty); } );
  readAndCreateMapMarkers('mapLarge', properties);
}
addOnloadEvent(initRegion);

function onClickProperty(event)
{
  var p = Event.findElement(event,'LI');
  document.location = p.select('h3 a')[0].href;
}

function onWindowResize()
{
  if (realtor_mapLargeStatus != 'hidden') return;
  resetMapCenter('mapLarge');
}

function toggleMap(event)
{
  Event.stop(event);
  if (realtor_mapLargeStatus == 'hidden') showMap();
  else if (realtor_mapLargeStatus == 'visible') hideMap();
}

function showMap()
{
  if (realtor_mapLargeStatus != 'hidden') return;
  realtor_mapLargeStatus = 'changing';
  showAllMapMarkers('mapLarge');
  $('container').addClassName('mapIsVisible');
  realtor_showMapBtnText = realtor_showMapBtn.down().innerHTML;
  realtor_showMapBtn.down().update('Karte ausblenden');
  var effects = new Array();
  effects.push( new Effect.Morph( 'page', { style:{ paddingBottom: '380px' }, sync: true } ));
  effects.push( new Effect.Move( 'page', { x:0, y: 380, mode: 'absolute', sync: true } ));
  effects.push( new Effect.Move( 'aside', { x:0, y: -380, mode: 'absolute', sync: true } ));
  effects.push( new Effect.Opacity( 'mapLarge', { to: 1, sync: true } ));
  effects.push( new Effect.Opacity( $('mapLarge').down('.mapControls'), { to: 1, sync: true } ));
  new Effect.Parallel( effects, { duration: 0.6, afterFinish: showMapComplete } );
}

function showMapComplete()
{
  realtor_mapLargeStatus = 'visible';
  var map = realtor_maps['mapLarge'];
  map.enableDoubleClickZoom();
  map.enableDragging();
  map.enableInfoWindow();
  map.enableScrollWheelZoom();
}

function hideMap()
{
  if (realtor_mapLargeStatus != 'visible') return;
  realtor_mapLargeStatus = 'changing';
  //
  var map = realtor_maps['mapLarge'];
  map.disableDoubleClickZoom();
  map.disableDragging();
  map.disableInfoWindow();
  map.disableScrollWheelZoom();
  //map.panTo( getMapCenter('mapLarge') );
  //
  $('container').removeClassName('mapIsVisible');
  var effects = new Array();
  effects.push( new Effect.Morph( 'page', { style:{ paddingBottom: '0px' }, sync: true } ));
  effects.push( new Effect.Move( 'page', { x:0, y: 0, mode: 'absolute', sync: true } ));
  effects.push( new Effect.Move( 'aside', { x:0, y: 0, mode: 'absolute', sync: true } ));
  effects.push( new Effect.Opacity( 'mapLarge', { to: 0.5, sync: true } ));
  effects.push( new Effect.Opacity( realtor_showMapBtn, { to: 0, sync: true } ));
  effects.push( new Effect.Opacity( $('mapLarge').down('.mapControls'), { to: 0, sync: true } ));
  new Effect.Parallel( effects, { duration: 0.6, afterFinish: hideMapComplete } );
}

function hideMapComplete()
{
  realtor_mapLargeStatus = 'hidden';
  realtor_showMapBtn.down().update(realtor_showMapBtnText);
  realtor_showMapBtn.appear( {duration: 0.2} );
}