//Written by Jeff Robertson
//Modified by Opher Shapiro

// Ofie Intro
document.write('<div id="ta_trialoffer_ad_intro" style="visibility:hidden; position:absolute; z-index:1001; top:-500px; left:100px; width: 288px; height:298px; border:1px solid #000000;"><a href="http://www.time.com/time/europe/custserv/dyban_jump.html" target="_blank"><img src="http://i.timeinc.net/subs2/images/ta/ta_trialoffer_ad_intro_gray.gif" width="288" height="298" border="0" /></a></div>');

// Write out search area button
document.write('<div id="td_trialoffer_ad_button" style="visibility:hidden; position:absolute; top:183px; left:530px; width:245px; height:11px; background-color:#CCCCCC;" onmouseover="showOfie(1),enterOfie();" onmouseout="leaveOfie();"><a href="http://www.time.com/time/europe/custserv/dyban_jump.html" target="_blank"><img src="http://i.timeinc.net/time/homepage/images/cm_textlink.gif" width="245" height="11" border="0" name="textImg" /></a></div>');

// Div to cover up button
document.write('<div id="td_trialoffer_ad_blank" style="visibility:visible; position:absolute; top:178px; left:512px; width:296px; height:20px; background-color:#CCCCCC;"></div>');

var imgPath = "http://i.timeinc.net/subs2/images/";
var inputStyle = "font-family:Verdana,Arial,sans-serif;font-size:10px;";
var offerDetails = "After the free trial period, you'll receive 52 more issues - 56 in all - for $29.95. That's 86% off the cover price!* You'll also receive the Ultronic Hand Held organizer. If you don't want to continue with TIME after your trial period, simply return your bill marked \"cancel\" and owe nothing. Offer is valid in the US Only. *Cover price is $3.95. TIME is published weekly, except for two issues combined periodically into one and occasional extra, expanded, or premium issues. Combined, expanded, and premium issues count as two subscription issues. Subscribers: if the Post Office alerts us that your magazine is undeliverable, we have no further obligation unless we receive a corrected address within two years. All orders subject to approval. By ordering, you agree not to resell any subscriptions purchased from this site.";


var ofie1Html = '<div id="td_trialoffer_ad_ofie" style="visibility:hidden; position:absolute; z-index:1001; top:-500px; left:518px; width:288px; height:317px; border:1px solid #000000; background-color:#333333;" onmouseover="enterOfie();" onmouseout="leaveOfie();">' +
'<a href="http://www.time.com/time/europe/custserv/dyban_jump.html" target="_blank"><img src="http://i.timeinc.net/subs2/images/ta/ta_trialoffer_ad_intro_gray.gif" width="288" height="298" border="0"></a><br>'+
'<a href="javascript:leaveOfie(true)"><font face="Arial" size="2" color="#FFFFFF"><b>Close</b></font></a>'+
'</div>';
document.write(ofie1Html);


function crtnDetect(userType){ //NEWSUB NEWSSTAND MAGICDN CURRENTSUB
  var crtnString = readCookie('ITG_CUR');
  if (!crtnString) return null;
  if (userType == "CURRENTSUB" ){
    if (!crtnString.match(/NEWSUB|NEWSSTAND/)||crtnString.match(/MAGICDN/))
      return true;
  }
  else if (crtnString.indexOf(userType) > -1) return true;
  return null;
}


// COOKIE STUFF
var expireTime = new Date();
expireTime.setTime(expireTime.getTime() + (1000 * 60 * 60 * 24 * 3)); // 3 days from now

var interimTime = new Date();
interimTime.setTime(interimTime.getTime() + (1000 * 60 * 60)); // 1 hour

var now = new Date();

var introDivs = ["ta_trialoffer_ad_intro"];
var trackers = ["cmconvodropdown"];
var textImages = ["http://i.timeinc.net/time/homepage/images/cm_textlink.gif"];
var ofieDivs = ["td_trialoffer_ad_ofie"];
//var forms = [td_trialoffer_ad_form]
var gbDoIntro = false;
var theSplit = new Split(1);
  
