// requires prototype.js, flashcom.js, popup.js

// position dropdown arrow on the middle of the link
function alignDropdownArrow(linkId,dropdownId) {
  leftOffset = 250 + $(linkId).getWidth()/2 + $(linkId).positionedOffset()[0] - 15;
  $(dropdownId).firstDescendant().setStyle({'left': leftOffset + 'px'});
}


function doNavMouseOut(menuId) {
  // initiate a hide sequence when the mouse leaves the nav item
  if ($(menuId).popup != undefined) $(menuId).popup.start_hide_timer();
}

// Displays the header dropdown on Fox/FOD pages (e.g. Watch Full Episodes)
function showDropDownMenu(menuId) {
  // first time: create the popup
  if ($(menuId).popup == undefined) new Popup(menuId, menuId,
					      {position: "0,-15",
						  autoclose: true,
						  onOpenCallback: function() { pauseCarousel(); },
						  onCloseCallback: function() { resumeCarousel(); }
					      });

  // hide any other open menus
  if (menuId == fullEpisodesMenuId && $(showsMenuId).popup != undefined) {
      $(showsMenuId).popup.hide();
  }
  else if (menuId == showsMenuId && $(fullEpisodesMenuId).popup != undefined) {
      $(fullEpisodesMenuId).popup.hide();
  }

  // show the active menu
  $(menuId).popup.stop_hide_timer();
  $(menuId).popup.show();

}

// Toggles the display of (shows or hides) the white info popup box in a episode/video clip thumbnail block
function showHideThumbInfoBox(thisElem) {
   var elem = thisElem;

   while (!hasClass(elem.parentNode, "thumbnailBlock") && elem.parentNode != undefined) {
      elem = elem.parentNode;
   }

   if (!hasClass(elem.parentNode, "thumbnailBlock"))
      return false;

   var thumbnailBlockElem = elem.parentNode;

   var thumbInfoBoxElem = getElementByClassName(thumbnailBlockElem, "thumbInfoBox");
   if (thumbInfoBoxElem == undefined || thumbInfoBoxElem == null)
      return false;

   if (thumbInfoBoxElem.style.display == "none" || thumbInfoBoxElem.style.display == "") {
      var thumbnailLinkElem = getElementByClassName(thumbnailBlockElem, "thumbnailLink");

      if (thumbnailLinkElem == undefined || thumbnailLinkElem == null)
         return false;

      var pos = Position.cumulativeOffset(thumbnailLinkElem);

      thumbInfoBoxElem.style.left = eval(pos[0] + thumbnailLinkElem.offsetWidth) + 'px';
      thumbInfoBoxElem.style.top = pos[1] + 1 + "px";

      thumbInfoBoxElem.style.display = "block";
   }
   else {
      thumbInfoBoxElem.style.display = "none";
   }
}

function getElementByClassName(thumbnailBlockElem, className) {
   var childElems = thumbnailBlockElem.getElementsByTagName("*");

   for (i = 0; i < childElems.length; i++) {
      if (hasClass(childElems[i], className))
         return childElems[i];
   }
}

function hasClass(elem,  className) {
   return (elem.className && elem.className.match(new RegExp("\\b" + className + "\\b"))) ? true : false;
}

function pauseCarousel() {
  if (typeof(sendToFlash) == 'function') sendToFlash('carousel', 'disable');
}

function resumeCarousel() {
  if (typeof(sendToFlash) == 'function') sendToFlash('carousel', 'enable');
}
