if (!window.TTPSilverlightBuild1)
	window.TTPSilverlightBuild1 = {};

TTPSilverlightBuild1.Scene = function() 
{
}

TTPSilverlightBuild1.Scene.prototype =
{
    handleLoad: function(control, userContext, rootElement) {
        this.control = control;
        this.scenePanePercentage = 1;   // Used for calculating size of the scene pane during resizes
        this.rootCanvas = this.control.content.findName("rootCanvas");
        //config settings
        var application_server_uri;
        //var application_server_dir;
        var media_server_uri;
        var media_server_dir;
        var media_dir_local;
        var web_service_uri;
        var web_service_filename;

        var language;
        var library_id;
        var online_book = Boolean(true); ;
        //end config settings

        this.fontFamily = "Verdana";

        this.bookLoaded = Boolean(false);
        this.MenuActive = Boolean(false);
        this.isPageTurnActive = Boolean(true);
        this.isAudioPlaying = Boolean(false);
        this.rotateTicker = 0;
        this.menuRowTracker = 0;

        this.targetWidth;
        this.targetHeight;

        this.bookScene;

        //Search for book results
        this.SearchResultsArray = new Array();
        this.SearchResultsForBooks;
        this.SearchResultsLength;
        this.currentSearchTerm;
        this.currentBookResultPage = 0;
        this.maxBookResultPage;

        //search for page resuts
        this.currentPageResultPage = 0;
        this.maxPageResultPage;
        this.currentActiveSearchResultArrayItem;
        this.SearchResultsForPages;

        this.spreadTicker = 0;
        this.lastSpread;
        this.fwButActive = false;
        this.bkButActive = false;
        this.isNaviAnimating = false;
        this.currentPageObjects;
        this.control.content.findName("spreadBack")["Visibility"] = "Collapsed";
        this.control.content.findName("spreadForward")["Visibility"] = "Collapsed";

        this.hasJumpedFw = false;
        this.hasJumpedBk = false;

        this.menuScrollTimer;
        this.menuScrollContent;
        this.menuScrollDown = Boolean(false);
        this.menuScrollUp = Boolean(false);

        //load config settings.
        this.configDownloader = this.control.createObject("Downloader");
        this.configDownloader.addEventListener("completed", Silverlight.createDelegate(this, this.downloadConfigXmlCompleted));
        this.configDownloader.open("GET", "ttp_config.xml");
        this.configDownloader.send();

        mainMenuXaml = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='700' Height='520' x:Name='mainMenuCanvas' RenderTransformOrigin='0.5,0.5'>";
        mainMenuXaml = mainMenuXaml + "<Canvas.RenderTransform>";
        mainMenuXaml = mainMenuXaml + "<TransformGroup>";
        mainMenuXaml = mainMenuXaml + "<ScaleTransform x:Name='mainMenuScale' ScaleX='0.6' ScaleY='0.6'/>";
        mainMenuXaml = mainMenuXaml + "<SkewTransform AngleX='0' AngleY='0'/>";
        mainMenuXaml = mainMenuXaml + "<RotateTransform Angle='0'/>";
        mainMenuXaml = mainMenuXaml + "<TranslateTransform X='0' Y='0'/>";
        mainMenuXaml = mainMenuXaml + "</TransformGroup>";
        mainMenuXaml = mainMenuXaml + "</Canvas.RenderTransform>";
        mainMenuXaml = mainMenuXaml + "<Canvas.Resources>";
        mainMenuXaml = mainMenuXaml + "<Storyboard BeginTime='0' Duration='0:0:0.01' x:Name='mainMenuScrollTimer'/>";
        mainMenuXaml = mainMenuXaml + "</Canvas.Resources>";
        mainMenuXaml = mainMenuXaml + "<Rectangle Width='700' Height='520' RadiusX='20' RadiusY='25'>";
        mainMenuXaml = mainMenuXaml + "<Rectangle.Fill>";
        mainMenuXaml = mainMenuXaml + "<LinearGradientBrush EndPoint='0.934,0.011' StartPoint='0.205,1.085'>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#FF757575' Offset='0.476'/>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#FF3E3E3E' Offset='0'/>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#FF3E3E3E' Offset='0.933'/>";
        mainMenuXaml = mainMenuXaml + "</LinearGradientBrush>";
        mainMenuXaml = mainMenuXaml + "</Rectangle.Fill>";
        mainMenuXaml = mainMenuXaml + "</Rectangle>";
        mainMenuXaml = mainMenuXaml + "<Path Stretch='Fill' StrokeThickness='3' Height='11.319' Data='M217,25 C217,12 207,12 207,12 207,12 207,12 125,12 L-85.794242,12' Width='660' Canvas.Left='19.152' Canvas.Top='6.384' >";
        mainMenuXaml = mainMenuXaml + "<Path.Stroke>";
        mainMenuXaml = mainMenuXaml + "<LinearGradientBrush EndPoint='0.517,1.917' StartPoint='0.517,-0.083'>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#1AFFFFFF' Offset='0'/>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#FFFFFFFF' Offset='0.323'/>";
        mainMenuXaml = mainMenuXaml + "</LinearGradientBrush>";
        mainMenuXaml = mainMenuXaml + "</Path.Stroke>";
        mainMenuXaml = mainMenuXaml + "</Path>";
        mainMenuXaml = mainMenuXaml + "<Rectangle Width='660' Height='443.617' Fill='#FFC5C5C5' StrokeThickness='0' RadiusX='10' RadiusY='15' Canvas.Left='20' Canvas.Top='53'>";
        mainMenuXaml = mainMenuXaml + "<Rectangle.Stroke>";
        mainMenuXaml = mainMenuXaml + "<LinearGradientBrush EndPoint='0.517,1.917' StartPoint='0.517,-0.083'>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#1AFFFFFF' Offset='0'/>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#FFFFFFFF' Offset='0.323'/>";
        mainMenuXaml = mainMenuXaml + "</LinearGradientBrush>";
        mainMenuXaml = mainMenuXaml + "</Rectangle.Stroke>";
        mainMenuXaml = mainMenuXaml + "</Rectangle>";
        mainMenuXaml = mainMenuXaml + "<TextBlock x:Name='titleBlock' FontSize='20' FontWeight='ExtraBold' Canvas.Left='23.403' Canvas.Top='20.639' FontFamily='" + this.fontFamily + "' Text='Menu - choose a book' TextWrapping='Wrap'/>";
        mainMenuXaml = mainMenuXaml + "<Canvas RenderTransformOrigin='0.5,0.5' Cursor='Hand' Width='20' Height='20' Canvas.Left='25' Canvas.Top='53' x:Name='mainMenuScrollUp' Visibility='Collapsed'>";
        /*mainMenuXaml = mainMenuXaml + "<Canvas.RenderTransform>";
        mainMenuXaml = mainMenuXaml + "<RotateTransform Angle='180'/>";
        mainMenuXaml = mainMenuXaml + "</Canvas.RenderTransform>";
        mainMenuXaml = mainMenuXaml + "<Path Width='20' Height='20' Fill='#FF000000' Stretch='Fill' Stroke='#FF000000' StrokeThickness='0' Canvas.Left='-625' Canvas.Top='22' Data='M174.79682,-0.45454545 L474.79663,-0.45454545 C482.97845,-0.90909101 326.61479,5.0007544 326.61479,5.0007544'/>";
        */
        mainMenuXaml = mainMenuXaml + "<Canvas Width='100' Height='20' Canvas.Top='-22' Canvas.Left='550' Cursor='Hand'>";
        mainMenuXaml = mainMenuXaml + "<Rectangle Width='100' Height='20' RadiusX='6' RadiusY='6' Stroke='#FF6A6666' StrokeThickness='2'>";
        mainMenuXaml = mainMenuXaml + "<Rectangle.Fill>";
        mainMenuXaml = mainMenuXaml + "<RadialGradientBrush>";
        mainMenuXaml = mainMenuXaml + "<RadialGradientBrush.RelativeTransform>";
        mainMenuXaml = mainMenuXaml + "<TransformGroup>";
        mainMenuXaml = mainMenuXaml + "<ScaleTransform CenterX='0.5' CenterY='0.5' ScaleX='2.997' ScaleY='2.997'/>";
        mainMenuXaml = mainMenuXaml + "<SkewTransform AngleX='0' AngleY='0' CenterX='0.5' CenterY='0.5'/>";
        mainMenuXaml = mainMenuXaml + "<RotateTransform Angle='0' CenterX='0.5' CenterY='0.5'/>";
        mainMenuXaml = mainMenuXaml + "<TranslateTransform X='0.014' Y='-0.279'/>";
        mainMenuXaml = mainMenuXaml + "</TransformGroup>";
        mainMenuXaml = mainMenuXaml + "</RadialGradientBrush.RelativeTransform>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#FF434343' Offset='0.168'/>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#FFFFFFFF' Offset='1'/>";
        mainMenuXaml = mainMenuXaml + "</RadialGradientBrush>";
        mainMenuXaml = mainMenuXaml + "</Rectangle.Fill>";
        mainMenuXaml = mainMenuXaml + "</Rectangle>";
        mainMenuXaml = mainMenuXaml + "<Path RenderTransformOrigin='0.5,0.5' Width='15' Height='15' Fill='#FF000000' Stretch='Fill' Stroke='#FF000000' StrokeThickness='0' Canvas.Left='80' Canvas.Top='3' Data='M174.79682,-0.45454545 L474.79663,-0.45454545 C482.97845,-0.90909101 326.61479,5.0007544 326.61479,5.0007544'>";
        mainMenuXaml = mainMenuXaml + "<Path.RenderTransform>";
        mainMenuXaml = mainMenuXaml + "<RotateTransform Angle='180'/>";
        mainMenuXaml = mainMenuXaml + "</Path.RenderTransform>";
        mainMenuXaml = mainMenuXaml + "</Path>";
        mainMenuXaml = mainMenuXaml + "<TextBlock FontSize='12' FontFamily='" + this.fontFamily + "' Canvas.Left='8' Canvas.Top='3' Foreground='White'>Scroll up</TextBlock> ";
        mainMenuXaml = mainMenuXaml + "</Canvas>";
        mainMenuXaml = mainMenuXaml + "</Canvas>";
        mainMenuXaml = mainMenuXaml + "<Canvas Width='650' Height='430' Canvas.Left='25' Canvas.Top='62'>";
        mainMenuXaml = mainMenuXaml + "<Canvas.Clip>";
        mainMenuXaml = mainMenuXaml + "<RectangleGeometry Rect='0,0,650,430'/>";
        mainMenuXaml = mainMenuXaml + "</Canvas.Clip>";
        mainMenuXaml = mainMenuXaml + "<Canvas x:Name='menuScrollContent' />";
        mainMenuXaml = mainMenuXaml + "</Canvas>";
        mainMenuXaml = mainMenuXaml + "<Canvas Cursor='Hand' Width='20' Height='20' Canvas.Left='25' Canvas.Top='490' x:Name='mainMenuScrollDown' Visibility='Collapsed' >";
        /*mainMenuXaml = mainMenuXaml + "<TextBlock Text='Scroll down' Canvas.Top='3' Canvas.Left='550' />";
        mainMenuXaml = mainMenuXaml + "<Path Width='20' Height='20' Fill='#FF000000' Stretch='Fill' Stroke='#FF000000' StrokeThickness='0' Canvas.Left='625' Canvas.Top='8' Data='M174.79682,-0.45454545 L474.79663,-0.45454545 C482.97845,-0.90909101 326.61479,5.0007544 326.61479,5.0007544'/>";
        mainMenuXaml = mainMenuXaml + "</Canvas>";*/
        mainMenuXaml = mainMenuXaml + "<Canvas Width='100' Height='20' Canvas.Top='8' Canvas.Left='550' Cursor='Hand'>";
        mainMenuXaml = mainMenuXaml + "<Rectangle Width='100' Height='20' RadiusX='6' RadiusY='6' Stroke='#FF6A6666' StrokeThickness='2'>";
        mainMenuXaml = mainMenuXaml + "<Rectangle.Fill>";
        mainMenuXaml = mainMenuXaml + "<RadialGradientBrush>";
        mainMenuXaml = mainMenuXaml + "<RadialGradientBrush.RelativeTransform>";
        mainMenuXaml = mainMenuXaml + "<TransformGroup>";
        mainMenuXaml = mainMenuXaml + "<ScaleTransform CenterX='0.5' CenterY='0.5' ScaleX='2.997' ScaleY='2.997'/>";
        mainMenuXaml = mainMenuXaml + "<SkewTransform AngleX='0' AngleY='0' CenterX='0.5' CenterY='0.5'/>";
        mainMenuXaml = mainMenuXaml + "<RotateTransform Angle='0' CenterX='0.5' CenterY='0.5'/>";
        mainMenuXaml = mainMenuXaml + "<TranslateTransform X='0.014' Y='-0.279'/>";
        mainMenuXaml = mainMenuXaml + "</TransformGroup>";
        mainMenuXaml = mainMenuXaml + "</RadialGradientBrush.RelativeTransform>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#FF434343' Offset='0.168'/>";
        mainMenuXaml = mainMenuXaml + "<GradientStop Color='#FFFFFFFF' Offset='1'/>";
        mainMenuXaml = mainMenuXaml + "</RadialGradientBrush>";
        mainMenuXaml = mainMenuXaml + "</Rectangle.Fill>";
        mainMenuXaml = mainMenuXaml + "</Rectangle>";
        mainMenuXaml = mainMenuXaml + "<Path Width='15' Height='15' Fill='#FF000000' Stretch='Fill' Stroke='#FF000000' StrokeThickness='0' Canvas.Left='80' Canvas.Top='3' Data='M174.79682,-0.45454545 L474.79663,-0.45454545 C482.97845,-0.90909101 326.61479,5.0007544 326.61479,5.0007544'/>";
        mainMenuXaml = mainMenuXaml + "<TextBlock FontSize='12' FontFamily='" + this.fontFamily + "' Canvas.Left='8' Canvas.Top='3' Foreground='White'>Scroll down</TextBlock> ";
        mainMenuXaml = mainMenuXaml + "</Canvas>";
        mainMenuXaml = mainMenuXaml + "</Canvas>";
        mainMenuXaml = mainMenuXaml + "</Canvas>";
        this.mainMenuXaml = this.control.content.createFromXaml(mainMenuXaml);

        this.setCallback(this.control.content, "onResize", this.resize);

        if (BrowserDetect.browser == "Safari") {
            var slDiv = document.getElementById('SilverlightControlHost');
            var helpDiv = document.getElementById('displayHelp');
            var textDiv = document.getElementById('displayText');
            slDiv.appendChild(helpDiv);
            slDiv.appendChild(textDiv);
        }

        this.menuButton = control.content.findName("menuButton");
        this.menuButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMenuButtonMouseDown));
        this.backButton = control.content.findName("backButton");
        this.backButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleBackButtonMouseDown));
        this.resetButton = control.content.findName("resetButton");
        this.resetButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleResetButtonMouseDown));
        this.zoomInButton = control.content.findName("zoomIn");
        this.zoomInButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleZoomInButtonMouseDown));
        this.zoomOutButton = control.content.findName("zoomOut");
        this.zoomOutButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleZoomOutButtonMouseDown));
        this.rotateButton = control.content.findName("rotateButton");
        this.rotateButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleRotateButtonMouseDown));
        this.moveButton = control.content.findName("moveButton");
        this.moveButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMoveButtonMouseDown));
        this.audioButton = control.content.findName("audioButton");
        this.audioButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleAudioButtonMouseDown));
        this.audioButton2 = control.content.findName("audioButton2");
        this.audioButton2.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleAudio2ButtonMouseDown));
        this.textButton = control.content.findName("textButton");
        this.textButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleTextButtonMouseDown));
        this.textButton2 = control.content.findName("textButton2");
        this.textButton2.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleTextButton2MouseDown));
        this.helpButton = control.content.findName("helpButton");
        this.helpButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleHelpButtonMouseDown));

        this.searchButton = control.content.findName("searchButton");
        this.searchButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleSearchButtonMouseDown));
        this.closeResultsCanvas = control.content.findName("closeResultsCanvas");
        this.closeResultsCanvas.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleCloseResultsMouseDown));
        this.closePageResultsCanvas = control.content.findName("closePageResultsCanvas");
        this.closePageResultsCanvas.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleClosePageResultsMouseDown));
        this.backToSearchResults = control.content.findName("backToSearchResults");
        this.backToSearchResults.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleBackToSearchResultsMouseDown));

        this.bookResultsPageForward = control.content.findName("bookResultsPageForward");
        this.bookResultsPageForward.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlebookResultsPageForwardMouseDown));
        this.bookResultsPageBack = control.content.findName("bookResultsPageBack");
        this.bookResultsPageBack.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlebookResultsPageBackMouseDown));

        this.pageResultsPageForward = control.content.findName("pageResultsPageForward");
        this.pageResultsPageForward.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlepageResultsPageForwardMouseDown));
        this.pageResultsPageBack = control.content.findName("pageResultsPageBack");
        this.pageResultsPageBack.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlepageResultsPageBackMouseDown));

        /* spread navigator events start */
        this.fwButton = control.content.findName("spreadForward");
        this.fwButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleFwButtonMouseDown));
        this.fwButton.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleFwButtonMouseUp));

        this.fwTime = this.control.content.findname("fwTimeline");
        this.fwTime.addEventListener("completed", Silverlight.createDelegate(this, this.handleFwAniCompleted));

        this.bkButton = control.content.findName("spreadBack");
        this.bkButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleBkButtonMouseDown));
        this.bkButton.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleBkButtonMouseUp));

        this.bkTime = this.control.content.findname("bkTimeline");
        this.bkTime.addEventListener("completed", Silverlight.createDelegate(this, this.handleBkAniCompleted));
        /* spread navigator events finish */

        //this.removeBookButton = control.content.findName("removeBookButton");
        //this.removeBookButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleRemoveBookButtonMouseDown));

        this.pageTimer = this.control.content.findname("pageTurnCheckStoryboard");
        this.pageTimer.addEventListener("completed", Silverlight.createDelegate(this, this.pageTurnCheck));

        this.bookTimer = this.control.content.findName("bookLoadedStoryboard");
        this.bookTimer.addEventListener("completed", Silverlight.createDelegate(this, this.handleBookLoaded));


        this.coverTurnOpen = this.control.content.findname("openBookTimeline");
        this.coverTurnOpen.addEventListener("completed", Silverlight.createDelegate(this, this.openBookTimelineCompleted));
        this.coverTurnClose = this.control.content.findname("closeBookTimeline");
        this.coverTurnClose.addEventListener("completed", Silverlight.createDelegate(this, this.closeBookTimelineCompleted));

        this.control.content.findName("ttpInstructions")["isHitTestVisible"] = false;
    },

    downloadConfigXmlCompleted: function(sender, eventArgs) {
        var xml = sender.responseText;
        var doc = getXmlDocument(xml);

        //first get the root element. if this fails cant load app
        try
        { var xmlRoot = doc.getElementsByTagName('ttp_options')[0]; }
        catch (e) { alert("Could not find configuration settings."); }

        //get the server settings node.
        try { var server = xmlRoot.getElementsByTagName("server")[0]; }
        catch (e) { alert("Could not find server settings"); }

        try {
            this.application_server_uri = (server.getElementsByTagName("application_server_uri")[0].firstChild.nodeValue);
            //this.application_server_dir = (server.getElementsByTagName("application_server_dir")[0].firstChild.nodeValue);
            this.media_server_uri = (server.getElementsByTagName("media_server_uri")[0].firstChild.nodeValue);
            this.media_server_dir = (server.getElementsByTagName("media_server_dir")[0].firstChild.nodeValue);
            this.media_dir_local = (server.getElementsByTagName("media_dir_local")[0].firstChild.nodeValue);
            this.web_service_uri = (server.getElementsByTagName("web_service_uri")[0].firstChild.nodeValue);
            this.web_service_filename = (server.getElementsByTagName("web_service_filename")[0].firstChild.nodeValue);
        }
        catch (e) { alert("Failed to apply a server setting."); }

        //get the system settings node.
        try { var system = xmlRoot.getElementsByTagName("system")[0]; }
        catch (e) { alert("Could not find system settings"); }

        try
        { this.language = (system.getElementsByTagName("language")[0].firstChild.nodeValue); }
        catch (e) { alert("Failed to apply a system settings."); }

        //get the library settings node.
        try { var library = xmlRoot.getElementsByTagName("library")[0]; }
        catch (e) { alert("Could not find library settings"); }
        try
        { this.library_id = (library.getElementsByTagName("library_id")[0].firstChild.nodeValue); }
        catch (e) { alert("Failed to apply a library settings."); }
        try
        { this.online_book = (library.getElementsByTagName("online_book")[0].firstChild.nodeValue); }
        catch (e) { alert("Failed to apply a library online setting."); }

        var online_param = gup('online_book');
        if (online_param != null) {
            if (online_param.toLowerCase() == 'false' || online_param.toLowerCase() == 'true')
                this.online_book = online_param.toLowerCase();
        }
        
        var id_param = gup('id');
        var spread_param = gup('spread')
        if (id_param == null)
            this.handleMenuButtonMouseDown(this, null);
        else {
            //alert(id_param + "     " + spread_param);
            this.handleXamlItemClick(id_param, false, spread_param);
        }
    },

    handleSearchButtonMouseDown: function(sender, eventArgs) {
        this.handleDisableBookResultsBack();
        this.handleDisableBookResultsForward();
        this.handleDisablePageResultsBack();
        this.handleDisablePageResultsForward();
        if (this.rootCanvas.findName("resultsHolder").children.count >= 1) {
            this.rootCanvas.findName("resultsCanvas")["Visibility"] = "Collapsed";
            this.rootCanvas.findName("resultsHolder").children.clear();
        }

        if (this.rootCanvas.findName("pageResultsHolder").children.count >= 1) {
            this.rootCanvas.findName("pageResultsCanvas")["Visibility"] = "Collapsed";
            this.rootCanvas.findName("pageResultsHolder").children.clear();
        }

        var searchTerm = document.getElementById("searchBox").value;
        this.currentSearchTerm = trim(searchTerm);
        if (trim(searchTerm) == '') {
            alert("please enter a search term");
        }
        else {
            //alert(searchTerm);
            this.webservices = new WebServices(this.application_server_uri + this.web_service_filename);
            this.result = this.webservices.SearchForBooksWithMatchingSpreads(searchTerm, this.language, this.library_id, this.online_book);

            if (this.result != '') {
                this.SearchResultsArray.length = 0;

                if (this.result.length <= 3) {
                    this.rootCanvas.findName("resultsCanvas")["Visibility"] = "Visible";
                    this.rootCanvas.findName("bookResultsPage")["Text"] = "Page 1 of 1";
                    this.currentBookResultPage = 1;

                    for (var i = 0; i < this.result.length; i++) {
                        //alert(this.result[i].ShortTitle + this.result[i].Title + this.result[i].ShortDescription);
                        if (this.result[i].CoverWidth == "0" && this.result[i].CoverHeight == "0")
                            coverImage = this.media_server_uri + this.media_server_dir + this.result[i].BookID + "/sl_lo/" + this.result[i].ShortTitle + "_lo_p1.png";
                        else
                            coverImage = this.media_server_uri + this.media_server_dir + this.result[i].BookID + "/sl_lo/" + this.result[i].ShortTitle + "_lo_fc.png";

                        xamlResultString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='400' Height='110' Canvas.Top='$6' >";
                        xamlResultString = xamlResultString + "<Image Cursor='Hand' Source='" + coverImage + "' x:Name='$4Image' Width='100' Height='100'/>";
                        xamlResultString = xamlResultString + "<TextBlock FontFamily='" + this.fontFamily + "' Foreground='White' Width='280' Height='29' Canvas.Left='112' Canvas.Top='0'" + ' Text="$2" ' + " />";
                        xamlResultString = xamlResultString + "<TextBlock FontFamily='" + this.fontFamily + "' Foreground='White' Width='280' Height='43' TextWrapping='Wrap' Canvas.Left='112' Canvas.Top='25'" + ' Text="$3" ' + "/>";
                        /*xamlResultString = xamlResultString + "<Canvas Cursor='Hand' Width='130' Height='19' Canvas.Left='310' Canvas.Top='89' x:Name='$5Pages' Background='#FFFFFFFF'>";
                        xamlResultString = xamlResultString + "<TextBlock Width='150' Height='25' Canvas.Left='0' Canvas.Top='1' FontSize='12' Text='View matching pages' />";
                        xamlResultString = xamlResultString + "</Canvas>";*/
                        xamlResultString = xamlResultString + "<Canvas Width='140' Height='20' Canvas.Top='89' Canvas.Left='300' Cursor='Hand' x:Name='$5Pages'>";
                        xamlResultString = xamlResultString + "<Rectangle Width='140' Height='20' RadiusX='6' RadiusY='6' Stroke='#FF6A6666' StrokeThickness='2'>";
                        xamlResultString = xamlResultString + "<Rectangle.Fill>";
                        xamlResultString = xamlResultString + "<RadialGradientBrush>";
                        xamlResultString = xamlResultString + "<RadialGradientBrush.RelativeTransform>";
                        xamlResultString = xamlResultString + "<TransformGroup>";
                        xamlResultString = xamlResultString + "<ScaleTransform CenterX='0.5' CenterY='0.5' ScaleX='2.997' ScaleY='2.997'/>";
                        xamlResultString = xamlResultString + "<SkewTransform AngleX='0' AngleY='0' CenterX='0.5' CenterY='0.5'/>";
                        xamlResultString = xamlResultString + "<RotateTransform Angle='0' CenterX='0.5' CenterY='0.5'/>";
                        xamlResultString = xamlResultString + "<TranslateTransform X='0.014' Y='-0.279'/>";
                        xamlResultString = xamlResultString + "</TransformGroup>";
                        xamlResultString = xamlResultString + "</RadialGradientBrush.RelativeTransform>";
                        xamlResultString = xamlResultString + "<GradientStop Color='#FF434343' Offset='0.168'/>";
                        xamlResultString = xamlResultString + "<GradientStop Color='#FFFFFFFF' Offset='1'/>";
                        xamlResultString = xamlResultString + "</RadialGradientBrush>";
                        xamlResultString = xamlResultString + "</Rectangle.Fill>";
                        xamlResultString = xamlResultString + "</Rectangle>";
                        xamlResultString = xamlResultString + "<TextBlock FontFamily='" + this.fontFamily + "' Cursor='Hand' Canvas.Left='5' Canvas.Top='2' FontSize='12' Foreground='White'>View matching pages</TextBlock> ";
                        xamlResultString = xamlResultString + "</Canvas>";
                        xamlResultString = xamlResultString + "</Canvas>";

                        xamlResultString = xamlResultString.replace("$1", this.result[i].ShortTitle);
                        xamlResultString = xamlResultString.replace("$2", this.result[i].Title);
                        xamlResultString = xamlResultString.replace("$3", this.result[i].ShortDescription);
                        xamlResultString = xamlResultString.replace("$4", this.result[i].BookID);
                        xamlResultString = xamlResultString.replace("$5", this.result[i].BookID);
                        xamlResultString = xamlResultString.replace("$6", (i * 130));

                        var item = this.control.content.createFromXaml(xamlResultString);

                        //this.SearchResults = new Array();
                        this.SearchResultsArray[i] = new SearchResult(item, this.result[i].ShortTitle, this.result[i].BookID);
                        this.SearchResultsArray[i].bookClickHandler = delegate(this, this.handleXamlItemClick);
                        this.SearchResultsArray[i].viewPagesClickHandler = delegate(this, this.handleGetPageResultsItemClick);
                        this.SearchResultsArray[i].xaml = xamlResultString;

                        this.rootCanvas.findName("resultsHolder").children.add(item)
                    }
                    this.rootCanvas.findName("resultsBackground")["Height"] = (this.result.length * 130) + 35;
                }
                else {
                    this.setupMultiPageBookResult(this.result);
                }
            }
            else {
                alert("Your search produced no results. Please try again.");
            }
        }
    },

    handleGetPageResultsItemClick: function(xamlItem) {
        this.webservices = new WebServices(this.application_server_uri + this.web_service_filename);
        this.result = this.webservices.searchForSpreads(this.currentSearchTerm, xamlItem.id, this.language, this.library_id, this.online_book);
        //alert();
        if (this.result.length <= 3) {
            this.rootCanvas.findName("pageResultsCanvas")["Visibility"] = "Visible";
            this.rootCanvas.findName("resultsCanvas")["Visibility"] = "Collapsed";
            this.rootCanvas.findName("pageResultsTitle")["Text"] = "Search results for " + xamlItem.shortTitle;
            this.rootCanvas.findName("pageResultsBackground")["Height"] = (this.result.length * 110) + 80;
            this.rootCanvas.findName("backToSearchResults")["Canvas.Top"] = (this.result.length * 110) + 55;
            this.rootCanvas.findName("pageResultsPage")["Text"] = "Page 1 of 1";

            for (var i = 0; i < this.result.length; i++) {
                if (returnRotation(this.result[i].PreRotationAngleY) == 90) {
                    this.rotateAngle = 90;
                    this.canvasLeft = 45;
                    this.height = 75;
                }
                else {
                    this.rotateAngle = 0;
                    this.canvasLeft = 70;
                    this.height = 100;
                }
                if (this.result[i].RightToLeft == false) {
                    if (this.result[i].ImageVersoLo == null)
                        this.versoImage = "";
                    else
                        this.versoImage = this.media_server_uri + this.media_server_dir + xamlItem.id + "/sl_lo/" + this.result[i].ImageVersoLo + ".png";

                    this.rectoImage = this.media_server_uri + this.media_server_dir + xamlItem.id + "/sl_lo/" + this.result[i].ImageRectoLo + ".png";
                }
                else {
                    if (this.result[i].ImageVersoLo == null)
                        this.rectoImage = "";
                    else
                        this.rectoImage = this.media_server_uri + this.media_server_dir + xamlItem.id + "/sl_lo/" + this.result[i].ImageVersoLo + ".png";

                    this.versoImage = this.media_server_uri + this.media_server_dir + xamlItem.id + "/sl_lo/" + this.result[i].ImageRectoLo + ".png";
                }
                xamlResultString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Canvas.Top='$1'>";
                xamlResultString = xamlResultString + "<Canvas Width='0' Height='0' Canvas.Left='10' Canvas.Top='10'>";
                xamlResultString = xamlResultString + "<Canvas RenderTransformOrigin='0.5,0.5' Width='80' Height='" + this.height + "'>";
                xamlResultString = xamlResultString + "<Canvas.RenderTransform>";
                xamlResultString = xamlResultString + "<RotateTransform Angle='" + this.rotateAngle + "'/>";
                xamlResultString = xamlResultString + "</Canvas.RenderTransform>";
                xamlResultString = xamlResultString + "<Image Cursor='Hand' Height='" + this.height + "' Source='$2'/>";
                xamlResultString = xamlResultString + "<Image Cursor='Hand' Height='" + this.height + "' Canvas.Left='" + this.canvasLeft + "' Source='$3'/>";
                xamlResultString = xamlResultString + "</Canvas>";
                xamlResultString = xamlResultString + "<TextBlock FontFamily='" + this.fontFamily + "' x:Name='textContent" + i + "' Width='332' Height='100' Canvas.Left='151' FontSize='12' TextWrapping='Wrap'>";
                xamlResultString = xamlResultString + "<TextBlock.Clip>";
                xamlResultString = xamlResultString + "<RectangleGeometry Rect='0,0,332,90'/>";
                xamlResultString = xamlResultString + "</TextBlock.Clip>";
                xamlResultString = xamlResultString + "</TextBlock>";
                xamlResultString = xamlResultString + "</Canvas>";
                xamlResultString = xamlResultString + "</Canvas>";

                xamlResultString = xamlResultString.replace("$1", (i * 110));
                xamlResultString = xamlResultString.replace("$2", this.versoImage);
                xamlResultString = xamlResultString.replace("$3", this.rectoImage);
                //xamlResultString = xamlResultString.replace("$4", "Text to spread goes here");
                var item = this.control.content.createFromXaml(xamlResultString);
                xamlItem.pageResults[i] = new PageResult(item, this.result[i].BookID, this.result[i].SpreadID, this.result[i].ImageVersoLo, this.result[i].ImageRectoLo, this.result[i].TextContent, this.result[i].Position, xamlItem.shortTitle, this.result[i].RightToLeft, returnRotation(this.result[i].PreRotationAngleY));
                xamlItem.pageResults[i].pageClickHandler = delegate(this, this.handlePageResultItemClick);
                xamlItem.pageResults[i].xaml = xamlResultString;
                this.rootCanvas.findName("pageResultsHolder").children.add(item);
                //this.rootCanvas.findName("textContent" + i)["Text"] = this.result[i].TextContent;
                newi = i;

                this.resultArray = new Array();
                this.resultArray = this.result[i].TextContent.toString().split(" ");

                for (var j = 0; j < this.resultArray.length; j++) {
                    if (j < 60) {
                        this.rootCanvas.findName("textContent" + newi)["Text"] = this.rootCanvas.findName("textContent" + newi)["Text"] + this.resultArray[j] + " ";
                    }
                }
            }
        }
        else {
            // alert("more than one page. length = " + this.result.length);
            this.setupMultiPagePageResult(xamlItem, this.result);
        }
    },

    handlePageResultItemClick: function(xamlItem) {
        this.handleXamlItemClick(xamlItem.id, false, xamlItem.position);
        //alert(xamlItem.spreadID + " bookid: " + xamlItem.id + "\n" + xamlItem.xaml + "\n" + xamlItem.title + "\n" + xamlItem.position + "\n" + xamlItem.text);
    },

    handleCloseResultsMouseDown: function(sender, eventArgs) {
        this.rootCanvas.findName("resultsCanvas")["Visibility"] = "Collapsed";
        this.rootCanvas.findName("resultsHolder").children.clear();
        this.SearchResultsArray.length = 0;
    },

    handleClosePageResultsMouseDown: function(sender, eventArgs) {
        this.rootCanvas.findName("pageResultsCanvas")["Visibility"] = "Collapsed";
        this.rootCanvas.findName("pageResultsHolder").children.clear();
        this.rootCanvas.findName("resultsCanvas")["Visibility"] = "Collapsed";
        this.rootCanvas.findName("resultsHolder").children.clear();
        this.SearchResultsArray.length = 0;

        this.handleDisablePageResultsBack();
        this.handleDisablePageResultsForward();
    },

    handleBackToSearchResultsMouseDown: function(sender, eventArgs) {
        this.rootCanvas.findName("pageResultsCanvas")["Visibility"] = "Collapsed";
        this.rootCanvas.findName("pageResultsHolder").children.clear();
        this.rootCanvas.findName("resultsCanvas")["Visibility"] = "Visible";

        this.handleDisablePageResultsBack();
        this.handleDisablePageResultsForward();
    },

    setupMultiPageBookResult: function(searchResult) {
        this.SearchResultsForBooks = searchResult;
        this.rootCanvas.findName("resultsCanvas")["Visibility"] = "Visible";

        this.handleEnableBookResultsForward();
        this.SearchResultsLength = searchResult.length;
        this.currentBookResultPage = 1;
        this.maxBookResultPage = (Math.ceil((searchResult.length / 3)));
        this.rootCanvas.findName("bookResultsPage")["Text"] = "Page " + this.currentBookResultPage + " of " + this.maxBookResultPage;
        for (var i = 0; i < 3; i++) {
            //if(i <=3)
            //{
            var item = this.createXamlForBookResult(searchResult[i].BookID, searchResult[i].Title, searchResult[i].ShortTitle, searchResult[i].ShortDescription, searchResult[i].CoverWidth, searchResult[i].CoverHeight, i);
            this.SearchResultsArray[i] = new SearchResult(item, searchResult[i].ShortTitle, searchResult[i].BookID);
            this.SearchResultsArray[i].bookClickHandler = delegate(this, this.handleXamlItemClick);
            this.SearchResultsArray[i].viewPagesClickHandler = delegate(this, this.handleGetPageResultsItemClick);
            this.SearchResultsArray[i].xaml = xamlResultString;
            this.rootCanvas.findName("resultsHolder").children.add(item);
            //}
        }
        this.rootCanvas.findName("resultsBackground")["Height"] = (3 * 130) + 35;
    },

    setupMultiPagePageResult: function(xamlItem, pageSearchResult) {
        this.currentActiveSearchResultArrayItem = xamlItem;
        this.SearchResultsForPages = pageSearchResult;
        this.currentPageResultPage = 1;
        this.maxPageResultPage = (Math.ceil((pageSearchResult.length / 3)));
        this.rootCanvas.findName("pageResultsPage")["Text"] = "Page " + this.currentPageResultPage + " of " + this.maxPageResultPage;
        this.rootCanvas.findName("pageResultsCanvas")["Visibility"] = "Visible";
        this.rootCanvas.findName("resultsCanvas")["Visibility"] = "Collapsed";
        this.rootCanvas.findName("pageResultsTitle")["Text"] = "Search results for " + xamlItem.shortTitle;
        this.rootCanvas.findName("pageResultsBackground")["Height"] = (3 * 110) + 80;
        this.rootCanvas.findName("backToSearchResults")["Canvas.Top"] = (3 * 110) + 55;

        this.handleEnablePageResultsForward();
        for (var i = 0; i < 3; i++) {
            var item = this.createXamlForPageResult(xamlItem.id, xamlItem.shortTitle, pageSearchResult[i].TextContent, pageSearchResult[i].ImageVersoLo, pageSearchResult[i].ImageRectoLo, i, pageSearchResult[i].RightToLeft, returnRotation(pageSearchResult[i].PreRotationAngleY));
            xamlItem.pageResults[i] = new PageResult(item, pageSearchResult[i].BookID, pageSearchResult[i].SpreadID, pageSearchResult[i].ImageVersoLo, pageSearchResult[i].ImageRectoLo, pageSearchResult[i].TextContent, pageSearchResult[i].Position, xamlItem.shortTitle, pageSearchResult[i].RightToLeft, returnRotation(pageSearchResult[i].PreRotationAngleY));
            xamlItem.pageResults[i].pageClickHandler = delegate(this, this.handlePageResultItemClick);
            xamlItem.pageResults[i].xaml = xamlResultString;

            this.rootCanvas.findName("pageResultsHolder").children.add(item);
            newi = i;
            this.resultArray = new Array();
            this.resultArray = pageSearchResult[i].TextContent.toString().split(" ");
            for (var j = 0; j < this.resultArray.length; j++) {
                if (j < 60) {
                    this.rootCanvas.findName("textContent" + newi)["Text"] = this.rootCanvas.findName("textContent" + newi)["Text"] + this.resultArray[j] + " ";
                }
            }
            //this.rootCanvas.findName("textContent" + i)["Text"] = pageSearchResult[i].TextContent;
        }
    },

    createXamlForBookResult: function(bookID, title, shortTitle, desc, width, height, i) {
        if (width == "0" && height == "0")
            coverImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + shortTitle + "_lo_p1.png";
        else
            coverImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + shortTitle + "_lo_fc.png";

        xamlResultString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='400' Height='110' Canvas.Top='$6' >";
        xamlResultString = xamlResultString + "<Image Cursor='Hand' Source='" + coverImage + "' x:Name='$4Image' Width='100' Height='100'/>";
        xamlResultString = xamlResultString + "<TextBlock FontFamily='" + this.fontFamily + "' Foreground='White' Width='280' Height='29' Canvas.Left='112' Canvas.Top='0'" + ' Text="$2" ' + " />";
        xamlResultString = xamlResultString + "<TextBlock FontFamily='" + this.fontFamily + "' Foreground='White' Width='280' Height='43' TextWrapping='Wrap' Canvas.Left='112' Canvas.Top='25'" + ' Text="$3" ' + "/>";
        /*xamlResultString = xamlResultString + "<Canvas Cursor='Hand' Width='130' Height='19' Canvas.Left='310' Canvas.Top='89' x:Name='$5Pages' Background='#FFFFFFFF'>";
        xamlResultString = xamlResultString + "<TextBlock Width='150' Height='25' Canvas.Left='0' Canvas.Top='1' FontSize='12' Text='View matching pages' />";
        xamlResultString = xamlResultString + "</Canvas>";*/
        xamlResultString = xamlResultString + "<Canvas Width='140' Height='20' Canvas.Top='89' Canvas.Left='300' x:Name='$5Pages' Cursor='Hand'>";
        xamlResultString = xamlResultString + "<Rectangle Width='140' Height='20' RadiusX='6' RadiusY='6' Stroke='#FF6A6666' StrokeThickness='2'>";
        xamlResultString = xamlResultString + "<Rectangle.Fill>";
        xamlResultString = xamlResultString + "<RadialGradientBrush>";
        xamlResultString = xamlResultString + "<RadialGradientBrush.RelativeTransform>";
        xamlResultString = xamlResultString + "<TransformGroup>";
        xamlResultString = xamlResultString + "<ScaleTransform CenterX='0.5' CenterY='0.5' ScaleX='2.997' ScaleY='2.997'/>";
        xamlResultString = xamlResultString + "<SkewTransform AngleX='0' AngleY='0' CenterX='0.5' CenterY='0.5'/>";
        xamlResultString = xamlResultString + "<RotateTransform Angle='0' CenterX='0.5' CenterY='0.5'/>";
        xamlResultString = xamlResultString + "<TranslateTransform X='0.014' Y='-0.279'/>";
        xamlResultString = xamlResultString + "</TransformGroup>";
        xamlResultString = xamlResultString + "</RadialGradientBrush.RelativeTransform>";
        xamlResultString = xamlResultString + "<GradientStop Color='#FF434343' Offset='0.168'/>";
        xamlResultString = xamlResultString + "<GradientStop Color='#FFFFFFFF' Offset='1'/>";
        xamlResultString = xamlResultString + "</RadialGradientBrush>";
        xamlResultString = xamlResultString + "</Rectangle.Fill>";
        xamlResultString = xamlResultString + "</Rectangle>";
        xamlResultString = xamlResultString + "<TextBlock FontFamily='" + this.fontFamily + "' Cursor='Hand' Canvas.Left='5' Canvas.Top='2' FontSize='12' Foreground='White'>View matching pages</TextBlock> ";
        xamlResultString = xamlResultString + "</Canvas>";
        xamlResultString = xamlResultString + "</Canvas>";

        xamlResultString = xamlResultString.replace("$1", shortTitle);
        xamlResultString = xamlResultString.replace("$2", title);
        xamlResultString = xamlResultString.replace("$3", desc);
        xamlResultString = xamlResultString.replace("$4", bookID);
        xamlResultString = xamlResultString.replace("$5", bookID);
        xamlResultString = xamlResultString.replace("$6", ((i % 3) * 130));

        var item = this.control.content.createFromXaml(xamlResultString);
        return item;
    },

    createXamlForPageResult: function(bookID, shortTitle, text, verso, recto, i, rightToLeft, preRotationAngleY) {

        if (preRotationAngleY == 90) {
            this.rotateAngle = 90;
            this.canvasLeft = 45;
            this.height = 75;
        }
        else {
            this.rotateAngle = 0;
            this.canvasLeft = 70;
            this.height = 100;
        }
        if (rightToLeft == false) {
            if (verso == null)
                this.versoImage = "";
            else
                this.versoImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + verso + ".png";

            this.rectoImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + recto + ".png";
        }
        else {
            if (verso == null)
                this.rectoImage = "";
            else
                this.rectoImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + verso + ".png";

            this.versoImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + recto + ".png";
        }

        xamlResultString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Canvas.Top='$1'>";
        xamlResultString = xamlResultString + "<Canvas Width='0' Height='0' Canvas.Left='10' Canvas.Top='10'>";
        xamlResultString = xamlResultString + "<Canvas RenderTransformOrigin='0.5,0.5' Width='80' Height='" + this.height + "'>";
        xamlResultString = xamlResultString + "<Canvas.RenderTransform>";
        xamlResultString = xamlResultString + "<RotateTransform Angle='" + this.rotateAngle + "'/>";
        xamlResultString = xamlResultString + "</Canvas.RenderTransform>";
        xamlResultString = xamlResultString + "<Image Cursor='Hand' Height='" + this.height + "' Source='$2'/>";
        xamlResultString = xamlResultString + "<Image Cursor='Hand' Height='" + this.height + "' Canvas.Left='" + this.canvasLeft + "' Source='$3'/>";
        xamlResultString = xamlResultString + "</Canvas>";
        xamlResultString = xamlResultString + "<TextBlock FontFamily='" + this.fontFamily + "' x:Name='textContent" + i + "' Width='332' Height='100' Canvas.Left='151' FontSize='12' TextWrapping='Wrap'>";
        xamlResultString = xamlResultString + "<TextBlock.Clip>";
        xamlResultString = xamlResultString + "<RectangleGeometry Rect='0,0,332,90'/>";
        xamlResultString = xamlResultString + "</TextBlock.Clip>";
        xamlResultString = xamlResultString + "</TextBlock>";
        xamlResultString = xamlResultString + "</Canvas>";
        xamlResultString = xamlResultString + "</Canvas>";




        xamlResultString = xamlResultString.replace("$1", ((i % 3) * 110));
        xamlResultString = xamlResultString.replace("$2", this.versoImage);
        xamlResultString = xamlResultString.replace("$3", this.rectoImage);
        //xamlResultString = xamlResultString.replace("$4", "Text to spread goes here");
        var item = this.control.content.createFromXaml(xamlResultString);
        return item;
    },

    handlebookResultsPageBackMouseDown: function(sender, eventArgs) {
        //alert("go back");
        this.rootCanvas.findName("resultsHolder").children.clear();
        this.rootCanvas.findName("resultsBackground")["Height"] = (3 * 130) + 35;
        this.currentBookResultPage--;

        if ((this.currentBookResultPage) != this.maxBookResultPage)
            this.handleEnableBookResultsForward();

        if ((this.currentBookResultPage) == 1)
            this.handleDisableBookResultsBack();

        this.loopStart = ((this.currentBookResultPage - 1) * 3);
        this.loopCount = this.loopStart + 3;

        for (var i = this.loopStart; i < this.loopCount; i++) {
            var item = this.createXamlForBookResult(this.SearchResultsForBooks[i].BookID, this.SearchResultsForBooks[i].Title, this.SearchResultsForBooks[i].ShortTitle, this.SearchResultsForBooks[i].ShortDescription, this.SearchResultsForBooks[i].CoverWidth, this.SearchResultsForBooks[i].CoverHeight, i);
            this.SearchResultsArray[i] = new SearchResult(item, this.SearchResultsForBooks[i].ShortTitle, this.SearchResultsForBooks[i].BookID);
            this.SearchResultsArray[i].bookClickHandler = delegate(this, this.handleXamlItemClick);
            this.SearchResultsArray[i].viewPagesClickHandler = delegate(this, this.handleGetPageResultsItemClick);
            this.SearchResultsArray[i].xaml = xamlResultString;
            this.rootCanvas.findName("resultsHolder").children.add(item);
        }
        this.rootCanvas.findName("bookResultsPage")["Text"] = "Page " + this.currentBookResultPage + " of " + this.maxBookResultPage;
    },

    handlebookResultsPageForwardMouseDown: function(sender, eventArgs) {
        this.rootCanvas.findName("resultsHolder").children.clear();
        this.currentBookResultPage++;

        if ((this.currentBookResultPage) >= this.maxBookResultPage)
            this.handleDisableBookResultsForward();

        if ((this.currentBookResultPage) >= 1)
            this.handleEnableBookResultsBack();

        this.loopStart = ((this.currentBookResultPage - 1) * 3);
        this.loopCount;

        if (this.currentBookResultPage == this.maxBookResultPage) {
            if ((this.SearchResultsLength % 3) == 0) {
                this.loopCount = this.loopStart + 3;
                this.rootCanvas.findName("resultsBackground")["Height"] = ((3) * 130) + 35;
            }
            else {
                this.loopCount = this.loopStart + (this.SearchResultsLength % 3);
                this.rootCanvas.findName("resultsBackground")["Height"] = ((this.SearchResultsLength % 3) * 130) + 35;
            }
        }
        else {
            this.loopCount = this.loopStart + 3;
        }

        for (var i = this.loopStart; i < this.loopCount; i++) {
            var item = this.createXamlForBookResult(this.SearchResultsForBooks[i].BookID, this.SearchResultsForBooks[i].Title, this.SearchResultsForBooks[i].ShortTitle, this.SearchResultsForBooks[i].ShortDescription, this.SearchResultsForBooks[i].CoverWidth, this.SearchResultsForBooks[i].CoverHeight, i);
            this.SearchResultsArray[i] = new SearchResult(item, this.SearchResultsForBooks[i].ShortTitle, this.SearchResultsForBooks[i].BookID);
            this.SearchResultsArray[i].bookClickHandler = delegate(this, this.handleXamlItemClick);
            this.SearchResultsArray[i].viewPagesClickHandler = delegate(this, this.handleGetPageResultsItemClick);
            this.SearchResultsArray[i].xaml = xamlResultString;
            this.rootCanvas.findName("resultsHolder").children.add(item);
        }
        this.rootCanvas.findName("bookResultsPage")["Text"] = "Page " + this.currentBookResultPage + " of " + this.maxBookResultPage;
    },

    handlepageResultsPageBackMouseDown: function(sender, eventArgs) {
        //alert("clicked");
        var xamlItem = this.currentActiveSearchResultArrayItem;
        this.rootCanvas.findName("pageResultsHolder").children.clear();
        this.rootCanvas.findName("pageResultsBackground")["Height"] = (3 * 110) + 80;
        this.rootCanvas.findName("backToSearchResults")["Canvas.Top"] = (3 * 110) + 55;
        this.currentPageResultPage--;
        //alert(this.currentPageResultPage + "             "+ this.maxPageResultPage + "        " + xamlItem.pageResults.length);
        if ((this.currentPageResultPage) != this.maxPageResultPage)
            this.handleEnablePageResultsForward();

        if ((this.currentPageResultPage) == 1)
            this.handleDisablePageResultsBack();

        this.loopStart = ((this.currentPageResultPage - 1) * 3);
        this.loopCount = this.loopStart + 3;
        for (var i = this.loopStart; i < this.loopCount; i++) {

            var item = this.createXamlForPageResult(xamlItem.id, xamlItem.shortTitle, this.SearchResultsForPages[i].TextContent, this.SearchResultsForPages[i].ImageVersoLo, this.SearchResultsForPages[i].ImageRectoLo, i, this.SearchResultsForPages[i].RightToLeft, returnRotation(this.SearchResultsForPages[i].PreRotationAngleY));
            xamlItem.pageResults[i] = new PageResult(item, this.SearchResultsForPages[i].BookID, this.SearchResultsForPages[i].SpreadID, this.SearchResultsForPages[i].ImageVersoLo, this.SearchResultsForPages[i].ImageRectoLo, this.SearchResultsForPages[i].TextContent, this.SearchResultsForPages[i].Position, xamlItem.shortTitle, this.SearchResultsForPages[i].RightToLeft, returnRotation(this.SearchResultsForPages[i].PreRotationAngleY));
            xamlItem.pageResults[i].pageClickHandler = delegate(this, this.handlePageResultItemClick);
            xamlItem.pageResults[i].xaml = xamlResultString;

            this.rootCanvas.findName("pageResultsHolder").children.add(item);

            newi = i;
            this.resultArray = new Array();
            this.resultArray = this.SearchResultsForPages[i].TextContent.toString().split(" ");
            for (var j = 0; j < this.resultArray.length; j++) {
                if (j < 60) {
                    this.rootCanvas.findName("textContent" + newi)["Text"] = this.rootCanvas.findName("textContent" + newi)["Text"] + this.resultArray[j] + " ";
                }
            }
            //this.rootCanvas.findName("textContent" + i)["Text"] = this.SearchResultsForPages[i].TextContent
        }
        this.rootCanvas.findName("pageResultsPage")["Text"] = "Page " + this.currentPageResultPage + " of " + this.maxPageResultPage;
    },

    handlepageResultsPageForwardMouseDown: function(sender, eventArgs) {
        //alert("clicked111111111");
        var xamlItem = this.currentActiveSearchResultArrayItem;
        this.rootCanvas.findName("pageResultsHolder").children.clear();
        this.currentPageResultPage++;
        //alert(this.currentPageResultPage + "             "+ this.maxPageResultPage + "        " + xamlItem.pageResults.length);
        if ((this.currentPageResultPage) >= this.maxPageResultPage)
            this.handleDisablePageResultsForward();

        if ((this.currentPageResultPage) >= 1)
            this.handleEnablePageResultsBack();

        this.loopStart = ((this.currentPageResultPage - 1) * 3);
        this.loopCount;

        if (this.currentPageResultPage == this.maxPageResultPage) {
            if ((this.SearchResultsForPages.length % 3) == 0) {
                this.loopCount = this.loopStart + 3;
                this.rootCanvas.findName("pageResultsBackground")["Height"] = ((3) * 110) + 80;
                this.rootCanvas.findName("backToSearchResults")["Canvas.Top"] = ((3) * 110) + 55;
            }
            else {
                this.loopCount = this.loopStart + (this.SearchResultsForPages.length % 3);
                this.rootCanvas.findName("pageResultsBackground")["Height"] = ((this.SearchResultsForPages.length % 3) * 110) + 80;
                this.rootCanvas.findName("backToSearchResults")["Canvas.Top"] = ((this.SearchResultsForPages.length % 3) * 110) + 55;
            }
        }
        else {
            this.loopCount = this.loopStart + 3;
        }

        for (var i = this.loopStart; i < this.loopCount; i++) {

            var item = this.createXamlForPageResult(xamlItem.id, xamlItem.shortTitle, this.SearchResultsForPages[i].TextContent, this.SearchResultsForPages[i].ImageVersoLo, this.SearchResultsForPages[i].ImageRectoLo, i, this.SearchResultsForPages[i].RightToLeft, returnRotation(this.SearchResultsForPages[i].PreRotationAngleY));
            xamlItem.pageResults[i] = new PageResult(item, this.SearchResultsForPages[i].BookID, this.SearchResultsForPages[i].SpreadID, this.SearchResultsForPages[i].ImageVersoLo, this.SearchResultsForPages[i].ImageRectoLo, this.SearchResultsForPages[i].TextContent, this.SearchResultsForPages[i].Position, xamlItem.shortTitle, this.SearchResultsForPages[i].RightToLeft, returnRotation(this.SearchResultsForPages[i].PreRotationAngleY));
            xamlItem.pageResults[i].pageClickHandler = delegate(this, this.handlePageResultItemClick);
            xamlItem.pageResults[i].xaml = xamlResultString;

            this.rootCanvas.findName("pageResultsHolder").children.add(item);

            newi = i;
            this.resultArray = new Array();
            this.resultArray = this.SearchResultsForPages[i].TextContent.toString().split(" ");
            for (var j = 0; j < this.resultArray.length; j++) {
                if (j < 60) {
                    this.rootCanvas.findName("textContent" + newi)["Text"] = this.rootCanvas.findName("textContent" + newi)["Text"] + this.resultArray[j] + " ";
                }
            }
            //this.rootCanvas.findName("textContent" + i)["Text"] = this.SearchResultsForPages[i].TextContent
        }
        this.rootCanvas.findName("pageResultsPage")["Text"] = "Page " + this.currentPageResultPage + " of " + this.maxPageResultPage;
    },

    handleEnableBookResultsForward: function() {
        this.rootCanvas.findName("bookResultsPageForward")["Visibility"] = "Visible";
    },

    handleEnableBookResultsBack: function() {
        this.rootCanvas.findName("bookResultsPageBack")["Visibility"] = "Visible";
    },

    handleDisableBookResultsForward: function() {
        this.rootCanvas.findName("bookResultsPageForward")["Visibility"] = "Collapsed";
    },

    handleDisableBookResultsBack: function() {
        this.rootCanvas.findName("bookResultsPageBack")["Visibility"] = "Collapsed";
    },

    handleEnablePageResultsForward: function() {
        this.rootCanvas.findName("pageResultsPageForward")["Visibility"] = "Visible";
    },

    handleEnablePageResultsBack: function() {
        this.rootCanvas.findName("pageResultsPageBack")["Visibility"] = "Visible";
    },

    handleDisablePageResultsForward: function() {
        this.rootCanvas.findName("pageResultsPageForward")["Visibility"] = "Collapsed";
    },

    handleDisablePageResultsBack: function() {
        this.rootCanvas.findName("pageResultsPageBack")["Visibility"] = "Collapsed";
    },

    openBookTimelineCompleted: function(sender, eventArgs) {
        this.jumpToSpreadNaviPage((0), true);
        this.checkSpreadIsPlayingAudio();
        this.updatePageText();
        this.doesSpreadAudioNeedUpdate();

        //turn the next spread to hi res    
        this.loadSpreadImages(this.spreadTicker + 1, "forward", "med");

        this.bookScene.navigationManager.enablePageTurn();

        if (this.isInstructionsTextVisible() == true)
            this.hideInstructionsText();
    },

    closeBookTimelineCompleted: function(sender, eventArgs) {
        this.jumpToSpreadNaviPage((1), false);
        this.checkSpreadIsPlayingAudio();
        this.updatePageText();
        this.doesSpreadAudioNeedUpdate();

        //turn p4 and p5 back to lo    
        this.loadSpreadImages(1, "back", "lo");

        this.bookScene.navigationManager.enablePageTurn();
    },

    loadSpreadImages: function(page, movement, res) {
        if (this.bookScene.bookDetails.backCoverImage == true) {
            switch (page) {
                case "fc":
                    var fc = this.control.content.findName("fc");
                    fc["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_fc.png";
                    break
                case "ifc":
                    var versoImage = this.control.content.findName("ifc");
                    versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_ifc.png";
                    var rectoImage = this.control.content.findName("p2");
                    rectoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p2.png";
                    break
                case "ibc":
                    var versoImage = this.control.content.findName("p" + ((this.currentPageObjects * 1) - 1));
                    versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((this.currentPageObjects * 1) - 1) + ".png";
                    var rectoImage = this.control.content.findName("ibc");
                    rectoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_ibc.png";
                    break
                default:
                    switch (res) {
                        case "med":
                            var versoImage = this.control.content.findName("p" + ((page * 2) - 1));
                            versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) - 1) + ".png";
                            var rectoImage = this.control.content.findName("p" + (page * 2));
                            rectoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + (page * 2) + ".png";
                            break
                        default: // can be lo
                            if (movement == "forward") {
                                if (page == 2) {
                                    var versoImage = this.control.content.findName("ifc");
                                    versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_ifc.png";
                                }
                                else if (page >= 3) {
                                    var versoImage = this.control.content.findName("p" + ((page * 2) - 3));
                                    versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) - 3) + ".png";
                                }
                                var rectoImage = this.control.content.findName("p" + ((page * 2) - 2));
                                rectoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) - 2) + ".png";
                            }
                            else if (movement == "spreadNavi") {
                                var versoImage = this.control.content.findName("p" + ((page * 2) - 1));
                                versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) - 1) + ".png";
                                var rectoImage = this.control.content.findName("p" + (page * 2));
                                rectoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + (page * 2) + ".png";
                            }
                            else {
                                var versoImage = this.control.content.findName("p" + ((page * 2) + 1));
                                versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) + 1) + ".png";
                                var rectoImage = this.control.content.findName("p" + ((page * 2) + 2));
                                rectoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) + 2) + ".png";
                            }
                    }
            }
        }
        else {
            switch (res) {
                case "med":
                    if (page == 0) {
                        var p1 = this.control.content.findName("p" + (page + 1));
                        p1["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + (page + 1) + ".png";
                    }
                    else {
                        var versoImage = this.control.content.findName("p" + (page * 2));
                        versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + (page * 2) + ".png";
                        var rectoImage = this.control.content.findName("p" + ((page * 2) + 1));
                        rectoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) + 1) + ".png";
                    }
                    break
                default: // lo
                    if (movement == "forward") {
                        if (page == 1) {
                            var p1 = this.control.content.findName("p" + (1));
                            p1["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + (1) + ".png";
                        }
                        else {
                            var versoImage = this.control.content.findName("p" + ((page * 2) - 1));
                            versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) - 1) + ".png";
                            var rectoImage = this.control.content.findName("p" + ((page * 2) - 2));
                            rectoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) - 2) + ".png";
                        }
                    }
                    else if (movement == "spreadNavi") {
                        if (this.spreadTicker == 0) {
                            var p1 = this.control.content.findName("p" + (page + 1));
                            p1["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + (page + 1) + ".png";
                        }
                        else {
                            var versoImage = this.control.content.findName("p" + (page * 2));
                            versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + (page * 2) + ".png";
                            var rectoImage = this.control.content.findName("p" + ((page * 2) + 1));
                            rectoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) + 1) + ".png";
                        }
                    }
                    else // back
                    {
                        var versoImage = this.control.content.findName("p" + ((page * 2) + 2));
                        versoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) + 2) + ".png";
                        var rectoImage = this.control.content.findName("p" + ((page * 2) + 3));
                        rectoImage["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookID + "/sl_" + res + "/" + this.bookScene.title + "_" + res + "_p" + ((page * 2) + 3) + ".png";
                    }
            }
        }
    },

    /*loadHiResImages: function(page)
    {
    if(this.bookScene.bookDetails.backCoverImage == true)
    {
    switch(page)
    {
    case "fc":
    var hiResImage = this.control.content.findName("fc");
    hiResImage["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/med/"+this.bookScene.title+"_med_fc.png";
    break
    case "ifc":
    var hiResImage = this.control.content.findName("ifc");
    hiResImage["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/med/"+this.bookScene.title+"_med_ifc.png"; 
    var hiResImage2 = this.control.content.findName("p2");
    hiResImage2["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/med/"+this.bookScene.title+"_med_p2.png";  
    break
    case "ibc":
    var hiResImage = this.control.content.findName("p"+((this.currentPageObjects * 1) - 1));
    hiResImage["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/med/"+this.bookScene.title+"_med_p"+((this.currentPageObjects * 1) - 1)+".png"; 
    var hiResImage2 = this.control.content.findName("ibc");
    hiResImage2["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/med/"+this.bookScene.title+"_med_ibc.png"; 
    break
    default:
    var hiResImage = this.control.content.findName("p"+((this.spreadTicker * 2) - 1));
    hiResImage["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/med/"+this.bookScene.title+"_med_p"+((this.spreadTicker * 2) - 1)+".png"; 
    var hiResImage2 = this.control.content.findName("p"+(this.spreadTicker * 2));
    hiResImage2["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/med/"+this.bookScene.title+"_med_p"+(this.spreadTicker * 2)+".png"; 
    }
    }
    else
    {
    book without binding can just use spreadTicker.
    }
    },
    
    loadLoResImages: function(page)
    {
    if(this.bookScene.bookDetails.backCoverImage == true)
    {
    switch(page)
    {
    case "fc":
    var loResImage = this.control.content.findName("fc");
    loResImage["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/lo/"+this.bookScene.title+"_lo_fc.png";
    break
    case "ifc":
    var loResImage = this.control.content.findName("ifc");
    loResImage["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/lo/"+this.bookScene.title+"_lo_ifc.png"; 
    var loResImage2 = this.control.content.findName("p2");
    loResImage2["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/lo/"+this.bookScene.title+"_lo_p2.png";  
    break
    case "ibc":
    var loResImage = this.control.content.findName("p"+((this.currentPageObjects * 1) - 1));
    loResImage["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/lo/"+this.bookScene.title+"_lo_p"+((this.currentPageObjects * 1) - 1)+".png"; 
    var loResImage2 = this.control.content.findName("ibc");
    loResImage2["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/lo/"+this.bookScene.title+"_lo_ibc.png"; 
    break
    default:
    if(this.spreadTicker == 2)
    {
    var loResImage = this.control.content.findName("ifc");
    loResImage["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/lo/"+this.bookScene.title+"_lo_ifc.png"; 
    }
    else if(this.spreadTicker >= 3)
    {
    var loResImage = this.control.content.findName("p"+((this.spreadTicker * 2) - 3));
    loResImage["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/lo/"+this.bookScene.title+"_lo_p"+((this.spreadTicker * 2) - 3)+".png"; 
    }
    var loResImage2 = this.control.content.findName("p"+((this.spreadTicker * 2) - 2));
    loResImage2["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/lo/"+this.bookScene.title+"_lo_p"+((this.spreadTicker * 2) - 2)+".png"; 
    }
    }
    else
    {
    //book without binding can just use spreadTicker.
    }
    },*/

    handleBookLoaded: function(sender, eventArgs) {
        /*var hiResImage = this.control.content.findName("fc");
        hiResImage["Source"] = "http://80.177.83.190/silverlight/ttpmedia/"+this.bookScene.title+"/med/"+this.bookScene.title+"_med_fc.png";
        //http://80.177.83.190/silverlight/ttpmedia/sherborne/med/sherborne_med_fc.png*/

        this.control.content.findName("loadingHolder")["Visibility"] = "Collapsed";
        this.control.content.findName("bookHolder")["Visibility"] = "Visible";
        this.control.content.findName("menuButton")["Opacity"] = 1;
        this.control.content.findName("menuButton")["isHitTestVisible"] = true;
        this.control.content.findName("searchButton")["Opacity"] = 1;
        this.control.content.findName("searchButton")["isHitTestVisible"] = true;
        this.loadSpreadNavigator();
        //load the appropriate hi res image
        if (this.spreadTicker == 0) {
            if (this.bookScene.bookDetails.backCoverImage == true) {
                this.loadSpreadImages("fc", null, "lo");
                this.loadSpreadImages("fc", null, "med");

                this.loadSpreadImages("ifc", null, "med");
            }
            else {
                this.loadSpreadImages(this.spreadTicker, null, "lo");
                this.loadSpreadImages(this.spreadTicker, null, "med");

                this.loadSpreadImages(this.spreadTicker + 1, null, "med");
            }
        }
        else  //book wants to load to specific spread. 
        {
            this.jumpToSpreadNaviPage((this.spreadTicker - 1), true);
            if (this.spreadTicker == 1) {
                var Evenchildren = this.control.content.findName("evenPageCanvas").children;
                var Oddchildren = this.control.content.findName("oddPageCanvas").children;
                this.hasJumpedFw = true;
                for (var i = 0; i < 4; i++) {
                    Evenchildren.insert(0, this.bookScene.arrayOfEvenPages[i]);
                    Oddchildren.add(this.bookScene.arrayOfOddPages[i]);
                }
                this.bookScene.navigationManager.jumpToPage(1);
                this.loadHiResImage();
            }
            else {
                this.loadSpreads(true);
                this.loadHiResImage();
            }
        }

        this.showInstructionsText();
        this.bookLoaded = true;
        this.resetBookPosition();
    },

    showInstructionsText: function() {
        this.instructText = this.rootCanvas.findName("ttpInstructions");
        this.instructText["Visibility"] = "Visible";
        if (this.bookScene.bookDetails.backCoverImage == true && this.spreadTicker == 0) {
            if (this.bookScene.bookDetails.instructions != null && this.spreadTicker == 0) {
                this.rootCanvas.findName("boundText")["Visibility"] = "Visible";
                this.rootCanvas.findName("boundRunText")["Text"] = this.bookScene.bookDetails.instructions;
            }
            else if (this.spreadTicker == 0) {
                this.rootCanvas.findName("boundText")["Visibility"] = "Visible";
                this.rootCanvas.findName("boundRunText")["Text"] = "Click on the front cover to open the book, then click and drag to turn the pages";
            }
            this.hideUnboundText();
        }
        else {
            if (this.bookScene.bookDetails.instructions != null && this.spreadTicker == 0) {
                this.rootCanvas.findName("unboundText")["Visibility"] = "Visible";
                this.rootCanvas.findName("boundRunText")["Text"] = this.bookScene.bookDetails.instructions;
            }
            else if (this.spreadTicker == 0) {
                this.rootCanvas.findName("unboundText")["Visibility"] = "Visible";
                this.rootCanvas.findName("boundRunText")["Text"] = "Click and drag the pages to turn back and forth through the book";
            }
            this.hideBoundText();
        }
    },

    hideInstructionsText: function() {
        this.instructText = this.rootCanvas.findName("ttpInstructions");
        this.instructText["Visibility"] = "Collapsed";

        this.hideBoundText();
        this.hideUnboundText();
    },

    hideBoundText: function() {
        if (this.rootCanvas.findName("boundText")["Visibility"] == "Visible")
            this.rootCanvas.findName("boundText")["Visibility"] = "Collapsed";
    },

    hideUnboundText: function() {
        if (this.rootCanvas.findName("unboundText")["Visibility"] == "Visible")
            this.rootCanvas.findName("unboundText")["Visibility"] = "Collapsed";
    },

    isInstructionsTextVisible: function() {
        this.instructText = this.rootCanvas.findName("ttpInstructions");
        if (this.instructText["Visibility"] == "Visible")
            return true;
        else
            return false;
    },

    pageTurnCheck: function(sender, eventArgs) {
        var count = this.control.content.findName("evenPageCanvas").children.count;
        var Evenchildren = this.control.content.findName("evenPageCanvas").children;
        var Oddchildren = this.control.content.findName("oddPageCanvas").children;

        var lastSpread = this.spreadTicker;
        this.jumpToSpreadNaviPage((this.bookScene.navigationManager.spreadNumber - 1), true);
        this.spreadTicker = this.bookScene.navigationManager.spreadNumber;
        this.checkSpreadIsPlayingAudio();
        this.updatePageText();
        this.doesSpreadAudioNeedUpdate();

        // -1 == forwards
        //  1 == backwards
        var checkMovement = lastSpread - this.spreadTicker;
        if (checkMovement == -1) {
            //first we'l make sure the currect spreads are in play.
            if (this.spreadTicker >= 2 && this.spreadTicker <= ((this.bookScene.maxNumPages / 2) - 2)) {
                var num = ((this.spreadTicker * 1) + 2);
                Evenchildren.insert(0, this.bookScene.arrayOfEvenPages[num]);
                if (this.spreadTicker <= ((this.bookScene.maxNumPages / 2) - 3))
                    Oddchildren.add(this.bookScene.arrayOfOddPages[num]);
            }
            if (Evenchildren.count >= 6) {
                Evenchildren.remove(this.bookScene.arrayOfEvenPages[(this.spreadTicker - 3)]);
                Oddchildren.remove(this.bookScene.arrayOfOddPages[(this.spreadTicker - 3)]);
            }

            if (this.hasJumpedBk == true && this.spreadTicker <= ((this.bookScene.maxNumPages / 2) - 3)) {
                var value = ((this.spreadTicker * 2) + 5);
                this.bookScene.navigationManager.updateScene(value, 420, true);
            }

            //then we'll show the currect hi res images.
            //if u have turned forward turn the previous spread to lo res.
            if (this.bookScene.bookDetails.backCoverImage == true) {
                switch (this.spreadTicker) {
                    case ((this.currentPageObjects * 1) / 2):

                        break
                    case ((this.currentPageObjects * 1) / 2) - 1:
                        this.loadSpreadImages("ibc", null, "med");
                        this.loadSpreadImages(this.spreadTicker, "forward", "lo");
                        break
                    case 2:
                        this.loadSpreadImages(this.spreadTicker + 1, "forward", "med");
                        this.loadSpreadImages("fc", null, "lo");
                        this.loadSpreadImages(this.spreadTicker, "forward", "lo");
                        break;
                    default:
                        this.loadSpreadImages(this.spreadTicker + 1, "forward", "med");
                        this.loadSpreadImages(this.spreadTicker, "forward", "lo");
                }
            }
            else {
                if (this.spreadTicker >= 1 && this.spreadTicker != ((this.currentPageObjects * 1) / 2)) {
                    switch (this.spreadTicker) {
                        case 1:
                            this.loadSpreadImages(this.spreadTicker + 1, "forward", "med");
                            break
                        case 2:
                            this.loadSpreadImages(this.spreadTicker + 1, "forward", "med");
                            this.loadSpreadImages(this.spreadTicker, "forward", "lo");
                            this.loadSpreadImages(1, "forward", "lo");
                            break
                        default:
                            this.loadSpreadImages(this.spreadTicker + 1, "forward", "med");
                            this.loadSpreadImages(this.spreadTicker, "forward", "lo");
                    }

                }
            }
        }
        else {
            if (this.spreadTicker >= 1 && this.spreadTicker <= ((this.bookScene.maxNumPages / 2) - 2)) {
                if (this.spreadTicker == ((this.bookScene.maxNumPages / 2) - 3) || this.spreadTicker == ((this.bookScene.maxNumPages / 2) - 2)) {
                    if (this.spreadTicker == ((this.bookScene.maxNumPages / 2) - 3)) {
                        Evenchildren.add(this.bookScene.arrayOfEvenPages[(this.spreadTicker * 1) - 2]);
                        Oddchildren.insert(0, this.bookScene.arrayOfOddPages[(this.spreadTicker * 1) - 2]);
                        Evenchildren.remove(this.bookScene.arrayOfEvenPages[(this.spreadTicker * 1) + 3]);
                        //Oddchildren.remove(this.bookScene.arrayOfOddPages[(this.spreadTicker * 1) + 3]);

                        if (this.hasJumpedFw == true) {
                            var value = ((((this.spreadTicker * 1) - 2) * 2) + 1);
                            this.bookScene.navigationManager.updateScene(value, 0, true);
                        }
                    }
                }
                else {
                    Evenchildren.remove(this.bookScene.arrayOfEvenPages[(this.spreadTicker * 1) + 3]);
                    Oddchildren.remove(this.bookScene.arrayOfOddPages[(this.spreadTicker * 1) + 3]);

                    if (this.spreadTicker >= 2) {
                        Evenchildren.add(this.bookScene.arrayOfEvenPages[(this.spreadTicker * 1) - 2]);
                        Oddchildren.insert(0, this.bookScene.arrayOfOddPages[(this.spreadTicker * 1) - 2]);

                        Evenchildren.remove(this.bookScene.arrayOfEvenPages[(this.spreadTicker * 1) + 3]);
                        Oddchildren.remove(this.bookScene.arrayOfOddPages[(this.spreadTicker * 1) + 3]);
                    }

                    if (this.hasJumpedFw == true) {
                        //if(((this.spreadTicker * 1) - 2) == 2)
                        //   this.bookScene.navigationManager.updateScene(5, 0);
                        if (this.spreadTicker >= 2) {
                            var value = ((((this.spreadTicker * 1) - 2) * 2) + 1);
                            this.bookScene.navigationManager.updateScene(value, 0, true);
                        }
                    }
                }

            }

            //show the currect hi res images
            //turning back then turn previous spread to lo res    
            if (this.bookScene.bookDetails.backCoverImage == true) {
                switch (this.spreadTicker) {
                    case 1:
                        this.loadSpreadImages("ifc", null, "med");
                        this.loadSpreadImages(this.spreadTicker + 1, "back", "lo");
                        this.loadSpreadImages("fc", null, "med");
                        break
                    case (((this.currentPageObjects) / 2) - 1):
                        break
                    case (((this.currentPageObjects) / 2) - 2):
                        this.loadSpreadImages(this.spreadTicker, "back", "med");
                        this.loadSpreadImages("ibc", null, "lo");
                        break
                    default:
                        this.loadSpreadImages(this.spreadTicker, "back", "med");
                        this.loadSpreadImages(this.spreadTicker + 1, "back", "lo");
                }
            }
            else {
                switch (this.spreadTicker) {
                    case 0:
                        this.loadSpreadImages(this.spreadTicker, null, "med");
                        break
                    case (((this.currentPageObjects) / 2) - 1):
                        break
                    case (((this.currentPageObjects) / 2)):
                        break
                    default:
                        this.loadSpreadImages(this.spreadTicker, "back", "med");
                        this.loadSpreadImages(this.spreadTicker + 1, "back", "lo");
                }
            }
        }

        if (this.isInstructionsTextVisible() == true)
            this.hideInstructionsText();
    },

    doesSpreadAudioNeedUpdate: function() {
        if (this.bookScene.bookDetails.spreadsWithAudio != 0 && this.bookScene.bookDetails.spreadsWithAudio != null) {
            this.checkSpreadForAudio(this.spreadsWithAudioArray, "audioButton1");
        }

        if (this.bookScene.bookDetails.spreadsWithAudio2 != 0 && this.bookScene.bookDetails.spreadsWithAudio2 != null) {
            this.checkSpreadForAudio(this.spreadsWithAudio2Array, "audioButton2");
        }
    },

    checkSpreadForAudio: function(spreadAudioArray, audioButton) {
        /*if(spreadAudioArray[this.spreadTicker] == ((this.spreadTicker * 1) + 1))
        {
        this.enableAudioButton(audioButton);
        }
        else
        {
        this.disableAudioButton(audioButton, false);
        }*/

        for (var i = 0; i < spreadAudioArray.length; i++) {
            if (spreadAudioArray[i] == ((this.spreadTicker * 1) + 1)) {
                this.enableAudioButton(audioButton);
                return;
            }
            else {
                this.disableAudioButton(audioButton, false);
            }
        }
    },

    checkSpreadIsPlayingAudio: function() {
        if (this.isAudioPlaying == true) {
            this.control.content.findName("audioPlayer").stop();
            this.isAudioPlaying = false;
            if (this.rootCanvas.findName("audioImageDown")["Visibility"] = "Visible") {
                this.rootCanvas.findName("audioImage")["Visibility"] = "Visible";
                this.rootCanvas.findName("audioImageDown")["Visibility"] = "Collapsed";
            }
            if (this.rootCanvas.findName("audioImageDown2")["Visibility"] = "Visible") {
                this.rootCanvas.findName("audioImage2")["Visibility"] = "Visible";
                this.rootCanvas.findName("audioImageDown2")["Visibility"] = "Collapsed";
            }
        }
    },

    checkBookForAudio: function(spreadsWithAudio, audioButton) {
        switch (spreadsWithAudio) {
            case "0":
                if (audioButton == "audioButton1") {
                    this.disableAudioButton(audioButton, true);
                }
                else
                    this.disableAudioButton(audioButton, true);
                break
            case null:
                this.enableAudioButton(audioButton);
                break
            default:
                if (audioButton == "audioButton1") {
                    this.spreadsWithAudioArray = this.bookScene.bookDetails.spreadsWithAudio.split(",");

                    if (this.spreadsWithAudioArray[this.spreadTicker] == (this.spreadTicker + 1)) {
                        this.enableAudioButton(audioButton);
                    }
                    else {
                        this.disableAudioButton(audioButton, false);
                    }
                }
                else {
                    this.spreadsWithAudio2Array = this.bookScene.bookDetails.spreadsWithAudio2.split(",");

                    if (this.spreadsWithAudio2Array[this.spreadTicker] == (this.spreadTicker + 1)) {
                        this.enableAudioButton(audioButton);
                    }
                    else {
                        this.disableAudioButton(audioButton, false);
                    }
                }
        }
    },

    enableAudioButton: function(audioButton) {
        if (audioButton == "audioButton1") {
            this.audioImage = "audioImage";
            this.audioButton = "audioButton";
        }
        else {
            this.audioImage = "audioImage2";
            this.audioButton = "audioButton2";
        }
        this.control.content.findName(this.audioButton)["Visibility"] = "Visible";
        this.control.content.findName(this.audioImage)["Opacity"] = 1;
        this.control.content.findName(this.audioButton)["IsHitTestVisible"] = true;
    },

    disableAudioButton: function(audioButton, hide) {
        if (audioButton == "audioButton1") {
            this.audioImage = "audioImage";
            this.audioButton = "audioButton";
        }
        else {
            this.audioImage = "audioImage2";
            this.audioButton = "audioButton2";
        }

        if (this.isAudioPlaying == true) {
            this.control.content.findName("audioPlayer").stop();
            this.isAudioPlaying = false;
        }

        if (hide == true) {
            this.control.content.findName(this.audioButton)["Visibility"] = "Collapsed";
        }
        else {
            this.control.content.findName(this.audioImage)["Opacity"] = 0.5;
            this.control.content.findName(this.audioButton)["Visibility"] = "Visible";
        }
        this.control.content.findName(this.audioButton)["IsHitTestVisible"] = false;
    },

    enableTextButton: function(textButton) {
        if (textButton == "1") {
            this.textImage = "textImage";
            this.textButton = "textButton";
        }
        else {
            this.textImage = "textImage2";
            this.textButton = "textButton2";
        }
        this.control.content.findName(this.textButton)["Visibility"] = "Visible";
        this.control.content.findName(this.textImage)["Opacity"] = 1;
        this.control.content.findName(this.textButton)["IsHitTestVisible"] = true;
    },

    disableTextButton: function(textButton, hide) {
        if (textButton == "1") {
            this.textImage = "textImage";
            this.textImageDown = "textImageDown";
            this.textButton = "textButton";
            this.displayText = "displayText";
        }
        else {
            this.textImage = "textImage2";
            this.textImageDown = "textImageDown2";
            this.textButton = "textButton2";
            this.displayText = "displayText2";
        }

        if (this.rootCanvas.findName(this.textImage)["Visibility"] == "Collapsed") {
            this.rootCanvas.findName(this.textImageDown)["Visibility"] = "Collapsed";
            this.rootCanvas.findName(this.textImage)["Visibility"] = "Visible";

            document.getElementById(this.displayText).style.display = "none";
        }

        if (hide == true) {
            this.control.content.findName(this.textButton)["Visibility"] = "Collapsed";
        }
        else {
            this.control.content.findName(this.textImage)["Opacity"] = 0.5;
            this.control.content.findName(this.textButton)["Visibility"] = "Visible";
        }
        this.control.content.findName(this.textButton)["IsHitTestVisible"] = false;
    },
    /*
    enableTextButton: function()
    {
    this.control.content.findName("textImage")["Opacity"] = 1;
    this.control.content.findName("textButton")["IsHitTestVisible"] = true;
    },
    
    disableTextButton: function()
    {
    this.control.content.findName("textImage")["Opacity"] = 0.5;
    this.control.content.findName("textButton")["IsHitTestVisible"] = false;
    this.rootCanvas.findName("textImageDown")["Visibility"] = "Collapsed";
    this.rootCanvas.findName("textImage")["Visibility"] = "Visible";
    document.getElementById('displayText').style.display = "none";
    },
    
    enableTextButton2: function()
    {
    this.control.content.findName("textImage2")["Opacity"] = 1;
    this.control.content.findName("textButton2")["IsHitTestVisible"] = true;
    this.control.content.findName("textButton2")["Visibility"] = "Visible";
    },
    
    disableTextButton2: function(hide)
    {
    this.control.content.findName("textButton2")["IsHitTestVisible"] = false;
    this.rootCanvas.findName("textImageDown2")["Visibility"] = "Collapsed";
    if(hide == true)
    {
    this.rootCanvas.findName("textButton2")["Visibility"] = "Collapsed";
    }
    else
    {
    this.rootCanvas.findName("textImage2")["Visibility"] = "Visible";
    }
    document.getElementById('displayText2').style.display = "none";
    },*/

    handleTextButtonMouseDown: function(sender, eventArgs) {
        if (this.rootCanvas.findName("bookHolder").children.count >= 1 && this.bookScene.bookDetails.bookText.length >= 1) {
            if (this.rootCanvas.findName("textImage")["Visibility"] == "Visible") {
                this.rootCanvas.findName("textImage")["Visibility"] = "Collapsed";
                this.rootCanvas.findName("textImageDown")["Visibility"] = "Visible";

                document.getElementById('displayText').style.display = "block";
            }
            else {
                this.rootCanvas.findName("textImageDown")["Visibility"] = "Collapsed";
                this.rootCanvas.findName("textImage")["Visibility"] = "Visible";

                document.getElementById('displayText').style.display = "none";
            }
        }
    },

    handleTextButton2MouseDown: function(sender, eventArgs) {
        if (this.rootCanvas.findName("bookHolder").children.count >= 1 && this.bookScene.bookDetails.bookText2.length >= 1) {
            if (this.rootCanvas.findName("textImage2")["Visibility"] == "Visible") {
                this.rootCanvas.findName("textImage2")["Visibility"] = "Collapsed";
                this.rootCanvas.findName("textImageDown2")["Visibility"] = "Visible";

                document.getElementById('displayText2').style.display = "block";
            }
            else {
                this.rootCanvas.findName("textImageDown2")["Visibility"] = "Collapsed";
                this.rootCanvas.findName("textImage2")["Visibility"] = "Visible";

                document.getElementById('displayText2').style.display = "none";
            }
        }
    },

    handleHelpButtonMouseDown: function(sender, eventArgs) {
        if (this.rootCanvas.findName("helpImage")["Visibility"] == "Visible") {
            this.rootCanvas.findName("helpImage")["Visibility"] = "Collapsed";
            this.rootCanvas.findName("helpImageDown")["Visibility"] = "Visible";

            document.getElementById('displayHelp').style.display = "block";
            document.getElementById('displayHelpHolder').scrollTop = 0;
        }
        else {
            this.rootCanvas.findName("helpImageDown")["Visibility"] = "Collapsed";
            this.rootCanvas.findName("helpImage")["Visibility"] = "Visible";

            document.getElementById('displayHelp').style.display = "none";
        }
    },

    updatePageText: function() {
        if (this.bookScene.bookDetails.bookText.length >= 1) {
            if (this.bookScene.bookDetails.bookText[this.spreadTicker] == null) {
                this.disableTextButton("1", false);
            }
            else {
                if (this.bookScene.bookDetails.bookText[this.spreadTicker].title == "<strong></strong>" &&
                this.bookScene.bookDetails.bookText[this.spreadTicker].text == "") {
                    this.disableTextButton("1", false);
                }
                else {
                    document.getElementById("displayBookTitle").innerHTML = this.bookScene.bookDetails.bookText[this.spreadTicker].title;
                    document.getElementById("displayBookText").innerHTML = this.bookScene.bookDetails.bookText[this.spreadTicker].text;
                    document.getElementById('displayTextHolder').scrollTop = 0;
                    this.enableTextButton("1", false);
                }
            }
        }

        if (this.bookScene.bookDetails.bookText2.length >= 1) {
            if (this.bookScene.bookDetails.bookText2[this.spreadTicker] == null) {
                this.disableTextButton("2", false);
            }
            else {
                if (this.bookScene.bookDetails.bookText2[this.spreadTicker].title == "<strong></strong>" &&
                    this.bookScene.bookDetails.bookText2[this.spreadTicker].text == "") {
                    this.disableTextButton("2", false);
                }
                else {
                    document.getElementById("displayBookTitle2").innerHTML = this.bookScene.bookDetails.bookText2[this.spreadTicker].title;
                    document.getElementById("displayBookText2").innerHTML = this.bookScene.bookDetails.bookText2[this.spreadTicker].text;
                    document.getElementById('displayText2Holder').scrollTop = 0;
                    this.enableTextButton("2", false);
                }
            }
        }
    },

    handleAudioButtonMouseDown: function(sender, eventArgs) {
        /*if(this.rootCanvas.findName("bookHolder").children.count >= 1)
        {
        this.audioPlayer = this.control.content.findName("audioPlayer");
        if(this.isAudioPlaying == false)
        {
        this.audioPlayer.addEventListener("MediaFailed", Silverlight.createDelegate(this, this.handleMediaFailedEvent));
        this.audioPlayer.addEventListener("MediaOpened", Silverlight.createDelegate(this, this.handleMediaOpenedEvent));
        this.audioPlayer.addEventListener("MediaEnded", Silverlight.createDelegate(this, this.handleMediaEndedEvent));
        if(this.bookScene.bookDetails.backCoverImage == true)
        {
        this.audioPlayer["Source"] = "http://80.177.83.190/poctestbooks/bookdata/books/" + this.bookScene.bookDetails.bookId + "/audio/" + this.bookScene.bookDetails.shortTitle + "_a" + this.spreadTicker + ".mp3";
        }
        else
        {
        this.audioPlayer["Source"] = "http://80.177.83.190/poctestbooks/bookdata/books/" + this.bookScene.bookDetails.bookId + "/audio/" + this.bookScene.bookDetails.shortTitle + "_a" + ((this.spreadTicker * 1) + 1) + ".mp3";
        }
        this.audioPlayer.play();
        this.rootCanvas.findName("audioImage")["Visibility"] = "Collapsed";
        this.rootCanvas.findName("audioImageDown")["Visibility"] = "Visible";
        }
        else
        {
        this.audioPlayer.stop();
        this.isAudioPlaying = false;
        this.rootCanvas.findName("audioImage")["Visibility"] = "Visible";
        this.rootCanvas.findName("audioImageDown")["Visibility"] = "Collapsed";
        }
        } */
        this.handleAudio("audioButton1");
    },

    handleAudio2ButtonMouseDown: function(sender, eventArgs) {
        /*if(this.rootCanvas.findName("bookHolder").children.count >= 1)
        {
        this.audioPlayer = this.control.content.findName("audioPlayer");
        if(this.isAudioPlaying == false)
        {
        this.audioPlayer.addEventListener("MediaFailed", Silverlight.createDelegate(this, this.handleMediaFailedEvent));
        this.audioPlayer.addEventListener("MediaOpened", Silverlight.createDelegate(this, this.handleMediaOpenedEvent));
        this.audioPlayer.addEventListener("MediaEnded", Silverlight.createDelegate(this, this.handleMediaEndedEvent));
        if(this.bookScene.bookDetails.backCoverImage == true)
        {
        this.audioPlayer["Source"] = "http://80.177.83.190/poctestbooks/bookdata/books/" + this.bookScene.bookDetails.bookId + "/audio/" + this.bookScene.bookDetails.shortTitle + "_2a" + this.spreadTicker + ".mp3";
        }
        else
        {
        this.audioPlayer["Source"] = "http://80.177.83.190/poctestbooks/bookdata/books/" + this.bookScene.bookDetails.bookId + "/audio/" + this.bookScene.bookDetails.shortTitle + "_2a" + ((this.spreadTicker * 1) + 1) + ".mp3";
        }
        this.audioPlayer.play();
        this.rootCanvas.findName("audioImage2")["Visibility"] = "Collapsed";
        this.rootCanvas.findName("audioImageDown2")["Visibility"] = "Visible";
        }
        else
        {
        this.audioPlayer.stop();
        this.isAudioPlaying = false;
        this.rootCanvas.findName("audioImage2")["Visibility"] = "Visible";
        this.rootCanvas.findName("audioImageDown2")["Visibility"] = "Collapsed";
        }
        }    */
        this.handleAudio("audioButton2");
    },

    handleAudio: function(audioButton) {
        if (this.rootCanvas.findName("bookHolder").children.count >= 1) {
            this.audioPlayer = this.control.content.findName("audioPlayer");
            if (audioButton == "audioButton1")
                audioTag = "";
            else
                audioTag = "2";

            if (this.rootCanvas.findName("audioImageDown" + audioTag)["Visibility"] == "Visible") {
                //alert("active audio clicked do something")
                if (this.isAudioPlaying == true) {
                    //alert("same audio is playin, end")
                    this.audioPlayer.stop();
                    this.isAudioPlaying = false;
                    this.rootCanvas.findName("audioImage" + audioTag)["Visibility"] = "Visible";
                    this.rootCanvas.findName("audioImageDown" + audioTag)["Visibility"] = "Collapsed";
                }
            }
            else {
                //alert("unactive audio clicked do something")
                if (this.isAudioPlaying == false) {
                    //alert("no audio is playing start")
                    this.audioPlayer.addEventListener("MediaFailed", Silverlight.createDelegate(this, this.handleMediaFailedEvent));
                    this.audioPlayer.addEventListener("MediaOpened", Silverlight.createDelegate(this, this.handleMediaOpenedEvent));
                    this.audioPlayer.addEventListener("MediaEnded", Silverlight.createDelegate(this, this.handleMediaEndedEvent));
                    if (this.bookScene.bookDetails.backCoverImage == true) {
                        this.audioPlayer["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookDetails.bookId + "/audio/" + this.bookScene.bookDetails.shortTitle + "_" + audioTag + "a" + this.spreadTicker + ".mp3";
                    }
                    else {
                        this.audioPlayer["Source"] = this.media_server_uri + this.media_server_dir + this.bookScene.bookDetails.bookId + "/audio/" + this.bookScene.bookDetails.shortTitle + "_" + audioTag + "a" + ((this.spreadTicker * 1) + 1) + ".mp3";
                    }
                    this.audioPlayer.play();
                    this.rootCanvas.findName("audioImage" + audioTag)["Visibility"] = "Collapsed";
                    this.rootCanvas.findName("audioImageDown" + audioTag)["Visibility"] = "Visible";
                }
                else {
                    //alert("different audio is playin, end and start this one.")
                    this.audioPlayer.stop();
                    this.isAudioPlaying = false;
                    if (audioTag == "") {
                        this.handleAudio("audioButton1");
                        this.rootCanvas.findName("audioImage2")["Visibility"] = "Visible";
                        this.rootCanvas.findName("audioImageDown2")["Visibility"] = "Collapsed";
                    }
                    else {
                        this.handleAudio("audioButton2");
                        this.rootCanvas.findName("audioImage")["Visibility"] = "Visible";
                        this.rootCanvas.findName("audioImageDown")["Visibility"] = "Collapsed";
                    }
                }
            }
        }
    },

    handleMediaFailedEvent: function(sender, eventArgs) {
        alert("failed: ");
    },

    handleMediaOpenedEvent: function(sender, eventArgs) {
        this.isAudioPlaying = true;
    },

    handleMediaEndedEvent: function(sender, eventArgs) {
        this.isAudioPlaying = false;
        this.rootCanvas.findName("audioImage")["Visibility"] = "Visible";
        this.rootCanvas.findName("audioImageDown")["Visibility"] = "Collapsed";
    },

    /*handleRemoveBookButtonMouseDown: function(sender, eventArgs)
    {
    this.pageTimer.Stop();
    if(this.isPageTurnActive == false)
    {
    this.rootCanvas.findName("oddPageCanvas")["isHitTestVisible"] = "true";
    this.rootCanvas.findName("evenPageCanvas")["isHitTestVisible"] = "true";
    this.rootCanvas.findName("MoveCanvas")["isHitTestVisible"] = "false";
    this.isPageTurnActive = true;
    this.rootCanvas.findName("moveImage")["Visibility"] = "Visible";
    this.rootCanvas.findName("moveImageDown")["Visibility"] = "Collapsed";
    this.rootCanvas["Cursor"] = "Default";
    }
	        
    if(this.rootCanvas.findName("bookHolder").children.count >= 1)
    {
    this.bookScene.arrayOfOddPages = null;
    this.bookScene.arrayOfEvenPages = null;
    this.bookScene = null;
    }
        
    this.rootCanvas.findName("bookHolder").children.clear(); 
    this.checkSpreadIsPlayingAudio();
    this.disableAudioButton();
    this.disableTextButton();
    this.removeSpreadNavi();
    //this.rootCanvas.findName("closeBookTimeline").begin();
    this.control.content.findname("searchBgStyling")["canvas.left"] = this.control.content.ActualWidth - 0;
    },*/

    handleMenuButtonMouseDown: function(sender, eventArgs) {
        if (this.MenuActive == false) {
            this.rootCanvas.findName("menuImage")["Visibility"] = "Collapsed";
            this.rootCanvas.findName("menuImageDown")["Visibility"] = "Visible";
            this.webservices = new WebServices(this.application_server_uri + this.web_service_filename);
            this.result = this.webservices.getLibrary(this.library_id, 0, "", this.online_book);
            this.rootCanvas.findName("mainMenuHolder").children.add(this.mainMenuXaml);
            this.MenuActive = true;

            this.LibraryMenuItem = new Array();
            function LibraryMenuItem(title, shortTitle, author, publishedDate, desc, shortDesc,
	        bookID, catID, coverWidth, coverHeight, imageFileExtention, menuImagePageNumber) {
                this.title = title;
                this.shortTitle = shortTitle;
                this.author = author;
                this.publishedDate = publishedDate;
                this.desc = desc;
                this.shortDesc = shortDesc;
                this.bookID = bookID;
                this.catID = catID;
                this.coverWidth = coverWidth;
                this.coverHeight = coverHeight;
                this.imageFileExtention = imageFileExtention;
                this.menuImagePageNumber = menuImagePageNumber;
            }

            for (var i = 0; i < this.result.length; i++) {
                this.LibraryMenuItem[i] = new LibraryMenuItem(this.result[i].Title, this.result[i].ShortTitle, this.result[i].Author, this.result[i].PublishedDate,
                this.result[i].Description, this.result[i].ShortDescription, this.result[i].BookID, this.result[i].CategoryID, this.result[i].CoverWidth, this.result[i].CoverHeight,
                this.result[i].ImageFileExtension, this.result[i].MenuImagePageNumber);
            }

            for (var i = 0; i < this.LibraryMenuItem.length; i++) {

                if ((i) % 2 == 0 && i != 0)
                    this.menuRowTracker++;

                this.addLibraryMenuItem(i, (330 * ((i) % 2)), (this.menuRowTracker) * 145, this.LibraryMenuItem[i].title, this.LibraryMenuItem[i].shortTitle,
                this.LibraryMenuItem[i].author, this.LibraryMenuItem[i].publishedDate, this.LibraryMenuItem[i].desc,
                this.LibraryMenuItem[i].shortDesc, this.LibraryMenuItem[i].bookID, this.LibraryMenuItem[i].catID,
                this.LibraryMenuItem[i].coverWidth, this.LibraryMenuItem[i].coverHeight, this.LibraryMenuItem[i].imageFileExtention, this.LibraryMenuItem[i].menuImagePageNumber);
            }

            if (this.result.length >= 7) {
                this.scrollContent = this.control.content.findName("menuScrollContent");
                this.scrollContent["Canvas.Top"] = 0;
                this.menuScrollUp = this.rootCanvas.findName("mainMenuScrollUp");
                this.menuScrollUp["Visibility"] = "Visible";
                this.menuScrollUp.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMenuScrollUpMouseDown));
                this.menuScrollUp.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMenuScrollUpMouseUp));
                this.menuScrollUp.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMenuScrollUpLeave));
                this.menuScrollDown = this.rootCanvas.findName("mainMenuScrollDown");
                this.menuScrollDown["Visibility"] = "Visible";
                this.menuScrollDown.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMenuScrollDownMouseDown));
                this.menuScrollDown.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMenuScrollDownMouseUp));
                this.menuScrollDown.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMenuScrollDownLeave));
                this.menuScrollTimer = this.rootCanvas.findName("mainMenuScrollTimer");
                this.menuScrollTimer.addEventListener("Completed", Silverlight.createDelegate(this, this.mainMenuScrollTimerCompleted));
            }

            if (this.control.content.actualHeight <= 550) {
                this.control.content.findName("mainMenuScale")["ScaleX"] = 0.6;
                this.control.content.findName("mainMenuScale")["ScaleY"] = 0.6;
            }
            else {
                this.control.content.findName("mainMenuScale")["ScaleX"] = 1;
                this.control.content.findName("mainMenuScale")["ScaleY"] = 1;
            }

            if (this.rootCanvas.findName("textImage")["Visibility"] == "Collapsed") {
                this.rootCanvas.findName("textImageDown")["Visibility"] = "Collapsed";
                this.rootCanvas.findName("textImage")["Visibility"] = "Visible";

                document.getElementById('displayText').style.display = "none";
            }

            if (this.rootCanvas.findName("textImage2")["Visibility"] == "Collapsed") {
                this.rootCanvas.findName("textImageDown2")["Visibility"] = "Collapsed";
                this.rootCanvas.findName("textImage2")["Visibility"] = "Visible";

                document.getElementById('displayText2').style.display = "none";
            }

            if (this.rootCanvas.findName("helpImage")["Visibility"] == "Collapsed") {
                this.rootCanvas.findName("helpImageDown")["Visibility"] = "Collapsed";
                this.rootCanvas.findName("helpImage")["Visibility"] = "Visible";

                document.getElementById('displayHelp').style.display = "none";
            }
        }
        else {
            this.clearMainMenu();
            this.MenuActive = false;
        }
    },

    handleBackButtonMouseDown: function(sender, eventArgs) {
        if (this.rootCanvas.findName("bookHolder").children.count >= 1 && this.bookLoaded == true && this.spreadTicker != 0) {
            this.lastSpread = this.spreadTicker;
            this.unloadHiResImage();

            if (this.bookScene.bookDetails.rightToLeft == false) {
                this.control.content.findName("bkStart")["Value"] = (this.spreadTicker * -180);
                this.control.content.findName("bkFin")["Value"] = (1 * -180) + 180;
            }
            else {
                this.control.content.findName("bkStart")["Value"] = (this.spreadTicker * +180);
                this.control.content.findName("bkFin")["Value"] = (1 * +180) - 180;
            }
            this.isNaviAnimating = true;
            this.control.content.findName("bkTimeline").Begin();

            this.spreadTicker = 0;

            this.checkSpreadIsPlayingAudio();
            this.updatePageText();
            this.doesSpreadAudioNeedUpdate();

            if (this.isInstructionsTextVisible() == true)
                this.hideInstructionsText();

            this.resetBookPosition();
        }
    },

    handleResetButtonMouseDown: function(sender, eventArgs) {
        this.resetBookPosition();
    },

    handleRotateButtonMouseDown: function(sender, eventArgs) {
        if (this.rootCanvas.findName("bookHolder").children.count >= 1) {
            this.rootCanvas.findName("PageCanvas")["RenderTransformOrigin"] = "0.5,0.5";
            if (this.rootCanvas.findName("bookHolder").children.count >= 1) {
                if (this.rotateTicker == 0) {
                    this.startAngle = this.bookScene.bookDetails.preRotationAngleX;
                    this.endAngle = this.bookScene.bookDetails.preRotationAngleX + 90;
                }
                else {
                    this.startAngle = this.startAngle + 90;
                    this.endAngle = this.endAngle + 90;
                }
                this.rootCanvas.findName("rotateStartTime")["Value"] = this.startAngle;
                this.rootCanvas.findName("rotateEndTime")["Value"] = this.endAngle;
                this.rootCanvas.findName("rotateAnimation").begin();
                this.rotateTicker++;

                this.bookScene.navigationManager.calulatePageDraggingProperties(this.endAngle, this.rotateTicker);
            }
        }
    },

    handleMoveButtonMouseDown: function(sender, eventArgs) {
        if (this.rootCanvas.findName("bookHolder").children.count >= 1) {
            if (this.isPageTurnActive == true) {
                this.rootCanvas.findName("oddPageCanvas")["isHitTestVisible"] = "false";
                this.rootCanvas.findName("evenPageCanvas")["isHitTestVisible"] = "false";
                this.rootCanvas.findName("MoveCanvas")["isHitTestVisible"] = "true";
                this.isPageTurnActive = false;
                //this.rootCanvas.findName("moveButton")["Background"] = "Red";
                this.rootCanvas.findName("moveImage")["Visibility"] = "Collapsed";
                this.rootCanvas.findName("moveImageDown")["Visibility"] = "Visible";
                this.rootCanvas["Cursor"] = "Hand";
            }
            else {
                this.rootCanvas.findName("oddPageCanvas")["isHitTestVisible"] = "true";
                this.rootCanvas.findName("evenPageCanvas")["isHitTestVisible"] = "true";
                this.rootCanvas.findName("MoveCanvas")["isHitTestVisible"] = "false";
                this.isPageTurnActive = true;
                //this.rootCanvas.findName("moveButton")["Background"] = "Blue";
                this.rootCanvas.findName("moveImage")["Visibility"] = "Visible";
                this.rootCanvas.findName("moveImageDown")["Visibility"] = "Collapsed";
                this.rootCanvas["Cursor"] = "Default";
            }
        }
    },

    handleZoomInButtonMouseDown: function(sender, eventArgs) {
        if (this.rootCanvas.findName("bookHolder").children.count >= 1) {
            var sceneScale = this.control.content.findName("sceneScale");
            // Handle any rounding errors
            var scale = Math.round(sceneScale.scaleX * 100);
            scale = scale / 100;

            if (this.bookScene.bookDetails.rightToLeft == false) {
                if (scale >= 5.1) {
                    alert("maximum zoom level reached");
                    return;
                }

                if (scale >= 1) {
                    sceneScale.scaleX += .25;
                    sceneScale.scaleY += .25;
                }
                else if (scale >= .1) {
                    sceneScale.scaleX += .1;
                    sceneScale.scaleY += .1;
                }
                else {
                    sceneScale.scaleX += .01;
                    sceneScale.scaleY += .01;
                }
            }
            else {
                if (scale <= -5.1) {
                    alert("maximum zoom level reached");
                    return;
                }
                if (scale <= 1) {
                    sceneScale.scaleX -= .25;
                    sceneScale.scaleY += .25;
                }
                else if (scale <= .1) {
                    sceneScale.scaleX -= .1;
                    sceneScale.scaleY += .1;
                }
                else {
                    sceneScale.scaleX -= .01;
                    sceneScale.scaleY += .01;
                }
            }
        }
    },

    handleZoomOutButtonMouseDown: function(sender, eventArgs) {
        if (this.rootCanvas.findName("bookHolder").children.count >= 1) {
            var sceneScale = this.control.content.findName("sceneScale");
            //this.rootCanvas.findName("canvas")["RenderTransformOrigin"] = "1,1";
            // Handle any rounding errors
            var scale = Math.round(sceneScale.scaleX * 100);
            scale = scale / 100;

            if (this.bookScene.bookDetails.rightToLeft == false) {
                if (scale <= .06) {
                    alert("minimum zoom level reached");
                    return;
                }

                if (scale <= .1) {
                    sceneScale.scaleX -= .01;
                    sceneScale.scaleY -= .01;
                }
                else if (scale <= 1) {
                    sceneScale.scaleX -= .1;
                    sceneScale.scaleY -= .1;
                }
                else {
                    sceneScale.scaleX -= .25;
                    sceneScale.scaleY -= .25;
                }
            }
            else {
                if (scale >= -.06) {
                    alert("minimum zoom level reached");
                    return;
                }
                if (scale >= -.1) {
                    sceneScale.scaleX += .01;
                    sceneScale.scaleY -= .01;
                }
                else if (scale >= -1) {
                    sceneScale.scaleX += .1;
                    sceneScale.scaleY -= .1;
                }
                else {
                    sceneScale.scaleX += .25;
                    sceneScale.scaleY -= .25;
                }
            }
        }
    },

    addLibraryMenuItem: function(index, canvas_Left, canvas_Top, title, shortTitle, author, publishedDate, desc, shortDesc,
	        bookID, catID, coverWidth, coverHeight, imageFileExtention, menuImagePageNumber) {
        /*alert(id + "\r\n\r\n" +
        canvas_Left + "\r\n\r\n" +
        canvas_Top + "\r\n\r\n" +
        title + "\r\n\r\n" +
        shortTitle + "\r\n\r\n" +
        author + "\r\n\r\n" +
        publishedDate + "\r\n\r\n" +
        desc + "\r\n\r\n" +
        shortDesc + "\r\n\r\n" +
        bookID + "\r\n\r\n" +
        catID + "\r\n\r\n" +
        coverWidth + "\r\n\r\n" +
        coverHeight + "\r\n\r\n" +
        imageFileExtention);*/
        /*xamlString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='100' Height='150' x:Name='$0Canvas' Canvas.Left='$5' Canvas.Top='$6'>";
        xamlString = xamlString + "<Image Source='http://80.177.83.190/silverlight/ttpMedia/covers/$1.jpg' Width='100' Height='100'/>";
        xamlString = xamlString + "<TextBlock Text='$2' Canvas.Top='100' TextWrapping='Wrap'></TextBlock>";
        xamlString = xamlString + "</Canvas>";
	    
	    xamlString = xamlString.replace("$0", shortTitle);
        xamlString = xamlString.replace("$1", shortTitle);
        xamlString = xamlString.replace("$2", shortTitle);
        xamlString = xamlString.replace("$3", shortTitle);
        xamlString = xamlString.replace("$4", shortTitle);
        xamlString = xamlString.replace("$5", canvas_Left);
        xamlString = xamlString.replace("$6", canvas_Top);*/

        if (coverWidth == "0" && coverHeight == "0") {
            if (menuImagePageNumber == 0) {
                coverImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + shortTitle + "_lo_p1.png";
            }
            else {
                bespokeCover = ((menuImagePageNumber * 1) + 1);
                coverImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + shortTitle + "_lo_p" + bespokeCover + ".png";
            }
        }
        else {
            if (menuImagePageNumber == 0) {
                coverImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + shortTitle + "_lo_fc.png";
            }
            else {
                switch (menuImagePageNumber) {
                    case 1:
                        bespokeCover = "ifc";
                        coverImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + shortTitle + "_lo_" + bespokeCover + ".png";
                        break
                    default:
                        bespokeCover = "p" + menuImagePageNumber;
                        coverImage = this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + shortTitle + "_lo_" + bespokeCover + ".png";
                }

            }
        }

        xamlString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='310' Height='135' Canvas.Left='$4' Canvas.Top='$5'>";
        xamlString = xamlString + "<Image Cursor='Hand' Source='" + coverImage + "' Stretch='Fill' Height='130'/>";
        xamlString = xamlString + "<TextBlock FontFamily='" + this.fontFamily + "' Canvas.Left='122' TextWrapping='Wrap' Width='200' >";
        xamlString = xamlString + "<Run FontWeight='ExtraBold' FontSize='14'>$2</Run>";
        xamlString = xamlString + "<LineBreak />";
        xamlString = xamlString + "<Run FontSize='12' FontWeight='Normal'>$3</Run>";
        xamlString = xamlString + "</TextBlock>";
        /*xamlString = xamlString + "<Canvas Cursor='Hand' Background='White' Width='100' Height='20' Canvas.Left='200' Canvas.Top='110'>";
        xamlString = xamlString + "<TextBlock Canvas.Left='10'>View Book</TextBlock> ";       
        xamlString = xamlString + "</Canvas>";*/
        xamlString = xamlString + "<Canvas Width='100' Height='25' Canvas.Top='110' Canvas.Left='200' Cursor='Hand'>";
        xamlString = xamlString + "<Rectangle Width='100' Height='25' RadiusX='6' RadiusY='6' Stroke='#FF6A6666' StrokeThickness='2'>";
        xamlString = xamlString + "<Rectangle.Fill>";
        xamlString = xamlString + "<RadialGradientBrush>";
        xamlString = xamlString + "<RadialGradientBrush.RelativeTransform>";
        xamlString = xamlString + "<TransformGroup>";
        xamlString = xamlString + "<ScaleTransform CenterX='0.5' CenterY='0.5' ScaleX='2.997' ScaleY='2.997'/>";
        xamlString = xamlString + "<SkewTransform AngleX='0' AngleY='0' CenterX='0.5' CenterY='0.5'/>";
        xamlString = xamlString + "<RotateTransform Angle='0' CenterX='0.5' CenterY='0.5'/>";
        xamlString = xamlString + "<TranslateTransform X='0.014' Y='-0.279'/>";
        xamlString = xamlString + "</TransformGroup>";
        xamlString = xamlString + "</RadialGradientBrush.RelativeTransform>";
        xamlString = xamlString + "<GradientStop Color='#FF434343' Offset='0.168'/>";
        xamlString = xamlString + "<GradientStop Color='#FFFFFFFF' Offset='1'/>";
        xamlString = xamlString + "</RadialGradientBrush>";
        xamlString = xamlString + "</Rectangle.Fill>";
        xamlString = xamlString + "</Rectangle>";
        xamlString = xamlString + "<TextBlock FontFamily='" + this.fontFamily + "' Canvas.Left='13' Canvas.Top='3' Foreground='White'>View Book</TextBlock> ";
        xamlString = xamlString + "</Canvas>";

        xamlString = xamlString + "</Canvas>";




        xamlString = xamlString.replace("$1", shortTitle);
        xamlString = xamlString.replace("$2", title);
        if (shortDesc == null)
            shortDesc = "";
        xamlString = xamlString.replace("$3", shortDesc);
        xamlString = xamlString.replace("$4", canvas_Left);
        xamlString = xamlString.replace("$5", canvas_Top);

        var item = this.control.content.createFromXaml(xamlString);
        //this.target.children.add(item);
        this.rootCanvas.findName("menuScrollContent").children.add(item)

        this.MainMenuItems = new Array();
        this.MainMenuItems[index] = new MainMenuItem(item, shortTitle, bookID);

        // Attach a click handler to the item
        this.MainMenuItems[index].clickHandler = delegate(this, this.handleXamlItemClick);
        this.MainMenuItems[index].xaml = xamlString;
    },

    clearMainMenu: function() {
        //while (this.rootCanvas.findName("mainMenuCanvas").children.getItem(this.rootCanvas.findName("mainMenuCanvas").children.count - 1).name != "titleBlock")
        //{
        //    this.rootCanvas.findName("mainMenuCanvas").children.removeAt(this.rootCanvas.findName("mainMenuCanvas").children.count - 1);
        //}
        this.rootCanvas.findName("menuScrollContent").children.clear();
        this.rootCanvas.findName("mainMenuHolder").children.clear();
        this.menuRowTracker = 0;

        this.rootCanvas.findName("menuImage")["Visibility"] = "Visible";
        this.rootCanvas.findName("menuImageDown")["Visibility"] = "Collapsed";
    },

    handleMenuScrollDownMouseDown: function(sender, eventArgs) {
        if (this.scrollContent["Canvas.Top"] <= 0) {
            this.menuScrollDown = true;
            this.menuScrollTimer.Begin();
        }
        else {
            this.menuScrollDown = false;
        }
    },

    handleMenuScrollDownLeave: function(sender, eventArgs) {
        this.menuScrollDown = false;
    },

    handleMenuScrollDownMouseUp: function(sender, eventArgs) {
        this.menuScrollDown = false;
    },

    handleMenuScrollUpMouseUp: function(sender, eventArgs) {
        this.menuScrollUp = false;
    },

    handleMenuScrollUpMouseDown: function(sender, eventArgs) {
        if (this.scrollContent["Canvas.Top"] >= (Math.round(this.scrollContent.children.count / 2)) * (-145)) {
            this.menuScrollUp = true;
            this.menuScrollTimer.begin();
        }
        else {
            this.menuScrollUp = false;
        }
    },

    handleMenuScrollUpLeave: function(sender, eventArgs) {
        this.menuScrollUp = false;
    },

    mainMenuScrollTimerCompleted: function(sender, eventArgs) {
        if (this.menuScrollDown == true && this.scrollContent["Canvas.Top"] >= (Math.round(this.scrollContent.children.count / 2) - 3) * (-145)) {
            this.scrollContent["Canvas.Top"] = this.scrollContent["Canvas.Top"] - 3;
            this.menuScrollTimer.begin();
        }
        else if (this.menuScrollUp == true && this.scrollContent["Canvas.Top"] <= -3) {
            this.scrollContent["Canvas.Top"] = this.scrollContent["Canvas.Top"] + 3;
            this.menuScrollTimer.begin();
        }
        //else
        //{
        //    this.menuScrollDown = false;
        //}
    },

    handleXamlItemClick: function(bookId, fromMainMenu, spread) {
        this.control.content.findName("menuButton")["Opacity"] = 0.5;
        this.control.content.findName("menuButton")["isHitTestVisible"] = false;
        this.control.content.findName("searchButton")["Opacity"] = 0.5;
        this.control.content.findName("searchButton")["isHitTestVisible"] = false;
        this.control.content.findName("bookHolder")["Visibility"] = "Collapsed";
        searchBgStyling = this.control.content.findName("searchBgStyling");
        searchBgStyling["width"] = 330;
        searchBgStyling["Canvas.Left"] = this.control.content.actualWidth - 320
        this.control.content.findName("searchBgDivider")["Visibility"] = "Collapsed";
        if (this.rootCanvas.findName("bookHolder").children.count >= 1) {
            this.bookScene.arrayOfOddPages = null;
            this.bookScene.arrayOfEvenPages = null;
            this.bookScene = null;
            this.bookLoaded = false;
        }
        if (fromMainMenu == true) {
            this.clearMainMenu();
            if (this.control.content.findName("resultsCanvas")["Visibility"] == "Visible" || this.control.content.findName("pageResultsCanvas")["Visibility"] == "Visible")
                this.handleClosePageResultsMouseDown(this, null);
        }
        else {
            //this.handleCloseResultsMouseDown(this, null);
            if (this.MenuActive == true) {
                this.clearMainMenu();
                this.MenuActive = false;
            }
            this.handleClosePageResultsMouseDown(this, null);
        }
        if (this.isPageTurnActive == false) {
            this.rootCanvas.findName("oddPageCanvas")["isHitTestVisible"] = "true";
            this.rootCanvas.findName("evenPageCanvas")["isHitTestVisible"] = "true";
            this.rootCanvas.findName("MoveCanvas")["isHitTestVisible"] = "false";
            this.isPageTurnActive = true;
            this.rootCanvas.findName("moveImage")["Visibility"] = "Visible";
            this.rootCanvas.findName("moveImageDown")["Visibility"] = "Collapsed";
            this.rootCanvas["Cursor"] = "Default";
        }
        this.MenuActive = false;

        this.pageTimer.Stop();
        this.checkSpreadIsPlayingAudio();
        //this.disableAudioButton();
        this.disableTextButton("1", false);
        this.disableTextButton("2", false);

        this.rootCanvas.findName("bookHolder").children.clear();
        this.removeSpreadNavi();

        xamlBookString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='MainCanvas' RenderTransformOrigin='0.5,0.5'>";
        xamlBookString = xamlBookString + "<Canvas.Resources>";
        xamlBookString = xamlBookString + "<Storyboard BeginTime='0' Duration='0:0:0' x:Name='timerStoryboard'/>";
        xamlBookString = xamlBookString + "</Canvas.Resources>";

        xamlBookString = xamlBookString + "<Canvas Width='840' Height='570' Canvas.Top='50' x:Name='PageCanvas' RenderTransformOrigin='0.5,0.5'>";
        xamlBookString = xamlBookString + "<Canvas.Resources>";
        xamlBookString = xamlBookString + "<Storyboard x:Name='rotateAnimation'>";
        xamlBookString = xamlBookString + "<DoubleAnimationUsingKeyFrames BeginTime='00:00:00' Storyboard.TargetName='sceneRotate' Storyboard.TargetProperty='(RotateTransform.Angle)'>";
        xamlBookString = xamlBookString + "<SplineDoubleKeyFrame KeyTime='00:00:00' x:Name='rotateStartTime' Value=''/>";
        xamlBookString = xamlBookString + "<SplineDoubleKeyFrame KeyTime='00:00:01' x:Name='rotateEndTime' Value=''/>";
        xamlBookString = xamlBookString + "</DoubleAnimationUsingKeyFrames>";
        xamlBookString = xamlBookString + "</Storyboard>";
        xamlBookString = xamlBookString + "</Canvas.Resources>";
        xamlBookString = xamlBookString + "<Canvas.RenderTransform>";
        xamlBookString = xamlBookString + "<TransformGroup>";
        xamlBookString = xamlBookString + "<ScaleTransform x:Name='sceneScale' ScaleX='1' ScaleY='1'/>";
        xamlBookString = xamlBookString + "<SkewTransform AngleX='0' AngleY='0'/>";
        xamlBookString = xamlBookString + "<RotateTransform x:Name='sceneRotate' Angle='0'/>";
        xamlBookString = xamlBookString + "<TranslateTransform X='0' Y='0'/>";
        xamlBookString = xamlBookString + "</TransformGroup>";
        xamlBookString = xamlBookString + "</Canvas.RenderTransform>";

        xamlBookString = xamlBookString + "<Image Canvas.Top='0' Canvas.Left='455' Source='assets/shadowPage01.png' Opacity='0.8'/>";
        xamlBookString = xamlBookString + "<Image x:Name='shadowBehindPage01' Canvas.Top='0' Canvas.Left='0' Opacity='0' IsHitTestVisible='false' Source='assets/shadowPage01.png'/>";
        xamlBookString = xamlBookString + "<Canvas x:Name='evenPageCanvas' Canvas.Top='0' Canvas.Left='420' Opacity='1'/>";
        xamlBookString = xamlBookString + "<Polygon x:Name='shadowOnEvenPage' Canvas.Top='0' Canvas.Left='420' Points='420,570 420,570 420,570 420,570' Fill='Black' Opacity='0.25'/>";
        xamlBookString = xamlBookString + "<Canvas x:Name='oddPageCanvas' Canvas.Top='0' Canvas.Left='0' Opacity='1'/>";
        xamlBookString = xamlBookString + "<Canvas Width='840' Height='570' Canvas.Top='0' Canvas.Left='0' x:Name='MoveCanvas' IsHitTestVisible='false' Background='transparent' />";
        xamlBookString = xamlBookString + "<Canvas x:Name='mouseCaptureCanvas' Canvas.Top='0' Canvas.Left='0' Opacity='0' IsHitTestVisible='false' Background='transparent' Width='840' Height='570'/>";
        xamlBookString = xamlBookString + "</Canvas>";

        xamlBookString = xamlBookString + "<Canvas x:Name='lines' Opacity='0.01'>";
        xamlBookString = xamlBookString + "<Image x:Name='lineRight' Source='assets/whiteLine.jpg' Height='44' Width='1' Canvas.Left='849'/>";
        xamlBookString = xamlBookString + "<Canvas Canvas.Left='69'>";
        xamlBookString = xamlBookString + "<Canvas.RenderTransform>";
        xamlBookString = xamlBookString + "<TransformGroup>";
        xamlBookString = xamlBookString + "<TranslateTransform x:Name='lineLeft' X='780' Y='0'/>";
        xamlBookString = xamlBookString + "</TransformGroup>";
        xamlBookString = xamlBookString + "</Canvas.RenderTransform>";
        xamlBookString = xamlBookString + "<Image Source='assets/whiteLine.jpg' Height='44' Width='1'/>";
        xamlBookString = xamlBookString + "</Canvas>";
        xamlBookString = xamlBookString + "</Canvas>";

        xamlBookString = xamlBookString + "</Canvas>";

        this.xamlBook = this.control.content.createFromXaml(xamlBookString);

        this.rootCanvas.findName("bookHolder").children.add(this.xamlBook);

        this.webservices = new WebServices(this.application_server_uri + this.web_service_filename);
        this.result = this.webservices.getBookConfig(bookId);
        this.currentBook = new Book(this.xamlBook, this.result.DcmiTitle, this.result.ShortTitle, this.result.DcmiDescription, this.result.BookID, this.result.ImageFileExtension, this.result.Online, this.result.ShortDescription,
                this.result.SpreadsWithAudio, this.result.SpreadsWithAudioTwo, this.result.SpreadNavigatorText, this.result.RightToLeft, this.result.PageObjects, this.result.Width, this.result.Height, this.result.RectoLo, this.result.VersoLo, this.result.BackCoverImage,
                this.result.CoverWidth, this.result.CoverHeight, this.result.PreRotationAngleY, this.result.Instructions);

        // Attach a click handler to the item
        this.currentBook.clickHandler = delegate(this, this.handleXamlBookClick);
        this.currentBook.xaml = xamlBookString;
        //this.loadSpreadNavigator(this.currentBook);
        //alert(this.result.PreRotationAngleY);
        if (spread == null)
            this.spread = -1;
        else {
            this.spread = (spread * 2) - 1;
            this.spreadTicker = spread;
            //this.jumpToSpreadNaviPage((this.spreadTicker -1), true);
        }

        //i think if we can get the downloader to load all the lo res here then were sorted to go ahead with below.

        var bookScene = new PageTurn(this.currentBook, this.control, this.spread, (this.media_server_uri + this.media_server_dir));
        this.bookScene = bookScene;

        //if(this.spread != -1)
        //this.jumpToSpreadNaviPage((this.spreadTicker -1), true);

        this.loadBookText();
        this.checkBookForAudio(this.bookScene.bookDetails.spreadsWithAudio, "audioButton1");
        this.checkBookForAudio(this.bookScene.bookDetails.spreadsWithAudio2, "audioButton2");

        this.startAngle = 0;
        this.endAngle = 0;
    },

    loadBookText: function() {
        this.textDownloader = this.control.createObject("Downloader");
        this.textDownloader.addEventListener("completed", Silverlight.createDelegate(this, this.downloadBookTextCompleted));
        //this.textDownloader.open("GET", this.application_server_uri + "/" + this.bookScene.bookDetails.shortTitle + "_text.xml");
        this.textDownloader.open("GET", this.media_server_uri + this.media_server_dir + this.bookScene.bookDetails.bookId + "/text/" + this.bookScene.bookDetails.shortTitle +"_text.xml");
        this.textDownloader.send();
    },

    downloadBookTextCompleted: function(sender, args) {
        // get the response
        var xml = sender.responseText;
        var doc = getXmlDocument(xml);

        //first get the root element. if this fails apply no text to the book.
        try
        { var xmlRoot = doc.getElementsByTagName('pages')[0]; }
        catch (e) {
            try
            { var xmlRoot = doc.getElementsByTagName('Pages')[0]; }
            catch (e) { alert("set no text for book"); return; }
        }

        //get the number of pages
        try { var pages = xmlRoot.getElementsByTagName("page"); }
        catch (e) {
            try { var pages = xmlRoot.getElementsByTagName("Page"); }
            catch (e) { alert("set no text for the book"); return; }
        }

        //if we have made it this far loop throu the pages and apply title and text for each.
        for (var i = 0; i < pages.length; i++) {
            // get one page after another
            var page = pages[i];

            //apply title
            try { var title = page.getElementsByTagName("title")[0].firstChild.nodeValue; }
            catch (e) {
                try { var title = page.getElementsByTagName("Title")[0].firstChild.nodeValue; }
                catch (e) { var title = ''; }
            }

            //apply text
            try { var text = page.getElementsByTagName("text")[0].firstChild.nodeValue; }
            catch (e) {
                try { var text = page.getElementsByTagName("Text")[0].firstChild.nodeValue; }
                catch (e) { var text = ''; }
            }

            var reNewLines = /[\n\r]/g;
            //this.htmlText = text.split(reNewLines).join("");
            //this.htmlText = text.split("\n\n").join("\n");
            this.htmlTitle = title.split("\n").join("<br />");
            this.htmlText = text.split("\n").join("<br />");

            this.bookScene.bookDetails.bookText[i] = new this.bookScene.bookDetails.PageText(("<strong>" + this.htmlTitle + "</strong>"), this.htmlText);
        }

        //check for 2nd text.
        this.textDownloader2 = this.control.createObject("Downloader");
        this.textDownloader2.addEventListener("completed", Silverlight.createDelegate(this, this.downloadBookText2Completed));
        this.textDownloader2.addEventListener("downloadFailed", Silverlight.createDelegate(this, this.downloadBookText2Failed));
        //this.textDownloader2.open("GET", this.application_server_uri + "/" + this.bookScene.bookDetails.shortTitle + "_text2.xml");
        this.textDownloader2.open("GET", this.media_server_uri + this.media_server_dir + this.bookScene.bookDetails.bookId + "/text/" + this.bookScene.bookDetails.shortTitle +"_text2.xml");
        this.textDownloader2.send();

        if (this.bookScene.bookDetails.bookText.length >= 1) {
            this.enableTextButton("1");
            this.updatePageText();
        }
    },

    downloadBookText2Completed: function(sender, args) {
        // get the response
        var xml = sender.responseText;
        var doc = getXmlDocument(xml);

        //first get the root element. if this fails apply no text to the book.
        try
        { var xmlRoot = doc.getElementsByTagName('pages')[0]; }
        catch (e) {
            try
            { var xmlRoot = doc.getElementsByTagName('Pages')[0]; }
            catch (e) { alert("set no text for book"); return; }
        }

        //get the number of pages
        try { var pages = xmlRoot.getElementsByTagName("page"); }
        catch (e) {
            try { var pages = xmlRoot.getElementsByTagName("Page"); }
            catch (e) { alert("set no text for the book"); return; }
        }

        //if we have made it this far loop throu the pages and apply title and text for each.
        for (var i = 0; i < pages.length; i++) {
            // get one page after another
            var page = pages[i];

            //apply title
            try { var title = page.getElementsByTagName("title")[0].firstChild.nodeValue; }
            catch (e) {
                try { var title = page.getElementsByTagName("Title")[0].firstChild.nodeValue; }
                catch (e) { var title = ''; }
            }

            //apply text
            try { var text = page.getElementsByTagName("text")[0].firstChild.nodeValue; }
            catch (e) {
                try { var text = page.getElementsByTagName("Text")[0].firstChild.nodeValue; }
                catch (e) { var text = ''; }
            }

            var reNewLines = /[\n\r]/g;
            //this.htmlText = text.split(reNewLines).join("");
            //this.htmlText = text.split("\n\n").join("\n");
            this.htmlTitle = title.split("\n").join("<br />");
            this.htmlText = text.split("\n").join("<br />");

            this.bookScene.bookDetails.bookText2[i] = new this.bookScene.bookDetails.PageText2(("<strong>" + this.htmlTitle + "</strong>"), this.htmlText);
        }
        if (this.bookScene.bookDetails.bookText2.length >= 1) {
            this.enableTextButton("2");
            this.updatePageText();
        }

    },

    downloadBookText2Failed: function(sender, args) {
        this.disableTextButton("2", true);
    },

    loadSpreadNavigator: function() {
        var bookName = this.bookScene.bookDetails.shortTitle;
        var bookID = this.bookScene.bookDetails.bookId;
        var rotateAngle = this.bookScene.bookDetails.preRotationAngleX;
        var pages = getNewPageObjects(this.bookScene.bookDetails.backCoverImage, this.bookScene.bookDetails.pageObjects);

        this.spreadText = false;

        if (this.bookScene.bookDetails.spreadNavigatorText != null) {
        		this.bookScene.bookDetails.spreadNavigatorText = this.bookScene.bookDetails.spreadNavigatorText.replace(/&/g, "&amp;");
            this.spreadTextArray = new Array();
            this.spreadTextArray = this.bookScene.bookDetails.spreadNavigatorText.split("^");
            if (this.spreadTextArray.length >= ((pages / 2) + 1)) {
                this.spreadText = true;
            }
        }

        this.control.content.findName("spreadBack")["Visibility"] = "Visible";
        this.control.content.findName("spreadForward")["Visibility"] = "Visible";

        var buttonBarHeight = this.rootCanvas.findName("buttonBar")["Height"];
        if (rotateAngle == 0 || rotateAngle == 180) {
            var newImageHeight = buttonBarHeight / 1.6;
        }
        else {
            var newImageHeight = buttonBarHeight / 2.2;
        }
        var spreadNaviCenterPoint = this.rootCanvas.findName("spreadNaviHolder")["Width"] / 2;
        var newImageWidth = this.bookScene.bookDetails.width / (this.bookScene.bookDetails.height / newImageHeight);

        //alert(newImageHeight + "   " + newImageWidth);
        if (rotateAngle == 0 || rotateAngle == 180) {
            if (this.bookScene.bookDetails.rightToLeft == false) {
                var versoTopPosition = 16;
                var rectoTopPosition = 16;
                var versoLeftPosition = spreadNaviCenterPoint - newImageWidth;
                var rectoLeftPosition = spreadNaviCenterPoint;
            }
            else {
                var versoTopPosition = 16;
                var rectoTopPosition = 16;
                var versoLeftPosition = spreadNaviCenterPoint;
                var rectoLeftPosition = spreadNaviCenterPoint - newImageWidth;
            }
        }
        else {
            var versoTopPosition = 10;
            var rectoTopPosition = newImageWidth + versoTopPosition;
            var versoLeftPosition = spreadNaviCenterPoint - (newImageHeight / 2) + 5;
            var rectoLeftPosition = spreadNaviCenterPoint - (newImageHeight / 2) + 5;
        }

        if (this.bookScene.bookDetails.backCoverImage == true) {

            if (this.spreadText == true)
                spreadNaviText = this.spreadTextArray[0];
            else
                spreadNaviText = "Cover";

            xamlCoverSpreadString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>";
            xamlCoverSpreadString = xamlCoverSpreadString + "<TextBlock FontFamily='" + this.fontFamily + "' x:Name='spreadText_fc' Canvas.Left='70' FontSize='12' Foreground='White' Text='" + spreadNaviText + "' />";
            xamlCoverSpreadString = xamlCoverSpreadString + "<Image RenderTransformOrigin='0.5,0.5' x:Name='spreadImage_fc' Height='" + newImageHeight + "' Width='" + newImageWidth + "' Canvas.Left='" + (spreadNaviCenterPoint - (newImageWidth / 2)) + "' Canvas.Top='16' Source='" + this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + bookName + "_lo_fc.png'>";
            xamlCoverSpreadString = xamlCoverSpreadString + "<Image.RenderTransform>";
            xamlCoverSpreadString = xamlCoverSpreadString + "<RotateTransform Angle='" + rotateAngle + "'/>";
            xamlCoverSpreadString = xamlCoverSpreadString + "</Image.RenderTransform>";
            xamlCoverSpreadString = xamlCoverSpreadString + "</Image>";
            xamlCoverSpreadString = xamlCoverSpreadString + "</Canvas>";
            this.xamlCoverSpread = this.control.content.createFromXaml(xamlCoverSpreadString);
            this.control.content.findName("spreadNaviHolder").children.add(this.xamlCoverSpread);
            this.repositionTextItem(this.rootCanvas.findName("spreadText_fc"), 7, this.rootCanvas.findName("spreadNaviHolder")["Width"]);
            this.currentPageObjects = pages;
            for (var i = 1; i < pages; i = i + 2) {
                if (this.spreadText == true)
                    spreadNaviText = this.spreadTextArray[Math.floor((i + 1) / 2)]
                else
                    spreadNaviText = 'p.' + (i + 1) + ' and ' + (i + 2);
                switch (i) {
                    case 1:
                        if (this.bookScene.bookDetails.rightToLeft == false)
                            xamlPageSpreadString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='164' Canvas.Left='" + (((i - 1) / 2) + 1) * 180 + "'>";
                        else
                            xamlPageSpreadString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='164' Canvas.Left='" + (((i - 1) / 2) + 1) * -180 + "'>";
                        xamlPageSpreadString = xamlPageSpreadString + "<TextBlock FontFamily='" + this.fontFamily + "' x:Name='spreadText_ifc' Canvas.Left='50' FontSize='12' Foreground='White' Text='" + spreadNaviText + "' />";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image RenderTransformOrigin='0.5,0.5' x:Name='spreadImage_ifc' Stretch='Fill' Height='" + newImageHeight + "' Width='" + newImageWidth + "' Canvas.Top='" + versoTopPosition + "' Canvas.Left='" + versoLeftPosition + "' Source='" + this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + bookName + "_lo_ifc.png'>";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "<RotateTransform Angle='" + rotateAngle + "'/>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image>";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image RenderTransformOrigin='0.5,0.5' x:Name='spreadImage_p2' Stretch='Fill' Height='" + newImageHeight + "' Width='" + newImageWidth + "' Canvas.Top='" + rectoTopPosition + "' Canvas.Left='" + rectoLeftPosition + "' Source='" + this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + bookName + "_lo_p2.png'>";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "<RotateTransform Angle='" + rotateAngle + "'/>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Canvas>";
                        this.xamlPageSpread = this.control.content.createFromXaml(xamlPageSpreadString);
                        this.control.content.findName("spreadNaviHolder").children.add(this.xamlPageSpread);
                        this.repositionTextItem(this.rootCanvas.findName("spreadText_ifc"), 7, this.rootCanvas.findName("spreadNaviHolder")["Width"]);
                        break
                    case (pages - 1):
                        if (this.bookScene.bookDetails.rightToLeft == false)
                            xamlPageSpreadString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='164' Canvas.Left='" + (((i - 1) / 2) + 1) * 180 + "'>";
                        else
                            xamlPageSpreadString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='164' Canvas.Left='" + (((i - 1) / 2) + 1) * -180 + "'>";
                        xamlPageSpreadString = xamlPageSpreadString + "<TextBlock FontFamily='" + this.fontFamily + "' x:Name='spreadText_ibc' Canvas.Left='50' FontSize='12' Foreground='White' Text='" + spreadNaviText + "' />";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image RenderTransformOrigin='0.5,0.5' x:Name='spreadImage_p" + (pages - 1) + "' Stretch='Fill' Height='" + newImageHeight + "' Width='" + newImageWidth + "' Canvas.Top='" + versoTopPosition + "' Canvas.Left='" + versoLeftPosition + "' Source='" + this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + bookName + "_lo_p" + (pages - 1) + ".png'>";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "<RotateTransform Angle='" + rotateAngle + "'/>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image>";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image RenderTransformOrigin='0.5,0.5' x:Name='spreadImage_ibc' Stretch='Fill' Height='" + newImageHeight + "' Width='" + newImageWidth + "' Canvas.Top='" + rectoTopPosition + "' Canvas.Left='" + rectoLeftPosition + "' Source='" + this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + bookName + "_lo_ibc.png'>";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "<RotateTransform Angle='" + rotateAngle + "'/>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Canvas>";
                        this.xamlPageSpread = this.control.content.createFromXaml(xamlPageSpreadString);
                        this.control.content.findName("spreadNaviHolder").children.add(this.xamlPageSpread);
                        this.repositionTextItem(this.rootCanvas.findName("spreadText_ibc"), 7, this.rootCanvas.findName("spreadNaviHolder")["Width"]);
                        break
                    default:
                        if (this.bookScene.bookDetails.rightToLeft == false)
                            xamlPageSpreadString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='164' Canvas.Left='" + (((i - 1) / 2) + 1) * 180 + "'>";
                        else
                            xamlPageSpreadString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='164' Canvas.Left='" + (((i - 1) / 2) + 1) * -180 + "'>";
                        xamlPageSpreadString = xamlPageSpreadString + "<TextBlock FontFamily='" + this.fontFamily + "' x:Name='spreadText_p" + i + "' Canvas.Left='50' FontSize='12' Foreground='White' Text='" + spreadNaviText + "' />";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image RenderTransformOrigin='0.5,0.5' x:Name='spreadImage_p" + i + "' Stretch='Fill' Height='" + newImageHeight + "' Width='" + newImageWidth + "' Canvas.Top='" + versoTopPosition + "' Canvas.Left='" + versoLeftPosition + "' Source='" + this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + bookName + "_lo_p" + (i) + ".png'>";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "<RotateTransform Angle='" + rotateAngle + "'/>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image>";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image RenderTransformOrigin='0.5,0.5' x:Name='spreadImage_p" + (i + 1) + "' Stretch='Fill' Height='" + newImageHeight + "' Width='" + newImageWidth + "' Canvas.Top='" + rectoTopPosition + "' Canvas.Left='" + rectoLeftPosition + "' Source='" + this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + bookName + "_lo_p" + (i + 1) + ".png'>";
                        xamlPageSpreadString = xamlPageSpreadString + "<Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "<RotateTransform Angle='" + rotateAngle + "'/>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image.RenderTransform>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Image>";
                        xamlPageSpreadString = xamlPageSpreadString + "</Canvas>";
                        this.xamlPageSpread = this.control.content.createFromXaml(xamlPageSpreadString);
                        this.control.content.findName("spreadNaviHolder").children.add(this.xamlPageSpread);
                        this.repositionTextItem(this.rootCanvas.findName("spreadText_p" + i), 7, this.rootCanvas.findName("spreadNaviHolder")["Width"]);
                }
            }


        }
        else {
            if (this.spreadText == true)
                spreadNaviText = this.spreadTextArray[0]
            else
                spreadNaviText = "Cover";

            xamlCoverSpreadString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>";
            xamlCoverSpreadString = xamlCoverSpreadString + "<TextBlock FontFamily='" + this.fontFamily + "' x:Name='spreadText_p1' Canvas.Left='70' FontSize='12' Foreground='White' Text='" + spreadNaviText + "' />";
            xamlCoverSpreadString = xamlCoverSpreadString + "<Image x:Name='spreadImage_p1' Stretch='Fill' Height='" + newImageHeight + "' Width='" + newImageWidth + "' Canvas.Left='" + (spreadNaviCenterPoint - (newImageWidth / 2)) + "' Canvas.Top='16' Source='" + this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + bookName + "_lo_p1.png'/>";
            xamlCoverSpreadString = xamlCoverSpreadString + "</Canvas>";
            this.xamlCoverSpread = this.control.content.createFromXaml(xamlCoverSpreadString);
            this.control.content.findName("spreadNaviHolder").children.add(this.xamlCoverSpread);
            this.repositionTextItem(this.rootCanvas.findName("spreadText_p1"), 7, this.rootCanvas.findName("spreadNaviHolder")["Width"]);

            this.currentPageObjects = pages;
            for (var i = 1; i < pages; i = i + 2) {
                if (this.spreadText == true)
                    spreadNaviText = this.spreadTextArray[Math.floor((i + 1) / 2)]
                else
                    spreadNaviText = 'p.' + (i + 1) + ' and ' + (i + 2);

                if (this.bookScene.bookDetails.rightToLeft == false)
                    xamlPageSpreadString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='164' Canvas.Left='" + (((i - 1) / 2) + 1) * 180 + "'>";
                else
                    xamlPageSpreadString = "<Canvas xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' Width='164' Canvas.Left='" + (((i - 1) / 2) + 1) * -180 + "'>";
                xamlPageSpreadString = xamlPageSpreadString + "<TextBlock FontFamily='" + this.fontFamily + "' x:Name='spreadText_p" + (i + 1) + "' Canvas.Left='50' FontSize='12' Foreground='White' Text='" + spreadNaviText + "' />";
                xamlPageSpreadString = xamlPageSpreadString + "<Image x:Name='spreadImage_p" + (i + 1) + "' Stretch='Fill' Height='" + newImageHeight + "' Canvas.Top='" + versoTopPosition + "' Canvas.Left='" + versoLeftPosition + "' Width='" + newImageWidth + "' Source='" + this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + bookName + "_lo_p" + (i + 1) + ".png'/>";
                xamlPageSpreadString = xamlPageSpreadString + "<Image x:Name='spreadImage_p" + (i + 2) + "' Stretch='Fill' Height='" + newImageHeight + "' Canvas.Top='" + rectoTopPosition + "' Canvas.Left='" + rectoLeftPosition + "' Width='" + newImageWidth + "' Source='" + this.media_server_uri + this.media_server_dir + bookID + "/sl_lo/" + bookName + "_lo_p" + (i + 2) + ".png'/>";
                xamlPageSpreadString = xamlPageSpreadString + "</Canvas>";
                this.xamlPageSpread = this.control.content.createFromXaml(xamlPageSpreadString);
                this.control.content.findName("spreadNaviHolder").children.add(this.xamlPageSpread);
                this.repositionTextItem(this.rootCanvas.findName("spreadText_p" + (i + 1)), 7, this.rootCanvas.findName("spreadNaviHolder")["Width"]);
            }
        }

        this.placeBookTitle();
    },

    placeBookTitle: function() {
        this.control.content.findName("spreadTitleBlock")["Text"] = this.bookScene.bookDetails.title;
        this.repositionBookTitle();
        this.control.content.findName("spreadTitleBlock")["Visibility"] = "Visible";
    },

    repositionBookTitle: function() {
        var leftPosition = this.control.content.actualWidth - 320 - (this.bookScene.bookDetails.title.length * 7);
        this.control.content.findName("spreadTitleBlock")["Canvas.Left"] = leftPosition;
        this.control.content.findName("spreadTitleBlock")["Canvas.Top"] = 15;

        searchBgStyling = this.control.content.findName("searchBgStyling");
        searchBgStyling["width"] = 330 + (this.bookScene.bookDetails.title.length * 7) + 10;
        searchBgStyling["Canvas.Left"] = this.control.content.actualWidth - 330 - (this.bookScene.bookDetails.title.length * 7);

        searchBgDivider = this.control.content.findName("searchBgDivider");
        searchBgDivider["Visibility"] = "Collapsed";
        searchBgDivider["Canvas.Left"] = this.control.content.actualWidth - 312;
    },

    repositionTextItem: function(textItem, charWidth, areaWidth) {
        var centerPoint = (areaWidth / 2);
        var textWidth = textItem["Text"].length * charWidth;
        textItem["Canvas.Left"] = centerPoint - (textWidth / 2);
    },

    removeSpreadNavi: function() {
        this.control.content.findName("spreadNaviHolder").children.clear();
        this.control.content.findName("spreadNaviHolderTranslate")["X"] = 0;
        this.control.content.findName("spreadTitleBlock")["Visibility"] = "Collapsed";
        this.control.content.findName("spreadBack")["Visibility"] = "Collapsed";
        this.control.content.findName("spreadForward")["Visibility"] = "Collapsed";
        this.spreadTicker = 0;
        this.fwButActive = false;
        this.bkButActive = false;
        this.isNaviAnimating = false;
    },

    unloadHiResImage: function() {
        if (this.bookScene.bookDetails.backCoverImage == true) {
            switch (this.lastSpread) {
                case 0:
                    this.loadSpreadImages("fc", null, "lo");
                    this.loadSpreadImages("ifc", null, "lo");
                    break
                case 1:
                    this.loadSpreadImages("ifc", null, "lo");
                    this.loadSpreadImages(2, "spreadNavi", "lo");
                    this.loadSpreadImages("fc", null, "lo");
                    break
                case ((this.currentPageObjects) / 2):
                    this.loadSpreadImages("ibc", null, "lo");
                    this.loadSpreadImages(((this.currentPageObjects) / 2) - 1, "spreadNavi", "lo");
                    break
                case ((this.currentPageObjects) / 2) - 1:
                    this.loadSpreadImages(((this.currentPageObjects) / 2) - 1, "spreadNavi", "lo");
                    this.loadSpreadImages("ibc", null, "lo");
                    break
                default:
                    this.loadSpreadImages(this.lastSpread, "spreadNavi", "lo");
                    this.loadSpreadImages(this.lastSpread + 1, "spreadNavi", "lo");
            }
        }
        else {
            switch (this.lastSpread) {
                case ((this.currentPageObjects) / 2):
                    this.loadSpreadImages(this.lastSpread, "spreadNavi", "lo");
                    this.loadSpreadImages(this.lastSpread - 1, "spreadNavi", "lo");
                    break
                default:
                    this.loadSpreadImages(this.lastSpread, "spreadNavi", "lo");
                    this.loadSpreadImages(this.lastSpread + 1, "spreadNavi", "lo");
            }
        }
    },

    loadHiResImage: function() {
        if (this.bookScene.bookDetails.backCoverImage == true) {
            switch (this.spreadTicker) {
                case 0:
                    this.loadSpreadImages("fc", null, "lo");
                    this.loadSpreadImages("fc", null, "med");
                    this.loadSpreadImages("ifc", null, "lo");
                    this.loadSpreadImages("ifc", null, "med");
                    break
                case 1:
                    this.loadSpreadImages("ifc", null, "lo");
                    this.loadSpreadImages("ifc", null, "med");
                    this.loadSpreadImages(2, "spreadNavi", "lo");
                    this.loadSpreadImages(2, "spreadNavi", "med");
                    this.loadSpreadImages("fc", null, "lo");
                    this.loadSpreadImages("fc", null, "med");
                    break
                case ((this.currentPageObjects) / 2):
                    this.loadSpreadImages("ibc", null, "lo");
                    this.loadSpreadImages("ibc", null, "med");
                    this.loadSpreadImages(((this.currentPageObjects) / 2) - 1, "spreadNavi", "lo");
                    this.loadSpreadImages(((this.currentPageObjects) / 2) - 1, "spreadNavi", "med");
                    break
                case ((this.currentPageObjects) / 2) - 1:
                    this.loadSpreadImages(((this.currentPageObjects) / 2) - 1, "spreadNavi", "lo");
                    this.loadSpreadImages(((this.currentPageObjects) / 2) - 1, "spreadNavi", "med");
                    this.loadSpreadImages("ibc", null, "lo");
                    this.loadSpreadImages("ibc", null, "med");
                    break
                default:
                    //when jumping using the navi i think i have to add the lo res image first(even thou it already exists
                    //because it seems to not display the active image object until the hi res image has loaded.
                    this.loadSpreadImages(this.spreadTicker, "spreadNavi", "lo");
                    this.loadSpreadImages(this.spreadTicker, "spreadNavi", "med");
                    this.loadSpreadImages(this.spreadTicker + 1, "spreadNavi", "lo");
                    this.loadSpreadImages(this.spreadTicker + 1, "spreadNavi", "med");
            }
        }
        else {
            switch (this.spreadTicker) {
                case ((this.currentPageObjects) / 2):
                    this.loadSpreadImages(this.spreadTicker, "spreadNavi", "lo");
                    this.loadSpreadImages(this.spreadTicker, "spreadNavi", "med");
                    this.loadSpreadImages(this.spreadTicker - 1, "spreadNavi", "lo");
                    this.loadSpreadImages(this.spreadTicker - 1, "spreadNavi", "med");
                    break
                default:
                    this.loadSpreadImages(this.spreadTicker, "spreadNavi", "lo");
                    this.loadSpreadImages(this.spreadTicker, "spreadNavi", "med");
                    this.loadSpreadImages(this.spreadTicker + 1, "spreadNavi", "lo");
                    this.loadSpreadImages(this.spreadTicker + 1, "spreadNavi", "med");
            }
        }
    },

    loadSpreads: function(forward) {
        var Evenchildren = this.control.content.findName("evenPageCanvas").children;
        var Oddchildren = this.control.content.findName("oddPageCanvas").children;
        if (forward == true) {
            switch (this.spreadTicker) {
                case 1:
                    //forward from front cover to p2. will already have neccessary pages so dont have to manipulate anything.
                    this.bookScene.navigationManager.jumpToPage(1);
                    break
                case (this.currentPageObjects / 2): // last page
                    var arrayValue = this.spreadTicker - 4;
                    this.hasJumpedFw = true;
                    Evenchildren.clear();
                    Oddchildren.clear();
                    for (var i = 0; i < 5; i++) {
                        Evenchildren.insert(0, this.bookScene.arrayOfEvenPages[(arrayValue + i)]);
                        if (i != 4)
                            Oddchildren.add(this.bookScene.arrayOfOddPages[(arrayValue + i)]);
                    }
                    //need to update the scene on the below 2 spreads because they arent handled by the default jumptopage.
                    this.bookScene.navigationManager.updateScene((this.currentPageObjects - 7), 0, true);
                    this.bookScene.navigationManager.updateScene((this.currentPageObjects - 5), 0, true);
                    //then go ahead and show the currect spread
                    this.bookScene.navigationManager.jumpToPage((this.spreadTicker * 2) - 1);
                    break
                case ((this.currentPageObjects / 2) - 1): // first from last
                    var arrayValue = this.spreadTicker - 3;
                    this.hasJumpedFw = true;
                    Evenchildren.clear();
                    Oddchildren.clear();
                    for (var i = 0; i < 5; i++) {
                        Evenchildren.insert(0, this.bookScene.arrayOfEvenPages[(arrayValue + i)]);
                        if (i != 4)
                            Oddchildren.add(this.bookScene.arrayOfOddPages[(arrayValue + i)]);
                    }
                    this.bookScene.navigationManager.updateScene((this.currentPageObjects - 7), 0, true);
                    this.bookScene.navigationManager.jumpToPage((this.spreadTicker * 2) - 1);
                    break
                case ((this.currentPageObjects / 2) - 2): // second from last
                    var arrayValue = this.spreadTicker - 2;
                    this.hasJumpedFw = true;
                    Evenchildren.clear();
                    Oddchildren.clear();
                    for (var i = 0; i < 5; i++) {
                        Evenchildren.insert(0, this.bookScene.arrayOfEvenPages[(arrayValue + i)]);
                        if (i != 4)
                            Oddchildren.add(this.bookScene.arrayOfOddPages[(arrayValue + i)]);
                    }
                    //this.bookScene.navigationManager.updateScene((this.currentPageObjects - 7), 0, true);
                    this.bookScene.navigationManager.jumpToPage((this.spreadTicker * 2) - 1);
                    this.bookScene.navigationManager.updateScene((this.spreadTicker * 2) + 3, 420, true);
                    break
                default:
                    this.hasJumpedFw = true;
                    Evenchildren.clear();
                    Oddchildren.clear();
                    for (var i = 0; i < 5; i++) {
                        Evenchildren.insert(0, this.bookScene.arrayOfEvenPages[(((this.spreadTicker * 1) - 2) + i)]);
                        Oddchildren.add(this.bookScene.arrayOfOddPages[(((this.spreadTicker * 1) - 2) + i)]);
                    }
                    this.bookScene.navigationManager.jumpToPage((this.spreadTicker * 2) - 1);
                    this.bookScene.navigationManager.updateScene((this.spreadTicker * 2) + 3, 420, true);
                    this.bookScene.navigationManager.updateScene((this.spreadTicker * 2) + 5, 420, true);
            }
        }
        else // going back.
        {
            switch (this.spreadTicker) {
                case 0: //fc or p1
                    this.hasJumpedBk = true;
                    Evenchildren.clear();
                    Oddchildren.clear();
                    for (var i = 0; i < 4; i++) {
                        Evenchildren.insert(0, this.bookScene.arrayOfEvenPages[(((this.spreadTicker * 1)) + i)]);
                        Oddchildren.add(this.bookScene.arrayOfOddPages[(((this.spreadTicker * 1)) + i)]);
                    }
                    this.bookScene.navigationManager.jumpToPage(-1);
                    //ok bug fix. mayb look at in more depth later** oddpagenumber 7 isnt updated correctly. so if u jump somewhere
                    //without it bein updated and then jump to the front page it stil shows page 007. below manually updates it.
                    this.bookScene.navigationManager.updateScene(7, 420, false);
                    break
                case 1: //ifc p2 or p2 p3
                    this.hasJumpedBk = true;
                    Evenchildren.clear();
                    Oddchildren.clear();
                    for (var i = 0; i < 4; i++) {
                        Evenchildren.insert(0, this.bookScene.arrayOfEvenPages[(((this.spreadTicker * 1) - 1) + i)]);
                        Oddchildren.add(this.bookScene.arrayOfOddPages[(((this.spreadTicker * 1) - 1) + i)]);
                    }
                    this.bookScene.navigationManager.jumpToPage(1);
                    this.bookScene.navigationManager.updateScene((this.spreadTicker * 2) - 1, 0, false);
                    break
                case ((this.currentPageObjects / 2) - 1): // first from last
                    this.bookScene.navigationManager.jumpToPage((this.spreadTicker * 2) - 1);
                    break
                case ((this.currentPageObjects / 2) - 2): // second from last
                    this.hasJumpedBk = true;
                    var arrayValue = this.spreadTicker - 2;
                    Evenchildren.clear();
                    Oddchildren.clear();
                    for (var i = 0; i < 5; i++) {
                        Evenchildren.insert(0, this.bookScene.arrayOfEvenPages[(arrayValue + i)]);
                        if (i != 4)
                            Oddchildren.add(this.bookScene.arrayOfOddPages[(arrayValue + i)]);
                    }
                    this.bookScene.navigationManager.jumpToPage((this.spreadTicker * 2) - 1);
                    break
                default:
                    this.hasJumpedBk = true;
                    Evenchildren.clear();
                    Oddchildren.clear();
                    for (var i = 0; i < 5; i++) {
                        Evenchildren.insert(0, this.bookScene.arrayOfEvenPages[(((this.spreadTicker * 1) - 2) + i)]);
                        Oddchildren.add(this.bookScene.arrayOfOddPages[(((this.spreadTicker * 1) - 2) + i)]);
                    }
                    this.bookScene.navigationManager.jumpToPage((this.spreadTicker * 2) - 1);
                    this.bookScene.navigationManager.updateScene((this.spreadTicker * 2) - 3, 0, true);
                    this.bookScene.navigationManager.updateScene((this.spreadTicker * 2) - 1, 0, true);
            }
        }
    },

    handleFwButtonMouseDown: function(sender, eventArgs) {
        if (this.isNaviAnimating == false) {
            sender.captureMouse();
            this.fwButActive = true;
            this.isNaviAnimating = true;
            if (this.spreadTicker * 2 <= this.currentPageObjects - 1) {
                this.lastSpread = this.spreadTicker;
                this.unloadHiResImage();
                if (this.bookScene.bookDetails.rightToLeft == false) {
                    this.control.content.findName("fwStart")["Value"] = this.spreadTicker * -180;
                    this.control.content.findName("fwFin")["Value"] = (this.spreadTicker * -180) - 180;
                }
                else {
                    this.control.content.findName("fwStart")["Value"] = this.spreadTicker * +180;
                    this.control.content.findName("fwFin")["Value"] = (this.spreadTicker * +180) + 180;
                }
                this.control.content.findName("fwTimeline").Begin();
                this.spreadTicker++;
            }
            else {
                alert("Last page reached, can't turn forward");
                this.isNaviAnimating = false;
            }
        }
    },

    handleFwButtonMouseUp: function(sender, eventArgs) {
        sender.releaseMouseCapture();
        this.fwButActive = false;
        this.checkSpreadIsPlayingAudio();
        this.updatePageText();
        this.doesSpreadAudioNeedUpdate();

        if (this.isInstructionsTextVisible() == true)
            this.hideInstructionsText();
    },

    handleFwAniCompleted: function(sender, eventArgs) {
        if (this.spreadTicker * 2 <= this.currentPageObjects - 1) {
            if (this.fwButActive == true) {
                if (this.bookScene.bookDetails.rightToLeft == false) {
                    this.control.content.findName("fwStart")["Value"] = this.spreadTicker * -180;
                    this.control.content.findName("fwFin")["Value"] = (this.spreadTicker * -180) - 180;
                }
                else {
                    this.control.content.findName("fwStart")["Value"] = this.spreadTicker * +180;
                    this.control.content.findName("fwFin")["Value"] = (this.spreadTicker * +180) + 180;
                }
                this.control.content.findName("fwTimeline").Begin();
                this.spreadTicker++;
            }
            else {   //alert(this.spreadTicker);
                this.loadSpreads(true);
                this.isNaviAnimating = false;
                this.loadHiResImage();
            }
        }
        else {
            //this is the last page.
            //this.bookScene.navigationManager.jumpToPage((this.spreadTicker * 2) - 1);
            this.loadSpreads(true);
            this.isNaviAnimating = false;
            this.loadHiResImage();
        }
    },

    handleBkButtonMouseDown: function(sender, eventArgs) {
        if (this.isNaviAnimating == false) {
            sender.captureMouse();
            this.bkButActive = true;
            this.isNaviAnimating = true;
            if (this.spreadTicker != 0) {
                this.lastSpread = this.spreadTicker;
                this.unloadHiResImage();
                if (this.bookScene.bookDetails.rightToLeft == false) {
                    this.control.content.findName("bkStart")["Value"] = (this.spreadTicker * -180);
                    this.control.content.findName("bkFin")["Value"] = (this.spreadTicker * -180) + 180;
                }
                else {
                    this.control.content.findName("bkStart")["Value"] = (this.spreadTicker * +180);
                    this.control.content.findName("bkFin")["Value"] = (this.spreadTicker * +180) - 180;
                }
                this.control.content.findName("bkTimeline").Begin();
                this.spreadTicker--;
            }
            else {
                alert("Book closed, can't turn backward");
                this.isNaviAnimating = false;
            }
        }
    },

    handleBkButtonMouseUp: function(sender, eventArgs) {
        sender.releaseMouseCapture();
        this.bkButActive = false;
        this.checkSpreadIsPlayingAudio();
        this.updatePageText();
        this.doesSpreadAudioNeedUpdate();

        if (this.isInstructionsTextVisible() == true)
            this.hideInstructionsText();
    },

    handleBkAniCompleted: function(sender, eventArgs) {
        var Evenchildren = this.control.content.findName("evenPageCanvas").children;
        var Oddchildren = this.control.content.findName("oddPageCanvas").children;
        if (this.spreadTicker != 0) {
            if (this.bkButActive == true) {
                if (this.bookScene.bookDetails.rightToLeft == false) {
                    this.control.content.findName("bkStart")["Value"] = (this.spreadTicker * -180);
                    this.control.content.findName("bkFin")["Value"] = (this.spreadTicker * -180) + 180;
                }
                else {
                    this.control.content.findName("bkStart")["Value"] = (this.spreadTicker * +180);
                    this.control.content.findName("bkFin")["Value"] = (this.spreadTicker * +180) - 180;
                }
                this.control.content.findName("bkTimeline").Begin();
                this.spreadTicker--;
            }
            else {
                this.loadSpreads(false);
                this.isNaviAnimating = false;
                this.loadHiResImage();
            }
        }
        else {
            //alert("on first spread");
            this.loadSpreads(false);
            this.isNaviAnimating = false;
            this.loadHiResImage();
        }
    },

    jumpToSpreadNaviPage: function(spreadNumber, fw) {
        if (fw == true) {
            if (this.bookScene.bookDetails.rightToLeft == false) {
                this.control.content.findName("jumpStart")["Value"] = this.spreadTicker * -180;
                this.control.content.findName("jumpFin")["Value"] = (spreadNumber * -180) - 180;
            }
            else {
                this.control.content.findName("jumpStart")["Value"] = this.spreadTicker * +180;
                this.control.content.findName("jumpFin")["Value"] = (spreadNumber * +180) + 180;
            }
            this.control.content.findName("jumpTimeline").Begin();
            this.spreadTicker = spreadNumber + 1;
        }
        else {
            if (this.bookScene.bookDetails.rightToLeft == false) {
                this.control.content.findName("jumpStart")["Value"] = (this.spreadTicker * -180);
                this.control.content.findName("jumpFin")["Value"] = (spreadNumber * -180) + 180;
            }
            else {
                this.control.content.findName("jumpStart")["Value"] = (this.spreadTicker * +180);
                this.control.content.findName("jumpFin")["Value"] = (spreadNumber * +180) - 180;
            }
            this.control.content.findName("jumpTimeline").Begin();
            this.spreadTicker = spreadNumber - 1;
        }
    },

    handleXamlBookClick: function(xamlBookItem) {
        xamlBookItem.mouseDownX = -1;
        xamlBookItem.mouseDownY = -1;
    },

    resetBookPosition: function(xamlBookItem) {
        if (this.rootCanvas.findName("bookHolder").children.count >= 1) {
            this.centerBookPosition();
            this.scale = this.rootCanvas.findName("sceneScale"); // = 10;
            if (this.bookScene.bookDetails.rightToLeft == false)
                this.scale.scaleX = 0.9;
            else
                this.scale.scaleX = -0.9;
            this.scale.scaleY = 0.9;
            this.rootCanvas.findName("rotateStartTime")["Value"] = this.endAngle;
            this.rootCanvas.findName("rotateEndTime")["Value"] = this.bookScene.bookDetails.preRotationAngleX;
            this.rootCanvas.findName("rotateAnimation").Begin();

            this.startAngle = this.bookScene.bookDetails.preRotationAngleX;
            this.endAngle = this.bookScene.bookDetails.preRotationAngleX;
            this.rotateTicker = 0;

            this.bookScene.navigationManager.calulatePageDraggingProperties(this.endAngle, this.rotateTicker);
        }
    },

    centerBookPosition: function() {
        this.position = this.rootCanvas.findName("sceneTranslate"); // = 10;
        if (this.bookScene.bookDetails.rightToLeft == false)
            this.position.X = (this.targetWidth / 2) - 420;
        else {
            this.position.X = (this.targetWidth / 2) - 420;
        }
        this.position.Y = (this.targetHeight / 2) - 285 - (this.targetHeight / 9);
    },

    resizeSpreadNavi: function() {
        var buttonBarHeight = this.rootCanvas.findName("buttonBar")["Height"];
        var rotateAngle = this.bookScene.bookDetails.preRotationAngleX;
        if (rotateAngle == 0 || rotateAngle == 180)
            var newImageHeight = buttonBarHeight / 1.6;
        else
            var newImageHeight = buttonBarHeight / 2.2;
        var spreadNaviCenterPoint = this.rootCanvas.findName("spreadNaviHolder")["Width"] / 2;

        var newImageWidth = this.bookScene.bookDetails.width / (this.bookScene.bookDetails.height / newImageHeight);
        if (rotateAngle == 0 || rotateAngle == 180) {
            var versoTopPosition = 16;
            var rectoTopPosition = 16;
            var leftPosition = spreadNaviCenterPoint - newImageWidth;
            var leftPosition1 = spreadNaviCenterPoint;
        }
        else {
            var versoTopPosition = 10;
            var rectoTopPosition = newImageWidth + versoTopPosition;
            var leftPosition = spreadNaviCenterPoint - (newImageHeight / 2) + 5;
            var leftPosition1 = spreadNaviCenterPoint - (newImageHeight / 2) + 5;
        }
        if (this.bookScene.bookDetails.backCoverImage == true) {
            var spreadImage = this.control.content.findName("spreadImage_fc");
            spreadImage["Height"] = newImageHeight;
            spreadImage["Width"] = newImageWidth;
            spreadImage["Canvas.Left"] = spreadNaviCenterPoint - (newImageWidth / 2);
            spreadImage["Canvas.Top"] = versoTopPosition;
            var spreadText = this.rootCanvas.findName("spreadText_fc");
            spreadText["Canvas.Left"] = spreadNaviCenterPoint - ((spreadText["Text"].length * 7) / 2);
            for (var i = 1; i < this.currentPageObjects; i = i + 2) {
                switch (i) {
                    case 1:
                        var spreadImage = this.control.content.findName("spreadImage_ifc");
                        spreadImage["Height"] = newImageHeight;
                        spreadImage["Width"] = newImageWidth;
                        spreadImage["Canvas.Top"] = versoTopPosition;
                        if (this.bookScene.bookDetails.rightToLeft == false)
                            spreadImage["Canvas.Left"] = leftPosition;
                        else
                            spreadImage["Canvas.Left"] = leftPosition1;
                        var spreadImage1 = this.control.content.findName("spreadImage_p2");
                        spreadImage1["Height"] = newImageHeight;
                        spreadImage1["Width"] = newImageWidth;
                        spreadImage1["Canvas.Top"] = rectoTopPosition;
                        if (this.bookScene.bookDetails.rightToLeft == false)
                            spreadImage1["Canvas.Left"] = leftPosition1;
                        else
                            spreadImage1["Canvas.Left"] = leftPosition;
                        var spreadText = this.rootCanvas.findName("spreadText_ifc");
                        spreadText["Canvas.Left"] = spreadNaviCenterPoint - ((spreadText["Text"].length * 7) / 2);
                        break
                    case (this.currentPageObjects - 1): //ibc etc
                        var spreadImage = this.control.content.findName("spreadImage_p" + (this.currentPageObjects - 1));
                        spreadImage["Height"] = newImageHeight;
                        spreadImage["Width"] = newImageWidth;
                        spreadImage["Canvas.Top"] = versoTopPosition;
                        if (this.bookScene.bookDetails.rightToLeft == false)
                            spreadImage["Canvas.Left"] = leftPosition;
                        else
                            spreadImage["Canvas.Left"] = leftPosition1;
                        var spreadImage1 = this.control.content.findName("spreadImage_ibc");
                        spreadImage1["Height"] = newImageHeight;
                        spreadImage1["Width"] = newImageWidth;
                        spreadImage1["Canvas.Top"] = rectoTopPosition;
                        if (this.bookScene.bookDetails.rightToLeft == false)
                            spreadImage1["Canvas.Left"] = leftPosition1;
                        else
                            spreadImage1["Canvas.Left"] = leftPosition;
                        var spreadText = this.rootCanvas.findName("spreadText_ibc");
                        spreadText["Canvas.Left"] = spreadNaviCenterPoint - ((spreadText["Text"].length * 7) / 2);
                        break
                    default: //rest of pages
                        var spreadImage = this.control.content.findName("spreadImage_p" + (i));
                        spreadImage["Height"] = newImageHeight;
                        spreadImage["Width"] = newImageWidth;
                        spreadImage["Canvas.Top"] = versoTopPosition;
                        if (this.bookScene.bookDetails.rightToLeft == false)
                            spreadImage["Canvas.Left"] = leftPosition;
                        else
                            spreadImage["Canvas.Left"] = leftPosition1;
                        var spreadImage1 = this.control.content.findName("spreadImage_p" + (i + 1));
                        spreadImage1["Height"] = newImageHeight;
                        spreadImage1["Width"] = newImageWidth;
                        spreadImage1["Canvas.Top"] = rectoTopPosition;
                        if (this.bookScene.bookDetails.rightToLeft == false)
                            spreadImage1["Canvas.Left"] = leftPosition1;
                        else
                            spreadImage1["Canvas.Left"] = leftPosition;
                        var spreadText = this.rootCanvas.findName("spreadText_p" + (i));
                        spreadText["Canvas.Left"] = spreadNaviCenterPoint - ((spreadText["Text"].length * 7) / 2);
                }
            }
        }
        else {
            var spreadImage = this.control.content.findName("spreadImage_p1");
            spreadImage["Height"] = newImageHeight;
            spreadImage["Width"] = newImageWidth;
            spreadImage["Canvas.Top"] = versoTopPosition;
            spreadImage["Canvas.Left"] = spreadNaviCenterPoint - (newImageWidth / 2);
            var spreadText = this.rootCanvas.findName("spreadText_p1");
            spreadText["Canvas.Left"] = spreadNaviCenterPoint - ((spreadText["Text"].length * 7) / 2);

            for (var i = 1; i < this.currentPageObjects; i = i + 2) {
                var spreadImage = this.control.content.findName("spreadImage_p" + (i + 1));
                spreadImage["Height"] = newImageHeight;
                spreadImage["Width"] = newImageWidth;
                spreadImage["Canvas.Top"] = versoTopPosition;
                if (this.bookScene.bookDetails.rightToLeft == false)
                    spreadImage["Canvas.Left"] = leftPosition;
                else
                    spreadImage["Canvas.Left"] = leftPosition1;
                var spreadImage1 = this.control.content.findName("spreadImage_p" + (i + 2));
                spreadImage1["Height"] = newImageHeight;
                spreadImage1["Width"] = newImageWidth;
                spreadImage1["Canvas.Top"] = rectoTopPosition;
                if (this.bookScene.bookDetails.rightToLeft == false)
                    spreadImage1["Canvas.Left"] = leftPosition1;
                else
                    spreadImage1["Canvas.Left"] = leftPosition;
                var spreadText = this.rootCanvas.findName("spreadText_p" + (i + 1));
                spreadText["Canvas.Left"] = spreadNaviCenterPoint - ((spreadText["Text"].length * 7) / 2);
            }
        }
    },

    resize: function(sender, eventArgs) {
        // Find the new size of the control
        var targetWidth = this.control.content.actualWidth;
        var targetHeight = this.control.content.actualHeight;
        this.targetHeight = targetHeight;
        this.targetWidth = targetWidth;
        // Calculate height based on percentage
        var sceneHeight = targetHeight / (1 / this.scenePanePercentage);

        if (targetWidth >= 775 && targetHeight >= 410) {
            var sceneRoot = sender.findName("rootCanvas");
            sceneRoot.height = sceneHeight;
            sceneRoot.width = targetWidth;

            var buttonBar = sender.findName("buttonBar");
            buttonBar.height = (sceneHeight / 9);
            buttonBar.width = sceneRoot.width;
            buttonBar["canvas.top"] = targetHeight - buttonBar.height;

            var buttonBarButtonWidth = (buttonBar.width / 15) - 10;
            var buttonBarTopMargin = (buttonBar.height / 7);

            var hideSpreadsCanvas = sender.findName("hideSpreadsCanvas");
            hideSpreadsCanvas.height = buttonBar.height - buttonBarTopMargin - 1;
            hideSpreadsCanvas.width = buttonBar.width;
            hideSpreadsCanvas["canvas.top"] = buttonBar.height - hideSpreadsCanvas.height;

            var spreadNaviHolder = sender.findName("spreadNaviHolder");
            spreadNaviHolder.height = buttonBar.height - buttonBarTopMargin;
            spreadNaviHolder.width = 180;
            spreadNaviHolder["canvas.left"] = (buttonBar.width / 2) - (spreadNaviHolder.width / 2);
            spreadNaviHolder["canvas.top"] = buttonBarTopMargin;

            var spreadBack = sender.findName("spreadBack");
            spreadBack.height = buttonBar.height - buttonBarTopMargin;
            //spreadBack.width = 70;
            //spreadBack["canvas.left"] = (buttonBar.width / 2) - (180 / 2);
            spreadBack.width = buttonBarButtonWidth;
            spreadBack["canvas.left"] = (buttonBarButtonWidth * 6) + 60;
            spreadBack["canvas.top"] = buttonBarTopMargin;

            var spreadBackImage = sender.findName("spreadBackImage");
            spreadBackImage.height = (buttonBar.height / 2.5);
            spreadBackImage.width = spreadBackImage.height * 0.9;
            spreadBackImage["Canvas.Top"] = ((buttonBar.height) - (spreadBackImage.height)) / 2;
            spreadBackImage["Canvas.Left"] = (spreadBack.width - spreadBackImage.width) / 1.5;

            var spreadForward = sender.findName("spreadForward");
            spreadForward.height = buttonBar.height - buttonBarTopMargin;
            //spreadForward.width = 70;
            //spreadForward["canvas.left"] = (buttonBar.width / 2) + (180 / 2) - spreadForward.width;
            spreadForward.width = buttonBarButtonWidth;
            spreadForward["canvas.left"] = (buttonBarButtonWidth * 8) + 90;
            spreadForward["canvas.top"] = buttonBarTopMargin;

            var spreadForwardImage = sender.findName("spreadForwardImage");
            spreadForwardImage.height = (buttonBar.height / 2.5);
            spreadForwardImage.width = spreadForwardImage.height * 0.9;
            spreadForwardImage["Canvas.Top"] = ((buttonBar.height) - (spreadForwardImage.height)) / 2;
            spreadForwardImage["Canvas.Left"] = (spreadForward.width - spreadForwardImage.width) / 3.5;

            var searchButton = sender.findName("searchButton");
            searchButton["canvas.top"] = 10;
            searchButton["canvas.left"] = sceneRoot.width - searchButton.width - 5;

            var searchBgStyling = sender.findName("searchBgStyling");
            searchBgStyling["canvas.left"] = sceneRoot.width - 320;

            var menuButton = sender.findName("menuButton");
            menuButton.height = buttonBar.height - buttonBarTopMargin;
            menuButton.width = buttonBarButtonWidth;
            menuButton["canvas.top"] = buttonBarTopMargin;

            var resetButton = sender.findName("resetButton");
            resetButton.height = buttonBar.height;
            resetButton.width = buttonBarButtonWidth;
            resetButton["canvas.left"] = (buttonBarButtonWidth * 2) + 35;
            resetButton["canvas.top"] = buttonBarTopMargin;

            var backButton = sender.findName("backButton");
            backButton.height = buttonBar.height;
            backButton.width = buttonBarButtonWidth;
            backButton["canvas.left"] = (buttonBarButtonWidth) + 20;
            backButton["canvas.top"] = buttonBarTopMargin;

            var zoomInButton = sender.findName("zoomIn");
            zoomInButton.height = buttonBar.height / 2;
            zoomInButton.width = (buttonBarButtonWidth);
            zoomInButton["canvas.left"] = (buttonBarButtonWidth * 4) + 40;
            zoomInButton["canvas.top"] = buttonBarTopMargin;

            var zoomInImage = sender.findName("zoomInImage");
            /*zoomInImage.height = zoomInButton.height - 15;
            zoomInImage["canvas.top"] = buttonBarTopMargin + 5;
            zoomInImage["canvas.left"] = (zoomInButton.width / 2) - (zoomInImage.height / 2);*/
            zoomInImage.height = zoomInButton.height - 13;
            zoomInImage["canvas.top"] = 13;
            zoomInImage["canvas.left"] = (zoomInButton.width / 2) - (zoomInImage.height / 2);

            sender.findName("zoomText")["Canvas.Left"] = zoomInImage["canvas.left"] - 5;
            this.repositionTextItem(sender.findName("zoomText"), 8, buttonBarButtonWidth);

            var zoomOutButton = sender.findName("zoomOut");
            zoomOutButton.height = (buttonBar.height / 2) - buttonBarTopMargin;
            zoomOutButton.width = (buttonBarButtonWidth);
            zoomOutButton["canvas.left"] = (buttonBarButtonWidth * 4) + 40;
            zoomOutButton["canvas.top"] = zoomInButton.height + buttonBarTopMargin + 0;

            var zoomOutImage = sender.findName("zoomOutImage");
            /*zoomOutImage.height = zoomInButton.height - 15;
            zoomOutImage["canvas.left"] = (zoomOutButton.width / 2) - (zoomOutImage.height / 2);
            zoomOutImage["canvas.top"] = 10;*/
            zoomOutImage.height = zoomInImage.height;
            zoomOutImage["canvas.top"] = 0;

            zoomOutImage["canvas.left"] = (zoomOutButton.width / 2) - (zoomOutImage.height / 2);

            var rotateButton = sender.findName("rotateButton");
            rotateButton.height = buttonBar.height;
            rotateButton.width = buttonBarButtonWidth;
            rotateButton["canvas.left"] = (buttonBarButtonWidth * 3) + 30;
            rotateButton["canvas.top"] = buttonBarTopMargin;

            /*var magnifyButton = sender.findName("magnifyButton");
            magnifyButton.height = buttonBar.height;
            magnifyButton.width = buttonBarButtonWidth;
            magnifyButton["canvas.left"] = (buttonBarButtonWidth * 3) + 20;
            magnifyButton["canvas.top"] = buttonBarTopMargin;*/

            var moveButton = sender.findName("moveButton");
            moveButton.height = buttonBar.height;
            moveButton.width = buttonBarButtonWidth;
            moveButton["canvas.left"] = (buttonBarButtonWidth * 5) + 50;
            moveButton["canvas.top"] = buttonBarTopMargin;

            var audioButton = sender.findName("audioButton");
            audioButton.height = buttonBar.height;
            audioButton.width = buttonBarButtonWidth;
            audioButton["canvas.left"] = (buttonBarButtonWidth * 11) + 110;
            audioButton["canvas.top"] = buttonBarTopMargin;

            var audioButton2 = sender.findName("audioButton2");
            audioButton2.height = buttonBar.height;
            audioButton2.width = buttonBarButtonWidth;
            audioButton2["canvas.left"] = (buttonBarButtonWidth * 12) + 120;
            audioButton2["canvas.top"] = buttonBarTopMargin;

            var textButton = sender.findName("textButton");
            textButton.height = buttonBar.height;
            textButton.width = buttonBarButtonWidth;
            textButton["canvas.left"] = (buttonBarButtonWidth * 9) + 90;
            textButton["canvas.top"] = buttonBarTopMargin;

            var textButton2 = sender.findName("textButton2");
            textButton2.height = buttonBar.height;
            textButton2.width = buttonBarButtonWidth;
            textButton2["canvas.left"] = (buttonBarButtonWidth * 10) + 100;
            textButton2["canvas.top"] = buttonBarTopMargin;

            /*var notesButton = sender.findName("notesButton");
            notesButton.height = buttonBar.height;
            notesButton.width = buttonBarButtonWidth;
            notesButton["canvas.left"] = (buttonBarButtonWidth * 12) + 120;
            notesButton["canvas.top"] = buttonBarTopMargin;*/

            var helpButton = sender.findName("helpButton");
            helpButton.height = buttonBar.height;
            helpButton.width = buttonBarButtonWidth;
            helpButton["canvas.left"] = (buttonBarButtonWidth * 14) + 140;
            helpButton["canvas.top"] = buttonBarTopMargin;

            var mainMenuHolder = sender.findName("mainMenuHolder");
            mainMenuHolder["canvas.left"] = (targetWidth / 2) - (mainMenuHolder.width / 2)
            mainMenuHolder["canvas.top"] = (targetHeight / 2) - (mainMenuHolder.height / 2) - (buttonBar.height / 2);

            var resultsCanvas = sender.findName("resultsCanvas");
            resultsCanvas["canvas.left"] = (targetWidth / 2) - (resultsCanvas.width / 2);
            resultsCanvas["canvas.top"] = 100;

            var pageResultsCanvas = sender.findName("pageResultsCanvas");
            pageResultsCanvas["canvas.left"] = (targetWidth / 2) - (pageResultsCanvas.width / 2)
            pageResultsCanvas["canvas.top"] = 100;

            var menuImage = sender.findName("menuImage");
            menuImage.height = buttonBar.height - buttonBarTopMargin - 15;
            menuImage["canvas.top"] = buttonBarTopMargin + 5;
            this.repositionTextItem(sender.findName("menuText"), 6, (sender.findName("menuImage")["Height"] - 10));

            var menuImageDown = sender.findName("menuImageDown");
            menuImageDown.height = buttonBar.height - buttonBarTopMargin - 15;
            menuImageDown["canvas.top"] = buttonBarTopMargin + 5;

            var backImage = sender.findName("backImage");
            backImage.height = buttonBar.height - buttonBarTopMargin - 25;
            backImage["canvas.top"] = buttonBarTopMargin + 8;
            this.repositionTextItem(sender.findName("backText"), 6, sender.findName("backImage")["Height"]);

            var resetImage = sender.findName("resetImage");
            resetImage.height = buttonBar.height - buttonBarTopMargin - 25;
            resetImage["canvas.top"] = buttonBarTopMargin + 8;
            this.repositionTextItem(sender.findName("resetText"), 6, sender.findName("resetImage")["Height"]);

            var rotateImage = sender.findName("rotateImage");
            rotateImage.height = buttonBar.height - buttonBarTopMargin - 15;
            rotateImage["canvas.top"] = buttonBarTopMargin + 3;
            this.repositionTextItem(sender.findName("rotateText"), 6, sender.findName("rotateImage")["Height"]);

            /*var magnifyImage = sender.findName("magnifyImage");
            magnifyImage.height = buttonBar.height - buttonBarTopMargin - 20;
            magnifyImage["canvas.top"] = buttonBarTopMargin + 3;*/

            var moveImage = sender.findName("moveImage");
            moveImage.height = buttonBar.height - buttonBarTopMargin - 12;
            moveImage["canvas.top"] = buttonBarTopMargin;
            this.repositionTextItem(sender.findName("moveText"), 6, (sender.findName("moveImage")["Height"] - 10));

            var moveImageDown = sender.findName("moveImageDown");
            moveImageDown.height = buttonBar.height - buttonBarTopMargin - 12;
            moveImageDown["canvas.top"] = buttonBarTopMargin;

            var audioImage = sender.findName("audioImage");
            audioImage.height = buttonBar.height - buttonBarTopMargin - 20;
            audioImage["canvas.top"] = buttonBarTopMargin + 6;
            this.repositionTextItem(sender.findName("audioText"), 6, sender.findName("audioImage")["Height"]);

            var audioImageDown = sender.findName("audioImageDown");
            audioImageDown.height = buttonBar.height - buttonBarTopMargin - 20;
            audioImageDown["canvas.top"] = buttonBarTopMargin + 6;

            var audioImage2 = sender.findName("audioImage2");
            audioImage2.height = buttonBar.height - buttonBarTopMargin - 20;
            audioImage2["canvas.top"] = buttonBarTopMargin + 6;
            this.repositionTextItem(sender.findName("audioText2"), 6, sender.findName("audioImage2")["Height"]);

            var audioImageDown2 = sender.findName("audioImageDown2");
            audioImageDown2.height = buttonBar.height - buttonBarTopMargin - 20;
            audioImageDown2["canvas.top"] = buttonBarTopMargin + 6;

            var textImage = sender.findName("textImage");
            textImage.height = buttonBar.height - buttonBarTopMargin - 20;
            textImage["canvas.top"] = buttonBarTopMargin + 6;
            this.repositionTextItem(sender.findName("textText"), 6, sender.findName("textImage")["Height"]);

            var textImageDown = sender.findName("textImageDown");
            textImageDown.height = buttonBar.height - buttonBarTopMargin - 20;
            textImageDown["canvas.top"] = buttonBarTopMargin + 6;

            var textImage2 = sender.findName("textImage2");
            textImage2.height = buttonBar.height - buttonBarTopMargin - 20;
            textImage2["canvas.top"] = buttonBarTopMargin + 6;
            this.repositionTextItem(sender.findName("textText2"), 6, sender.findName("textImage2")["Height"]);

            var textImageDown2 = sender.findName("textImageDown2");
            textImageDown2.height = buttonBar.height - buttonBarTopMargin - 20;
            textImageDown2["canvas.top"] = buttonBarTopMargin + 6;

            /*var notesImage = sender.findName("notesImage");
            notesImage.height = buttonBar.height - buttonBarTopMargin - 30;
            notesImage["canvas.top"] = buttonBarTopMargin + 5;*/

            var helpImage = sender.findName("helpImage");
            helpImage.height = buttonBar.height - buttonBarTopMargin - 20;
            helpImage["canvas.top"] = buttonBarTopMargin + 6;
            this.repositionTextItem(sender.findName("helpText"), 6, sender.findName("helpImage")["Height"]);

            var helpImageDown = sender.findName("helpImageDown");
            helpImageDown.height = buttonBar.height - buttonBarTopMargin - 20;
            helpImageDown["canvas.top"] = buttonBarTopMargin + 6;

            if (this.rootCanvas.findName("bookHolder").children.count >= 1 && this.bookLoaded == true) {
                this.centerBookPosition();
                var spreadTitleBlock = sender.findName("spreadTitleBlock");
                var searchBgDivider = sender.findName("searchBgDivider");
                spreadTitleBlock["canvas.top"] = 15;
                spreadTitleBlock["canvas.left"] = targetWidth - 320 - (this.bookScene.bookDetails.title.length * 7); //(buttonBar.width / 2) - 30;
                searchBgStyling["width"] = 330 + (this.bookScene.bookDetails.title.length * 7) + 10;
                searchBgStyling["Canvas.Left"] = sceneRoot.width - 330 - (this.bookScene.bookDetails.title.length * 7);
                searchBgDivider["Visibility"] = "Collapsed";
                searchBgDivider["Canvas.Left"] = sceneRoot.width - 312;
                this.resizeSpreadNavi();
            }

            var ttpInstructions = sender.findName("ttpInstructions");
            ttpInstructions["canvas.left"] = (targetWidth / 2) - 300;
            ttpInstructions["canvas.top"] = targetHeight - buttonBar.height - 100;
        }
        if (this.rootCanvas.findName("mainMenuHolder").children.count >= 1) {
            if (this.control.content.actualHeight <= 550) {
                sender.findName("mainMenuScale")["ScaleX"] = 0.7;
                sender.findName("mainMenuScale")["ScaleY"] = 0.7;
            }
            else {
                sender.findName("mainMenuScale")["ScaleX"] = 1;
                sender.findName("mainMenuScale")["ScaleY"] = 1;
            }
        }
    },

    setCallback: function(target, eventName, callback) {
        if (!window.methodIndex)
            window.methodIndex = 0;

        var callbackName = "uniqueCallback" + (window.methodIndex++);
        var controller = this;
        var func = function() {
            callback.apply(controller, arguments);
        }

        callbackName = func;
        target[eventName] = callbackName;
    }
}
