$(document).ready(function() {
  //$("#header").height(210);
  var flashvars = {
    mapState: "collapsed",
    state: "collapsed",
    q: "",
    isAdmin: false,
    basePath: "",
    themePath: ""
  };
  var params = {
    quality: "High",
    wmode: "transparent",
    menu: "false"
  };
  //swfobject.embedSWF('/shell.swf', 'flashMap', '901', '64', '10.0.0', '', flashvars, params, {id: "flashMap", name: "flashMap"});
  swfobject.embedSWF('/shell.swf', 'flashMap', '1024', '64', '10.0.0', '', flashvars, params, {id: "flashMap", name: "flashMap"});
});

$(window).load(function() {
  // Use the mouse when we're over flash, stop it from scrolling too
  /*
  $("#flashMap").mousewheel(function(e, delta) {
    var map = swfobject.getObjectById('flashMap');
    if(typeof(map.externalMouseEvent) == 'function' ) {
      map.externalMouseEvent(delta*15);
      e.preventDefault();
    }
  });
  */
  
  // Let's pre-load the waiting blip just in case we need it
  $waiter = $('<div id="mapWaiter"></div>')
  var waiterImg = new Image();
  waiterImg.src = '/loadingIcon.gif';
  $waiter.append(waiterImg);
});

/****************************************************
 *************** Map Interactions *******************
****************************************************/
var waitingForOMDMap = true;
var $waiter;

/**
 * Focus on the map and show the specified lot
**/
function clickMapNav(type) {
  var offsety = $(window).scrollTop();
  if(offsety > 0) {
    $.scrollTo(0, offsety, function() {
      tryMapFunction('clickMapNav', type);
    });
  } else {
    tryMapFunction('clickMapNav', type);
  }
}
function showLotOnMap(lotURL) {
  var offsety = $(window).scrollTop();
  if(offsety > 0) {
    $.scrollTo(0, offsety, function() {
      tryMapFunction('showLotOnMap', lotURL);
    });
  } else {
    tryMapFunction('showLotOnMap', lotURL);
  }
}

function tryMapFunction(func, param) {
  var map = swfobject.getObjectById('flashMap');
  if(map && map[func] != undefined) {
    map[func](param);
    
    // If we're waiting for the map, show the little html notice
    if(waitingForOMDMap) {
      $waiter.css({position: 'absolute', top: '0px', left: '40px', zIndex: '100'});
      $("#flashMapContainer").prepend($waiter);
      $waiter.fadeIn(400);
    }
  } else {
    var t = setTimeout('tryMapFunction("' + func + '", "' + param + '");', 10);
  }
}

function onMapReady() {
  if(waitingForOMDMap) {
    $('#mapWaiter').stop().fadeOut(400);
    waitingForOMDMap = false;
  }
  waitingForMap = false;
}

/**
 * Expand and Collapse the map from within flash
**/
function expandMap() {
  $("#flashMap").height(455);
  $("#flashMapContainer").stop().animate({height: "455px"}, 600);
}
function collapseMap() {
  $("#flashMapContainer").stop().animate({height: "64px"}, 600, function () { $("#flashMap").css('height', '64px'); });
}

function log(s) {
    console.log(s);
}

