﻿/// <reference path="MicrosoftAjax.js" />
/// <reference path="Silverlight.js" />
/// <reference path="Defatlt.html" />
if (!window.ViewDemosTab)
	ViewDemosTab = {};

ViewDemosTab.Page = function()
{
	this.videoRootUrl = "http://mediadl.microsoft.com/MediaDL/WWW/l/learningspace/";

	this.demoLists = {
		MathDemoList: new DemoList(1, 3, 8),
		SemblioDemoList: new DemoList(1, 1, 2)
	};

	this.demoLists.MathDemoList.videos = new Array("overview.wmv", "3dGraphingCalc.wmv", "conversions.wmv",
                                                 "equationsolver.wmv", "graphingCalc.wmv", "scientificCalc.wmv",
                                                 "triangleSolver.wmv", "tutorials.wmv");

	this.demoLists.SemblioDemoList.videos = new Array("semblio/semblio_solar_demo.wmv", "semblio/semblio_gas_demo.wmv");
	this.currentDemoList = "MathDemoList";
}

ViewDemosTab.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement)
	{
		this.control = control;

		this.videoNo = null;
		this.videoTitleCanvas = this.control.content.findName("VideoTitleCanvas");
		this.videoTitle = this.control.content.findName("VideoTitle");
		this.nowPlayingTitle = this.control.content.findName("NowPlayingTitle");

		this.fullScreenButton = this.control.content.findName("FullScreen");
		this.fullScreenButton.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.fullScreenButtonClick));


		this.videoPlayer = new EmbeddedPlayer(this.control, this.control.content.findName("Page"), 0,
	                        640,
	                        480,
	                        this.control.id);

		this.demoWrappers = {
			MathDemos: this.control.content.findName("MathDemos"),
			SemblioDemos: this.control.content.findName("SemblioDemos")
		};

		this.demoMouseEnterAnim = this.control.content.findName("Demo_MouseEnter");
		this.demoMouseLeaveAnim = this.control.content.findName("Demo_MouseLeave");

		var demoMouseEnterDelegate = Silverlight.createDelegate(this, this.demoMouseEnter);
		var demoMouseLeaveDelegate = Silverlight.createDelegate(this, this.demoMouseLeave);
		var demoMouseClickDelegate = Silverlight.createDelegate(this, this.demoMouseClick);

		for (var i = 0; i < this.demoLists.MathDemoList.get_videoCount(); i++)
		{
			var demo = this.control.content.findName("MathDemo" + i);
			demo.addEventListener("MouseEnter", demoMouseEnterDelegate);
			demo.addEventListener("MouseLeave", demoMouseLeaveDelegate);
			demo.addEventListener("MouseLeftButtonUp", demoMouseClickDelegate);
		}

		for (var i = 0; i < this.demoLists.SemblioDemoList.get_videoCount(); i++)
		{
			var demo = this.control.content.findName("SemblioDemo" + i);
			demo.addEventListener("MouseEnter", demoMouseEnterDelegate);
			demo.addEventListener("MouseLeave", demoMouseLeaveDelegate);
			demo.addEventListener("MouseLeftButtonUp", demoMouseClickDelegate);
		}

		this.carouselControls = {
			CarouselLeft: this.control.content.findName("CarouselLeft"),
			CarouselRight: this.control.content.findName("CarouselRight"),
			MathDemoLink: this.control.content.findName("MathDemoLink"),
			SemblioDemoLink: this.control.content.findName("SemblioDemoLink")
		}


		this.carouselControls.CarouselLeft.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.carouselLeftClick));
		this.carouselControls.CarouselRight.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.carouselRightClick));

		var demoLinkClickDelegate = Silverlight.createDelegate(this, this.demoLinkClick);
		this.carouselControls.MathDemoLink.addEventListener("MouseLeftButtonUp", demoLinkClickDelegate);
		this.carouselControls.SemblioDemoLink.addEventListener("MouseLeftButtonUp", demoLinkClickDelegate);

		var controlMouseEnterDelegate = Silverlight.createDelegate(this, this.controlMouseEnter);
		var controlMouseLeaveDelegate = Silverlight.createDelegate(this, this.controlMouseLeave);
		for (var i in this.carouselControls)
		{
			this.carouselControls[i].addEventListener("MouseEnter", controlMouseEnterDelegate);
			this.carouselControls[i].addEventListener("MouseLeave", controlMouseLeaveDelegate);
		}

		var product = new Object();
		window.location.search.replace(new RegExp("([^?=&]+)(=([^&]*))?", "g"), function($0, $1, $2, $3) { product[$1] = $3; });
	
		if (product["prod"])
		{
			switch (product["prod"])
			{
				case "semblio":
					this.toggleDemos("SemblioDemoLink");
					this.currentDemoList = "SemblioDemoList";
					break;

				default:
					this.toggleDemos("MathDemoLink");
					this.currentDemoList = "MathDemoList";
					break;
			}
		}
		else
		{
			this.toggleDemos("MathDemoLink");
		}
	},

	carouselLeftClick: function(sender, eventArgs)
	{
		var currPos = this.demoLists[this.currentDemoList].get_currentPosition();
		var maxPos = this.demoLists[this.currentDemoList].get_maximumPosition();
		switch (currPos)
		{
			case 1:
				break;

			default: var anim = sender.findName("MoveRight0" + currPos);
				anim.Stop();
				anim["Storyboard.TargetName"] = this.currentDemoList;
				anim.Begin();
				currPos -= 1;
				this.demoLists[this.currentDemoList].set_currentPosition(currPos);
		}
	},

	carouselRightClick: function(sender, eventArgs)
	{
		var currPos = this.demoLists[this.currentDemoList].get_currentPosition();
		var maxPos = this.demoLists[this.currentDemoList].get_maximumPosition();
		switch (currPos)
		{
			case maxPos:
				break;

			default: var anim = sender.findName("MoveLeft0" + currPos);
				anim.Stop();
				anim["Storyboard.TargetName"] = this.currentDemoList;
				anim.Begin();
				currPos += 1;
				this.demoLists[this.currentDemoList].set_currentPosition(currPos);
		}
	},

	demoLinkClick: function(sender, eventArgs)
	{
		this.toggleDemos(sender.Name);
	},

	toggleDemos: function(linkName)
	{
		switch (linkName)
		{
			case "MathDemoLink": this.currentDemoList = "MathDemoList";
				this.demoWrappers.MathDemos.Visibility = "Visible";
				this.demoWrappers.SemblioDemos.Visibility = "Collapsed";

				this.controlMouseEnter(this.carouselControls.MathDemoLink);
				this.controlMouseLeave(this.carouselControls.SemblioDemoLink);
				break;

			case "SemblioDemoLink": this.currentDemoList = "SemblioDemoList";
				this.demoWrappers.MathDemos.Visibility = "Collapsed";
				this.demoWrappers.SemblioDemos.Visibility = "Visible";

				this.controlMouseLeave(this.carouselControls.MathDemoLink);
				this.controlMouseEnter(this.carouselControls.SemblioDemoLink);
		}

		if (this.demoLists[this.currentDemoList].get_videoCount() <= 3)
		{
			this.carouselControls.CarouselLeft.Visibility = "Collapsed";
			this.carouselControls.CarouselRight.Visibility = "Collapsed";
		}
		else
		{
			this.carouselControls.CarouselLeft.Visibility = "Visible";
			this.carouselControls.CarouselRight.Visibility = "Visible";
		}
	},

	controlMouseLeave: function(sender, eventArgs)
	{
		if (sender.Name.split("Link")[0] != this.currentDemoList.split("List")[0])
			sender.findName(sender.Name + "_MouseLeave").Begin();
	},

	controlMouseEnter: function(sender, eventArgs)
	{
		sender.findName(sender.Name + "_MouseEnter").Begin();
	},

	demoMouseEnter: function(sender, eventArgs)
	{
		this.demoMouseEnterAnim.Stop();
		this.demoMouseEnterAnim["Storyboard.TargetName"] = sender.Name;
		this.demoMouseEnterAnim.Begin();

		this.videoTitle.Text = sender.Tag;
		this.videoTitle["Canvas.Left"] = (this.videoTitleCanvas.Width / 2) - (this.videoTitle.ActualWidth / 2);
	},

	demoMouseLeave: function(sender, eventArgs)
	{
		this.demoMouseLeaveAnim.Stop();
		this.demoMouseLeaveAnim["Storyboard.TargetName"] = sender.Name;
		this.demoMouseLeaveAnim.Begin();

		this.videoTitle.Text = "";
	},

	demoMouseClick: function(sender, eventArgs)
	{
		this.nowPlayingTitle.Text = sender.Tag;


		this.videoNo = sender.Name.substring(sender.Name.length - 1, sender.Name.length);

		var videoName = this.demoLists[this.currentDemoList].videos[this.videoNo];
		this.videoPlayer.ChangeVideo(this.videoRootUrl + videoName);

		var linkName = getPage() + "_" + getSelectedSlider() + "_viewdemos_" +
	    this.currentDemoList.substring(0, this.currentDemoList.indexOf('DemoList')).toLowerCase() +
	    videoName.substring(2, videoName.indexOf('.wmv')).toLowerCase();
		trackLink(true, 'o', linkName);
	},

	StopVideo: function(sender, eventArgs)
	{
		this.videoPlayer.Stop();
		this.nowPlayingTitle.Text = '';
	},

	fullScreenButtonClick: function(sender, eventArgs)
	{
		if (this.demoLists[this.currentDemoList].videos[this.videoNo])
		{
			this.videoPlayer.Stop();
			displayFullScreenPlayer(this.videoRootUrl + this.demoLists[this.currentDemoList].videos[this.videoNo]);
		}
	}
}

DemoList = function(currentPosition, maximumPosition, videoCount)
{
  this.currentPosition = currentPosition;
  this.maximumPosition = maximumPosition;
  this.videoCount = videoCount;
  this.videos = null;
}

DemoList.prototype = 
{
  get_currentPosition: function(){ return this.currentPosition; },
  set_currentPosition: function(value){ this.currentPosition = value; },
  get_maximumPosition: function(){ return this.maximumPosition; },
  set_maximumPosition: function(value){ this.maximumPosition = value; },
  get_videoCount: function(){ return this.videoCount; },
  set_videoCount: function(value){ this.videoCount = value; }
}