/// <reference path="jquery-1.3.2.min-vsdoc.js" />
var map = null;

jQuery(document).ready(function($) {

  $('#lot_body .wide').columnize({
    columns: 2,
    buildOnce: true,
    lastNeverTallest: true
  });
  
  if ($('#lot_map').is('*')) loadMap();

  $('#fullWidthButton').click(function() {
    if ($('#lot_visuals .photo').is(':visible')) {
      map.Resize(804, 306);
    } else {
      map.Resize(176, 306);
    }
    return false;
  });

});

function OnResize(e) {
  if ($('#lot_visuals .photo').is(':visible')) {
    $('#lot_visuals .photo, #lot_visuals ul').hide();
    $('#fullWidthButton').css('width', '796px').css('text-align','right').html('Small Map &#187;');
  } else {
    $('#lot_visuals .photo, #lot_visuals ul').show();
    $('#fullWidthButton').css('width', '160px').css('text-align','left').html('&#171; Full Width');
  }
}

var failedFullAddress = false;

function loadMap() {
  map = new VEMap('lot_map');
  map.LoadMap();
  map.AttachEvent("onresize", OnResize);
  try {
    map.Find(null, property_address, null, null, null, 1, null, null, false, true, locationFound);
  }
  catch (e) {
    //alert(e);
  }
}

function locationFound(layer, resultsArray, places, hasMore, veErrorMessage) {
  if (veErrorMessage) {
    if (!failedFullAddress) {
      failedFullAddress = true;
      try {
        map.Find(null, property_postcode, null, null, null, 1, null, null, false, true, locationFound);
      }
      catch (e) {
        //alert(e);
      }
    } else {
      //map.LoadMap(new VELatLong(53.76056527411437,-2.746550515294075), 14, VEMapStyle.Road, false, VEMapMode.Mode2D, false, 1);
      map.Dispose();
      $('#lot_map').css('line-height', '300px');
      $('#lot_map').css('font-size', '12px');
      $('#lot_map').css('color', '#AAAAAA');
      $('#lot_map').css('text-align', 'center');
      $('#lot_map').html('Map Unavailable');
      $('#fullWidthButton').hide();
    }
  } else {
    map.AddPushpin(map.GetCenter());
  }
  $('#lot_map').css('visibility', 'visible');
}
