// zip code
var honorInterceptCookie = true;  // change to true when going live so nobody 
                                   // is invited twice (if that's desired 
                                   // behavior)
var percentToIntercept = 1.0;      // set to appropriate number based on 
                                   // desired completes, 
                                   // desired time frame to collect completes, 
                                   // percentage of 
                                   // page views that have ad served if 
                                   // applicable, 
                                   // and overall expected traffic, assuming 
                                   // that approximately 1%
                                   // of people intercepted will actually 
                                   // click through and complete
                                   // the survey
var interceptWidth = 258;          // adjust if reelzchannel.com creates 
                                   // their own intercept
var interceptHeight = 282;         // adjust if reelzchannel.com creates 
                                   // their own intercept
var interceptCookieName = "com.magid.intercept.W1000735A";
//
//  trailers = 4
//  star trek = 5
//  zip code = 6 
//  show times = 7 
//
var interceptDestinationURL = "http://d45.surveysonline.com/mrIWeb/mrIWeb.dll?I.Project=W1000735A&I.User2=6"; 
var interceptImageURL = "http://content.reelzchannel.com/assets/js/magid/intercept.png";  // need full URL to where image 
                                          // is hosted if not in 
                                          // same directory as this 
                                          // intercept.js on reelzchannel.com 
                                          // web server
var interceptBlankURL = "http://content.reelzchannel.com/assets/js/magid/blank.html";     // need full URL to where blank.html 
                                          // is hosted if not in 
                                          // same directory as this 
                                          // intercept.js on reelzchannel.com 
                                          // web server

// upper left x, upper left y, lower right x, lower right y (x1,y1,x2,y2)
var acceptInterceptButtonCoordinates = "65,210,191,228";
var declineInterceptButtonCoordinates = "95,247,161,267";


//
// adjust Y coordinate position, speed of flyin/flyout, and pause before flyin  
// adjust these if want intercept at different location on page
//
var topOffset = 50;
var flyInStep = 30;
var flyOutStep = 100;
var flyInterval = 1;
var pauseBeforeFlyIn = 1000;
var stopX = -1; // set to -1 to fly in to calculated middle of window



//
// calculated values and working variables
// do not modify
//
var initialLeft = -2 * interceptWidth;
var currentLeft = initialLeft;
var isNetscape = false;
var isInternetExplorer = false;
var isUnknownUserAgent = false;
var interceptWasShown = false;
var interceptTimer;

var suppressOneDismissal = false;

var browserName = navigator.appName;

if ( browserName == "Netscape" ) {
  isNetscape = true;
} else {
  if ( browserName == "Microsoft Internet Explorer" ) {
    isInternetExplorer = true;
  } else {
    isUnknownUserAgent = true;
  }
}

function calculateStopPoint() {

  // default in case unknown browser
  var windowWidth = 1024;

  if ( isNetscape ) {
    windowWidth = window.innerWidth;
  }

  if ( isInternetExplorer ) {

    windowWidth = document.body.offsetWidth;
  }

  // if stopX isn't set to an explicit value, calculate middle
  // of page to stop it at.
  if ( stopX == -1 ) {
    stopX = parseInt( ( windowWidth - interceptWidth ) / 2, 10 );
  }
}


function hideIntercept() {
  document.getElementById( "floatingIntercept" ).style.display = "none";
  document.getElementById( "floatingInterceptIframe" ).style.display = "none";
}


function moveIntercept( maxLeft, step, hideWhenDone ) {

  window.clearTimeout( interceptTimer );

  if ( currentLeft < maxLeft ) {

    currentLeft = currentLeft + step;

    if ( currentLeft > maxLeft ) {
      currentLeft = maxLeft;
    }

    document.getElementById( "floatingIntercept" ).style.left =
      currentLeft + "px";

    document.getElementById( "floatingInterceptIframe" ).style.left =
      currentLeft + "px";

    if ( currentLeft < maxLeft ) {

      interceptTimer =
        window.setTimeout(
          "moveIntercept( " +
          maxLeft +
          ", " +
          step +
          ", " +
          hideWhenDone +
          " )",
          flyInterval
        );

    } else {

      if ( hideWhenDone ) {
        hideIntercept();
      }

    }
  }
}

function flyOutIntercept() {
  if ( ! suppressOneDismissal ) {
    moveIntercept( 2000, flyOutStep, true );
  } else {
    suppressOneDismissal = false;
  }
}

function writeInterceptCookie( value, expires, remove ) {

  var now = new Date();
  var expiration_date;

  if ( remove ) {

    expiration_date =
      new Date((new Date()).getTime() - ((1*24)*3600000));

  } else {

    expiration_date =
      new Date((new Date()).getTime() + ((expires*24)*3600000));

  }

  expiration_date = expiration_date.toGMTString();

  document.cookie =
    interceptCookieName +
    "=" +
    value +
    "; path=/; expires=" +
    expiration_date;
}

function getInterceptCookie( cookieName ) {

  var cookieValue = "";

  var allCookies = document.cookie;

  var cookiePosition = allCookies.indexOf( cookieName + '=' );

  if ( cookiePosition != -1 ){

    var valueStart = cookiePosition + cookieName.length + 1;

    var valueEnd = allCookies.indexOf( ';', valueStart );

    if ( valueEnd == -1 ) {

      valueEnd = allCookies.length;

    }

    cookieValue = allCookies.substring( valueStart, valueEnd );

    cookieValue = unescape( cookieValue );

  }

  return cookieValue;
}