function Split(numItems) {
  var split;
  var interimValue = readCookie('td_slide_interim'); 

  
  split = 0;
  
  this.introDiv = introDivs[split];
  this.tracker = "";//trackers[split];
  this.ofieDiv = ofieDivs[split];
  //this.ofieForm = forms[split];  
  document.images["textImg"].src = textImages[split];
  
 if (document.cookie.indexOf('td_sliding_ad') == -1)  {
	 document.cookie = "td_sliding_ad=" + split + ";expires=" + expireTime.toGMTString() + ";path=/";  
	 if (navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Win') != -1) gbDoIntro = true;	 	 
 }
 else return null;   
  
}


if (gbDoIntro && theSplit.introDiv && navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Win') != -1) { 

  window.onload = function() {
    if (typeof setUp != 'undefined') {
      setUp();
    }    
    if (typeof P_checkLoad != 'undefined') {
      P_checkLoad();
    }
    init();
  }
}
else {

  if (navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Win') != -1) init();	
  document.images["ITEM"].style.visibility = "visible";
  hide("td_trialoffer_ad_blank");
}


// DHTML FUNCTIONS
function getRawObject(obj) {
  var theObj;
  if (typeof obj == "string") {
    theObj = document.getElementById(obj);
  }
  else {
    theObj = obj;
  }
  return theObj;
}

function getObject(obj) {
  var theObj = getRawObject(obj);
  if (theObj) {
    theObj = theObj.style;
  }
  return theObj;
}

function moveTo(obj, xPos, yPos) {
  var theObj = getObject(obj);
  theObj.left = xPos + "px";
  theObj.top = yPos + "px";
}

function moveBy(obj, changeX, changeY) {
  var theObj = getObject(obj);
  theObj.left = getObjectLeft(obj) + changeX + "px";
  theObj.top = getObjectTop(obj) + changeY + "px";
}

function clipTo(obj, clipTop, clipRight, clipBottom, clipLeft) {
  var theObj = getObject(obj);
  theObj.clip = "rect(" + clipTop + "px " + clipRight + "px " + clipBottom + "px " + clipLeft + "px)";
}

function clipBy(obj, changeTop, changeRight, changeBottom, changeLeft) {
  var theObj = getObject(obj);
  theObj.clip = "rect(" + (getObjectClip(obj).top + changeTop) + "px " + (getObjectClip(obj).right + changeRight) + "px " + (getObjectClip(obj).bottom + changeBottom) + "px " + (getObjectClip(obj).left + changeLeft) + "px)";
}

function setZIndex(obj, zLevel) {
  var theObj = getObject(obj);
  theObj.zIndex = zLevel;
}

function show(obj) {
  var theObj = getObject(obj);
  theObj.visibility = "visible";
}

function hide(obj) {
  var theObj = getObject(obj);
  theObj.visibility = "hidden";
}

function getObjectLeft(obj) {
  var elem = getRawObject(obj);
  var result = elem.style.left;
  return parseInt(result);
}

function getObjectTop(obj) {
  var elem = getRawObject(obj);
  var result = elem.style.top;
  return parseInt(result);
}

function getObjectHeight(obj) {
  var elem = getRawObject(obj);
  var result = elem.offsetHeight;
  return parseInt(result);
}

function getObjectWidth(obj) {
  var elem = getRawObject(obj);
  var result = elem.offsetWidth;
  return parseInt(result);
}

function getObjectClip(obj) {
  var theObj = getObject(obj);
  var clipVals = theObj.clip.split("rect(")[1].split(")")[0].split("px");
  var clipObj = {
    top: parseInt(clipVals[0]),
    right: parseInt(clipVals[1]),
    bottom: parseInt(clipVals[2]),
    left: parseInt(clipVals[3])
  };
  return clipObj;
}

function slide(slideDiv, endX, endY, moveIncrement, clipX, clipY) {
  isSliding = true;
  var startX = getObjectLeft(slideDiv);
  var startY = getObjectTop(slideDiv);

  var changeX = startX - endX;
  var changeY = startY - endY;
  
  if (endX < startX || endY < startY) {
    moveIncrement = -moveIncrement;
  }
  
  // this next could be better, but what the hell...
  if (Math.abs(changeX) < Math.abs(moveIncrement) && Math.abs(changeY) < Math.abs(moveIncrement)) { // finish up the move
    moveTo(slideDiv, endX, endY);
    if (clipX || clipY) {
      // THIS MAY NEED TO BE FIXED
      if (!clipX) clipX = 0;
      if (!clipY) clipY = 0;
      var topClip = clipY - getObjectTop(slideDiv);
      var rightClip = (getObjectLeft(slideDiv) + getObjectWidth(slideDiv)) - clipX;
      var bottomClip = (getObjectTop(slideDiv) + getObjectHeight(slideDiv)) - clipY;
      var leftClip = clipX - getObjectLeft(slideDiv);
      clipTo(slideDiv, topClip, rightClip, bottomClip, leftClip);
    }
    window.clearInterval(slideInterval);
    isSliding = false;
    return;
  }
  
  // move in the desired direction
  var moveX = (changeX == 0) ? 0 : moveIncrement;
  var moveY = (changeY == 0) ? 0 : moveIncrement;
  moveBy(slideDiv, moveX, moveY);

  // clip, if necessary
  if (clipX) {
    // ADD SOMETHING HERE

  }
  else if (clipY) {
    if (clipY < getObjectTop(slideDiv)) {
      clipBy(slideDiv, 0, 0, moveIncrement, 0);
    }
    else if (clipY > getObjectTop(slideDiv)) {
      clipBy(slideDiv, -moveIncrement, 0, 0, 0);
    }
  }
}

function init() {
  // set initial position
  moveTo(theSplit.introDiv, getObjectLeft(theSplit.introDiv), 203 - getObjectHeight(theSplit.introDiv));

  //set initial clip
  clipTo(theSplit.introDiv, getObjectHeight(theSplit.introDiv), getObjectWidth(theSplit.introDiv), getObjectHeight(theSplit.introDiv), 0);
  
  // make it visible
  show(theSplit.introDiv);
  
  // start the animation
  if (gbDoIntro) runIntro(1);
}

function runIntro(step) {
  if (isSliding) { // rerun the function if the intro is currently sliding
    window.setTimeout("runIntro(" + step + ");", 500);
    return;
  }
  switch(step) {
    case 1:
      slideIntro(1);
      break;
    case 2:
      window.setTimeout("slideIntro(2);", 1000);
      break;
    case 3:
      window.setTimeout("slideIntro(3);", 1000);
      break;
    case 4:
      hide(theSplit.introDiv);
      hide("td_trialoffer_ad_blank");
      show("td_trialoffer_ad_button");
      break;
  }
}

function slideIntro(step) {
  isSliding = true;
  switch(step) {
    case 1:
      slideInterval = window.setInterval("slide(theSplit.introDiv, 100, 201, 4, null, 196);", 5);
      window.setTimeout("runIntro(2);", 500);
      break;
    case 2:
      slideInterval = window.setInterval("slide(theSplit.introDiv, 515, 201, 4);", 5);
      window.setTimeout("runIntro(3);", 500);
      break;
    case 3:
      var stopPos = 201 - getObjectHeight(theSplit.introDiv);
      slideInterval = window.setInterval("slide(theSplit.introDiv, 515, " + stopPos + ", 4, null, 201);", 5);
      window.setTimeout("runIntro(4);", 10);
      break;
  }
}


function showOfie(step) {
  if (navigator.userAgent.indexOf('MSIE') == -1 || navigator.userAgent.indexOf('Win') == -1) return;
  if (rolledOver) {
    return;
  }
  else if (!rolledOver) {
    rolledOver = true;
    switch(step) {
      case 1:
        moveTo(theSplit.ofieDiv, getObjectLeft(theSplit.ofieDiv), 201 - getObjectHeight(theSplit.ofieDiv));
        clipTo(theSplit.ofieDiv, getObjectHeight(theSplit.ofieDiv), getObjectWidth(theSplit.ofieDiv), getObjectHeight(theSplit.ofieDiv), 0);
        show(theSplit.ofieDiv);
        slideInterval = window.setInterval("slide(theSplit.ofieDiv, getObjectLeft(theSplit.ofieDiv), 201, 6, null, 201);", 5);
        window.setTimeout("showOfie(2);", 50);
        break;
      case 2:
        if (isSliding) {
          window.setTimeout('showOfie(2);', 500);
        }
        else {
          //hide('ofieSmall');
        }
        break;
    }
  }
}

function hideOfie(step) {
  switch(step) {
    case 1:
      slideInterval = window.setInterval("slide(theSplit.ofieDiv, getObjectLeft(theSplit.ofieDiv), (201 - getObjectHeight(theSplit.ofieDiv)), 6, null, 201);", 5);
      window.setTimeout("hideOfie(2);", 10);
      break;
    case 2:
      if (isSliding) {
        window.setTimeout("hideOfie(2);", 50);
      }
      else {
        hide(theSplit.ofieDiv);
        rolledOver = false;
      }
      break;
  }
}

// Div mouseover and mouseout functions
var killTimer, leaveTimer;

function leaveOfie(now) {
  if (isSliding) {
    leaveTimer = window.setTimeout("leaveOfie();", 50);
  }
  else {   
    if (!fieldFocus) {
	  if (now) killTimer = window.setTimeout("hideOfie(1);", 100);
      else killTimer = window.setTimeout("hideOfie(1);", 2000);
    }
  }
}

function enterOfie() {
  if (navigator.userAgent.indexOf('MSIE') == -1 || navigator.userAgent.indexOf('Win') == -1) return;
  window.clearTimeout(killTimer);
  window.clearTimeout(leaveTimer);
}


// Global vars
var slideInterval;
var isSliding = false;
var fieldFocus = false;
var rolledOver = false;

function readCookie(cookieName) {
  var allCookies = document.cookie.split('; ');
  for (var i = 0; i < allCookies.length; i++) {
    if (allCookies[i].split('=')[0] == cookieName) {
      return allCookies[i].split('=')[1];
    }
  }
  return false;
}<!--CLD live=07-23-08-15:53 -->
