/* Requirements for using this javascript include:

javascript for getObj() and getObjNN4() must be included (found in 'utils.js')

there must be a div defined within the page with a NAME and ID attrib set both
to "contentBox" (case IS important), and a style of absolute positioning. a
z-index of 1 or higher is also recommended, as is initially hiding the content
box (can be accomplished through the STYLE attrib or better by calling
hidePopover() from the BODY tag's 'onLoad' event).
for example:
  <BODY onLoad="hidePopover();">
  <DIV ID="contentBox" NAME="contentBox" STYLE="position:absolute;top:0;left:0">
  [LOADING]
  </DIV>

trigger object (what the user mouses over to display the floating box) must
call showPopover() on an 'onMousemove' or 'onMouseover' event, and hidePopover()
on an 'onMouseout' event
  showPopover( UPC, URL, WIDTH, HEIGHT);
  where UPC is the upc number to send to URL.  the resulting html will then be
  placed inside the content box, which will be resized to WIDTH pixels wide
  and HEIGHT pixels high.

multiple calls to showPopover() with the same UPC will cache the results until
the page is reloaded.

*/

// define global vars initially
var http = null;
var popoverCache = new Array();
var upcValue = 0;
var waitForIt = 0;

// check for access method to elements (depending on browser)
if(document.getElementById || document.all) var isNN = true;

// bind getMouseXY to mousemove event for document, store coords in X and Y
if (!document.all) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;
var X = 0;
var Y = 0;

/* grabs mouse coordinates, using different methods depending on browser */
function getMouseXY(e)
{
  if (document.all) // browser is IE compatible
  {
    X = event.clientX;
    Y = event.clientY;
  }
  else // browser is NS4 compatible
  { 
    X = e.pageX;
    Y = e.pageY;
  }
  // catch possible negative values
  if(X < 0){X = 0}
  if(Y < 0){Y = 0}
  return true;
}

/* creates an object for HTTP requests */
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

/* event handler, executes when HTTP object receives a response */
function handleHttpResponse() {
  handle = new getObj('contentBox');
  // assuming success, cache and display information
  if (http.readyState == 4) {
    code = http.responseText;
    handle.obj.innerHTML = '';
    handle.obj.innerHTML = code;
    popoverCache[upcValue] = code;
    waitForIt = 0;
  }
}

/* grabs content, resizes, relocates, and displays floating content box */
function showPopover(upc,url,width,height)
{
  // if a trigger is still waiting for an HTTP response, dont start another one
  if(waitForIt != 0) return;
  
  handle = new getObj('contentBox');
  
  // if the same upc is IN content box now, no need for any of this
  if(upcValue != upc)
  {
    upcValue = upc;
    // check cache, otherwise send HTTP request
    if(popoverCache[upc] != undefined && popoverCache[upc].length > 0)
    {
      handle.obj.innerHTML = popoverCache[upc];
    }
    else
    {
      if(http == null) http = getHTTPObject();
      upcValue = upc;
      handle.obj.innerHTML = '';
      waitForIt = 1;
      http.open("POST", url, true);
      http.onreadystatechange = handleHttpResponse;
      http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      http.send("passitin=" + upcValue);
    }
  }
  
  // calculate new left position for content box
  newLeft = X - (width/4);
  if(newLeft < 0) newLeft = 0;
  
  // calculate new top position for content box
  newTop = Y + 30;
  
  // fix an IE-specific scrolling problem
  if( navigator.userAgent.match(/msie/i) ) newTop += document.body.scrollTop;
  
  // relocates, resizes, and displays content box
  handle.style.left = newLeft;
  handle.style.top = newTop;
  handle.style.width = width;
  handle.style.height = height;
  if(isNN) handle.style.visibility = 'visible';
  else handle.style.visiblity = 'show';
}