function displayIntercept() {

  var interceptCookie = getInterceptCookie( interceptCookieName );

  if ( honorInterceptCookie && interceptCookie == "true" ) {
    interceptWasShown = true;
  }

  writeInterceptCookie( true, 30, false );

  if ( ! interceptWasShown ) {

    // only show if within random percentage of people we wish to intercept
    if ( Math.random() <= percentToIntercept ) {

      var bodyHtml = document.body.innerHTML;
      var zMatches = bodyHtml.match( /z[\-]index[\:][\ ]*\d+[\ ]*[\;]/ig );
      var zMax = 1000;
      
      if ( zMatches != null ) {
        for ( var i = 0; i < zMatches.length; i++ ) {
          var zMatch = zMatches[ i ];
          var z = zMatch.match( /\d+/ )[ 0 ];
          if ( z > zMax ) zMax = z + 1;
        }
      }

      var interceptDiv = 
        "<iframe " +
        "  id=\"floatingInterceptIframe\" " +
        "  style=\"" +
        "   position: absolute; " +
        "   z-index: " + zMax++ + ";" +
        "   width: " + interceptWidth + ";" +
        "   height: " + interceptHeight + ";" +
        "   left: " + initialLeft + "px;" +
        "   top: " + topOffset + "px;\"" +
        "  src=\"" + interceptBlankURL + "\" " +
        "  marginheight=\"0\" " +
        "  marginwidth=\"0\" "  +
        "  noresize " +
        "  frameborder=\"0\""  +
        "  scrolling=\"no\">" +
        "</iframe>";

      interceptDiv += 
        "<div id=\"floatingIntercept\" " +
        "style=\"position: absolute; z-index: " + zMax + "; left: " +
        initialLeft +
        "px; top: " +
        topOffset +
        "px; border: 0; background-color: transparent;\" >";

      interceptDiv += 
        "<img src=\"" +
        interceptImageURL +
        "\" width=\"" +
        interceptWidth +
        "\" height=\"" +
        interceptHeight +
        "\" border=\"0\" alt=\"\" usemap=\"#interceptMap\" >";

      interceptDiv += "<map name=\"interceptMap\">";

      // decline intercept button
      interceptDiv += 
        "<area shape=\"rect\" alt=\"\" coords=\"" +
        declineInterceptButtonCoordinates +
        "\" href=\"javascript:flyOutIntercept();\" />";

      // accept intercept button
      interceptDiv += 
        "<area shape=\"rect\" alt=\"\" coords=\"" +
        acceptInterceptButtonCoordinates +
        "\" href=\"" +
        interceptDestinationURL +
        "\" onclick=\"hideIntercept()\" target=\"_new\" />";

      // default, do nothing, areas listed above take precedence
      interceptDiv += 
        "<area shape=\"rect\" alt=\"\" coords=\"0,0," +
        interceptWidth +
        "," +
        interceptHeight +
        "\" nohref onclick=\"suppressOneDismissal = true;\" />";

      interceptDiv += "</map>";

      interceptDiv += "</div>";

      var dummyDiv = document.createElement( "div" );
      
      dummyDiv.innerHTML = interceptDiv;
      
      document.body.appendChild( dummyDiv );

      calculateStopPoint();

      interceptTimer =
        window.setTimeout(
          "moveIntercept( " +
          stopX +
          ", " +
          flyInStep +
          ", " +
          false +
          " )",
          pauseBeforeFlyIn
        );

      interceptWasShown = true;
      
      if ( document.addEventListener )
        document.addEventListener( "click", flyOutIntercept, false );
      
      if ( document.attachEvent )
        document.attachEvent( "onclick", flyOutIntercept );
      
    } else {
      if ( debugMode ) {
        alert( 
          "You weren't picked to be part of the random intercept sample.\n" +
          "Current intercept rate set at " + 
          ( percentToIntercept * 100 ) +
          "%\n\n" +
          "Now that you have visited this site once, you will not be " +
          "considered a candidate to be intercepted unless you remove the " +
          "cookie named: " +
          interceptCookieName +
          " which has now been placed into your browser.\n\n" +
          "NOTE:  This message will not show in production mode"
        );
      }
    }
  } else {
    if ( debugMode ) {
      alert( 
        "You have already visited this site so the intercept will not " +
        "be displayed unless you remove the cookie named: " +
        interceptCookieName +
        " from your browser.\n\n" +
        "You may not have been presented an intercept on your " +
        "initial visit to this site depending on whether you were " +
        "selected as part of the random sample, which is currently set to " +
        ( percentToIntercept * 100 ) +
        "%.\n\n" +
        "NOTE:  This message will not show in production mode"
      );
    }
  }
}

function interceptStart() {
/*
  try {
    var temp = "";
    if ( document.body != null ) {
      temp = document.body.innerHTML;
    }
    if ( temp != "" && document.readyState ) {
      if ( document.readyState != "complete" ) {
        temp = "";
      }
    }
    if ( temp != "" ) {
      displayIntercept();
    } else {
      window.setTimeout( interceptStart, 100 );
    }
  } catch ( e ) {
    window.setTimeout( interceptStart, 100 );
  }
*/
}

// interceptStart();
