Page.obamaUrlFrame="";
if (window.User_Details) {
  Page.obamaUrlFrame=window.User_Details.obamaFrame;
 }

function loadObamaFrame() {
  if (Page.obamaUrlFrame) {
    var el=document.getElementById('extraIncludeIframe');
    if (el) {
      el.src=Page.obamaUrlFrame;
    }
  }
}

function obamaIframeLoad() {
  var elc=document.getElementById('extraIncludeIframeContainer');
  elc.style.display="block";
  if (!window.flashElementsOnPage) {
    window.flashElementsOnPage=["extraIncludeIframeContainer"];
  } else {
    flashElementsOnPage.push("extraIncludeIframeContainer");
  }
}


function checkDisplayPre() {
  var now=new Date().valueOf();
  // if the difference between now and the start time is less than 1
  // minute load the no-cache page, this shouldn't happen, but could
  // in weird situations.  5 second delay in doing it, since this
  // could end in a reload loop if this is buggy somewhere.
  if ((Page.startTime-now)<60*1000) {
    setTimeout(function() {
                 location="/Obama_Inauguration_Live?nocache=1";
               },5000);
    return;
  } else {
    //  set a time out to reload the page 5 minutes before the start
    //  of the event as timed by the "time to live" of the loading.
    setTimeout(function() {
                 location="/Obama_Inauguration_Live?nocache=1";
               },Page.timeToLive-5*60*1000);
  }
}

var swapTime=60*1000+Math.random()*1*60*1000;
function checkDisplay() {
  var now=new Date().valueOf();
  if ((Page.startTime-now)<swapTime) {
    var obamaLiveEl=document.getElementById('obamaLive');
    if (obamaLiveEl) {
      analyticsTrackEvent("start_obama_live");
      obamaLiveEl.style.display="block";
      obamaStartPlaying();
      var obamaCountdownEl=document.getElementById('obamaCountdown');
      if (obamaCountdownEl) {
        obamaCountdownEl.style.display="none";
        for (var i=0;i<window.flashElementsOnPage.length;i++) {
          if (window.flashElementsOnPage[i]=="obamaCountdownFlash") {
            window.flashElementsOnPage.splice(i,1);
            break;
          }
        }
      }
      loadObamaFrame();
      setTimeout(loadEndUpdateCheck,30000);
    } else {
      // no live element found, how did I get here?!
      // load the nocache page as it will now be during the event.
      setTimeout(function() {
                   location="/Obama_Inauguration_Live?nocache=1";
                 },5000);
    }
    return;
  } else {
    checkDisplayTimer=setTimeout(checkDisplay,10000);
  }
}

function loadEndUpdateCheck() {
  // url needs to be completely not cached.
  // doesn't use a callback, but simply executes the code returned.
  var url="/obamalive.js?t="+new Date().valueOf();
  jsonRequest(url);
}

function hideVideo() {
  analyticsTrackEvent("end_obama_live");
  var obamaPostEl=document.getElementById('obamaPost');
  if (obamaPostEl) {
    obamaPostEl.style.display="block";
  } else {
    // no post element found, how did I get here?!
    // load the nocache page as it will now be after the event.
    setTimeout(function() {
                 location="/Obama_Inauguration_Live?nocache=1";
               },5000);
    return;
  }
  var obamaLiveEl=document.getElementById('obamaLive');
  obamaLiveEl.innerHTML="";
}

core.plugin.events.onVideoStart.push(function(vid) {
                                       failureCount=0;
    });
core.plugin.events.onFailure.push(obamaError);

function updateObamaFrame(newUrl) {
  if (Page.obamaUrlFrame!=newUrl) {
    Page.obamaUrlFrame=newUrl;
    if (newUrl=="") {
      // hide the iframe
      var elc=document.getElementById('extraIncludeIframeContainer');
      if (elc) {
        elc.style.display="none";
      }
      var el=document.getElementById('extraIncludeIframe');
      if (el) {
        el.src="/blank.html";
        el.onload=function() { };
      }
    } else {
      var elc=document.getElementById('extraIncludeIframeContainer');
      if (elc) {
        elc.style.display="block";
      }
      loadObamaFrame()
    }
  }
}

failureCount=0;
obamaTimeout=0;
function obamaError(err) {
  if (failureCount++<8) {
    clearTimeout(obamaTimeout)
    obamaTimeout=setTimeout(function() {
                 core.plugin.jPlug.playVideo(Page.obamaVideo);
               },5000);
  } else {
    // 10 failures, fail for real.
    analyticsTrackEvent("obama_live_fail");
    var obamaLiveEl=document.getElementById('joostplugin');
    if (obamaLiveEl) {
      obamaLiveEl.style.display="none";
    }
    var obamaFailEl=document.getElementById('obamaFailure');
    if (obamaFailEl) {
      obamaFailEl.style.display="block";
    }
    setTimeout(function() {
                 location.reload(true);
               },60000);
  }
}

function obamaStartPlaying() {
  var p = document.getElementById('joostplugin');
  if (p) {
    if (!window.flashElementsOnPage) {
      window.flashElementsOnPage=["joostplugin"];
    } else {
      flashElementsOnPage.push("joostplugin");
    }
    // the plugin is there, this must be ROW so...
    core.plugin.onReady();
    if (!core.plugin.loaded()) {
      clearTimeout(obamaTimeout);
      obamaTimeout=setTimeout(obamaStartPlaying,1000);
      return;
    }
    core.plugin.jPlug.playVideo(Page.obamaVideo);
  } else {
    clearTimeout(obamaTimeout);
    obamaTimeout=setTimeout(obamaStartPlaying,1000);
  }
};
