﻿/// <reference Name="MicrosoftAjax.js" />
/// <reference Name="MicrosoftAjaxTimer.js" />
/// <reference path="Timer.js" />

Type.registerNamespace("ServerTransformed");

ServerTransformed.TabContentView = function(xamlCanvas)
{
    this._controller = xamlCanvas;
    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._interval = 100;
    this._onTickHandler = Function.createDelegate(this, this._paint);
    this._progressBarWidth = 423;
    this._progressButtonStart = 60;
    this._mouseDownPosition = 0;
    this._mouseDownValue = -1;
    this._progressButtonCenter = 4;
    
    this._exitHandler = Function.createDelegate(this, this._exit);
    
}

ServerTransformed.TabContentView.prototype = 
{
    add_exitHandler: function(handler) { this.get_events().addHandler("exit", handler); },
    remove_exitHandler: function(handler) { this.get_events().removeHandler("exit", handler); },
    
    add_videoEndedHandler: function(handler) { this.get_events().addHandler("videoEnded", handler); },
    remove_videoEndedHandler: function(handler) { this.get_events().removeHandler("videoEnded", handler); },
    
    add_xamlLoadedHandler: function(handler) { this.get_events().addHandler("xamlLoaded", handler); },
    remove_xamlLoadedHandler: function(handler) { this.get_events().removeHandler("xamlLoaded", handler); },
    
    add_animationEndedHandler: function(handler) { this.get_events().addHandler("animationEnded", handler); },
    remove_animationEndedHandler: function(handler) { this.get_events().removeHandler("animationEnded", handler); },
    
    add_backBtnClickHandler: function(handler) { this.get_events().addHandler("backClick", handler); },
    remove_backBtnClickHandler: function(handler) { this.get_events().removeHandler("backClick", handler); },
    
    add_positionChangeRequest: function(handler) { this.get_events().addHandler("positionChangeRequest", handler); },
    remove_positionChangeRequest: function(handler) { this.get_events().removeHandler("positionChangeRequest", 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); },
      
    add_trackEventHandler: function(handler) { this.get_events().addHandler("trackEvent", handler); },
    remove_trackEventHandler: function(handler) { this.get_events().removeHandler("trackEvent", handler); },
    
    set_view: function(value)
    {
        if(value == this._currentView)
            return;
        else if(value == "video")
        {
            this._playVideoSelector.Cursor = "Arrow";
            this._readStorySelector.Cursor = "Hand";
            this._playVideoSelector.Visibility = "Collapsed";
            this._readStorySelector.Visibility = "Visible";
            this._videoTransition.Begin();
        }
        else if (value == "story")
        {
            if(this._playing)
            {
                this._stopVideo();
            }
            this._playVideoSelector.Cursor = "Hand";
            this._readStorySelector.Cursor = "Arrow";
            
            this._playVideoSelector.Visibility = "Visible";
            this._readStorySelector.Visibility = "Collapsed";
            
            this._storyTransition.Begin();
            this._raiseEvent("trackEvent", {verb:"read"});
        }
        
        this._currentView = value;
    },
    
    _tabTransitionEnded: function(sender, eventArgs)
    {
        if(this._currentView == "story")
        {
            this._controller.content.FindName("Story_Copy")["Canvas.ZIndex"] = 99;
            this._controller.content.FindName("Scroll_Bar")["Canvas.ZIndex"] = 99;
            this._controller.content.FindName("Main_Video_Fill")["Canvas.ZIndex"] = -99;
            this._controller.content.FindName("Main_Video")["Canvas.ZIndex"] = -99;
            this._controller.content.FindName("Video_Controls")["Canvas.ZIndex"] = -99;
        }
        else
        {
            this._controller.content.FindName("Story_Copy")["Canvas.ZIndex"] = -99;
            this._controller.content.FindName("Scroll_Bar")["Canvas.ZIndex"] = -99
            this._controller.content.FindName("Main_Video_Fill")["Canvas.ZIndex"] = 99;
            this._controller.content.FindName("Main_Video")["Canvas.ZIndex"] = 99;
            this._controller.content.FindName("Video_Controls")["Canvas.ZIndex"] = 99;
        }
    },
    
    get_mainVideo: function() { return this._mainVideo.Source; },
    set_mainVideo: function(videoURL)
    {
        this._bigPlayButton.Opacity = "0";
        this._smallPlayButton.Opacity = "0";
        this._smallPauseButton.Opacity = "1";
        this._mainVideo.Stop();
        this._mainVideo.AutoPlay = "True";
        this._playing = true;
        this._mainVideo.Source = videoURL;
        this._progressButton["Canvas.Left"] = this._progressButtonStart;
        this._playTime.Text = "00:00:00";
        
        if((this._mainVideo.BufferingProgress == 0) &&
            (this._mainVideo.DownloadProgress == 1))
        {
            this._progressBar.Width = 433;
            this._mainVideo.removeEventListener("BufferingProgressChanged", "this._bufferingProgressChangedDelegate");
        }
        this._raiseEvent("trackEvent", {verb:"Play"});
        this._updateTimer = $create(ServerTransformed.Timer, {interval:this._interval, enabled:true}, {tick:this._onTickHandler}, null, null);
    },
    
    set_currentAnimation: function(value)
    {
        var currentAnim = this._controller.content.FindName(value)
        this._animationEndedDelegate = Function.createDelegate(this, this._animationEnded);
        currentAnim.addEventListener("Completed", this._animationEndedDelegate);
        try
        {
            currentAnim.Stop();
            currentAnim.Begin();
        }
        catch(ex)
        {
            Sys.Debug.trace(ex.message);
        }
    },
    
    set_pageXAML: function(value)
    {
        this._contentViewport.children.Clear();
        this._contentViewport.children.Add(value);
        
        this.add_exitHandler(this._exitHandler);
        
        this._videoEndedDelegate = Function.createDelegate(this, this._videoEnded);
        
        this._bodyCopy = this._controller.content.FindName("BodyCopy");
        
        this._playVideoSelector = this._controller.content.FindName("Play_Video_btn");
        this._readStorySelector = this._controller.content.FindName("Read_Story_btn");
        this._mainVideo = this._controller.content.FindName("Main_Video");
        this._bigPlayButton = this._controller.content.FindName("Large_Play");
        this._smallPlayButton = this._controller.content.FindName("Play_Btn");
        this._smallPauseButton = this._controller.content.FindName("Pause_btn");
        this._progressButton = this._controller.content.FindName("Progress_Scrub");
        this._playTime = this._controller.content.FindName("Time");
        this._shareButton = this._controller.content.FindName("Share_text");
        this._downloadClick = this._controller.content.FindName("Download_text");
        this._progressBar = this._controller.content.FindName("Progress_Bar");
        
        this._progressButtonMouseDownDelegate = Function.createDelegate(this, this._progressButtonMouseDown);
        this._progressButton.addEventListener("MouseLeftButtonDown", this._progressButtonMouseDownDelegate);
        this._progressButtonMouseUpDelegate = Function.createDelegate(this, this._progressButtonMouseUp);
        this._progressButton.addEventListener("MouseLeftButtonUp", this._progressButtonMouseUpDelegate);
        this._progressButtonMouseMoveDelegate = Function.createDelegate(this, this._progressButtonMouseMove);
        this._progressButton.addEventListener("MouseMove", this._progressButtonMouseMoveDelegate);
        this._progressBarMouseDownDelegate = Function.createDelegate(this, this._progressBarMouseDown);
        this._progressBar.addEventListener("MouseLeftButtonDown", this._progressBarMouseDownDelegate);
        
        this._storyTransition = this._controller.content.FindName("Read_Story_Transition");
        this._videoTransition = this._controller.content.FindName("Play_Video_Transition");
        
        this._tabTransitionEndedHandler = Function.createDelegate(this, this._tabTransitionEnded);
        this._storyTransition.addEventListener("Completed", this._tabTransitionEndedHandler);
        this._videoTransition.addEventListener("Completed", this._tabTransitionEndedHandler);
        
        this._playPauseClickDelegate = Function.createDelegate(this, this._togglePlay);
        this._bigPlayButton.addEventListener("mouseleftbuttonup", this._playPauseClickDelegate);
        this._smallPlayButton.addEventListener("mouseleftbuttonup", this._playPauseClickDelegate);
        this._smallPauseButton.addEventListener("mouseleftbuttonup", this._playPauseClickDelegate);
        
        this._bufferingProgressChangedDelegate = Function.createDelegate(this, this._bufferingProgressChanged);
        this._mainVideo.addEventListener("BufferingProgressChanged", this._bufferingProgressChangedDelegate);
        
        this._mainVideoEndedDelegate = Function.createDelegate(this, this._mainVideoEnded);
        this._mainVideo.addEventListener("mediaended", this._mainVideoEndedDelegate);
        
        this._playVideoClickDelegate = Function.createDelegate(this, this._playVideoClick);
        this._playVideoSelector.addEventListener("mouseleftbuttonup", this._playVideoClickDelegate);
        this._playVideoOverDelegate = Function.createDelegate(this, this._playVideoOver);
        this._playVideoSelector.addEventListener("mouseenter", this._playVideoOverDelegate);
        this._playVideoLeaveDelegate = Function.createDelegate(this, this._playVideoLeave);
        this._playVideoSelector.addEventListener("mouseleave", this._playVideoLeaveDelegate);
        
        this._readStoryClickDelegate = Function.createDelegate(this, this._readStoryClick);
        this._readStorySelector.addEventListener("mouseleftbuttonup", this._readStoryClickDelegate);
        this._readStoryOverDelegate = Function.createDelegate(this, this._readStoryOver);
        this._readStorySelector.addEventListener("mouseenter", this._readStoryOverDelegate);
        this._readStoryLeaveDelegate = Function.createDelegate(this, this._readStoryLeave);
        this._readStorySelector.addEventListener("mouseleave", this._readStoryLeaveDelegate);
        
        this._downloadClickHandler = Function.createDelegate(this, this._downloadLinkClicked);
        this._downloadClick.addEventListener("MouseLeftButtonUp", this._downloadClickHandler);
        this._shareBtnClickDelegate = Function.createDelegate(this, this._shareBtnClick);
        this._shareButton.addEventListener("mouseleftbuttonup", this._shareBtnClickDelegate);
        this._buildShareLinks(this._controller.content.FindName("Share"));

        this._playVideoSelector.Cursor = "Arrow";
        this._readStorySelector.Cursor = "Hand";
        
        this._progressBar.Width = 1;
        this._progressButton["Canvas.Left"] = this._progressButtonStart;
        this._playTime.Text = "00:00:00";
        
        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._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("Story_Copy")["Canvas.Top"];

        this._raiseEvent("xamlLoaded");
    },
    
    set_Volume: function(value) { this._mainVideo.Volume = value; },
    
    get_currentPosition: function() { return this._progressButton["Canvas.Left"]; },
    set_currentPosition: function(value)
    {
        if(value > this._progressBarWidth + this._progressButtonStart)
        {
            value = this._progressBarWidth + this._progressButtonStart;
        }
        if (value <= this._progressButtonStart)
        {
            value = this._progressButtonStart;
        }
        
        this._progressButton["Canvas.Left"] = value;
        this._set_VideoPosition(value);
    },
    
    _progressButtonMouseDown: function(sender, eventArgs)
    {
        sender.captureMouse();
        this._mouseDownValue = this.get_currentPosition();
        this._mouseDownPosition = eventArgs.getPosition(null).x;
    },
    
    _progressButtonMouseUp: function(sender, eventArgs)
    {
        sender.releaseMouseCapture();
        this._mouseDownValue = -1;
    },
    
    _progressButtonMouseMove: function(sender, eventArgs)
    {
        if(this._mouseDownValue != -1)
        {
            var newValue = this._mouseDownValue + (eventArgs.getPosition(null).x - this._mouseDownPosition);
            this._raiseEvent("positionChangeRequest", {"newValue":newValue});
        }
    },
    
    _progressBarMouseDown: function(sender, eventArgs)
    {
        var coordinate = eventArgs.getPosition(null).x - 112;
        coordinate -= this._progressBar["Canvas.Left"] + (this._progressBar.StrokeThickness * 2);
        var newValue = coordinate - this._progressButtonCenter;
        this._raiseEvent("positionChangeRequest", {"newValue":newValue});
    },
    
    _downloadLinkClicked: function(sender, eventArgs)
    {
        window.open(this._mainVideo.Source);
        this._raiseEvent("trackEvent", {verb:"Download"});
    },
    
    _buildHyperlink: function(linkCanvas)
    {
        var clickFunction = Function.createDelegate(this, this._shareLinkClicked);
        linkCanvas.addEventListener("MouseLeftButtonUp", clickFunction);
    },
    
    _shareLinkClicked: function(sender, eventArgs)
    {
        var URL = ServerTransformed.Utilities.URLLookup(sender.name.split('_')[0], "www.serverunleashed.com", "Windows Server 2008");
        window.open(URL);
        this._shareBtnClick(sender, eventArgs);
        this._raiseEvent("trackEvent", {verb:"Share"});
    },
    
    _buildShareLinks: function(shareCanvas)
    {
        for(var i = 0; i < shareCanvas.children.count; i++)
        {
            var linkCanvas = shareCanvas.children.GetItem(i);
            if(linkCanvas.toString() == "Canvas")
            {
                this._buildHyperlink(linkCanvas);
            }
        }
    },
    
    _shareBtnClick: function(sender, eventArgs)
    {
        if(this._isShareOpen)
        {
            this._controller.content.FindName("Share_Default").Begin();
            this._isShareOpen = false;
            this._controller.content.FindName("Share").Visibility = "Collapsed";
        }
        else
        {
            this._controller.content.FindName("Share").Visibility = "Visible";
            this._controller.content.FindName("Share_Opening").Begin();
            this._isShareOpen = true;
        }
    },
    
    set_storyText: function(value)
    { 
        var copyXAML = this._controller.content.CreateFromXaml(value);
        
        //copyXAML.Name = bodyCopy.Name;
        copyXAML.Width = this._bodyCopy.Width;
        copyXAML.TextWrapping = this._bodyCopy.TextWrapping;
        copyXAML.FontFamily = this._bodyCopy.FontFamily;
        copyXAML.FontSize = this._bodyCopy.FontSize;
        copyXAML.Foreground = "#FF355970";
        
        this._controller.content.FindName("Story_Copy").Children.Clear();
        this._controller.content.FindName("Story_Copy").Children.Add(copyXAML);
        this.set_TextPosition(0, false);
    },
    
    _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("Story_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;
    },
    
    _playVideoClick: function(sender, eventArgs)
    {
        this.set_view("video");
    },
    
    _playVideoOver: function(sender, eventArgs)
    {
        this.set_currentAnimation("Play_Video_btn_Hover");
    },
    
    _playVideoLeave: function(sender, eventArgs)
    {
        this.set_currentAnimation("Play_Video_btn_Default");
    },
    
    _readStoryClick: function(sender, eventArgs)
    {
        this.set_view("story");
    },
    
    _readStoryOver: function(sender, eventArgs)
    {
        this.set_currentAnimation("Read_Story_btn_Hover");
    },
    
    _readStoryLeave: function(sender, eventArgs)
    {
        this.set_currentAnimation("Read_Story_btn_Default");
    },
        
    _bufferingProgressChanged: function(sender, eventArgs)
    {
        if(sender.BufferingProgress != 0)
        {
            this._progressBar.Width = (433 * sender.BufferingProgress);
        }
        else
        {
            this._progressBar.Width = 433;
            this._mainVideo.removeEventListener("BufferingProgressChanged", "this._bufferingProgressChangedDelegate");   
        }
    },
    
    _set_VideoPosition: function(value)
    {
        var percentCompleted = (value-this._progressButtonStart)/this._progressBarWidth;
        var newValue = this._mainVideo.NaturalDuration.Seconds * percentCompleted;
        this._mainVideo.Position = ServerTransformed.Utilities.ConvertSecondsToTimeCode(newValue);
        this._updateCurrentTime();
    },
    
    _mainVideoEnded: function(sender, eventArgs)
    {
        this._raiseEvent("trackEvent", {verb:"Complete"});
        this._togglePlay(sender, eventArgs);
    },
    
    _stopVideo: function()
    {
        this._playing = false;
        
        if(this._updateTimer)
        {
            this._updateTimer.set_enabled(false);
            this._updateTimer = null;
        }
        
        this._mainVideo.Pause();
        this._bigPlayButton.Opacity = "1";
        this._smallPlayButton.Opacity = "1";
        this._smallPauseButton.Opacity = "0";
    },
    
    _playVideo: function()
    {
        if (Math.ceil(this._mainVideo.Position.Seconds) >= Math.floor(this._mainVideo.NaturalDuration.Seconds))
        {
            this._progressButton["Canvas.Left"] = 60;
            this._playTime.Text = "00:00:00";
            this._mainVideo.Stop();
        }
        
        if(Math.floor(this._mainVideo.Position.Seconds) <= 1)
        {
            this._raiseEvent("trackEvent", {verb:"Play"});
        }
        
        this._playing = true;

        this._updateTimer = $create(ServerTransformed.Timer, {interval:this._interval, enabled:true}, {tick:this._onTickHandler}, null, null);
        this._mainVideo.Volume = 1;
        this._mainVideo.Play();
        this._bigPlayButton.Opacity = "0";
        this._smallPlayButton.Opacity = "0";
        this._smallPauseButton.Opacity = "1";
    },
    
    _togglePlay: function(sender, eventArgs)
    {
        if(this._playing)
        {
            this._stopVideo();
        }
        else
        {
            this._playVideo();
        }
    },
    
    _paint: function(sender, eventArgs)
    {
        if(this._mouseDownValue == -1)
            this._progressButton["Canvas.Left"] = (428 * (this._mainVideo.Position.Seconds/(this._mainVideo.NaturalDuration.Seconds + 1))) + 60;
        
        this._updateCurrentTime();
    },
    
    _updateCurrentTime: function()
    {
        var currentTime = Math.ceil(this._mainVideo.Position.Seconds);

        this._playTime.Text = ServerTransformed.Utilities.ConvertSecondsToTimeCode(currentTime);
    },
    
    playVideo: function(url, args)
    {
        this._videoPlayer2.Opacity = 1;
        
        var player = this._videoPlayer2;
        
        if(args == Sys.EventArgs.Empty)
        {
            this._contentViewport.Opacity = 0;
            player.addEventListener("mediaended", this._videoEndedDelegate);
            player.AutoPlay = "true";
        }
        else
        {
            player.addEventListener("mediaended", Function.createDelegate(this, this._exit));
            player.addEventListener("mediaended", args);
            player.AutoPlay = "false";
        }
                
        player.Source = url;
        this._videoPlayer1.Pause();       
    },
    
    stopVideo: function()
    {
        this._mainVideo.Stop();
    },
    
    _animationEnded: function(sender, eventArgs) 
    { 
        sender.removeEventListener("Completed", "this._animationEndedDelegate");
        this._raiseEvent("animationEnded"); 
    },
    
    _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)
    {
        if(this._updateTimer)
        {
            this._updateTimer.set_enabled(false);
            this._updateTimer = null;
        }
        
        this.remove_exitHandler(this._exitHandler);
        
        this._videoPlayer2.Opacity = 0;
        this._videoPlayer2.Stop();
        this._raiseEvent("exit");
    },
    
    dispose: function()
    {
        this._exit(this, Sys.EventArgs.Empty);
    },
    
    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.TabContentView.registerClass("ServerTransformed.TabContentView");