/**
 * Utility method used to standardize how a swf is referenced in the js
 */
function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

/**
 * Used to mark a link as active on hover of map items in the swf
 * Using the building_slug parameter, it will look through the map_group list and
 * the building list for any href's that contain the slug, and will mark that link as
 * the active link.
 */
function activateBuildingLinkFromSwf(building_slug) {
  
  // first, deactivate all other building links
  // deactivateAllBuildingLinks();
  
  // now, look for any building links that contain the slug, marking them as active
  // if we wanted to add the map_group links then #secondNav a[href*='+building_slug+'] would be the query
  // However, if we do both the #white links and the #secondNav links, the flash gets glitchy
  
  // special case: the quad for some reason catches the towers, since that name contains quad... even when using $= ? bug in jquery?
  //if(building_slug == 'quad') return;
  
  jQuery.each(jQuery('#white .column a[href$='+building_slug+']'), activateBuildingLinkFromJs);
}

/**
 * Used to mark a link as in-active on hover-out of map items in the swf
 * Using the building_slug parameter, it will look through the map_group list and
 * the building list for any href's that contain the slug, and will mark that link as
 * an in-active link.
 */
function deactivateBuildingLinkFromSwf(building_slug) {
  // make an ajax call to get the value for the map-label (near the top left of the map)
  jQuery('#mapLabelHolder .item p').html('');
  
  // now, look for any building links that contain the slug, marking them as inactive
  jQuery('#white .column a[href$='+building_slug+']').removeClass('current');
}

/**
 * Used to send the browser to the url of the link that matches the building name given from the swf
 */
function clickBuildingLinkFromSwf(building_slug) {
  // get the first link that has the slug in the href attribute
  var links = jQuery('#white .column a[href$='+building_slug+']');
  var link = links[0];
  if(link == undefined) return false;
  // strip the GET params from the url
  var href = link.href;
  href = href.split('?');
  window.location = href[0];
}

/**
 * called to make the calls to mark a link in the html as active
 * as called from the js
 * @param event
 */
function activateBuildingLinkFromJs(event, target) {
  
  var hover_over_html_link = true;
  // if the call is from a jQuery().click() callback, then we have a 'target' defined (the a-link in this case probably)
  // if the call is from a jQuery().each() callback, then the second argument typeof == 'undefined'
  if(typeof target == 'undefined') {
    hover_over_html_link = false;
  }
  
  // make sure we only process links with href attributes set.
  if(this.href == undefined) return false;
  
  // break out the buildings from the url
  var params = {};
  var get_vars = this.href.split('?');
  if(get_vars.length > 1) {
    var get_vars = get_vars[1].split('&');
    for(i in get_vars) {
      var tmp = get_vars[i].split('=');
      if(tmp.length > 1) {
        params[tmp[0]] = tmp[1];
      }
    }
  }
  
  // if we have a 'bldgs' GET param, use it to activate the link
  if(typeof params.bldgs != "undefined") {

    // deactivate all existing links that are active
    deactivateAllBuildingLinks();

    // make an ajax call to get the value for the map-label (near the top left of the map)
    jQuery('#mapLabelHolder .item p').html(jQuery(this).html());

    // call our flash movie to update the highlighted items there
    getFlashMovie('mapSwf').swfActivateBuildings(params.bldgs);


    // for caudell hall / annex.
    // though technically, this is being implimented in a way that is not a special case, this allows
    // us to give different actions to the page depending if the user hovers over a link in the map or in the html
    if(hover_over_html_link) {
      // this activates all matching slug-links (e.g. caudell hall and annex)
      jQuery('#white .column a[href$='+params.bldgs+']').addClass('current');
    } else {
      // activate the current link only
      jQuery(this).addClass('current');
    }
  }
  //return false;
}

/**
 * Take one link, and activate it so it communicates with flash and the map label correctly.
 */
function initBuildingLinks(index) {
  jQuery(this)
    .mouseover(activateBuildingLinkFromJs)
    .mouseout(deactivateAllBuildingLinks);

}

/**
 * deactivate all existing links that are active
 */
function deactivateAllBuildingLinks() {
  // make an ajax call to get the value for the map-label (near the top left of the map)
  jQuery('#mapLabelHolder .item p').html('');
  
  // call our flash movie to update the highlighted items there
  getFlashMovie('mapSwf').swfDeactivateBuildings();
  
  // mark html links as inactive everywhere
  jQuery('#white .column a.current, #secondNav a.current').removeClass('current');
}


/**
 * Called from *somewhere*.
 * At the time of this definition, it's being made from the SWF, using ExternalInterface.call('initFlashLink')
 * Could be ExternalInterface.call(), swfobject.embedSWF callback, or jQuery(document.ready)
 *
 * Went with the ExternalInterface.call() to make sure actionscript was available first, since our js will call into it.
 */
function initFlashLinks() {
  // activate the link actions for the interaction with the flash map
  jQuery.each(jQuery('#white .column a[class!=other], #secondNav a'), initBuildingLinks);
  
  // to smooth things out, turn off the click actions for the #secondNav links
  jQuery('#secondNav a').click(function(){return false;});
}

//jQuery(document).ready(initFlashLinks);