/*****************************************
   promo js for EAS.com
   code by Allan (allan.chang(AT)blastradius.com)
*****************************************/




/*************************************
   EA Promo tab script
*************************************/
function showNewReleases() {
   pagecount = 1;
   currentPage = 1;
   document.getElementById("promo-newgames").style.display   = "block";
   document.getElementById("promo-comingsoon").style.display = "none";
   document.getElementById('tab-new-releases').className = 'tab On';
   document.getElementById('tab-coming-soon').className = 'tab';
   init_pagination('new');
}

function showComingSoon() {
   pagecount = 1;
   currentPage = 1;
   document.getElementById("promo-newgames").style.display   = "none";
   document.getElementById("promo-comingsoon").style.display = "block";
   document.getElementById('tab-new-releases').className = 'tab';
   document.getElementById('tab-coming-soon').className = 'tab On';
   init_pagination('soon');
}







/*************************************
   EA Promo Detail popup
*************************************/
function doDetailInfo(feedType,xmlRef) {
   var infoBlock = getElm(feedType);
   if (!infoBlock) return;
   if (xmlRef) {
      if (!xmlDocs[xmlRef]) return;
      var infoXML = xmlDocs[xmlRef].xml;
   } else {
      if (!xmlDocs[feedType]) return;
      var infoXML = xmlDocs[feedType].xml;
   }
   if (!infoXML) return;

   clearElm(infoBlock);
   var infoHtml = '';

   var errorMsgs = infoXML.getElementsByTagName('Error');
   if (errorMsgs.length != 0) {
      for (var i=0;i<errorMsgs.length;i++) {
        infoHtml += getNodeValue(errorMsgs[i]);
      }
   } else {

      var gameID        = getNodeValue(infoXML.getElementsByTagName('id')[0]);
      var gameBoxartURL = getNodeValue(infoXML.getElementsByTagName('boxart')[0]);
      var gameTitle     = getNodeValue(infoXML.getElementsByTagName('title')[0]);
      var gameInfo      = getNodeValue(infoXML.getElementsByTagName('info')[0]);
      var gameRating    = getNodeValue(infoXML.getElementsByTagName('rating')[0]);
      var gameFeatures  = getNodeValue(infoXML.getElementsByTagName('features')[0]);

      infoHtml += '<img class="game-boxart" src="' + gameBoxartURL + '" border="0" />';
      infoHtml += '<div class="promo-detail-info">';
      infoHtml += '<p class="txt-title"><strong>' + gameTitle + '</strong></p>';
      infoHtml += '<p class="txt-info">' + gameInfo + '</p>';
      infoHtml += '<p class="txt-rating">ESRB Rating: ' + gameRating + '</p>';
      infoHtml += '</div>';
      infoHtml += '<div class="promo-detail-features">';
      infoHtml += '<div class="promo-detail-feature-hdr"><span>Game Features Included</span></div>';
      infoHtml += '<p class="txt-info">' + gameFeatures + '</p>';
      infoHtml += '</div>';

      writeHTML(infoBlock,infoHtml);
   }
}






/*************************************
   EA Promo detail Popup
*************************************/
function viewPromoDetail(targetID, gameID) {
   var idArrow = 'arrowbox';
   var idName = 'detailbox';
   var idThumb = 'thumbid';
   var itemLocationBox = 0;
   var distanceFromTop = 0;
   var distanceFromBottom = 0;
   var windowPageOffset = 0;
   var windowHeight = getInnerHeight();

	var detailBlock = getElm(idName);
	var infoHtml = '';
	clearElm(detailBlock);
	var infoHtml = '<div id="' + idName + targetID + '"></div>';
	writeHTML(detailBlock,infoHtml);

     loadXMLDoc('/promo_game_detail/'+gameID,idName + targetID,function(){doDetailInfo(idName + targetID)},function(){doWait(idName + targetID)});

   // re-position detail box
   if (document.getElementById(idName)) {
      if (window.pageYOffset==null) {
         windowPageOffset = document.documentElement.scrollTop;
      } else {
         windowPageOffset = window.pageYOffset;
      }
      itemLocationBox = document.getElementById("gus-header").offsetHeight + document.getElementById('game_promos').offsetTop + document.getElementById(idThumb + targetID).offsetParent.offsetTop;
      distanceFromTop = itemLocationBox - windowPageOffset;
      distanceFromBottom = (windowPageOffset + windowHeight)-(document.getElementById("gus-header").offsetHeight + document.getElementById('game_promos').offsetTop + document.getElementById(idThumb + targetID).offsetParent.offsetTop);

      if ((distanceFromTop > -34) && (distanceFromBottom > 67)) {
         if (document.body.clientHeight < 437) {
            document.getElementById(idName).style.top = ((document.getElementById(idThumb + targetID).offsetParent.offsetTop - 48)+(-1*(document.getElementById("gus-header").offsetHeight + document.getElementById('game_promos').offsetTop + document.getElementById(idThumb + targetID).offsetParent.offsetTop - 50 - windowPageOffset)))+ 'px';
         } else {
            if (distanceFromTop < 48) {
               document.getElementById(idName).style.top = ((document.getElementById(idThumb + targetID).offsetParent.offsetTop - 48)+(-1*(document.getElementById("gus-header").offsetHeight + document.getElementById('game_promos').offsetTop + document.getElementById(idThumb + targetID).offsetParent.offsetTop - 50 - windowPageOffset)))+ 'px';
            } else {
               if (distanceFromBottom < 385) {
                  // detail box needs to move up a bit.
                  document.getElementById(idName).style.top = ((document.getElementById(idThumb + targetID).offsetParent.offsetTop - 50)-(document.getElementById("gus-header").offsetHeight + document.getElementById('game_promos').offsetTop + document.getElementById(idThumb + targetID).offsetParent.offsetTop - 49 + 437 - windowPageOffset - windowHeight)) + 'px';
               } else {
                  document.getElementById(idName).style.top = (document.getElementById(idThumb + targetID).offsetParent.offsetTop - 50) + 'px';
               }
            }
         }

         // show detail box
         document.getElementById(idArrow).style.top = (document.getElementById(idThumb + targetID).offsetParent.offsetTop + 40) + 'px';
         document.getElementById(idArrow).style.display = "block";
         document.getElementById(idName).style.display = "block";
      }
   }
}




function hidePromoDetail() {
   var idArrow = 'arrowbox';
   var idName = 'detailbox';
   if (document.getElementById(idArrow)) document.getElementById(idArrow).style.display = "none";
   if (document.getElementById(idName)) document.getElementById(idName).style.display = "none";
}




function getInnerHeight() {
    var y;
    if (self.innerHeight) { // all except Explorer
       y = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        y = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        y = document.body.clientHeight;
    }
    return y;
}




/*************************************
   home page pagination script
*************************************/
function init_pagination(targetID) {
   var count = 1;
   while (document.getElementById(targetID + "-page-" + pagecount)) {
      if (pagecount == 1) {
         document.getElementById(targetID + "-page-" + pagecount).style.display = "block";
      } else {
         document.getElementById(targetID + "-page-" + pagecount).style.display = "none";
      }
      pagecount++;
   }
   showPagecount(targetID);
   if (targetID == 'new') {
      document.getElementById(targetID + "-prev").onclick = function(){page_flip(-1, 'new')};
      document.getElementById(targetID + "-next").onclick = function(){page_flip(1, 'new')};
   } else {
      document.getElementById(targetID + "-prev").onclick = function(){page_flip(-1, 'soon')};
      document.getElementById(targetID + "-next").onclick = function(){page_flip(1, 'soon')};
   }
}




