﻿/// <reference Name="MicrosoftAjax.js" />
Type.registerNamespace("ServerTransformed");

ServerTransformed.MainNavigationPresenter = function(model, view)
{
    this._model = model;
    this._view = view;
    
    this._xamlLoadedDelegate = Function.createDelegate(this, this._contentLoaded);
    this._filterSlectedDelegate = Function.createDelegate(this, this._filterSelected);
    this._exitHandlerDelegate = Function.createDelegate(this, this._processViewEnd);
    this._pageLoadedDelegate = Function.createDelegate(this, this._pageLoad);
    this._videoChangedDelegate = Function.createDelegate(this, this._loadVideo);
    this._exitAnimationDelegate = Function.createDelegate(this, this._exit);
    this._onFontsLoadedHandler = Function.createDelegate(this, this._fontsLoaded);
    
    this._onImpatientFire = Function.createDelegate(this, this._showImpatient);
}

ServerTransformed.MainNavigationPresenter.prototype = 
{
    init: function() 
    {
        //attach to view events
        this._view.add_xamlLoadedHandler(this._xamlLoadedDelegate);
        this._view.add_filterSelectedHandler(this._filterSlectedDelegate);
        this._view.add_exitHandler(this._exitHandlerDelegate);
        
        //attach to model events
        //model will fire an event after fonts are loaded
        this._model.add_pageLoadedHandler(this._pageLoadedDelegate);
        this._model.add_videoChangedHandler(this._videoChangedDelegate);
        this._model.add_exitHandler(this._exitAnimationDelegate);
        this._model.add_playImpatientVideoHandler(this._onImpatientFire);
        //this._model.add_filterChangedHandler(Function.createDelegate(this, this._playFilterAnimations));
        
        this._model.add_fontsLoadedHandler(this._onFontsLoadedHandler);
        
        //start the ball rolling...
        this._model.set_page("mainNavigation");
    },
    
    _showImpatient: function(sender, eventArgs)
    {
        this._view.playVideo(this._model.get_impatientVideo(), {impatient:true});
    },
    
    _contentLoaded: function(sender, eventArgs)
    {
        this._model.configureNavigationItems();
        this._model.loadFonts();
    },
    
    _fontsLoaded: function(sender, eventArgs)
    {
        this._view.set_currentAnimation(this._model.get_animation());
    },
    
    _loadVideo: function(sender, eventArgs)
    {
        this._view.set_currentAnimation(this._model.get_animation());
        this._view.playVideo(this._model.get_video(), eventArgs);
    },
    
    _exit: function(sender, eventArgs)
    {
        // The problem with this is that I am too eager to change pages.
        // by the time this is actually called the page has already loaded the
        // next section and the Exit animation is no longer visible.
        // I need to figure this out better.
        //this._view.set_currentAnimation(this._model.get_animation());
        //
    },
        
    _filterSelected: function(sender, eventArgs)
    {
        this._model.set_filter(eventArgs.filter);
    },
    
    _playFilterAnimations: function(sender, eventArgs)
    {
        this._view.set_animationPlayList(this._model.get_filterAnimations());
    },
    
    _processViewEnd: function(sender, eventArgs)
    {
        this._view.remove_xamlLoadedHandler(this._xamlLoadedDelegate);
        this._view.remove_filterSelectedHandler(this._filterSlectedDelegate);
        this._view.remove_exitHandler(this._exitHandlerDelegate);
        this._view.clearEvents();
        this._model.remove_pageLoadedHandler(this._pageLoadedDelegate);
        this._model.remove_videoChangedHandler(this._videoChangedDelegate);
        this._model.exit();        
    },
    
    _pageLoad: function(sender, eventArgs)
    {
        this._view.set_pageXAML(this._model.get_pageXAML());
    }
}
ServerTransformed.MainNavigationPresenter.registerClass("ServerTransformed.MainNavigationPresenter");