/*********************************
   global.js for EA.com
   code by Byron Tredwell (byron(AT)blastradius.com)
*********************************/

// basic buggy browsers
var ua = navigator.userAgent.toLowerCase();    
var isIE = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) );
var isSafari = (ua.indexOf('safari') !=-1);
var isMac = (ua.indexOf('mac') !=-1);
   
//global init
var initArray = new Array();
function init()
{
   for(var zZ=0; zZ<initArray.length; zZ++)
   {
      initArray[zZ]();
   }
   removefocus();
   setTitleTags();
	showFooter();
   attachEasterEgg();
}

// call init when document finishes loading
if (typeof window.addEventListener != 'undefined') {
   window.addEventListener('load', init, false);
}
else if (typeof window.attachEvent != 'undefined') {
   window.attachEvent('onload', init);
}else{
   // mac ie5 - boo - this will override any other window load events
   window.onload = init;
} 

//apply title tags to imgTxt divs
function setTitleTags()
{
   var divs = document.getElementsByTagName("div");
   for(var i=0;i<divs.length;i++)
   {
      if(divs[i].className.indexOf("imgTxt") >=0)
      {
         var txtSpan = divs[i].getElementsByTagName("span")
         if(check(txtSpan[0]))
            divs[i].title = txtSpan[0].innerHTML.replace("&amp;","&");
      }
   }
}

// remove the focus border form clicked links
function removefocus()
{
   for(i=0;i<document.links.length;i++)
      document.links[i].onfocus=function() {if(this.blur)this.blur()}; 
      
   var inputs = document.getElementsByTagName("input");
   for(i=0;i<inputs.length;i++)
   {
      if(inputs[i].type == "image" || inputs[i].type == "radio")
         inputs[i].onfocus=function() {if(this.blur)this.blur()}; 
   }
}

//even the heights of 2 cols
function adjustHeights(colOne,colTwo)
{
  var p = getElm(colOne);
  var s = getElm(colTwo);
  if(!p || !s)
   return;
  var h = Math.max(p.offsetHeight,s.offsetHeight);
  
  p.style.height = h +"px";
  s.style.height = h +"px";
}

//Jog a redraw for Safari
function redrawWin()
{
   document.body.style.height = "1px";
   document.body.style.height = "100%";
}

//INIT SCROLLING COMPONENENTS
function initScrollingComponent(gridNum)
{
   var comp  = getElm("comp_"+gridNum);
   var cont  = getElm("cont_"+gridNum);
   var list  = getElm("list_"+gridNum);
   var hdr   = getElm("comphdr_"+gridNum);
   var ftr   = getElm("compftr_"+gridNum);
   var first = getElm("listFirstChild_"+gridNum+"_0");
   var sb    = getElm("elScrollbar_"+gridNum); 
   var hdrH  = PADDING;
   var ftrT  = comp.offsetHeight-PADDING;
   if(hdr && typeof hdr != "undefind"){hdrH = hdr.offsetHeight+hdr.offsetTop+PADDING};
   if(ftr && typeof ftr != "undefind"){ftrT = ftr.offsetTop-PADDING};
   var contW  = comp.offsetWidth;
   if(first && typeof first != "undefined"){
      first.style.marginTop = "0px";
   }
   cont.style.top    = (hdrH) +"px";
   cont.style.height = (ftrT - hdrH)+"px";
   sb.style.top = cont.style.top;
   if(list && typeof list != "undefined"){
      list.style.width  = (contW-WIDTH_W_SCROLL) +"px";
   }
}

//LEGAL CONFRIM DIALOG
function confirmLegal() 
{
	var acceptsTerms = "Terms & Conditions of Downloading Materials\n\nThe materials provided on this web site are provided \"as is\" without warranties of any kind.  Electronic Arts Inc., its subsidiaries, divisions, affiliates and licensors (\"EA\") disclaim all warranties, either express of implied, including but not limited to, warranties of merchantability and fitness for a particular purpose. To the extent allowed by applicable law, in no event will EA be liable for damages of any kind to your hardware, peripherals or software programs as a result of your download or use of our materials.\n\n You may download one copy of the materials onto a single computer for your personal, non-commercial, home use only, provided you keep intact all copyright, trademark and other proprietary notices.  No materials from this web site may be copied, reproduced, modified, republished, uploaded, posted, transmitted, broadcast or distributed in any way without the express written consent of EA.  Unauthorized use of the materials is a violation of EA's copyright and constitutes infringement of EA's proprietary rights.\n\nTo use these materials, you must agree to the above terms and conditions.  To accept this agreement and proceed with the download, click \"OK\" or click \"Cancel\" to decline.";
    if(confirm(acceptsTerms)) { return true; }
	else { return false; }
}
function popLegal(url,width,height,confirmation) {
   if(confirmLegal()){
      openCenteredWindow(url,'pop', width, height,'yes');
   }
}
function popWallpaper(url, width, height) {
   if(confirmLegal()){
      openCenteredWindow(url,'pop', width, height,'yes');
   }
}

function popExternal(url) {
 if(confirmExtLegal()){
      openFullChromeWindow(url);
   }
}

