var ajaxLoaded = 0;
var ulistsLoaded = 0;

function addProdToList_callback(HtmlResp) {
   var my_overlay = document.createElement('div');
   my_overlay.setAttribute('id', 'overlay');
   var my_overlaybubble = document.createElement('div');
   my_overlaybubble.setAttribute('id', 'overlaybubble');
   my_overlaybubble.innerHTML = HtmlResp;
   document.body.appendChild(my_overlay);
   document.body.appendChild(my_overlaybubble);
   addProdToListPost();

}

function addProdToList() {
  if(!document.getElementById('overlaybubble')){
     ajax = new AjaxRequest('POST', '/ulists.php/bubbleContent', true, 3000);
     if(ulist_masterid>0){
        ajax.setParameter('masterid', ulist_masterid);
     }
     if(ulist_isbn>0){
        ajax.setParameter('isbn', ulist_isbn);
     }
     ajax.setResponseType('html');
     ajax.setCallback( addProdToList_callback );
     ajax.send();
   }else{
      addProdToListPost();
   }
}

function addProdToListPost() {
  is_modal = true;
  if(!ulistsLoaded) {
	var s2 = document.createElement('script'); s2.src = 'http://www.pricegrabber.com/js/ulists_from_prodpage.js.php?prodpage_lists_only=1';
	document.body.appendChild(s2);
  }
  if(!ajaxLoaded) {
	var s1 = document.createElement('script'); s1.src = 'http://ah.pricegrabber.com/js/classes/ajaxrequest.js';
	document.body.appendChild(s1);
  }
  waitForLoad();
}
function waitForLoad() {
	if(!ulistsLoaded) {
		if(window.ulists_js) { ulistsLoaded = 1; ShowListMessage('<img src="http://i.pgcdn.com/images/small_wait.gif" width=16 height=16>','Please hold while we process your request ...','#666666');}
		else { setTimeout('waitForLoad("");',200); return 0; }
	}
	if(!ajaxLoaded) {
		if(window.ajax) { ajaxLoaded = 1; }
		else { setTimeout('waitForLoad("");',200); return 0; }
	}
	if(ajaxLoaded && ulistsLoaded) { addToList(); }
}
function openBookmark(ut)
{
    var bkmrk="http://www.pricegrabber.com/?mode=bkmrk";
    var title="PriceGrabber.com - Shopping Comparison";
	window.external.AddFavorite(bkmrk,title);
	url = "/redirect_bookmark.php?ut="+ ut + "";	
	features = "toolbar=no,width=1,height=1,resizable=yes,bgcolor=#000000,scrollbars=no";
    window.name="bookmark";
	window.open( url, "", features );
}
function pg_openPromo(src) {
url = '/info_rebate.php?masterid='+src;
window.open(url, 'promo','height=450,width=550,innerHeight=450,innerWidth=550,menubar=no,status=no,toolbar=no,resizable=yes,scrollbars=yes');
}
function openSoundclips(masterid)
{
	url = "/info_soundclips.php?masterid=" + masterid + "";	
	features = "toolbar=no,width=500,height=450,resizable=yes,bgcolor=#000000,scrollbars=yes";
	window.open( url, "", features );
}
function openVideo( masterid )
{
	url = "/info_streaming.php?PublishedId=" + masterid;	
	features = "toolbar=no,width=300,height=285,resizable=yes,bgcolor=#000000,scrollbars=no";
    window.name="MyWindow";
	window.open( url, "", features );
}
function openlarge( masterid, big_x, big_y )
{
	url = "/info_picture.php?masterid=" + masterid;
	features = "toolbar=no,width=" + big_x + ",height=" + big_y + ",resizable=no,scrollbars=no";
	window.open( url, "techspecs"+masterid+"image", features );
}
function openptw( masterid, desc, topcat )
{
	if( desc )
	{
		url = "/info_ptw.php?masterid=" + masterid + "#description";
	}
	else
	{
		url = "/info_ptw.php?masterid=" + masterid + "&tab_id=" + topcat;	
	}
	features = "toolbar=no,width=540,height=450,resizable=yes,scrollbars=yes";
	window.open( url, "", features );
}
function opengetproddef(type)
{
	url = "/info_getproddef.php?type=" + type;
	features = "toolbar=no,width=400,height=275,resizable=yes,scrollbars=yes";
	window.open( url, "", features );
}
function open_def_win(type)
{
	url = "/info_minipage.php?type=" + type;
	features = "toolbar=no,width=500,height=250,resizable=yes,scrollbars=yes";
	window.open( url, "", features );
}
function openGetCoupon( coupon_id, masterid, redirurl )
{
        url = "/coupons_popup.php?coupon_id=" + coupon_id + "&masterid=" + masterid + "&url=" + redirurl;
		
		features = "toolbar=no,width=444,height=500,resizable=yes,scrollbars=yes";
		window.open( url, "", features );
}
function openGameTrailers( id )
{
  
	url = "/info_streaming_gt.php?gt_id=" + id;
	features = "toolbar=no,width=660,height=400,resizable=yes,bgcolor=#000000,scrollbars=no";
	window.open( url, "", features );
}
function attrib_toggles(subnum,subtype) { 
   if ( subtype == 'a' ) { 
      document.getElementById("sub" + subnum + "_a").style.display = "none"; 
	  document.getElementById("sub" + subnum + "_b").style.display = "block"; 	  
   } 
   else if ( subtype == 'b' ) { 
      document.getElementById("sub" + subnum + "_a").style.display = "block"; 
	  document.getElementById("sub" + subnum + "_b").style.display = "none"; 	  
   } 
}
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);
}
