﻿/// <reference path="~/Default_aspx.js" />
/// <reference path="Utilities.js" />
Type.registerNamespace("ServerTransformed");

ServerTransformed.TextContentPresenter = function(model, view)
{
    this._model = model;
    this._view = view;

    this._thumbCenter = 4;
    
    this._contentLoadedDelegate = Function.createDelegate(this, this._contentLoaded);
    this._playIntroAnimationDelegate = Function.createDelegate(this, this._playIntroAnimation);
    this._pageLoadDelegate = Function.createDelegate(this, this._pageLoad);
    this._playVideoDelegate = Function.createDelegate(this, this._backHome);
    this._processViewEndDelegate = Function.createDelegate(this, this._processViewEnd);
    this._onFontsLoadedHandler = Function.createDelegate(this, this._fontsLoaded);
    
    this._scrollTrackMouseDownDelegate = Function.createDelegate(this, this._scrollBarMouseDown);
    this._scrollDraggerMouseMoveDelegate = Function.createDelegate(this, this._thumbMouseMove);
    this._copyMouseWheelMoveDelegate = Function.createDelegate(this, this._mouseWheelScroll);
}

ServerTransformed.TextContentPresenter.prototype = 
{
    init: function()
    {
        //attach to view events
        this._view.add_xamlLoadedHandler(this._contentLoadedDelegate);
        this._view.add_videoEndedHandler(this._playIntroAnimationDelegate);
        this._view.add_scrollTrack_MouseDown(this._scrollTrackMouseDownDelegate);
        this._view.add_scrollDragger_MouseMove(this._scrollDraggerMouseMoveDelegate);
        this._view.add_copy_MouseWheelMove(this._copyMouseWheelMoveDelegate);
        
        //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();
    },
    
    _scrollBarMouseDown: function(sender, eventArgs)
    {
        var coordinate = eventArgs.CurrentPosition;
        coordinate -= this._view.get_canvasTop();
        var scrollAmount;
        var currentValue = this._view.get_ThumbTop();
        var diff = coordinate-currentValue;
        if(diff<0)
            scrollAmount = -150;
        else if (diff == 0)
            scrollAmount = 0;
        else
            scrollAmount = 150;
        
        var newPosition = currentValue + scrollAmount;
        
        this._view.set_TextPosition(newPosition, true);
    },
    
    _thumbMouseMove: function(sender, eventArgs)
    {
        if(eventArgs.MouseDownValue != -1)
        {
            var newValue = eventArgs.MouseDownValue + 
                (eventArgs.CurrentPosition - eventArgs.MouseDownPosition);
            this._view.set_TextPosition(newValue, false);
        }
    },
    
    _mouseWheelScroll:function(sender, eventArgs)
    {
        var newPosition = this._view.get_ThumbTop() - eventArgs.delta;
        this._view.set_TextPosition(newPosition);
    },
    
    _backHome: function(sender, eventArgs)
    {
        //this._view.remove_playAnimationHandler(this._model.get_animation(), this._playAnimationDelegate);
        this._view.remove_xamlLoadedHandler(this._contentLoadedDelegate);
        this._view.remove_videoEndedHandler(this._playIntroAnimationDelegate);
        this._model.remove_fontsLoadedHandler(this._onFontsLoadedHandler);
        this._model.remove_pageLoadedHandler(this._pageLoadDelegate);
        this._model.remove_pageUnloadHandler(this._processViewEndDelegate);
        
        this._model.exit();
    },
    
    _contentLoaded: function(sender, eventArgs)
    {
        this._model.configureNavigationItems();
        this._model.loadFonts();
        this._setSubhead();
        this._setCopy();
    },
    
    _fontsLoaded: function(sender, eventArgs)
    {
        this._view.playVideo(this._model.get_introVideo(), Sys.EventArgs.Empty);
        this._view.set_TextPosition(0, false);
        
        // VV: put in code to only call function below if page is loaded NOT from home
        this._model.trackPageLoad();
    },
    
    _playIntroAnimation: function(sender, eventArgs)
    {
        this._view.set_currentAnimation(this._model.get_animation());
        this._view.playVideo(this._model.get_exitVideo(), this._playVideoDelegate);
    },
    
    _processViewEnd: function(sender, eventArgs)
    {
        this._model.removeHandlersForExit();
        this._playAnimationDelegate = Function.createDelegate(this, this._playReverseVideo);
        this._view.playAnimation(this._model.get_animation(), this._playAnimationDelegate);
    },
    
    _playReverseVideo: function(sender, eventArgs)
    {
        
        this._view.playExitVideo();
    },
    
    _setSubhead: function(sender, eventArgs)
    {
        this._view.set_subHead(this._model.get_subHead());
    },
    
    _setCopy: function(sender, eventArgs)
    {
        this._view.set_copy(this._model.get_copy());
    },
    
    _pageLoad: function(sender, eventArgs)
    {
        this._view.set_pageXAML(this._model.get_pageXAML());
    }
}

ServerTransformed.TextContentPresenter.registerClass("ServerTransformed.TextContentPresenter");