﻿/// <reference Name="MicrosoftAjax.js" />

Type.registerNamespace("ServerTransformed");

ServerTransformed.ToyBoxPresenter = function(model, view)
{
    this._model = model;
    this._view = view;
    
    this._contentLoadedDelegate = Function.createDelegate(this, this._contentLoaded);
    this._playIntroAnimationDelegate = Function.createDelegate(this, this._playIntroAnimation);
    
    this._loadMainNavigationDelegate = Function.createDelegate(this, this._loadMainNavigation);
    
    this._pageLoadDelegate = Function.createDelegate(this, this._pageLoad);
    this._processViewEndDelegate = Function.createDelegate(this, this._processViewEnd);
    
    this._onFontsLoadedHandler = Function.createDelegate(this, this._fontsLoaded);
    this._tabChangedDelegate = Function.createDelegate(this, this._tabChanged);
    
    this._backHomeDelegate = Function.createDelegate(this, this._backHome);
    this._playReverseVideoDelegate = Function.createDelegate(this, this._playReverseVideo);
}

ServerTransformed.ToyBoxPresenter.prototype = 
{
    init: function()
    {
        //attach to view events
        this._view.add_xamlLoadedHandler(this._contentLoadedDelegate);
        this._view.add_videoEndedHandler(this._playIntroAnimationDelegate);
        this._view.add_backBtnClickHandler(this._loadMainNavigationDelegate);
        
        //attach to model events
        //model will fire an event after fonts are loaded
        this._model.add_pageLoadedHandler(this._pageLoadDelegate);
        this._model.add_pageUnloadHandler(this._processViewEndDelegate);
        this._model.add_tabChangedHandler(this._tabChangedDelegate);
        
        this._model.add_fontsLoadedHandler(this._onFontsLoadedHandler);
        
        //start the ball rolling...
        this._model.init();
    },
    
    _backHome: function(sender, eventArgs)
    {
        this._view.remove_xamlLoadedHandler(this._contentLoadedDelegate);
        this._view.remove_videoEndedHandler(this._playIntroAnimationDelegate);
        this._model.remove_pageLoadedHandler(this._pageLoadDelegate);
        this._model.remove_pageUnloadHandler(this._processViewEndDelegate);
        this._model.remove_fontsLoadedHandler(this._onFontsLoadedHandler);
        this._model.remove_tabChangedHandler(this._tabChangedDelegate);
        this._view.remove_animationEndedHandler(this._backHomeDelegate);
        this._model.exit();
    },
    
    _contentLoaded: function(sender, eventArgs)
    {
        this._model.configureNavigationItems();
        this._model.loadFonts();
    },
    
    _fontsLoaded: function(sender, eventArgs)
    {
        this._model.set_currentTabContent($get("Wallpapers"));
        this._view.playVideo(this._model.get_introVideo(), Sys.EventArgs.Empty);
    },
    
    _playIntroAnimation: function(sender, eventArgs)
    {
        this._view.set_currentAnimation(this._model.get_animation());
        this._view.playVideo(this._model.get_exitVideo(), this._backHomeDelegate);
    },
    
    _loadMainNavigation: function(sender, eventArgs)
    {
        this._model.set_pageURL("Exit");
    },
    
    _processViewEnd: function(sender, eventArgs)
    {
        this._model.removeHandlersForExit();
        this._view.add_animationEndedHandler(this._playReverseVideoDelegate);
        this._view.set_currentAnimation(this._model.get_animation());
    },
    
    _playReverseVideo: function(sender, eventArgs)
    {
        this._view.playExitVideo();
    },
    
    _pageLoad: function(sender, eventArgs)
    {
        this._view.set_pageXAML(this._model.get_pageXAML());
    },
    
    _tabChanged: function(sender, eventArgs)
    {
        this._view.set_tabContent(this._model.get_currentTabContent());
    }
}

ServerTransformed.ToyBoxPresenter.registerClass("ServerTransformed.ToyBoxPresenter");