if (!window.ProductTour)
	window.ProductTour = {};
ProductTour.Scene = function() 
{
}


ProductTour.Scene.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		this.root = rootElement;
		
		//Empty Defaults
		this.myPopups = new Array();
		this.DLImages = new Array();
		this.DLImages2 = new Array();
		this.DLImages3 = new Array();
		this.NumScreens = 1;
		this.AssetsFile = "";
		this.LearnMoreLink = "";
		this.DownloadNowLink = "";
		
		this.customSetup(); //For each tour, see individual prototypes
		
		this.SelectedScreen = 0;
		this.SelectedHotSpot = "";
		
		this.loadingBar_maxBar = this.root.findName("loadingBar_maxBar");
		this.loadingBar_progressBar = this.root.findName("loadingBar_progressBar");
		
		this.addEvent(this.root.findName("HotSpotCover"), "MouseLeftButtonDown", this, "onHotSpotCoverClick");

		for (i in this.myPopups) {
			var myHotSpot = this.root.findName("hot_"+this.myPopups[i]);
			//alert(myHotSpot+": "+this.myPopups[i]);
			myHotSpot.Opacity = 0.7;
			myHotSpot.Cursor = "Hand";
			this.root.findName("popup_"+this.myPopups[i]).Visibility = "Collapsed";
			
			this.addEvent(myHotSpot, "MouseEnter", this, "onHotSpotRollover");
			this.addEvent(myHotSpot, "MouseLeave", this, "onHotSpotRollout");
			this.addEvent(myHotSpot, "MouseLeftButtonDown", this, "onHotSpotClick");
			this.addEvent(this.root.findName("popup_"+this.myPopups[i]+"_out"), "Completed", this, "onPopupOutComplete");
		}


		for(i=1;i<=this.NumScreens;i++) {
			this["btn_Page"+i] = this.root.findName("btn_Page"+i);
			this.addEvent(this["btn_Page"+i], "MouseEnter", this, "onPageButtonRollover");
			this.addEvent(this["btn_Page"+i], "MouseLeave", this, "onPageButtonRollout");
			this.addEvent(this["btn_Page"+i], "MouseLeftButtonDown", this, "onPageButtonClick");
			
			this.root.findName("popup_background"+i).Visibility = "Collapsed";
			this.root.findName("productTour_Screen"+i).Visibility = "Collapsed";
		}
		for(i=this.NumScreens+1;i<=5;i++) {
			this.root.findName("btn_Page"+i).Visibility = "Collapsed";
		}
		
		if(this.NumScreens<=1) {
			this.root.findName("Pagination").Visibility = "Collapsed";
		}

		this.btn_next = this.root.findName("btn_next");
		this.addEvent(this.btn_next, "MouseEnter", this, "onButtonRollover");
		this.addEvent(this.btn_next, "MouseLeave", this, "onButtonRollout");
		this.addEvent(this.btn_next, "MouseLeftButtonDown", this, "onbtn_nextClick");
		this.btn_prev = this.root.findName("btn_prev");
		this.addEvent(this.btn_prev, "MouseEnter", this, "onButtonRollover");
		this.addEvent(this.btn_prev, "MouseLeave", this, "onButtonRollout");
		this.addEvent(this.btn_prev, "MouseLeftButtonDown", this, "onbtn_prevClick");
		
		
		this.addEvent(this.root.findName("downloadNow"), "MouseEnter", this, "onButtonRollover");
		this.addEvent(this.root.findName("downloadNow"), "MouseLeave", this, "onButtonRollout");
		this.addEvent(this.root.findName("downloadNow"), "MouseLeftButtonDown", this, "ondownloadNow_Click");
		this.addEvent(this.root.findName("learnMore"), "MouseEnter", this, "onButtonRollover");
		this.addEvent(this.root.findName("learnMore"), "MouseLeave", this, "onButtonRollout");
		this.addEvent(this.root.findName("learnMore"), "MouseLeftButtonDown", this, "onlearnMore_Click");
		this.addEvent(this.root.findName("closeWindow"), "MouseEnter", this, "onButtonRollover");
		this.addEvent(this.root.findName("closeWindow"), "MouseLeave", this, "onButtonRollout");
		this.addEvent(this.root.findName("closeWindow"), "MouseLeftButtonDown", this, "oncloseWindow_Click");
		
		this.addEvent(this.root.findName("productTourWindow_out"), "Completed", this, "onproductTourWindow_outComplete");

		this.downloader = control.createObject("downloader");
		this.downloader.addEventListener("downloadProgressChanged", Silverlight.createDelegate(this, this.onDownloadProgressChanged));
		this.downloader.addEventListener("completed", Silverlight.createDelegate(this, this.onDownloadCompleted));
		this.downloader.open("GET", this.AssetsFile);
		this.downloader.send();

	},
	customSetup: function() {
		//placeholder
	},
	onError: function() {
		//Legolas! What do your Elven eyes see?
		//A red Sun rises... a scripting error has been spilled this day.
	},
	onDownloadProgressChanged: function(sender, eventArgs)
	{
		var percentage = sender.downloadProgress * 100;
		// Update the Rectangle and TextBlock objects of the visual progress indicator.
		this.loadingBar_progressBar.Width = Math.floor(percentage * this.loadingBar_maxBar.Width/100);
	},

	onDownloadCompleted: function(sender, eventArgs)
	{
		for(i in this.DLImages) {
			var myImageName = this.DLImages[i];
			var myImageNickname = myImageName.split(".")[0];
			//alert("Searching for placeholder for image '"+myImageNickname+"'. Found "+this.root.findName("image_"+myImageNickname));
			if(this.root.findName("image_"+myImageNickname)==null) {
				//alert("Could not find placeholder for image '"+myImageNickname+"'.");
			} else {
				this.root.findName("image_"+myImageNickname).setSource(this.downloader, myImageName);
			}
		}
		for(i in this.DLImages2) {
			var myImageName = this.DLImages2[i];
			var myImageNickname = myImageName.split(".")[0];
			if(this.root.findName("image_"+myImageNickname)==null) {
				//alert("Could not find placeholder for image '"+myImageNickname+"' (1 of 2).");
			} else {
				this.root.findName("image_"+myImageNickname).setSource(this.downloader, myImageName);
			}
			var myImageNickname = myImageNickname+"2";
			if(this.root.findName("image_"+myImageNickname)==null) {
				//alert("Could not find placeholder for image '"+myImageNickname+"' (2 of 2).");
			} else {
				this.root.findName("image_"+myImageNickname).setSource(this.downloader, myImageName);
			}
		}
		
		this.root.findName("productTourWindow_in").Begin();
		this.root.findName("tour_in").Begin();
		this.selectScreen(1, true);
	},
	oncloseWindow_Click: function(sender, eventArgs) {
		this.root.findName("productTourWindow_out").Begin();
	},
	onlearnMore_Click: function(sender, eventArgs) {
		this.requestLink(this.LearnMoreLink);
	},
	ondownloadNow_Click: function(sender, eventArgs) {
		this.requestLink(this.DownloadNowLink);
	},
	onproductTourWindow_outComplete: function(sender, eventArgs) {
		//From Default.htm
		CloseProductTour();
	},
	
	
	
	/**************************************************
		SCREEN NAV
	**************************************************/
	onPageButtonRollover: function(sender, eventArgs) {
		if(sender.Name.substr(8)!=this.SelectedScreen) {
			this.root.findName(sender.Name+"_over").Begin();
		}
	},
	onPageButtonRollout: function(sender, eventArgs) {
		if(sender.Name.substr(8)!=this.SelectedScreen) {
			this.root.findName(sender.Name+"_out").Begin();
		}
	},
	onPageButtonClick: function(sender, eventArgs) {
		this.selectScreen(Number(sender.Name.substr(8)), false);
	},
	onbtn_nextClick: function(sender, eventArgs) {
		this.selectScreen(this.SelectedScreen+1, false);
	},
	onbtn_prevClick: function(sender, eventArgs) {
		this.selectScreen(this.SelectedScreen-1, false);
	},
	selectScreen: function(p_screenIndex, p_immediate) {
		if(this.SelectedHotSpot!="") {
			this.hideSelectedPopup();
		}
		if(this.SelectedScreen>0) {
			this.root.findName("btn_Page"+this.SelectedScreen+"_out").Begin();
			this.root.findName("productTour_Screen"+this.SelectedScreen).Visibility = "Collapsed";
			this.root.findName("productTour_Screen"+this.SelectedScreen).Opacity = 0;
		}
		
		this.SelectedScreen = p_screenIndex;
		
		this.root.findName("btn_Page"+this.SelectedScreen+"_over").Begin();
			this.root.findName("productTour_Screen"+this.SelectedScreen).Opacity = 1;
		this.root.findName("productTour_Screen"+this.SelectedScreen).Visibility = "Visible";
		
		if(this.SelectedScreen==1) {
			this.btn_prev.Visibility = "Collapsed";
		} else {
			this.btn_prev.Visibility = "Visible";
		}

		if(this.SelectedScreen==this.NumScreens) {
			this.btn_next.Visibility = "Collapsed";
		} else {
			this.btn_next.Visibility = "Visible";
		}
		
		if(p_immediate) {
			
		} else {
			
		}
	},
	oninterestDetailPanels_outComplete: function(sender, eventArgs) {
		this.displaySelectedInterestPage();
	},
	displaySelectedInterestPage: function() {
		var firstItem = (this.SelectedInterestPageIndex-1)*3;
		var lastItem = Math.min(firstItem+2,this.InterestArticles[this.InterestNames[this.SelectedInterestIndex]].length-1);
		var count = 1;
		for(var i=firstItem; i<=firstItem+2; i++) {
			if(i<=lastItem) {
				this.root.findName("InterestTitle"+count)["Text"] = this.InterestArticles[this.InterestNames[this.SelectedInterestIndex]][i]["title"];
				this.root.findName("InterestDesc"+count)["Text"] = this.InterestArticles[this.InterestNames[this.SelectedInterestIndex]][i]["description"];
				this.root.findName("interestDetail"+count).Visibility = "Visible";
			} else {
				this.root.findName("interestDetail"+count).Visibility = "Collapsed";
			}
			count++;
		}
		this.root.findName("interestDetailPanels_in").Begin();
	},
	onViewDemoButtonClick: function(sender, eventArgs) {
		var chosenLink = Number(sender.Name.substr(12));
		var chosenArticleIndex = (this.SelectedInterestPageIndex-1)*3 + chosenLink-1;
		this.requestLink(this.InterestArticles[this.InterestNames[this.SelectedInterestIndex]][chosenArticleIndex]["url"]);
	},
	
	
	
	/**************************************************
		HOTSPOT INTERACTIONS
	**************************************************/
	onHotSpotRollover: function(sender, eventArgs) {
		sender.Opacity = 1;
	},
	onHotSpotClick: function(sender, eventArgs) {
		this.SelectedHotSpot = sender.Name.substr(4);
		this.root.findName("HotSpotCover").Visibility = "Visible";
		this.root.findName("popup_"+this.SelectedHotSpot).Visibility = "Visible";
		this.root.findName("popup_"+this.SelectedHotSpot+"_in").Begin();
		
		this.root.findName("popup_background"+this.SelectedScreen).Visibility = "Visible";
	},
	onHotSpotRollout: function(sender, eventArgs) {
		sender.Opacity = 0.7;
	},
	onHotSpotCoverClick: function(sender, eventArgs) {
		this.hideSelectedPopup();
	},
	hideSelectedPopup: function() {
		this.root.findName("HotSpotCover").Visibility = "Collapsed";
		this.root.findName("popup_"+this.SelectedHotSpot+"_out").Begin();
		this.SelectedHotSpot = "";
	},
	onPopupOutComplete: function(sender, eventArgs) {
		this.root.findName("popup_background"+this.SelectedScreen).Visibility = "Collapsed";
		this.root.findName(sender.Name.substring(0,sender.Name.length-4)).Visibility = "Collapsed";
	},
	
	
	
	/**************************************************
		BUTTON INTERACTIONS
	**************************************************/
	onButtonRollover: function(sender, eventArgs) {
		this.root.findName(sender.Name+"_over").Begin();
	},
	onButtonRollout: function(sender, eventArgs) {
		this.root.findName(sender.Name+"_out").Begin();
	},
	
	
	
	/**************************************************
		UTILITIES
	**************************************************/
	requestLink: function(p_url) {
		//on Default.htm
		RequestLink(p_url);
	},
	addEvent: function(a,b,c,d) {
		a.addEventListener(b, Silverlight.createDelegate(c, c[d]));
	},
	centerText: function(textObject) {
		textObject["Canvas.Left"] += Math.round((textObject.Width - textObject.ActualWidth)/2);
		textObject.Width = Math.ceil(textObject.ActualWidth);
	},	
	rightAlignText: function(textObject) {
		textObject["Canvas.Left"] += Math.round(textObject.Width - textObject.ActualWidth);
		textObject.Width = Math.ceil(textObject.ActualWidth);
	}
}