﻿
///////////////////////////////////////////////////////////////////////////////
//
// PreloaderMultifileClassLibrary.js
// The MIT License
//
// Copyright (c) 2007 Ryan Christensen, drawk llc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////

// setup the namespace objects to be good javascript form
// Ag1 = Silverlight 1.0 library
if(!window.B)
	window.B={};
if(!window.B.Ag1)
	window.B.Ag1={};

if (!window.Sys)
	window.Sys = {};

if (!window.Silverlight)
	window.Silverlight = {};

Silverlight.createDelegate = function(instance, method) {
	return function() {
		return method.apply(instance, arguments);
	}
}

B.Ag1.PreloaderMultifileAssets = function()
{
	// TODO alow JSON or Array passed
	// loads up the asset arry to cycle through
	this.resourceArray = new Array();
}

B.Ag1.PreloaderMultifileAssets.prototype =
{
	loadAssetsArray : function()
	{
		var i=0;
		//this.resourceArray[0] = "PersonalBestChap013_thumb.wmv";
		//var i=1;
		this.resourceArray[i++] = "PersonalBestChap013_thumb_ns.wmv";
		this.resourceArray[i++] = "SKU-000070772_COVER.jpg";
		// add your images/zips/files to load here
//		for (var i=1; i<=16; i++)
//		{
//			this.resourceArray[i] = "drive_in_files/image" + i + ".png";
//		}
		//this.resourceArray[i++] = "AchmedFromAmerica_BookSleeve_002.jpg";
		this.resourceArray[i++] = "AhmedFromAmerica_Cover.jpg";
		this.resourceArray[i++] = "ED_n_Helen_small.jpg";
		//this.resourceArray[i++] = "innerPage.zip";  // compression runs a bit slowly on some computers
		//this.resourceArray[i++] = "EdwardMueschAuthor_Sleeve.jpg";
		this.resourceArray[i++] = "Helen&Ed'sboat.jpg";
		//this.resourceArray[i++] = "LandOfMenDesc_noTear.jpg";
		//this.resourceArray[i++] = "LandOfMenDesc_yesTear.jpg";
		//this.resourceArray[i++] = "PBestTeaser.jpg";
		this.resourceArray[i++] = "PersonalBest_Cover.jpg";
		this.resourceArray[i++] = "149_Indie_Winner_Scan.png";
		this.resourceArray[i++] = "bestbooks2006_02.png";
		this.resourceArray[i++] = "Authors_Pict.jpg";
		this.resourceArray[i++] = "svTahlequahRoutesPage.jpg";
		//this.resourceArray[i++] = "RATWDesc_001_noTear.jpg";
		//this.resourceArray[i++] = "RATWDesc_001_yesTear.jpg";
	}
}

// total pages available
B.Ag1.PreloaderMultifile = function(control)
{
	this.plugIn = control;      // Store the host plug-in
    // show our downloader UI
//    control.content.findName("downloadUI").Visibility ='Visible';   
//    control.content.findName("downloadUI").Opacity = 0;  
	this.plugIn.content.findName("lightDownloadUI").Begin();
	//pausecomp(2000);    // pause long enough to light downloader progress indicator

	this.currentDownload = 0;   // Current resource to be downloaded
	// this.maxNumPages = maxNumPages;
	this.preloaderMultifileAssets = new B.Ag1.PreloaderMultifileAssets();
	this.preloaderMultifileAssets.loadAssetsArray();
	// alert(this.preloaderMultifileAssets.resourceArray);
	//setTimeout(EdwardMueschBooks.preloader.downloadAssets(),2000);
	this.downloadAssets();
}