function confirmExtLegal() 
{
	var acceptsTerms = "You are about to leave the Electronic Arts website and go to a website owned by a third party.  Electronic Arts is not responsible for content o third party sites, and our privacy policy does not apply to their information collection practices.  Press OK to access the linked site or press CANCEL to return to your original page.";
    if(confirm(acceptsTerms)) { return true; }
	else { return false; }
}

 


//PLAYERS ONLINE
function getPlayersOnline(){
	loadXMLDoc("/framework/_includes/nation/xml_players_online.jsp","playersOnline",displayPlayersOnline,wait);	
}

function displayPlayersOnline(){
	var poXML = xmlDocs["playersOnline"].xml;
   for(var i=0; i < poXML.childNodes.length; i++)
   {
      if(poXML.childNodes[i].nodeType == Node.ELEMENT_NODE)
      {
         poXML = poXML.childNodes[i];
         break;
      }
   }  
   for(var i=0; i < poXML.childNodes.length; i++)
   {
      if(poXML.childNodes[i].nodeType != Node.ELEMENT_NODE)
         continue;
         
      var HTMLtext = getNodeValue(poXML.childNodes[i]);
      if(poXML.childNodes[i].nodeName == "number")
      {
         writeHTML(getElm("plplayercounter"),HTMLtext);
         if(getElm("playercounter"))
            writeHTML(getElm("playercounter"),HTMLtext);
      }
	}
}

function showFooter(){
	var footer = getElm("footer");
	if(footer) {
		if(footer.className){
			if(footer.className == "hidden"){
				footer.className = "visible";
			}
		}
	}
}

function wait(){
}


/**********************************************************************
   Easter Egg
 *********************************************************************/

var easterEggKeyIndex = 0;

function attachEasterEgg() {
   var trigger = document.createElement('img');
   trigger.width = '5';
   trigger.height = '5';
   trigger.src = 'http://images.ea.com/_img/p.gif';
   trigger.style.position = 'absolute';
   trigger.style.top = '0';
   trigger.style.left = '0';
   trigger.style.zIndex = '1000';
   trigger.onmouseup = function() { document.getElementById('easterEggInput').focus(); return true; };
   
   var easterEggInput = document.createElement('input');
   easterEggInput.id = 'easterEggInput';
   easterEggInput.style.position = 'absolute';
   easterEggInput.style.top = '0';
   easterEggInput.style.left = '-1000px';
   easterEggInput.onkeypress = easterEggListener;
   
   var body = document.getElementsByTagName('body')[0];
   body.appendChild(trigger);
   body.appendChild(easterEggInput);
}

function easterEggListener(e) {
   if (!e) e = event;
   var charCode = (e.charCode || e.charCode == 0) ? e.charCode : ((e.keyCode) ? e.keyCode : e.which);
   switch (easterEggKeyIndex) {
      case 0:
         if (charCode == 35 && e.shiftKey) {
            easterEggKeyIndex = 1;
         }
         else {
            easterEggKeyIndex = 0;
         }
         break;
      case 1:
         if (charCode == 51) {
            easterEggKeyIndex = 2;
         }
         else {
            easterEggKeyIndex = 0;
         }
         break;
      case 2:
         if (charCode == 49) {
            easterEggKeyIndex = 3;
            doEasterEgg();
         }
         else {
            easterEggKeyIndex = 0;
         }
         break;
   }
}

function doEasterEgg() {

   // modify all text nodes
   doTextEasterEgg(document.getElementsByTagName('body')[0]);
   
   // modify all images
   var imageNodes = document.getElementsByTagName('img');
   for (var i = 0; i < imageNodes.length; i++) {
      imageNodes[i].src = 'http://images.ea.com/_img/31.gif';
   }
   
   // modify all objects
   var objectNodes = document.getElementsByTagName('object');
   for (var i = 0; i < objectNodes.length; i++) {
      var imageNode = document.createElement('img');
      imageNode.src = 'http://images.ea.com/_img/31.gif';
      imageNode.width = objectNodes[i].getAttribute('width');
      imageNode.height = objectNodes[i].getAttribute('height');
      objectNodes[i].replaceNode(imageNode);
   }
   
   // modify all background images (brute force)
   var allNodes = document.all ? document.all : document.getElementsByTagName('*');
   for (var i = 0; i < allNodes.length; i++) {
      allNodes[i].style.backgroundImage = 'url(http://images.ea.com/_img/bg_31.gif)';
   }
}
   
function doTextEasterEgg(node) {
   if (node.nodeType == 3) { 
      node.nodeValue = node.nodeValue.replace(/\b\w+\b/g, '#31');
   }
   else if (node.nodeType == 1 && node.childNodes != null) {
      for (var i=0; i < node.childNodes.length; i++) {
         doTextEasterEgg(node.childNodes.item(i));
      }
   }
}


/**********************************************************************
   Basic Event Registration
   - has issues see http://www.quirksmode.org/blog/archives/2005/08/addevent_consid.html
 *********************************************************************/

function isDefined(property) {
  return (typeof property != 'undefined');
}

function addEvent(object, eventName, functionRef) {
   if (isDefined(window.addEventListener)) {
      object.addEventListener(eventName, functionRef, false);
   }
   else if (isDefined(window.attachEvent)) {
      object.attachEvent('on' + eventName, functionRef);
   }
}
