// predictorJavaScript.js contains the javascript for updating the banner ads
// and the predictions for the Netscape & MSIE 4.0 fancy version of the website.

var predictionURL;
var extraQueryString = "";
var adURL                 = "ad.shtml";
var MSIEAlternateAdURL    = "ad2.shtml";
var adInterval            = 180000;      // In msec
var maxAdInterval         = 360000;      // In msec
var maxPredictionInterval = 60000;  // In msec


function isAtLeast40Browser() {
  return parseInt(navigator.appVersion.charAt(0)) >= 4;
}

function is40Browser() {
  return parseInt(navigator.appVersion.charAt(0)) == 4;
}


function isNavigator4() {
  return navigator.appName == "Netscape" && is40Browser();
}


function isAtLeastIE4() {
  return navigator.appName == "Microsoft Internet Explorer" && isAtLeast40Browser();
}


function moveAndShowLayer(layer, locLayer) {
  if (isNavigator4()) {
    layer.moveTo(locLayer.pageX, locLayer.pageY);
    layer.visibility="show";
  }
}


// Note: layerOrFrameName is a string parameter
function displayNewFile(layerOrFrameName, fileName) {
  if (isNavigator4()) {
    eval("document." + layerOrFrameName + ".src = \"" + fileName + "\"");
  } else {
    eval(layerOrFrameName + ".location = \"" + fileName + "\"");
  }
}


function onResizeCommon(predictionFileName) {
  // With Netscape 4.0, images revert to their original file when window is resized.  Therefore need to display logo again.
  displayProperLogo();

  if (isNavigator4() && initialWidth!=0 && initialHeight!=0) {
    moveAndShowLayer(document.predictionLayer, document.determineLocationLayer);

    document.predictionLayer.resizeTo(initialWidth, initialHeight);
    
    // Because resizeTo() only changes the clipping area (rather confusing, isn't it?),
    // also need to reload the page so that content is reflowed.
    document.predictionLayer.load(predictionFileName, document.predictionLayer.clip.width); 

    // Do the same for the ad layer
    moveAndShowLayer(document.adLayer, document.determineAdLocationLayer);
    document.adLayer.resizeTo(initialAdWidth, initialAdHeight);
    document.adLayer.load(adURL, document.adLayer.clip.width); 
  }
}


var initialWidth = 0;
var initialHeight = 0;

var initialAdWidth = 0;
var initialAdHeight = 0;

function onLoadCommon() {
  if (isNavigator4()) {
    // Remember the size of the layer in case onResize() is called.
    // Do this right away because onResize() seems to be called before
    // onLoad() is finished for some browsers.
    var c = document.determineLocationLayer.clip;
    initialWidth = c.right - c.left;
    initialHeight = c.bottom - c.top;

    // Move and size the layer to the proper place
    moveAndShowLayer(document.predictionLayer, document.determineLocationLayer);

    // Do the same for the ad layer
    c = document.determineAdLocationLayer.clip;
    initialAdWidth = c.right - c.left;
    initialAdHeight = c.bottom - c.top;
    moveAndShowLayer(document.adLayer, document.determineAdLocationLayer);
  }

  setInterval("makeSureAdLoopRunning()", maxAdInterval);
  setInterval("makeSurePredictionLoopRunning()", maxPredictionInterval);
}


var shouldDisplayAnotherPrediction = false;
var displayingAPrediction          = false;

var shouldDisplayAd = false;
var displayingAd    = false;

// Called when a prediction has finished loading.  Called by the onLoad() mechanism.
// This complicated stuff is to make sure display the right prediction for a stop,
// even when user quickly scrolls through the stop list.
// If another prediction has been specified but not yet loaded, then 
// shouldDisplayAnotherPrediction & predictionURL will have been set.  
// In this case, display prediction.  Otherwise remember that done displaying prediction.

function predictionLoaded() {
  displayingAPrediction = false;

  if (shouldDisplayAnotherPrediction) {
    displayPrediction();
  } else {
    // Load ad if need to
    if (shouldDisplayAd) {
      displayAd();
    }
  }
}


var lastPredictionDisplayedTimeMsec = (new Date()).getTime();

function displayPrediction() {
  if (displayingAPrediction || displayingAd) {
    shouldDisplayAnotherPrediction = true;
  } else {
    displayingAPrediction = true;
    shouldDisplayAnotherPrediction = false;

    lastPredictionDisplayedTimeMsec = (new Date()).getTime();

    // Actually display the new prediction
    displayNewFile("predictionLayer", predictionURL + extraQueryString);
	
	// Since the extraQueryString was used to tell server to write the profile
	// to the db, it is not needed anymore.
	extraQueryString = "";
  }
}


function displayLoadingPredictionMessage() {
  // Make sure don't interfere with displaying of other prediction or with ad 
  if (!displayingAPrediction && !displayingAd) {
    // With Netscape have a problem where prediction not displayed if try to
	// display it right after the display loading prediction message is displayed.
	// Therefore keep track that display a prediction so that the real prediction
	// isn't displayed until this message is fully displayed.
	displayingAPrediction = true;
	
    // Actually display the new prediction
    displayNewFile("predictionLayer", "loadingPrediction.htm");
  }
}


// Called by the prediction layer so that can set timeout.
var timeoutID = -1;
function updatePredictionAfterTime(msec) {
  if (timeoutID != -1)
      clearTimeout(timeoutID);
  timeoutID = setTimeout("displayPrediction()", msec);
}


function adLoaded() {
  displayingAd = false;

  if (shouldDisplayAd) {
    displayAd();
  } else {
    // load prediction if need to
    if (shouldDisplayAnotherPrediction) {
      displayPrediction();
    }
  }

  if (adInterval > 0)
    setTimeout("displayAd()", adInterval);
}


var lastAdURL;
var lastAdDisplayedTimeMsec =  (new Date()).getTime();

// Called every timeout to display new ad
function displayAd() {
  if (displayingAPrediction || displayingAd) {
    shouldDisplayAd = true;
  } else {
    displayingAd = true;
    shouldDisplayAd = false;

    lastAdDisplayedTimeMsec = (new Date()).getTime();

    // When MSIE gets the same JavaScript twice in a row, get the
    // same ad with commission-junction.  Therefore need to rotate
    // the ad html file.
    if (lastAdURL == adURL)
      lastAdURL = MSIEAlternateAdURL;
    else
      lastAdURL = adURL;

    // Actually display the new ad
    displayNewFile("adLayer", lastAdURL);
  }
}


// There is a bug in Netscape that when two layers load in a new file at once,
// only one of them does so successfully.  Therefore need to prevent that from
// happening.  Otherwise the ads or the predictions will not be updated anymore.
// Therefore want to determine when a layer is being updated but onLoad() doesn't
// really work for layers because it is called before the layer has been fully
// loaded.  Therefore need to have interval timers that check to make sure ad
// and prediction have been updated recently.  If not, the update loop is restarated.

function makeSureAdLoopRunning() {
  var now = new Date();
  var timeSinceLastAdMsec = now.getTime() - lastAdDisplayedTimeMsec;

  if (maxAdInterval > 0 && timeSinceLastAdMsec > maxAdInterval) {
//    alert("ad jump start");
    displayingAd = false;    // just in case it was stuck on
    displayAd();
  }
}


function makeSurePredictionLoopRunning() {
  var now = new Date();
  var timeSinceLastPredictionMsec = now.getTime() - lastPredictionDisplayedTimeMsec;

  if (timeSinceLastPredictionMsec > maxPredictionInterval) {
//    alert("prediction jump start");
    displayingAPrediction = false;    // just in case it was stuck on
    displayPrediction();
  }
}

