var buildings = {
  'building-1': {
    name: 'Building 1',
    height: 800,
    x: 615,
    y: 215
  },
  'building-2': {
    name: 'Building 2',
    height: 680,
    x: 485,
    y: 135
  },
  'building-3': {
    name: 'Building 3',
    height: 830,
    x: 425,
    y: 275
  },
  'building-4': {
    name: 'Building 4',
    height: 690,
    x: 525,
    y: 410
  },
  'building-5': {
    name: 'Building 5',
    height: 830,
    x: 315,
    y: 389
  }
}

function loadPropertyMap() {
  $('#property-map-image').animate({
    width: '940px',
    height: '600px',
    left: '0px',
    top: '0px'
  }, 1500);

  $('.sidebar').hide();
  $('.content').hide();
  $('#default-sidebar').show();

  $('.img-ttl strong').html('Property Map');
  $('.img-ttl span').html('Click a building to view the current tenants or available space');
  $('#property-img-area .img-box .map-building').fadeOut(1500);
  $('#property-img-area .img-box').animate({
    height: '600px'
  }, 1500);
  $('#property-img-area .img-ttl').animate({
    top: '600px'
  }, 1500);
}

function loadBuilding(building) {
  $('#property-map-image').animate({
    width: '1880px',
    height: '1200px',
    top: '-'+buildings[building].y+'px',
    left: '-'+buildings[building].x+'px'
  }, 1500).fadeOut(1500);
  $('#'+building).fadeIn(1800);

  $('#default-sidebar').hide();
  $('.sidebar').hide();
  $('.content').hide();

  $('#'+building+'-sidebar').show();
  $('#'+building+'-content').show();

  $('#property-img-area .img-box').animate({
    height: buildings[building].height + 'px'
  }, 1500);

  $('.img-ttl strong').html(buildings[building].name + ' <a href="#show-'+building+'-first-floor" id="'+building+'-first-floor-link" class="first-floor-link">First Floor</a>' + ' <a href="#show-'+building+'-second-floor" id="'+building+'-second-floor-link" class="second-floor-link">Second Floor</a>');
  $('.img-ttl span').html('<a href="property-map.html#map"><img src="images/back-arrow.png" class="btn-back" /></a>');

  $('#property-img-area .img-ttl').animate({top: buildings[building].height + 'px'}, 1500);

  setTimeout(function () {
  }, 1600)
}

function loadBuildingFloor(building, floor) {
  $('table.building-info').hide();
  $('.first-floor-link').removeClass('current-first-floor-link');
  $('.second-floor-link').removeClass('current-second-floor-link');

  $('#'+building+'-'+floor+'-floor').show();
  $('#'+building+'-'+floor+'-floor-link').addClass('current-'+floor+'-floor-link');
}

var currentAnchor = null;
var previousAnchor = null;

function checkAnchor(){  
  if (currentAnchor != document.location.hash) {
    previousAnchor = currentAnchor;
    currentAnchor = document.location.hash;

    if (currentAnchor && currentAnchor.substring(1).split('&')[0] == 'map') {
      loadPropertyMap();
    }

    if (currentAnchor && currentAnchor.match(/^#show-building/)) {
      var currentFloor = currentAnchor.substring(1).split('&')[0];

      if (previousAnchor && currentFloor.match(previousAnchor.substring(1).split('&')[0])) {
        loadBuildingFloor(previousAnchor.substring(1).split('&')[0], currentFloor.replace(/show-building-\d+-(first|second)-floor/, '$1'));
      } else {
        currentBuilding = currentFloor.replace(/^show-(.*)-(first|second)-floor/, '$1')

        loadBuilding(currentBuilding);
        loadBuildingFloor(currentBuilding, currentFloor.replace(/show-building-\d+-(first|second)-floor/, '$1'));
      }
    }

    if (currentAnchor && currentAnchor.match(/^#building-/)) {
      var currentBuilding = currentAnchor.substring(1).split('&')[0];

      for (building in buildings) {
        if (building == currentBuilding)
          setTimeout(function () {
            loadBuilding(currentBuilding);
            loadBuildingFloor(currentBuilding, 'first');
          }, 300);
      }
    }
  }
}

$(function(){
  $('.building').mouseover(function() {
    $('#property-map-image').attr('src', 'images/property-map-images/property-map-' + $(this).attr('class').split(' ')[1] + '.gif');
  });

  $('.building').mouseout(function() {
    $('#property-map-image').attr('src', 'images/property-map-images/property-map.gif');
  });

  setInterval("checkAnchor()", 300);  
});
