﻿/// <reference path="~/Default_aspx.js" />
/// <reference path="Utilities.js" />
Type.registerNamespace("ServerTransformed");

ServerTransformed.TextContentView = function(controller)
{
    this._controller = controller;
    this._contentViewport = this._controller.content.FindName("ContentViewport");
    this._videoPlayer1 = this._controller.content.FindName("VideoPlayer1");
    this._videoPlayer2 = this._controller.content.FindName("VideoPlayer2");
    this._animationFadeIn = this._controller.content.FindName("fadeIn");
    this._animationFadeOut = this._controller.content.FindName("fadeOut");
    
//    this._videoPlayer1.Stop();
//    this._videoPlayer2.Stop();
//    this._videoPlayer1.Opacity = 0;
//    this._videoPlayer2.Opacity = 0;
    
    this._mouseDownPosition = 0;
    this._mouseDownValue = -1;
    this._thumbCenter = 4;        
}

ServerTransformed.TextContentView.prototype =
{
    add_exitHandler: function(handler) { this.get_events().addHandler("exit", handler); },
    remove_exitHandler: function(handler) { this.get_events().removeHandler("exit", handler); },

    add_xamlLoadedHandler: function(handler) { this.get_events().addHandler("xamlLoaded", handler); },
    remove_xamlLoadedHandler: function(handler) { this.get_events().removeHandler("xamlLoaded", handler); },

    add_backButtonHandler: function(handler) { this.get_events().addHandler("goHome", handler); },
    remove_backButtonHandler: function(handler) { this.get_events().removeHandler("goHome", handler); },

    add_videoEndedHandler: function(handler) { this.get_events().addHandler("videoEnded", handler); },
    remove_videoEndedHandler: function(handler) { this.get_events().removeHandler("videoEnded", handler); },

    add_scrollTrack_MouseDown: function(handler) { this.get_events().addHandler("scrollTrackMouseDown", handler); },
    remove_scrollTrack_MouseDown: function(handler) { this.get_events().removeHandler("scrollTrackMouseDown", handler); },

    add_scrollDragger_MouseMove: function(handler) { this.get_events().addHandler("scrollDraggerMouseMove", handler); },
    remove_scrollDragger_MouseMove: function(handler) { this.get_events().removeHandler("scrollDraggerMouseMove", handler); },

    add_copy_MouseWheelMove: function(handler) { this.get_events().addHandler("mouseWheelMove", handler); },
    remove_copy_MouseWheelMove: function(handler) { this.get_events().removeHandler("mouseWheelMove", handler); },

    set_currentAnimation: function(value) { this._controller.content.FindName(value).Begin(); },

    set_pageXAML: function(value)
    {
        this._contentViewport.children.Clear();
        this._contentViewport.children.Add(value);

        this._mouseScrollDelegate = Function.createDelegate(this, this._mouseScroll);
        this._scrollBarMouseDownDelegate = Function.createDelegate(this, this._scrollBarMouseDown);
        this._thumbMouseDownDelegate = Function.createDelegate(this, this._thumbMouseDown);
        this._thumbMouseUpDelegate = Function.createDelegate(this, this._thumbMouseUp);
        this._thumbMouseMoveDelegate = Function.createDelegate(this, this._thumbMouseMove);
        this._contentMouseEnterDelegate = Function.createDelegate(this, this._contentMouseEnter);
        this._contentMouseLeaveDelegate = Function.createDelegate(this, this._contentMouseLeave);
        this._videoEndedDelegate = Function.createDelegate(this, this._videoEnded);

        this._scrollDragger = this._controller.content.FindName("scroll_dragger");
        this._scrollTrack = this._controller.content.FindName("scroll_track");
        this._scrollBar = this._controller.content.FindName("Scroll_Bar");
        this._copyCanvas = this._controller.content.FindName("Copy");

        this._scrollTween;

        this._scrollTrack.addEventListener("MouseLeftButtonDown", this._scrollBarMouseDownDelegate);
        this._scrollDragger.addEventListener("MouseLeftButtonDown", this._thumbMouseDownDelegate);
        this._scrollDragger.addEventListener("MouseLeftButtonUp", this._thumbMouseUpDelegate);
        this._scrollDragger.addEventListener("MouseMove", this._thumbMouseMoveDelegate);
        this._copyCanvas.addEventListener("MouseEnter", this._contentMouseEnterDelegate);
        this._copyCanvas.addEventListener("MouseLeave", this._contentMouseLeaveDelegate);

        this._bodyCopyTop = this._controller.content.FindName("Content")["Canvas.Top"] +
		    this._copyCanvas["Canvas.Top"] + this._controller.content.FindName("Body_Copy")["Canvas.Top"];

        //this.set_TextPosition(0, false); //move this to the set content function when we add it.
        this._raiseEvent("xamlLoaded");
    },

    set_subHead: function(value)
    {
        var subhead = this._controller.content.FindName("Subhead");
        var content = ServerTransformed.Utilities.ConvertHTMLToXAML2(value, 'Canvas.Top="0" FontFamily="Segoe" TextWrapping="Wrap" FontSize="12" Foreground="#FFFFFFFF"', "subheadContent");
        var contentBlock = this._controller.content.CreateFromXaml(content);
        subhead.Children.Clear();
        subhead.Children.Add(contentBlock);
    },

    set_copy: function(value)
    {
        var copyXAML = this._controller.content.CreateFromXaml(value);
        var bodyCopy = this._controller.content.FindName("BodyCopy");

        //copyXAML.Name = bodyCopy.Name;
        copyXAML.Width = bodyCopy.Width;
        copyXAML.TextWrapping = bodyCopy.TextWrapping;
        copyXAML.FontFamily = bodyCopy.FontFamily;
        copyXAML.FontSize = bodyCopy.FontSize;
        copyXAML.Foreground = "#FF355970";

        this._controller.content.FindName("Body_Copy").Children.Clear();
        this._controller.content.FindName("Body_Copy").Children.Add(copyXAML);

        //Sys.Debug.trace(copyXAML.Inlines.Count);

    },

    _contentMouseEnter: function(sender, eventArgs)
    {
        // MouseWheel code from http://adomas.org/javascript-mouse-wheel/
        if (window.addEventListener)
        {
            // DOMMouseScroll is for mozilla.
            document.addEventListener('DOMMouseScroll', this._mouseScrollDelegate, true);
        }
        // IE/Opera.
        else
            window.onmousewheel = document.onmousewheel = this._mouseScrollDelegate;
    },

    _contentMouseLeave: function(sender, eventArgs)
    {
        // MouseWheel code from http://adomas.org/javascript-mouse-wheel/
        if (window.addEventListener)
        {
            // DOMMouseScroll is for mozilla.
            document.removeEventListener('DOMMouseScroll', this._mouseScrollDelegate, true);

        }
        // IE/Opera.
        else
            window.onmousewheel = document.onmousewheel = null;
    },

    _mouseScroll: function(event)
    {
        var change = 0;
        if (!event)
        { // For IE.
            event = window.event;
        }

        if (event.wheelDelta)
        { //IE/Opera.
            change = (event.wheelDelta / 3)
            // In Opera 9, delta differs in sign as compared to IE.
            if (window.opera)
            {
                change = -change;
            }
        }
        else if (event.detail)
        { // Mozilla case.
            // In Mozilla, sign of delta is different than in IE.
            change = 8 * (-event.detail)

        }

        this._raiseEvent("mouseWheelMove", { delta: change });

        event.returnValue = false;
    },

    _scrollBarMouseDown: function(sender, eventArgs)
    {
        var args = {};
        args.CurrentPosition = eventArgs.getPosition(null).y;
        this._raiseEvent("scrollTrackMouseDown", args);
    },

    _thumbMouseDown: function(sender, eventArgs)
    {
        sender.captureMouse();
        this._mouseDownValue = this.get_ThumbTop();
        this._mouseDownPosition = eventArgs.getPosition(null).y;
    },

    _thumbMouseUp: function(sender, eventArgs)
    {
        sender.releaseMouseCapture();
        this._mouseDownValue = -1;
    },

    _thumbMouseMove: function(sender, eventArgs)
    {
        var args = {};
        args.MouseDownValue = this._mouseDownValue;
        args.MouseDownPosition = this._mouseDownPosition;
        args.CurrentPosition = eventArgs.getPosition(null).y;

        this._raiseEvent("scrollDraggerMouseMove", args);
    },

    get_ScrollTrackTop: function() { return this._scrollTrack["Canvas.Top"]; },

    get_ThumbTop: function() { return this._scrollDragger["Canvas.Top"]; },

    get_canvasTop: function() { return this._bodyCopyTop; },

    set_TextPosition: function(value, easeIntoIt)
    {
        if (value > this._scrollTrack.Height)
        {
            value = this._scrollTrack.Height - 9;
            this._mouseDownValue = -1;
        }

        if (value < 0)
        {
            value = 0;
            this._mouseDownValue = -1;
        }

        this._scrollDragger["Canvas.Top"] = value;

        this._set_textPosition(easeIntoIt);
    },

    _set_textPosition: function(easeIntoIt)
    {
        var textBlock = this._controller.content.FindName("BodyCopy");
        var textContentHeight = this._controller.content.FindName("BodyCopy").ActualHeight;
        var scrollBarHeight = this._scrollBar.Height;
        var sliderPosition = this._scrollDragger["Canvas.Top"];
        var textBoxHeight = this._controller.content.FindName("Body_Copy").Height;
        var canvasTopOffset = this._bodyCopyTop;

        if (textContentHeight - textBoxHeight <= 0)
        {
            this._scrollBar.Visibility = "Collapsed";
            return;
        }

        var scrollPercent = sliderPosition / scrollBarHeight;
        var pos = (textContentHeight - textBoxHeight) * scrollPercent;
        var topPosition = -Math.floor(pos);


        if (easeIntoIt)
        {
            timeToScroll = 0.75;

            if (this._scrollTween)
            {
                this._scrollTween.stop();
            }
            var scrollStart = textBlock["Canvas.Top"];

            this._scrollTween = new Tween(this, 'Canvas.Top', Tween.strongEaseOut, scrollStart,
		        topPosition, timeToScroll);

            this._scrollTween.onMotionChanged = function(event)
            {
                var startPos = textBlock["Canvas.Top"];
                textBlock["Canvas.Top"] = event.target._pos;

                new_val = event.target._pos;

                val = ((new_val - canvasTopOffset) / (scrollBarHeight)) * 100;
                var per = val / 100;
            }

            this._scrollTween.start();
        }
        else
            textBlock["Canvas.Top"] = topPosition;
    },

    playAnimation: function(id, handler)
    {
        this._animation = this._controller.content.FindName(id);
        this._animation.AddEventListener("Completed", handler);
        this._animation.Begin();
    },

    remove_playAnimationHandler: function(id, handler)
    {
        //this._animation = this._controller.content.FindName(id);
        //this._animation.RemoveEventListener("Completed", handler);
    },

    playVideo: function(url, args)
    {
        this._videoPlayer2.Opacity = 1;

        var player = this._videoPlayer2;

        if (args == Sys.EventArgs.Empty)
        {
            this._contentViewport.Opacity = 0;
            this._videoEndedDelegateID = player.addEventListener("mediaended", this._videoEndedDelegate);
            player.AutoPlay = "true";
        }
        else
        {
            player.removeEventListener("mediaended", this._videoEndedDelegateID);
            player.addEventListener("mediaended", Function.createDelegate(this, this._exit));
            player.addEventListener("mediaended", args);
            player.AutoPlay = "false";
        }

        player.Source = url;
        this._videoPlayer1.Pause();

    },

    _videoEnded: function(sender, eventArgs)
    {
        sender.removeEventListener("mediaended", "this._videoEndedDelegate");
        sender.AutoPlay = "false";
        sender.Pause();
        sender.Opacity = 0;
        this._contentViewport.Opacity = 1;
        this._raiseEvent("videoEnded");
    },

    playExitVideo: function()
    {
        this._contentViewport.Children.GetItem(0).Visibility = "Collapsed";
        var player = this._videoPlayer2;
        player.Play();
    },

    _exit: function(sender, eventArgs)
    {
        this._videoPlayer2.Opacity = 0;
        this._videoPlayer2.Stop();
        this._raiseEvent("exit");
    },

    get_events: function()
    {
        if (!this._events)
        {
            this._events = new Sys.EventHandlerList();
        }
        return this._events;
    },

    _raiseEvent: function(eventName, eventArgs)
    {
        var handler = this.get_events().getHandler(eventName);

        if (handler)
        {
            if (!eventArgs) eventArgs = Sys.EventArgs.Empty;
            handler(this, eventArgs);
        }
    }
}

ServerTransformed.TextContentView.registerClass("ServerTransformed.TextContentView");