/* grabs content, resizes, relocates, and displays floating content box */
function showPopoverAbsolute(upc,url,width,height,top,left)
{
  // if a trigger is still waiting for an HTTP response, dont start another one
  if(waitForIt != 0) return;
  
  handle = new getObj('contentBox');
  
  // if the same upc is IN content box now, no need for any of this
  if(upcValue != upc)
  {
    upcValue = upc;
    // check cache, otherwise send HTTP request
    if(popoverCache[upc] != undefined && popoverCache[upc].length > 0)
    {
      handle.obj.innerHTML = popoverCache[upc];
    }
    else
    {
      if(http == null) http = getHTTPObject();
      upcValue = upc;
      handle.obj.innerHTML = '<br><br><br><center><span class="bodybold16blue">LOADING</span><br><br><br><img src="/starzimages/loading.gif" border=0></center>';
      waitForIt = 1;
      http.open("POST", url, true);
      http.onreadystatechange = handleHttpResponse;
      http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      http.send("passitin=" + upcValue);
    }
  }
  
  // calculate new left position for content box
  newLeft = X - (width/4);
  if(newLeft < 0) newLeft = 0;
  
  // calculate new top position for content box
  newTop = Y + 30;
  
  // fix an IE-specific scrolling problem
  if( navigator.userAgent.match(/msie/i) ) newTop += document.body.scrollTop;
  
  // relocates, resizes, and displays content box
  handle.style.left = left;
  handle.style.top = top;
  handle.style.width = width;
  handle.style.height = height;
  if(isNN) handle.style.visibility = 'visible';
  else handle.style.visiblity = 'show';
}

/* event handler, executes when HTTP object receives a response */
function starzCheckPasswdHttpResponse() {
  handle = new getObj('contentBox');
  // assuming success, cache and display information
  //get posted vars so we can have the target_div
  if (http.readyState == 4) {
    code = http.responseText;
    handle.obj.innerHTML = '';
    handle.obj.innerHTML = code;
    waitForIt = 0;
  }
}

/* grabs content, resizes, relocates, and displays floating content box */
function showStarzPopover(target_div,upc,url,width,height,top,left,loading_img)
{
  // if a trigger is still waiting for an HTTP response, dont start another one
  if(waitForIt != 0) return;
  
  handle = new getObj(target_div);
  
  // if the same upc is IN content box now, no need for any of this
  if(http == null) http = getHTTPObject();
  if( loading_img == null)
     handle.obj.innerHTML = '';
  else 
     handle.obj.innerHTML = '<br><br><br><center><span class="bodybold16blue">LOADING</span><br><br><br><img src="' + loading_image + '" border=0></center>';
  waitForIt = 1;
  http.open("POST", url, true);
  http.onreadystatechange = starzCheckPasswdHttpResponse;
  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http.send("uname=" + uname + "passwd=" + passwd + "target_div=" + target_div);
  
  // fix an IE-specific scrolling problem
  if( navigator.userAgent.match(/msie/i) ) newTop += document.body.scrollTop;
  
  // relocates, resizes, and displays content box
  handle.style.left = left;
  handle.style.top = top;
  handle.style.width = width;
  handle.style.height = height;
  if(isNN) handle.style.visibility = 'hidden';
  else handle.style.visiblity = 'hide';
}

/* grabs content, resizes, relocates, and displays floating content box */
function showPopoverAbsoluteTargeted(target_div,upc,url,width,height,top,left,loading_img)
{
  // if a trigger is still waiting for an HTTP response, dont start another one
  if(waitForIt != 0) return;
  
  handle = new getObj(target_div);
  
  // if the same upc is IN content box now, no need for any of this
  if(upcValue != upc)
  {
    upcValue = upc;
    // check cache, otherwise send HTTP request
    if(popoverCache[upc] != undefined && popoverCache[upc].length > 0)
    {
      handle.obj.innerHTML = popoverCache[upc];
    }
    else
    {
      if(http == null) http = getHTTPObject();
      upcValue = upc;
      if( loading_img == null)
         handle.obj.innerHTML = '';
      else 
         handle.obj.innerHTML = '<br><br><br><center><span class="bodybold16blue">LOADING</span><br><br><br><img src="' + loading_image + '" border=0></center>';
      waitForIt = 1;
      http.open("POST", url, true);
      http.onreadystatechange = handleHttpResponse;
      http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      http.send("passitin=" + upcValue + "target_div=" + target_div);
    }
  }
  
  // calculate new left position for content box
  newLeft = X - (width/4);
  if(newLeft < 0) newLeft = 0;
  
  // calculate new top position for content box
  newTop = Y + 30;
  
  // fix an IE-specific scrolling problem
  if( navigator.userAgent.match(/msie/i) ) newTop += document.body.scrollTop;
  
  // relocates, resizes, and displays content box
  handle.style.left = left;
  handle.style.top = top;
  handle.style.width = width;
  handle.style.height = height;
  if(isNN) handle.style.visibility = 'visible';
  else handle.style.visiblity = 'show';
}

/* hides floating content box */
function hidePopover()
{
  handle = new getObj('contentBox');
  if(isNN) handle.style.visibility = 'hidden';
  else handle.obj.visibility = 'hide'
}
