// supports dynamic flash-over-html menubar divs

var allMenus = new Array('sports','athletes','media');

function popMenu(whichmenu) {
  printStatus('pop req '+whichmenu);
  if (!isVisible(whichmenu)) {
    hideAll();
    var menuDiv = document.getElementById('menu_'+whichmenu);
    var xy = getPosXY('positioner_'+whichmenu);
    var movie = grokMovie(whichmenu);
    queuemovie(movie);
    menuDiv.style.left = xy.x;
    menuDiv.style.top = xy.y;
    menuDiv.style.visibility = 'visible';
    playmovie(movie);
    printStatus('popped '+whichmenu);
  }
}

function popMailing() {
  if (!isVisible('mailing')) {
    hideAll();
    var menuDiv = document.getElementById('menu_mailing');
    var xy = getPosXY('positioner_mailing');
    menuDiv.style.left = xy.x + 5;
    menuDiv.style.top = xy.y;
    menuDiv.style.visibility = 'visible';
  } else {
    hideAll();
  }
}

function grokMovie(whichmenu) {
  if (whichmenu == 'sports') {
    return theMovie('menu1');
  } else if (whichmenu == 'athletes') {
    return theMovie('menu2');
  } else if (whichmenu == 'media') {
    return theMovie('menu3');
  } else {
    return theMovie('menu1'); // default to sports
  }
}

function updateMenus(callNum) {
  for (var i=0; i<allMenus.length; i++) {
    if (isVisible(allMenus[i])) {
      var menuObj = document.getElementById('menu_'+allMenus[i]);
      var xy = getPosXY('positioner_'+allMenus[i]);
      menuObj.style.left = xy.x;
      menuObj.style.top = xy.y;
    }
  }
  // is there a floating left nav menu on this page?
  // if so, it also needs to shift on a window resize event.
  if (typeof updateFloater == 'function') { updateFloater(); }

  if (callNum < 4) {
    // fix it again in two seconds
    setTimeout("updateMenus("+(callNum+1)+")",2000);
  }
}

function hideMenu(whichmenu) {
  var menuObj = document.getElementById('menu_'+whichmenu);
  menuObj.style.visibility = 'hidden';
  printStatus('hid '+whichmenu);
}

function printStatus(msg) {
  if (document.forms.debug && document.forms.debug.elements.status) {
      document.forms.debug.elements.status.value = msg + '\n' + document.forms.debug.elements.status.value;
  }
}

function hideAll() {
  for (var i=0; i<allMenus.length; i++) {
    var menuObj = document.getElementById('menu_'+allMenus[i]);
    menuObj.style.visibility = 'hidden';   
  }
}

function isVisible(whichmenu) {
  var menuObj = document.getElementById('menu_'+whichmenu);
  if (menuObj == undefined) return false;
  if (menuObj.style.visibility == 'visible') return true;
  return false;
}

// get the true offset of anything on NS4, IE4/5 & NS6, even if it's in a table!
function getAbsX(elt) { return (elt && elt.x) ? elt.x : getAbsPos(elt,"Left"); }
function getAbsY(elt) { return (elt && elt.y) ? elt.y : getAbsPos(elt,"Top"); }
function getAbsPos(elt,which) {
 iPos = 0;
 depth = 0;

 while (elt != null) {
  iPos += elt["offset" + which];
  elt = elt.offsetParent;
  // don't recurse forever -- this works around weird out of memory errors in MSIE
  depth++;
  if (depth > 10) { return iPos; }
 }
 return iPos;
}

function getPosXY(marker) {
  var coord = new Object();
  var item = (typeof(marker) == 'string') ? document.getElementById(marker) : marker;
  coord.x = parseInt(getAbsX(item));
  coord.y = parseInt(getAbsY(item));
  return coord;
}

function theMovie(movieName) {
  // IE and Netscape refer to the movie object differently.
  // This function returns the appropriate syntax depending on the browser.
  if (navigator.appName.indexOf ("Microsoft") !=-1) {
    return window[movieName]
  } else {
    return document[movieName]
  }
}

// Checks if movie is completely loaded.
// Returns true if yes, false if no.
function movieIsLoaded (theMovie) {
  if (typeof(theMovie) != "undefined") {
    return theMovie.PercentLoaded() == 100;
  } else {
    return false;
  }
}

var loadingMenuMovie;

function playmovie(movieObj) {
  if (movieIsLoaded(movieObj)) {
    movieObj.Play();
    if (movieObj.signalShow) {
      movieObj.signalShow();
    } else {
      // sometimes the callback doesn't register right away.
      // try signal again in 1/5 second
      loadingMenuMovie = movieObj;
      setTimeout('if(loadingMenuMovie.signalShow){loadingMenuMovie.signalShow();}',200);
    }
  }
}

function queuemovie(movieObj) {
  if (movieIsLoaded(movieObj)) {
    movieObj.StopPlay();
    movieObj.GotoFrame(0); // really frame 1 since this fn starts counting at zero
  }
}

function fadeMenu(movieName) {
  var movie = theMovie(movieName);
  // flash callback 'signalFade' should be registered
  if (movieIsLoaded(movie) && movie.signalFade) {
    movie.signalFade();
    printStatus('signalled fade '+movieName);
  }
  return true;
}

function isImageOk(img) {
    // During the onload event, IE correctly identifies any images
    // that weren't downloaded as not complete. Others should too.
    // Gecko-based browsers act like NS4 in that they report this
    // incorrectly: they always return true.
    if (!img.complete) {
        return false;
    }

    // However, they do have two very useful properties: naturalWidth
    // and naturalHeight. These give the true size of the image. If
    // it failed to load, either of these should be zero.
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    // No other way of checking: assume it's ok.
    return true;
}


// this workaround is for MSIE 7 with multiple movies cleanup bug
// (out of memory line 56) in flash player 9
function nullFlashLoopFunction() { __flash_savedUnloadHandler = null; }
window.onbeforeunload = nullFlashLoopFunction;


// for greying out the screen - nice!
function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}
