function MM_preloadImages() { 
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { 
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { 
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function setfocus() {
	if (document.forms['mastsearch'].form_keyword) document.forms['mastsearch'].form_keyword.focus();
}
var show;

function showmenu()
{
  var el = document.getElementById("channels");
  var iframe_menu = document.getElementById('iframe_menu');
  var moretab=document.getElementById("menupos");
  if (el) {
	  	el.style.top=(DomUtils.getElementTop(moretab)+23)+"px";
		el.style.left=(DomUtils.getElementLeft(moretab)-100)+"px";
    el.style.visibility = "visible";
    el.style.zIndex = '100';
      iframe_menu.style.width = el.offsetWidth;
      iframe_menu.style.height = el.offsetHeight;
      iframe_menu.style.top = el.style.top;
      iframe_menu.style.left = el.style.left;
      iframe_menu.style.zIndex = el.style.zIndex - 1;
      iframe_menu.style.display = "block";
    show = 1;
  }
}
function hidemenu()
{
  show = 0;
  setTimeout("hide()", 600);
}
function hide()
{
  var el = document.getElementById("channels");
  var iframe_menu = document.getElementById('iframe_menu');
  if (el) {
    if (!show)
    {
      el.style.visibility = "hidden";
      iframe_menu.style.display = "none";
    }
  }
}
function showmenu2()
{
  var el = document.getElementById("back");
  if (el) {
    el.style.visibility = "visible";
    show = 1;
  }
}
function hidemenu2()
{
  show = 0;
  setTimeout("hide2()", 400);
}
function hide2()
{
  var el = document.getElementById("back");
  if (el) {
    if (!show)
    {
      el.style.visibility = "hidden";
    }
  }
}



/**
 * Append this JavaScript to every page
 */
if (document.getElementById('footerMenuContent') != undefined) {
	var footerMenuContentEl = document.getElementById('footerMenuContent');
}

// When there is no footer menu in action in the page, we should not try to execute
// this series of actions.  It is not just pointless but also causes JS errors.
if (typeof footerMenuContentEl != undefined && footerMenuContentEl) {
  window.addOnLoad(padPopupLinks);
  new ypSlideOutMenu('footerMenu', 'up', getMenuLeftPos(), getMenuTopPos(), 140, 155);
  window.addOnResize(repositionMenu);
  initCursorTargets();

  // position main menu pointer needle
  // possible x coords
  var cursorTimeout;

  // init menu pointer
  setMenuCursorPos();
}

function padPopupLinks() {
    var anchorElements = footerMenuContentEl.getElementsByTagName('a');
	for (var i = 0; i < anchorElements.length; i++) {
		var text = anchorElements[i].firstChild.nodeValue;
		anchorElements[i].removeChild( anchorElements[i].firstChild );
		var span = document.createElement('span');
		span.style.position = 'relative';
		span.style.top  = '5px';
		span.style.left = '10px';
		span.appendChild( document.createTextNode(text) );
		anchorElements[i].appendChild( span );
	}
}

function getMenuLeftPos() {
	var targetLeft = DomUtils.getElementLeft( document.getElementById('i18nMoreButton') );
	var x = targetLeft - 67;
	return x;
}

function getMenuTopPos() {
	var cursorTop = DomUtils.getElementTop( document.getElementById('cursorElement') );
	var y = cursorTop - 160;
	return y;
}

function repositionMenu() {
	ypSlideOutMenu.repositionMenu('footerMenu', getMenuLeftPos(), getMenuTopPos());
}

function initCursorTargets() {
	var i18nEl = document.getElementById('i18n');
	var li = i18nEl.getElementsByTagName('li'); // make global
	var cursorEl = document.getElementById('cursorElement');
	var cursorElWidth = 0;
	for ( var i = 0; i < li.length; i++ ) {
		if ( document.all ) { /* IE */
			cursorElWidth += DomUtils.getElementWidth(li[i]) + 15;
		}
		else {
			cursorElWidth += DomUtils.getElementWidth(li[i]);
		}
		if ( i == (li.length - 1) ) break; /* skip more button element */
		window.addEvent(li[i], 'mouseover', setMenuCursorPos, cursorEl, false);
		window.addEvent(li[i], 'mouseout', resetMenuCursorPos, cursorEl, false);
	}
	cursorEl.style.width = cursorElWidth + 'px';
	i18nEl.style.width = cursorElWidth + 'px';
	var moreLangEl = document.getElementById('footerMenuContent');
	window.addEvent(moreLangEl, 'mouseover', hideMenuCursor, document, false);
	window.addEvent(moreLangEl, 'mouseout', resetMenuCursorPos, document, false);
	var moreLangButton = document.getElementById('i18nMoreButton');
	window.addEvent(moreLangButton, 'mouseover', hideMenuCursor, document, false);
	window.addEvent(moreLangButton, 'mouseout', resetMenuCursorPos, document, false);
}

function setMenuCursorPos(e) {
	var cursorElement;
	var targetEl;
	if (e) {
	/* 
	 * event triggered 
	 * e.target ( moz target )
	 * e.srcElement ( ie target )
	 */
		cursorElement = this;
		targetEl = (e.target) ? e.target : (e.srcElement) ? e.srcElement : null;
	}
	else {
		cursorElement = document.getElementById('cursorElement');
		var anchorElements = cursorElement.getElementsByTagName('a');
		for (var i = 0; i < anchorElements.length; i++) {
			if ( anchorElements[i].hreflang == PG_LOCALE ) {
				targetEl = anchorElements[i];
				break;
			}
		}
	}
	var absContainerLeft = DomUtils.getElementLeft(cursorElement);
	var absTargetLeft = DomUtils.getElementLeft(targetEl);
	var targetLeft = absTargetLeft - absContainerLeft;
	var targetWidth = DomUtils.getElementWidth(targetEl);
	// minus anchor left padding
	targetWidth = targetWidth - 5;
	cursorElement.style.backgroundPosition = ( targetLeft + (targetWidth / 2) ) +'px bottom';
	if ( cursorTimeout )
		clearTimeout(cursorTimeout);
}

function resetMenuCursorPos() {
	cursorTimeout = setTimeout('setMenuCursorPos()', 500);
}

function hideMenuCursor(e) {
	var cursorElement = this.getElementById('cursorElement');
	cursorElement.style.backgroundPosition = '-10px bottom';
	
	if ( cursorTimeout )
		clearTimeout(cursorTimeout);
}
