﻿/// <reference path="MicrosoftAjax.js" />
/// <reference path="Button.xaml.js" />

if (!window.HomePageVideoPlayer)
	HomePageVideoPlayer = {};

HomePageVideoPlayer.Page = function()
{
	this.videoRootUrl = "Media/";
}

HomePageVideoPlayer.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement)
	{
		this.autoPlay = true;
		this.control = control;
		this.playCounter = 1;
		this.currentVideo = null;
		this.vidType = null;
		this.videoStill = this.control.content.findName("VideoStill");

		this.progressIndicator = this.control.content.findName("ProgressIndicator");
		this.progressIndicatorAnim = this.control.content.findName("Rotate_ProgressIndicator");

		this.muteOnSymbol = this.control.content.findName("MuteOnSymbol");
		this.pauseBtnToolTip = this.control.content.findName("PauseButtonToolTip");
		this.playBtnToolTip = this.control.content.findName("PlayButtonToolTip");
		this.muteBtnToolTip = this.control.content.findName("MuteButtonToolTip");
		this.fastForwardToolTip = this.control.content.findName("FastForwardToolTip");
		this.rewindToolTip = this.control.content.findName("RewindToolTip");

		this.video = this.control.content.findName("HomeVideo");

		this.video.addEventListener("MediaOpened", Silverlight.createDelegate(this, this.onMediaOpened));
		this.video.addEventListener("MediaEnded", Silverlight.createDelegate(this, this.onMediaEnded));
		this.video.addEventListener("CurrentStateChanged", Silverlight.createDelegate(this, this.onCurrentStateChanged));

		this.playControls = {
			PlayButton: new McCannErickson.Silverlight.Controls.Button(this.control.content.findName("PlayButton"), "PlayButton"),
			PauseButton: new McCannErickson.Silverlight.Controls.Button(this.control.content.findName("PauseButton"), "PauseButton"),
			MuteButton: new McCannErickson.Silverlight.Controls.Button(this.control.content.findName("MuteButton"), "MuteButton"),
			FastForwardButton: new McCannErickson.Silverlight.Controls.Button(this.control.content.findName("FastForwardButton"), "FastForwardButton"),
			RewindButton: new McCannErickson.Silverlight.Controls.Button(this.control.content.findName("RewindButton"), "RewindButton")
		};

		this.playControlsClickDelegate = Silverlight.createDelegate(this, this.playerClick);
		this.playControlsMouseOverDelegate = Silverlight.createDelegate(this, this.playerMouseOver);
		this.playControlsMouseLeaveDelegate = Silverlight.createDelegate(this, this.playerMouseLeave);
		for (var c in this.playControls)
		{
			this.playControls[c].addMouseUpHandler(this.playControlsClickDelegate);
			this.playControls[c].addMouseOverHandler(this.playControlsMouseOverDelegate);
			this.playControls[c].addMouseOutHandler(this.playControlsMouseLeaveDelegate);
		}

		this.video.Source = "http://mediadl.microsoft.com/MediaDL/WWW/l/learningspace/homepage.wmv";

		this.playControls.PlayButton.setSelected(true);
		this.playControls.PlayButton.handleMouseEnter();
		this.playControls.PlayButton.handleMouseLeave();
	},

	hideVideos: function()
	{
		this.video.CurrentState == "Playing";
		this.video.Stop();
		this.video.Visibility = "Collapsed";
	},

	playerClick: function(sender, eventArgs)
	{
		if (this.vidType == null)
		{
			switch (sender.getName())
			{
				case "PlayButton":
					if (this.videoStill.Visibility == "Visible")
						this.videoStill.Visibility = "Collapsed";

					this.playControls.PauseButton.setSelected(false);
					this.playControls.PauseButton.handleMouseLeave();
					if (this.video.CurrentState != "Playing")
					{
						if (this.video.Visibility != "Visible")
							this.video.Visibility = "Visible";
						this.video.Play();
					}
					break;

				case "PauseButton":
					this.playControls.PlayButton.setSelected(false);
					this.playControls.PlayButton.handleMouseLeave();
					if (this.video.CurrentState == "Playing")
					{
						this.video.Pause();
					}
					break;

				case "MuteButton":
					this.playControls.MuteButton.setSelected(false);
					this.playControls.MuteButton.handleMouseLeave();

					var vol = 0;

					if (this.video.volume == 0)
					{

						vol = 0.5;
						this.muteOnSymbol.Visibility = "Collapsed";
					}
					else
					{
						vol = 0;
						this.muteOnSymbol.Visibility = "Visible";
					}

					this.video.Volume = vol;

					break;

				case "FastForwardButton":
					this.playControls.FastForwardButton.setSelected(false);
					this.playControls.FastForwardButton.handleMouseLeave();
					this.video.Pause();
					var position = this.video.Position;
					if (position.Seconds + 30 >= this.video.NaturalDuration)
					{
						this.video.Position = this.video.NaturalDuration;
					}
					else
					{
						position.Seconds += 30;
						this.video.Position = position;
					}
					this.video.Play();
					break;

				case "RewindButton":
					this.playControls.RewindButton.setSelected(false);
					this.playControls.RewindButton.handleMouseLeave();
					this.video.Pause();
					var position = this.video.Position;
					if (position.Seconds - 30 <= 0)
					{
						this.video.Position = "00:00:00";
					}
					else
					{
						position.Seconds -= 30;
						this.video.Position = position;
					}
					this.video.Play();
					break;
			}
		}
	},

	playerMouseOver: function(sender, eventArgs)
	{
		switch (sender.getName())
		{
			case "PlayButton":
				this.playBtnToolTip.Visibility = "Visible";
				break;

			case "PauseButton":
				this.pauseBtnToolTip.Visibility = "Visible";
				break;

			case "MuteButton":
				this.muteBtnToolTip.Visibility = "Visible";
				break;

			case "FastForwardButton":
				this.fastForwardToolTip.Visibility = "Visible";
				break;

			case "RewindButton":
				this.rewindToolTip.Visibility = "Visible";
				break;
		}
	},

	playerMouseLeave: function(sender, eventArgs)
	{
		this.playBtnToolTip.Visibility = "Collapsed";
		this.pauseBtnToolTip.Visibility = "Collapsed";
		this.muteBtnToolTip.Visibility = "Collapsed";
		this.fastForwardToolTip.Visibility = "Collapsed";
		this.rewindToolTip.Visibility = "Collapsed";
	},

	onMediaEnded: function(sender, eventArgs)
	{
		sender.Position = "00:00:00";

		this.videoStill.Visibility = "Visible";
		this.hideVideos();
	},

	onMediaOpened: function(sender, eventArgs)
	{
		if (this.hasPlayedOnce())
		{
			this.videoStill.Visibility = "Visible";
			sender.Stop();
		}
		else
		{
			sender.Visibility = "Visible";
			sender.Play();
		}
	},

	hasPlayedOnce: function(sender, eventArgs)
	{
		if (getCookie("HPVID") == "")
		{
			setCookie("HPVID", 1, 10);
			return false;
		}
		else
		{
			return true;
		}
	},

	stopVideos: function()
	{
		if (this.video.CurrentState == "Playing")
			this.video.Stop();
	},

	onCurrentStateChanged: function(sender, eventArgs)
	{
		if (sender.CurrentState == "Playing")
		{
			this.videoStill.Visibility = "Collapsed";
			this.video.Visibility = "Visible";
		}
	}
}