B.Ag1.PreloaderMultifile.prototype =
{
	downloadAssets : function()
	{
		//alert(this.preloaderMultifileAssets.resourceArray);
		var _file = this.preloaderMultifileAssets.resourceArray[this.currentDownload];
		if(_file != null && _file != undefined)
		{
			this.downloader = this.plugIn.createObject("downloader");
			this.downloader.addEventListener("downloadProgressChanged", Silverlight.createDelegate(this, this.downloadProgressChanged));
			this.downloader.addEventListener("completed", Silverlight.createDelegate(this, this.downloadCompleted));
			this.downloader.open("GET", this.preloaderMultifileAssets.resourceArray[this.currentDownload]);
			this.downloader.send();
		}
	},
	downloadProgressChanged : function(sender, args)
	{
		try
		{
			var progressRect = this.plugIn.content.findName("progressRect");
			var progressRect_Back = this.plugIn.content.findName("progressRect_Back");
			//progressRect.width = ((sender.downloadProgress) * progressRect_Back.width); //450;
			progressRect.width = Math.max(
			    ((this.currentDownload/this.preloaderMultifileAssets.resourceArray.length)*progressRect_Back.width) + 
			    (((sender.downloadProgress)/this.preloaderMultifileAssets.resourceArray.length) * progressRect_Back.width)
			    , progressRect.width ); // so we maintain nice starting circle at begin and never shrink //450;
		}
		catch(e)
		{
		}
	},
	downloadCompleted : function(sender, args)
	{
		this.currentDownload++;
		var progressText = this.plugIn.content.findName("progressText");
		var progressPercent= this.plugIn.content.findName("progressPercent");
        // NEED TO STOP DOUBLE-LOADDING AND USE setSource() for all images after removing source from Xaml ???

        // Create the objects from the XAML content.
        var plugin = sender.getHost();
        // Retrieve downloaded XAML content.
        var xamlFragment = sender.ResponseText;
        
		if (sender.uri == "innerPage.zip")
        {
//			progressText.text = "Assembling resources, please wait...";
//			progressPercent.text = parseInt((this.currentDownload/this.preloaderMultifileAssets.resourceArray.length)*100) + "%";

            //var canvas = plugin.content.createFromXaml(xamlFragment);
            var canvas = plugin.content.createFromXamlDownloader(sender,"innerPage.xaml");

            // Add downloaded XAML content to the root Canvas of the plug-in.
            var rootCanvas = sender.findName("Page");
            rootCanvas.children.add(canvas);
            plugin.content.findName("innerPage").Visibility ='Collapsed';   
            plugin.content.findName("innerPage").Opacity = 0;  
            
        }else if(sender.uri == "Helen&Ed'sboat.jpg"){
            var obj;
            for(i=1;i<6;i++){
                obj = plugin.content.findName("book"+i.toString()+"_RATW");
                obj.setSource(sender, "");
            }
        }else if(sender.uri == "PersonalBest_Cover.jpg"){
            var obj;
            for(i=1;i<6;i++){
                obj = plugin.content.findName("book" + i.toString() + "_PBest");
                obj.setSource(sender, "");
            }
        } else if (sender.uri == "SKU-000070772_COVER.jpg") {
            var obj;
            for(i=1;i<6;i++){
                //obj = plugin.content.findName("book" + i.toString() + "_LOM");
                obj = plugin.content.findName("book" + i.toString() + "_AfA");
                obj.setSource(sender, "");
            }
        } else if (sender.uri == "PersonalBestChap013_thumb_ns.wmv") {
            var obj;
            obj = plugin.content.findName("PersonalBestChap013_wmv_thumb");
            obj.setSource(sender, "");
        }else if(sender.uri == "AhmedFromAmerica_Cover.jpg"){
            var obj;
            for(i=1;i<6;i++){
                //obj = plugin.content.findName("book" + i.toString() + "_AfA");
                obj = plugin.content.findName("book" + i.toString() + "_LOM");
                obj.setSource(sender, "");
            }
        }else if(sender.uri == "svTahlequahRoutesPage.jpg"){
            var obj;
            //for(i=1;i<6;i++){
                obj = plugin.content.findName("svTahlequah");
                obj.setSource(sender, "");
        }else if(sender.uri == "149_Indie_Winner_Scan.png"){
            var obj;
            //for(i=1;i<6;i++){
                obj = plugin.content.findName("PBest_Award_Indie");
                obj.setSource(sender, "");
        }else if(sender.uri == "bestbooks2006_02.png"){
            var obj;
            //for(i=1;i<6;i++){
                obj = plugin.content.findName("PBest_Award_BestBooks");
                obj.setSource(sender, "");
            //}
//        }else if(sender.uri == "EdwardMueschAuthor_Sleeve.jpg"){
//            var obj;
//            //for(i=1;i<6;i++){
//                obj = plugin.content.findName("personalBio");
//                obj.setSource(sender, "");
//            //}
//        }else if(sender.uri == "PBestTeaser.jpg"){
//            var obj;
//            //for(i=1;i<6;i++){
//                obj = plugin.content.findName("PBestDesc");
//                obj.setSource(sender, "");
//            //}
        }else if(sender.uri == "ED_n_Helen_small.jpg"){
            var obj;
            //for(i=1;i<6;i++){
                obj = plugin.content.findName("small_pic");
                obj.setSource(sender, "");
                obj = plugin.content.findName("small_pic_copy");
                obj.setSource(sender, "");
            //}
        }else if(sender.uri == "Authors_Pict.jpg"){
            var obj;
            //for(i=1;i<6;i++){
                obj = plugin.content.findName("BIO_Pict_Author");
                obj.setSource(sender, "");
            //}
        }
        

		if (this.currentDownload < this.preloaderMultifileAssets.resourceArray.length)
		{
			progressText.text = "Downloading: " + this.preloaderMultifileAssets.resourceArray[this.currentDownload];
			progressPercent.text = parseInt((this.currentDownload/this.preloaderMultifileAssets.resourceArray.length)*100) + "%";
			this.downloader.open("GET", this.preloaderMultifileAssets.resourceArray[this.currentDownload]);
			this.downloader.send();
		}
		else
		{
			progressPercent.text = "100%";
			progressText.text = "Downloading Complete";
			
			// Hide progress UI
			var downloadUI = this.plugIn.content.findName("downloadUI");
			this.plugIn.content.findName("fadeDownloadUI").Begin();
			downloadUI.isHitTestVisible = false;
			
			scene.handleLoad2(sender.GetHost(),args,this.plugIn.content.findName("Page"));
		}
	}
}
