﻿/// <reference Name="MicrosoftAjax.js" />

Type.registerNamespace("ServerTransformed");

ServerTransformed.VideoContentPresenter = function(model, view)
{
    this._model = model;
    this._view = view;
    
    this._contentLoadedDelegate = Function.createDelegate(this, this._contentLoaded);
    this._playIntroAnimationDelegate = Function.createDelegate(this, this._playIntroAnimation);
    
    this._initMainVideoPlayerDelegate = Function.createDelegate(this, this._initMainVideoPlayer);
    this._loadMainNavigationDelegate = Function.createDelegate(this, this._loadMainNavigation);
    
    this._pageLoadDelegate = Function.createDelegate(this, this._pageLoad);
    this._processViewEndDelegate = Function.createDelegate(this, this._processViewEnd);
    
    this._backHomeDelegate = Function.createDelegate(this, this._backHome);
    this._playReverseVideoDelegate = Function.createDelegate(this, this._playReverseVideo);
    
    this._onFontsLoadedHandler = Function.createDelegate(this, this._fontsLoaded);
    
    this._sliderMouseMoveDelegate = Function.createDelegate(this, this._sliderMouseMove);
    
    this._trackingEventDelegate = Function.createDelegate(this, this._trackEvent);
}

ServerTransformed.VideoContentPresenter.prototype = 
{
    init: function()
    {
        //attach to view events
        this._view.add_xamlLoadedHandler(this._contentLoadedDelegate);
        this._view.add_videoEndedHandler(this._playIntroAnimationDelegate);
        this._view.add_animationEndedHandler(this._initMainVideoPlayerDelegate);
        this._view.add_backBtnClickHandler(this._loadMainNavigationDelegate);
        this._view.add_positionChangeRequest(this._sliderMouseMoveDelegate);
        this._view.add_trackEventHandler(this._trackingEventDelegate);
        
        //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_fontsLoadedHandler(this._onFontsLoadedHandler);
        
        //start the ball rolling...
        this._model.init();
    },
    
    _trackEvent: function(sender, eventArgs) { this._model.trackEvent(this._view.get_mainVideo(), eventArgs.verb); },
    
    _backHome: function(sender, eventArgs)
    {
        this._view.remove_xamlLoadedHandler(this._contentLoadedDelegate);
        this._view.remove_videoEndedHandler(this._playIntroAnimationDelegate);
        this._view.remove_animationEndedHandler(this._initMainVideoPlayerDelegate);
        this._view.remove_positionChangeRequest(this._sliderMouseMoveDelegate);
        this._view.remove_trackEventHandler(this._trackingEventDelegate);
        this._model.remove_pageLoadedHandler(this._pageLoadDelegate);
        this._model.remove_pageUnloadHandler(this._processViewEndDelegate);
        this._model.remove_fontsLoadedHandler(this._onFontsLoadedHandler);
        this._view.remove_animationEndedHandler(this._backHomeDelegate);
        this._view.dispose();
        this._model.exit();
    },
    
    _contentLoaded: function(sender, eventArgs)
    {
        this._model.configureNavigationItems();
        this._model.loadFonts();
    },
    
    _fontsLoaded: function(sender, eventArgs)
    {
        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);
    },
    
    _initMainVideoPlayer:function(sender, eventArgs)
    {
        this._view.remove_animationEndedHandler(this._initMainVideoPlayerDelegate);
        this._view.set_mainVideo(this._model.get_currentVideo());
    },
    
    _loadMainNavigation: function(sender, eventArgs)
    {
        this._model.set_pageURL("Exit");
    },
    
    _processViewEnd: function(sender, eventArgs)
    {
        this._model.removeHandlersForExit();
        this._view.stopVideo();
        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());
    },
    
    _sliderMouseMove: function(sender, eventArgs)
    {
        this._view.set_currentPosition(eventArgs.newValue);
    }
}

ServerTransformed.VideoContentPresenter.registerClass("ServerTransformed.VideoContentPresenter");