var doingTitleFlash = 0;
var lastTitle;
var currTitle;
var baPE;
var lastAFid = 0;

function startBAAFRefresh () {
   refreshAutofight();
   baPE = new PeriodicalExecuter(refreshAutofight, 5);
}

function stopBAAFRefresh () {
   baPE.stop();
}


function challBAAF() {
   new Effect.Appear('AFLoader', { duration: 0.4 });
   baPE.stop();
   new Ajax.Request("http://www.creatureworld.net/bin/chal_af.php?lastafid="+lastAFid+"&rand="+Math.random(),
       {
          method:'get',
          onSuccess: function (r) {
                 new Effect.Fade('AFLoader', { duration: 0.4 });
                var infoxml = r.responseXML;
                var info = infoxml.getElementsByTagName('info')[0];
                var afhtml = info.getElementsByTagName('afhtml')[0].firstChild.nodeValue;
                var afstate = info.getElementsByTagName('afstate')[0].firstChild.nodeValue;
                lastAFid = info.getElementsByTagName('lastAFid')[0].firstChild.nodeValue;
                document.getElementById('BAAFWrapper').innerHTML = afhtml;
                if(afstate==3) {
                   stopBAAFRefresh();
                  window.location.href="http://www.creatureworld.net/battle/new.php?type=2&lastafid="+lastAFid;
                  // document.getElementById('AFLoaderWRAP').style.display = 'none';
                }
          },
          onFailure:  function(){ alert('Autofight Error!') }
    });

}


function cancelBAAF() {
     new Effect.Appear('AFLoader', { duration: 0.4 });
   baPE.stop();
   lastAFid = 0;
   new Ajax.Request("http://www.creatureworld.net/bin/cancel_af.php?rand="+Math.random(),
   {
   method:'get',
   onSuccess: function (r) {
                 new Effect.Fade('AFLoader', { duration: 0.4 });
                document.getElementById('AFLoaderWRAP').style.display = 'none';
                document.getElementById('BAAFWrapper').innerHTML = '<center>Autofight has been cancelled</center>';
   }, 
          onFailure:  function(){ alert('Autofight Cancellation Error!') }
   });

}

function refreshAutofight () {
     document.getElementById('AFLoaderWRAP').style.display = 'block';
     new Effect.Appear('AFLoader', { duration: 0.4 });
    new Ajax.Request("http://www.creatureworld.net/bin/get_afinfo.php?lastafid="+lastAFid+"&rand="+Math.random(),
    {
          method:'get',
          onSuccess: function (r) {
                 new Effect.Fade('AFLoader', { duration: 0.4 });
                var infoxml = r.responseXML;
                var info = infoxml.getElementsByTagName('info')[0];
                var afhtml = info.getElementsByTagName('afhtml')[0].firstChild.nodeValue;
                var afstate = info.getElementsByTagName('afstate')[0].firstChild.nodeValue;
                lastAFid = info.getElementsByTagName('lastAFid')[0].firstChild.nodeValue;
                document.getElementById('BAAFWrapper').innerHTML = afhtml;
                if(afstate==2) {
                   document.getElementById('AFLoaderWRAP').style.display = 'none';
                   stopBAAFRefresh();
                }
          },
          onFailure:  function(){ alert('Autofight Error!') }
    });
}

function refreshInfo () {
    new Ajax.Request("http://www.creatureworld.net/bin/get_info.php?rand="+Math.random(),
    {
          method:'get',
          onSuccess: function (r) { 
                var infoxml = r.responseXML; 
                var info = infoxml.getElementsByTagName('info')[0];
                var newmsgs = info.getElementsByTagName('newmsgs')[0].firstChild.nodeValue;
                var newevents = info.getElementsByTagName('newevents')[0].firstChild.nodeValue;
//                if (newmsgs > 0 ) {
//                   document.title= "omg it worked msgs";
//                }
		if (newevents > 0 ) {
                   x = info.getElementsByTagName('events')[0].getElementsByTagName('event');
                   for (i=0;i<x.length;i++) {
                      if (x[i].nodeType==1) {
                         eventid = x[i].getElementsByTagName('eventid')[0].firstChild.nodeValue;
                         if( indexInArray(seenEvents,eventid) == -1 ) {
                            seenEvents.push(eventid);
                            eventbody = x[i].getElementsByTagName('body')[0].firstChild.nodeValue;
                            currevebody =  document.getElementById('eventsHolder').innerHTML;
                            document.getElementById('eventsHolder').innerHTML = currevebody + eventbody;
                            var doc2 = document.getElementById('round-content-EventDiv'+eventid);
                            curveBodyCorners(doc2);
                            if(doingTitleFlash == 0 ) {
                               doingTitleFlash = 1;
                               lastTitle = document.title;
                               currTitle = "New Event";
                               document.title = currTitle;
                               new PeriodicalExecuter(flashTitle, 1);
                            }
                         }
                     } 
                   }
                }
                
	  },
          onFailure:  function(){ alert('Info Error!') }
    });

}

function flashTitle() {
   document.title = lastTitle;
   lastTitle = currTitle;
   currTitle = document.title;

}

function indexInArray(arr,val){
   for(var i=0;i<arr.length;i++) {
      if(arr[i]==val){
          return i;
      }   
   }
   return -1;
} 

new PeriodicalExecuter(refreshInfo, 10);


