var http = null;
var timer=null;
var filename=null;
var last=null;
var img=null;
var original_img=null;

function Init()
{
  if (window.XMLHttpRequest) {
     http = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
     http = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (!http)
  {
		// Internet Explorer 6 und älter
		try {
			http  = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			http  = null;
			alert("Bitte aktivieren Sie ActiveX-Steuerelementeun versuch Sie es erneut.");
		}
	}
  http.onreadystatechange = ausgeben;
  timer=new Timer("RefreshTimer",2000);
  timer.ontick = OnRefresh;
  last = document.getElementById('buliChangeImgLastDateTime');
  img=document.getElementById('buliChangeImg');
  original_img=img.src;
}

function GetFileName()
{
  if(img)
  {
    filename=img.src.substr(0,img.src.lastIndexOf('.'));
    filename=filename.substr(filename.lastIndexOf('/')+1, filename.length-filename.lastIndexOf('/'));      
  }
}

function StartAjax()
{
        
    if(document.URL.search(/bundesliga-live/)!=-1)
	  {
	    Init();    
      GetFileName();       
      timer.start();
    }    
}

function OnRefresh(sender)
{
  timer.stop();
  loadXml();
  timer.start();
}

function loadXml() {
   if (http != null)
   {
      http.open("GET", "http://www.dot4you.de/cam/refresh.php?File="+filename+"&Datum="+last.value, true);
      http.send(null);               
   }
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}




function ausgeben() {
   if (http.readyState == 4 && http.status == 200) 
   {
      var daten=null;
      try
      {
        daten = http.responseText;
        if(daten)
        {
          if(daten!=last.value)
          {
            img=document.getElementById('buliChangeImg');
            if(img)
            {
              img.src=original_img+"?"+Math.random();              
            }
          }
          last.value=daten;
        }
        //timer.start();
      }
      catch(err)
      {
        alert(err.name+"\n"+err.description+"\n"+err.message);
        img.src=original_img;
      }         
   }
}

