function createRequestObject()
{
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer")
  {
    var ro = new ActiveXObject("Microsoft.XMLHTTP");
    //var ro = new ActiveXObject("MSXML2.XMLHTTP");
  }
  else
  {
    var ro = new XMLHttpRequest();
  }
  return ro;
}

var weatheranimatestate=0;

var weatherloader = null;
function startweatherwidget(io_uri)
{
  weatherloader = createRequestObject();
  weatherloader.open('GET', io_uri, true);
  weatherloader.onreadystatechange = gotweatherinfo;
  weatherloader.send('');
}

function gotweatherinfo()
{
  if(!weatherloader || weatherloader.readyState != 4)
    return;

  if(weatherloader.responseXML)
  {
    var weatherinfo = weatherloader.responseXML.documentElement;
    if(weatherinfo.nodeName == 'weather')
    {
      document.getElementById('weathericon').src = weatherinfo.getAttribute('icon');
      document.getElementById('weathertext').firstChild.nodeValue = weatherinfo.getAttribute('curtemp') + '\u00B0 C';
      window.setInterval(animateweatherspots, 33);
    }
  }
  weatherloader=null;

}

function setweatheropacities(opac1)
{
  document.getElementById('weatherspot1').style.opacity = opac1/100;
  document.getElementById('weatherspot1').style.filter = "alpha(opacity=" + opac1 + ")";
  document.getElementById('weatherspot2').style.opacity = (100-opac1)/100;
  document.getElementById('weatherspot2').style.filter = "alpha(opacity=" + (100-opac1) + ")";
}

function animateweatherspots()
{
  /* Animation plan:
     - we are triggered every 0.03s
     - from  0 .. 150 (5 secs): show frame 1
     - from 150 .. 200 : go from frame 1 to 2
     - from 200 .. 350: show frame 2
     - from 350 .. 400: go from frame 2 to 1
  */

  var current_plan_offset = weatheranimatestate%400; //cycle every 200 ticks

  if(current_plan_offset>=150 && current_plan_offset<=200)
  {
    setweatheropacities((200-current_plan_offset) * 2);
  }
  if(current_plan_offset>=350 && current_plan_offset<=400)
  {
    setweatheropacities((current_plan_offset-350) * 2);
  }

  ++weatheranimatestate;
}
