var currentSubMenu;
var headerBackground;
var clouds = new Object();
var cloudAnimations = new Object();
var cloudConnections = new Object();

dojo.addOnLoad(function(){
  currentSubMenu = dojo.byId('menu_home');
  headerBackground = dojo.byId('background');
  clouds.c1 = dojo.byId('cloud1');
  clouds.c2 = dojo.byId('cloud2');
  clouds.c3 = dojo.byId('cloud3');
  clouds.c4 = dojo.byId('cloud4');
  clouds.c5 = dojo.byId('cloud5');
  clouds.c6 = dojo.byId('cloud6');
  animateCloud(clouds.c1, true);
  animateCloud(clouds.c2, true);
  animateCloud(clouds.c3, true);
  animateCloud(clouds.c4, true);
  animateCloud(clouds.c5, true);
  animateCloud(clouds.c6, true);
  dojo.style(currentSubMenu, 'display', 'block');
  dojo.style(currentSubMenu, 'opacity', 1);
  
});

function changeSubMenu(menuName){
  //console.log(menuName);
 var newSubMenu = dojo.byId(menuName);
 var oldSubMenu = currentSubMenu;
  if(currentSubMenu != newSubMenu)
  {
    fade = dojo.fx.combine([dojo.fadeIn({node: newSubMenu, duration:100}),dojo.fadeOut({node: currentSubMenu, duration: 100})]);
    dojo.style(newSubMenu, 'display', 'block');
    dojo.connect(fade, 'onEnd', function(){
        //console.log('end');
        dojo.style(oldSubMenu, 'display', 'none');
        
      });
    fade.play();
    currentSubMenu = newSubMenu;
  }
}

function animateCloud(cloud, first)
{
  dojo.style(cloud, 'top', (Math.floor(Math.random()*90)-20)+'px');
  if(first==true)
  {
    var left=Math.floor(Math.random()*99);
    console.log(left);
    var animDuration = (40000+Math.floor(Math.random()*31000))*(1-left/100);
    console.log(animDuration);
  }
  else
  {
    var left=-40;
    var animDuration = (40000+Math.floor(Math.random()*31000));
  }
  var anim = dojo.animateProperty({
    node:cloud,
    easing: dojo.fx.easing.linear,
    duration: animDuration,
    properties: {
      left: {start: left, end: 100, units:"%"}
    }
  });
  dojo.connect(anim, 'onEnd', this, function(){
    animateCloud(cloud,false);
    }
  );
  anim.play();
}
