
dojo.require( "dojox.fx" );
//dojo.require( "dijit._base" );
//dojo.require( "dijit.Menu" );
//dojo.require( "dijit.Tooltip" );

var loading = {
  show: function() {
    dojo.style( "loading", "display", "" );
  },

  hide: function() {
    dojo.style( "loading", "display", "none" );
  }
}

var home = {
  show: function() {
    dojo.require( "dojo.cookie" );
    dojo.require( "dojo.date" );
    var visit = dojo.cookie( "fmaa_visit" );
    var now = new Date();
    if ( !visit || (dojo.date.difference(new Date(Number(visit)), now, "day") >= 7) ) {
      this.showSplash();
    }
    else {
      this.showHome();
    }
    dojo.cookie( "fmaa_visit", now.getTime() );
  },

  showHome: function() {
    loading.hide();

    this.finale();
  },

  showSplash: function() {
    loading.hide();
    dojo.style( "splash", "visibility", "" );

    var viewport = dijit.getViewport();
  
    var splashImg = dojo.byId("splashImg");
    var box = dojo.coords( splashImg );

    dojo.style( splashImg, "position", "absolute" );
    dojo.style( splashImg, "height", viewport.h + "px" );
    /*
    if ( box.h < viewport.h ) {
      var ratio = box.h / viewport.h;
    }
    else {
      var ratio = viewport.h / box.h;
    }
    dojo.style( splashImg, "width", (ratio * viewport.w) + "px" );
    */
    box = dojo.coords( splashImg );
    dojo.style( splashImg, "left", "" + ((viewport.w - box.w) / 2) + "px" );
    dojo.style( splashImg, "top", "" + ((viewport.h - box.h) / 2) + "px" );

    var splash = dojo.byId("splash");
    dojo.style( splash, "height", box.h + "px" );
  
    this.showLogo( viewport, box );
  },
  
  showLogo: function( viewport, box ) {
    var logoImg = dojo.byId("logo");
    dojo.style( logoImg, "position", "absolute" );
    dojo.style( logoImg, "width", "1px" );
    dojo.style( logoImg, "height", "1px" );
    dojo.style( logoImg, "left", viewport.w / 2 );
    dojo.style( logoImg, "top", viewport.h / 3 );
    //dojo.style( logoImg, "opacity", "0" );
    dojo.style( logoImg, "display", "" );
/*
    var anim = dojox.fx.sizeTo({
      node: logoImg,
      duration: 1000,
      width: box.w,
      height: box.w,
      method: "combine"
    });
*/
    var anim = dojo.fx.combine([
      dojox.fx.sizeTo({
        node: logoImg,
        duration: 1000,
        //delay: 100,
        width: box.w,
        height: box.w,
        method: "combine"
      }),
      dojo.fadeIn({
        node: logoImg,
        start: 0,
        duration: 800//,
        //delay: 200
      })
    ]);
    dojo.connect( anim, "onEnd", null, home.removeSplash );
    anim.play(2000);
  },
  
  removeSplash: function() {
    var splash = dojo.byId("splash");
    var animShrink = dojo.fx.combine([
      dojox.fx.sizeTo({
        node: "logo",
        duration: 750,
        width: 150,
        height: 150,
        method: "combine"
      }),
      dojox.fx.sizeTo({
        node: "splashImg",
        duration: 200,
        width: 1,
        height: 1,
        method: "combine"
      })
    ]);
    dojo.connect( animShrink, "onBegin", null, function(){
      dojo.style( splash, "background", "#ffffff" );
    });
    dojo.connect( animShrink, "onEnd", null, function(){
      var animSlide = dojo.fx.slideTo({
        node: "logo",
        duration: 200,
        left: 35,
        top: 5,
        unit: "px"
      });
      dojo.connect( animSlide, "onEnd", null, home.finale );
      animSlide.play(250);
    });
    animShrink.play(250);
  },

  finale: function() {
    dojo.style( "splash", "visibility", "hidden" );
    dojo.style( "home", "visibility", "" );
    //var homeImg = dojo.byId("homeImg");
    //homeImg.src = "images/back-to-back_principles2.gif";
  }
}

function jumpTo(url, target) {
  if ( !target || (target == "_top") ) {
    top.location.href = url;
  }
  else if ( target == "_self" ) {
    self.location.href = url;
  }
  else if ( target == "_blank" ) {
    window.open( url );
  }
  else {
    window.open( url, target );
  }
}

