// AdViewer.js
// Ryan van der Kooy
// 01/30/08
// eComSystems, Inc.

// Classes:
// AdViewer

var gblVScrnSzCoefficient = 1;
var gblHScrnSzCoefficient = 1;


var gblLastQueuedImage = "";
var gblLastDownloadedImage = "";



AdViewer = function(adViewerAd)
{
    this.adViewerAd = adViewerAd;
    this.plugIn;
    
    this.vSpace = 10;
    this.hSpace = 10;
  
}

    ///////////////////////////////////////////////////
    ////////////////AdViewer HandleLoad ///////////////
    ///////////////////////////////////////////////////
    //handleLoad is called as soon as the silverlight plugin
    //has been loaded on the client's computer
    //(see the createAdviewer events listed in the JSON notation in CreateAdViewer.js)
AdViewer.prototype.handleLoad = function(control, userContext, rootElement) {

    this.plugIn = control;      // Store the host plug-in
    this.root = rootElement;
    this.adPageCanvas;

    //set the width of the plugIn = the width of the client's screen.
    //this.plugIn.width = document.body.clientWidth;
    try {
        window.moveTo(0, 0);
        window.resizeTo(screen.width, screen.height);
    } catch (err) { }


    //this.plugIn.width = document.body.clientWidth;


    this.downloadedPageImages = 0;

    this.totalProductImages = 0;
    this.downloadedProductImages = 0;


    this.content1 = this.plugIn.content.findName("Content1");
    this.initialize();
    //begin downloading the pages
    this.downloadPageImages();

    showHTMLElements();


}

    ///////////////////////////////////////////////////
    ////////////////Page Image Downloader /////////////
    ///////////////////////////////////////////////////
AdViewer.prototype.downloadPageImages = function() {
    
   //downloads all images using the silverlight downloader object
    for (i=0;i<this.adViewerAd.getMaxNumPages();i++) {
    
    if (gblDebugMode) {
    //debugging info
    document.getElementById("debugger").value = "\n...Downloading Page " + this.adViewerAd.adPages[i].pageNumber + ": " + this.adViewerAd.adPages[i].getPageImageLocation() + document.getElementById("debugger").value;
          }
    
    this.adViewerAd.adPages[i].downloadedImage = this.plugIn.createObject("downloader");
    this.adViewerAd.adPages[i].downloadedImage.addEventListener("completed", Silverlight.createDelegate(this, this.handlePageDownloadComplete));
    this.adViewerAd.adPages[i].downloadedImage.addEventListener("downloadFailed", Silverlight.createDelegate(this, this.handlePageDownloadFailed));
    this.adViewerAd.adPages[i].downloadedImage.addEventListener("downloadProgressChanged", Silverlight.createDelegate(this, this.updateWindowStatus));
    this.adViewerAd.adPages[i].downloadedImage.open("GET", this.adViewerAd.adPages[i].getPageImageLocation());
    this.adViewerAd.adPages[i].downloadedImage.send();
    gblLastQueuedImage = this.adViewerAd.adPages[i].pageImageName;
    }
}

    //this is called each time a page image is successfully downloaded.
    //After the first page is downloaded, it calls initialize(), which
    //builds the UI.
AdViewer.prototype.handlePageDownloadComplete = function(sender) {
    this.downloadedPageImages++;
    var test = sender.uri;
    gblLastDownloadedImage = sender.uri;
    if (gblDebugMode) {
        document.getElementById("debugger").value = "\n...Completed Download: " + sender.uri + document.getElementById("debugger").value;
    }

    //specify how many page images need to be downloaded in order to start
    //downloading the products.
    //if (this.downloadedPageImages == 1) {
    //    this.previewPanel.stopDownloadProgress();
    //}
    if (this.downloadedPageImages == this.adViewerAd.getMaxNumPages()) {
        this.previewPanel.stopDownloadProgress();
        this.start();

    }

}

AdViewer.prototype.handlePageDownloadFailed = function(sender) {
    this.downloadedPageImages++;
     if (gblDebugMode) {
    try {
        document.getElementById("debugger").value = "\n..." + sender.uri + "*********************************  DOWNLOAD FAILURE!!!!! *************************" + document.getElementById("debugger").value;
    } catch(err) {
        document.getElementById("debugger").value = "\n..." + sender + "*********************************  DOWNLOAD FAILURE!!!!! *************************" + document.getElementById("debugger").value;
    }
}
}




    //this is called from the downloadProgressChanged event in all downloaders.
AdViewer.prototype.updateWindowStatus = function() {
    window.status = 'Loading AdViewer Images...';
}





    ///////////////////////////////////////////////////
    ////////////////Product Image Downloader //////////
    ///////////////////////////////////////////////////
AdViewer.prototype.downloadProductImages = function() {
    
    for (i=this.downloadedProductImages;i<this.adViewerAd.getMaxNumPages();i++) {
            //every time we increment which page we're downloading,
            //lets figure out how many more images have to be added to the total.
            this.totalProductImages += this.adViewerAd.adPages[i].getMaxNumProductImages(); 
        var debugImgIncr = 0;
        
        for (p=0;p<this.adViewerAd.adPages[i].getMaxNumProducts();p++) {
          if (this.adViewerAd.adPages[i].productAreas[p].productImageName != "") {
          
          if (gblDebugMode) {
    //debugging info
    debugImgIncr++;
    document.getElementById("debugger").value = "\n...Downloading Product----------" + this.adViewerAd.getProductImageLocation(this.adViewerAd.adPages[i].productAreas[p].productImageName) + document.getElementById("debugger").value;

          }
            this.adViewerAd.adPages[i].productAreas[p].downloadedImage = this.plugIn.createObject("downloader");
            this.adViewerAd.adPages[i].productAreas[p].downloadedImage.addEventListener("completed", Silverlight.createDelegate(this, this.handleProductDownloadComplete));
            this.adViewerAd.adPages[i].productAreas[p].downloadedImage.addEventListener("downloadProgressChanged", Silverlight.createDelegate(this, this.updateWindowStatus));
            this.adViewerAd.adPages[i].productAreas[p].downloadedImage.addEventListener("downloadFailed", Silverlight.createDelegate(this, this.handleProductDownloadFailed));
            this.adViewerAd.adPages[i].productAreas[p].downloadedImage.open("GET", this.adViewerAd.getProductImageLocation(this.adViewerAd.adPages[i].productAreas[p].productImageName));
            this.adViewerAd.adPages[i].productAreas[p].downloadedImage.send();
            gblLastQueuedImage = this.adViewerAd.adPages[i].productAreas[p].productImageName;
            } else {
                //this.handlePageDownloadFailed(this.adViewerAd.adPages[i].productAreas[p].productImageName);
            }
        }
        
    }
}

AdViewer.prototype.handleProductDownloadComplete = function(sender) {
    this.downloadedProductImages++;
    gblLastDownloadedImage = sender.uri;
    
     if (gblDebugMode) {
    //debugging info
    document.getElementById("debugger").value = "\n...Completed Download of Product " + this.downloadedProductImages + " of " + this.totalProductImages + "----------" + sender.uri + document.getElementById("debugger").value;
   
    
    }
     
    //if all of the product images have been downloaded, WE'RE DONE!
    if (this.downloadedProductImages == this.totalProductImages) {
        //this.previewPanel.stopDownloadProgress();
         if (gblDebugMode) {
        //debugging info
         document.getElementById("debugger").value = "\n...Success...all images downloaded." + document.getElementById("debugger").value;
         }
        window.status='';  
    }
}

    
    ///////////////////////////////////////////////////
    ////////////////Initialize UI /////////////////////
    ///////////////////////////////////////////////////
    //this runs after all the page images have been downloaded.
    //it creates the UI.
AdViewer.prototype.initialize = function() {


    ////////////////Create Imprint Area/////////////////////// 
    /*var imprintXamlString = "<Image ";
    imprintXamlString += "xmlns='http://schemas.microsoft.com/client/2007' ";
    imprintXamlString += "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ";
    imprintXamlString += "x:Name='Imprint_Image' ";
    imprintXamlString += "Height='114' ";
    imprintXamlString += "Canvas.Left='" + this.hSpace + "' ";
    imprintXamlString += "Canvas.Top='" + this.vSpace + "' ";
    imprintXamlString += "/> ";
    this.imprint = this.plugIn.content.createFromXaml(imprintXamlString);
    this.content1.children.add(this.imprint);*/
    this.imprintCanvas = new ImprintCanvas(this.plugIn, "");
    this.content1.children.add(this.imprintCanvas.getXamlElement());
    this.imprintCanvas.setTop(this.vSpace);
    this.imprintCanvas.setHeight(100);
    this.imprintCanvas.setWidth(675);
    this.imprintCanvas.setLeft(250);
    this.imprintCanvas.setStore(this.adViewerAd.dist, this.adViewerAd.storeNumber);




    ////////////////Create Preview Panel//////////////////////

    this.previewPanel = new PreviewPanel(this.plugIn);

    this.previewPanel.setTop((this.hSpace));
    this.previewPanel.setLeft(this.hSpace);
    this.previewPanel.setHeight(700);
    this.previewPanel.setWidth(200);

    this.previewPanel.setFill(gblPrimaryColor);


    this.content1.children.add(this.previewPanel.getXamlElement());
    this.previewPanel.beginDownloadProgress();


    var goodThruXamlString = "<TextBlock ";
    goodThruXamlString += "FontSize='15' ";
    goodThruXamlString += "FontFamily='Arial Narrow' ";
    goodThruXamlString += "Text='" + this.adViewerAd.goodThru + "' ";
    goodThruXamlString += "Foreground='Black' ";
    goodThruXamlString += "FontWeight='Bold' ";
    goodThruXamlString += "TextWrapping='Wrap' "
    goodThruXamlString += "Width='" + this.previewPanel.width + "' ";
    goodThruXamlString += "/>";
    this.goodThru = this.plugIn.content.createFromXaml(goodThruXamlString);
    this.content1.children.add(this.goodThru);
    this.goodThru["Canvas.Top"] = 665;
    this.goodThru["Canvas.Left"] = this.hSpace + 5;


    var adStudioText = "<Image ";
    //adStudioText += "Source='assets/AdStudio-Product-of-eCom.png' ";
    adStudioText += "Source='assets/AdViewer-color-logo-small.png' ";
    adStudioText += "Canvas.Left=' " + this.hSpace + "' ";
    adStudioText += "Stretch='Uniform' ";
    //adStudioText += "Cursor='Hand' ";
    adStudioText += "/>";
    this.adStudioLogo = this.plugIn.content.createFromXaml(adStudioText);
    this.content1.children.add(this.adStudioLogo);
    this.adStudioLogo["Canvas.Top"] = this.previewPanel.height + this.previewPanel.top + this.vSpace;
    this.adStudioLogo.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.redirect));

    /*while (parseInt(this.adViewerLogo.actualWidth) < parseInt(this.adViewerLogo.Width)) {
      
    this.adViewerLogo.FontSize++;
    }*/



    ////////////////Create Thumbnail Panel/////////////
    this.thumbnailPanel = new ThumbnailPanel(this.plugIn, "ThumbnailPanel");



    this.thumbnailPanel.setTop(this.previewPanel.top);
    this.thumbnailPanel.setHeight(this.previewPanel.height);
    this.thumbnailPanel.setLeft(this.previewPanel.left + this.previewPanel.width);
    this.thumbnailPanel.setWidth(80);
    this.thumbnailPanel.setFill("Transparent");

    this.thumbnailPanel.setNumberOfPages(this.adViewerAd.getMaxNumPages());

    this.content1.children.add(this.thumbnailPanel.getXamlElement());
    this.thumbnailPanel.NextButton.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.setNextAdPage));
    this.thumbnailPanel.PreviousButton.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.setPreviousAdPage));
    this.thumbnailPanel.hidePreviousButton();


}

AdViewer.prototype.start = function() {



    ///////////////////////////////////////////////////
    ////////////////AdPage Canvas /////////////////////
    ///////////////////////////////////////////////////
    this.adPageCanvas = new AdPageCanvas(this.plugIn);
    this.content1.children.add(this.adPageCanvas.getXamlElement());
    this.adPageCanvas.setWidth(675);

    this.adPageCanvas.setTop(this.imprintCanvas.top + this.imprintCanvas.height + (this.vSpace));
    //this.adPageCanvas.setLeft(this.thumbnailPanel.left); 
    if (this.adViewerAd.getMaxNumPages() == 1) {
        this.thumbnailPanel.backgroundPanel.hide();
        this.adPageCanvas.setLeft(this.thumbnailPanel.left + (this.thumbnailPanel.width / 2));
    } else {
        this.adPageCanvas.setLeft(this.thumbnailPanel.left + this.thumbnailPanel.width);
    }



    var adPage = this.adViewerAd.adPages[0];
    this.adPageCanvas.setAdPage(adPage, true);
    var sizeRatio = this.adPageCanvas.width / adPage.pageWidth;
    this.previewPanel.setAreas(adPage, sizeRatio);

    //this.jumpToPage(1);
    ////////////////Create Page Thumbnails/////////////


    for (i = 0; i < this.adViewerAd.getMaxNumPages(); i++) {

        var currThumbnail = new ThumbnailCanvas(this.plugIn, i + 1);
        currThumbnail.mainCanvas.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.jumpToPage));

        if (currThumbnail.setImage(this.adViewerAd.adPages[i].downloadedImage) == 0) {
            currThumbnail.setImage(this.adViewerAd.adPages[i].pageImageName);
        }

        this.thumbnailPanel.addThumbnail(currThumbnail);
    }
    this.thumbnailPanel.setActiveThumbnail(0);

    ////////////////Create Shopping List Panel////////////////
    this.shoppingListPanel = new ShoppingListPanel(this.plugIn, this.adViewerAd);
    this.shoppingListPanel.setWidth(250);
    this.shoppingListPanel.setHeight(700);

    if (this.adViewerAd.getMaxNumPages() == 1) {
        this.shoppingListPanel.setLeft(this.adPageCanvas.left + this.adPageCanvas.width + this.hSpace + (this.thumbnailPanel.width / 2));
    } else {
        this.shoppingListPanel.setLeft(this.adPageCanvas.left + this.adPageCanvas.width + this.hSpace);
    }
    this.shoppingListPanel.setTop(this.vSpace)
    this.shoppingListPanel.setFill(gblPrimaryColor);


    this.content1.children.add(this.shoppingListPanel.getXamlElement());

    this.shoppingListPanel.setAreas(adPage);

    this.imprintCanvas.setLeft((this.adPageCanvas.width / 2) - (this.imprintCanvas.width / 2) + this.adPageCanvas.left);
    
    this.copyrightInfoPanel = new PanelCanvas(this.plugIn, "CopyrightInfo");

    this.copyrightInfoPanel.setWidth(900);
    this.copyrightInfoPanel.setHeight(830);
    this.copyrightInfoPanel.setLeft(75);
    this.copyrightInfoPanel.setTop(15);

    this.copyrightInfoPanel.setTitle("Copyright and Legal Information");
    this.copyrightInfoPanel.setAsCloseable();
    this.copyrightInfoPanel.setAsDraggable();
    this.copyrightInfoPanel.hide();
    

    var copyrightInfoButtonXamlString = "<TextBlock ";
    copyrightInfoButtonXamlString += "FontSize='9' ";
    copyrightInfoButtonXamlString += "FontFamily='Arial Narrow' ";
    copyrightInfoButtonXamlString += "Text='Copyright and Legal Information' ";
    copyrightInfoButtonXamlString += "Foreground='White' ";
    copyrightInfoButtonXamlString += "Cursor='Hand' ";
    copyrightInfoButtonXamlString += "/>";
    this.copyrightInfoButton = this.plugIn.content.createFromXaml(copyrightInfoButtonXamlString);

    this.content1.children.add(this.copyrightInfoButton);
    this.content1.children.add(this.copyrightInfoPanel.getXamlElement());
    this.copyrightInfoButton["Canvas.Left"] = this.hSpace;
    this.copyrightInfoButton["Canvas.Top"] = this.adStudioLogo["Canvas.Top"] + 43;

    this.copyrightInfoButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this.copyrightInfoPanel, this.copyrightInfoPanel.show));

    var copyrightString = "<TextBlock ";
    copyrightString += "FontSize='9' ";
    copyrightString += "TextWrapping='Wrap' ";
    copyrightString += "Foreground='Black' ";
    copyrightString += "Width='890' ";
    copyrightString += "Canvas.Top='35' ";
    copyrightString += "Canvas.Left='5' ";
    copyrightString += ">";
    copyrightString += "All content included on this site, such as text, graphics, logos, images, data, buttons, icons, audio clips, software and the like, and the compilation or all content on this site is exclusive property of eComSystems, Inc. and is protected by U.S. and international copyright laws.  All software used in conjunction with this site is the property of eComSystems, Inc., or its software suppliers and is protected by U.S. and international copyright laws. ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "<Run FontWeight='Bold'>Trademarks</Run>";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "\"eComSystems, Inc.\", \"AdStudio\", \"AM/PM Service Group, Inc.\", \"AdTracker\" and eComSystems' graphics, logos, service marks and trade names are trademarks of eComSystems, Inc.  eComSystems, Inc.'s trademarks may not be used in connection with any other product or service that is not owned by eComSystems in any manner.  ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "<Run FontWeight='Bold'>Site Usage and Licensing</Run>";
    copyrightString += "<LineBreak/>";
    copyrightString += "By using this site, the user agrees to be bound by the following:";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "1.	User agrees to utilize this site in accordance with the terms and conditions as set forth by eComSystems (\"Licensor\"), including those in the Licensing Agreement, those provided herein and those posted on the website through which the Service is accessed. ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "2.	Upon agreement to the terms of this User Agreement, User is granted a limited, non-transferable, non-exclusive sublicense to access and use the System as set forth herein for the Term of this User Agreement.  All right, title, and interest in the System shall remain with Licensor.  The license granted herein is not a sale of the System or any portion thereof and conveys neither title to, nor ownership of, the System or any Object Code or source code for the System (or any enhancements, upgrades or customization thereto) to User.  Neither User, nor its employees nor its agents shall transfer, assign, rent, or convey this license or the System, or any interest therein, to any third party, or permit the copying, modification, display, disclosure, or printing of the System under any circumstances.  ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "3.	<Run TextDecorations='Underline'>Ad Content</Run>.  User represents and warrants that it has the right to submit (if applicable) the Ad Content provided herein for the intended use, and that User’s receipt and retention of the Ad Content, and the manipulation, transfer, and other actions upon the Ad Content performed by User, Licensor, or others through the System, will not violate the copyright, trademark, or other intellectual property or proprietary interests of a third party. ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "4.	<Run TextDecorations='Underline'>Access Codes</Run>.  All Users of the System must be legally bound by License Agreement, to the terms of the User Agreement or must otherwise separately agree to the User Agreement terms.  User is responsible for the proper acquisition, use, and security of Access Codes granted in accordance with this User Agreement.  User agrees that it is not to allow third parties or other unauthorized users to use Access Codes and that it will not seek to acquire additional Access Codes through deception or misrepresentation regarding the nature of the corporate or employment relationship of any entities or persons to User. ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "5.	<Run TextDecorations='Underline'>Proprietary Nature of System and Licensor’s Intellectual Property</Run>.  User acknowledges that the System and the associated website and materials are subject to know how and intellectual property interest including but not limited to those pertaining to patents, copyright, trade secrets, confidential business information, trademarks, service marks, trade dress, logos, and trade names, all developed at substantial expense by Licensor, and that the System is subject to U.S. Patent No. 7,315,983 as well as a patent pending.  The aspects and portions of the System that Licensor claims are proprietary include, but are not limited to, the software code that controls the placement of graphic and textual database elements delivered via the Internet to produce advertising media and transfer of such media for final production electronically, and the business model associated therewith (\"Licensor’s Proprietary Information\").  User agrees that User shall not copy, reverse engineer, decompile, disassemble or modify any portion of the System, or disseminate any of Licensor’s Proprietary Information except as permitted under this Agreement, nor shall User in any manner circumvent Licensor’s ownership of Licensor’s Proprietary Information. ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "6.	<Run TextDecorations='Underline'>Non-compete</Run>.  During the term of this User Agreement, User agrees that Licensor shall be the sole and exclusive provider to User of the Licensor services contemplated under this Agreement including but not limited to services related to the composing and distributing of advertising circulars through the use of templates and a database of product images and descriptions, and similar services or services in competition with those provided by Licensor.  ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "7.	<Run TextDecorations='Underline'>Disclaimer of Warranties</Run>.  EXCEPT AS OTHERWISE EXPRESSLY STATED HEREIN, THE ADSTUDIO SYSTEM IS PROVIDED \"AS IS\" AND THERE ARE NO WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NONINFRINGEMENT.  ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "8.	<Run TextDecorations='Underline'>Additional Disclaimer</Run>.  LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES REGARDING THE ACCURACY OF AD CONTENT AND RELATED INFORMATION RECEIVED FROM, USED BY, OR CREATED BY USER, AND DISCLAIMS ALL LIABILITY RELATED THERETO, INCLUDING FOR DAMAGES RESULTING FROM CLIENT USERS’ USE OF OR RELIANCE THEREON.  ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "9.	<Run TextDecorations='Underline'>No transfer of license</Run>.   Nothing in this Agreement shall be construed to transfer to the User any right, title or interest or copyright in any Confidential and Proprietary Information, or any license to reverse engineer, decompile, sell, exploit, copy or further develop any such Confidential and Proprietary Information. This Agreement does not in any way bind the parties to enter into a business relationship of any nature. All right, title and interest in and to any inventions and any patents that may be based upon or arise out of the Company’s property or information, including, without limitation, the Confidential and Proprietary Information shall belong to the Company.  ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "10.	<Run TextDecorations='Underline'>Applicable Law</Run>.  This site is created and controlled by eComSystems, Inc. in the State of Florida, USA.  As such, the laws of the State of Florida will govern these disclaimers, terms and conditions, without giving effect to any principles of conflicts of law.  eComSystems reserves the right to make changes to this site and these disclaimer, terms and conditions at any time.  ";
    copyrightString += "<LineBreak/>";
    copyrightString += "<LineBreak/>";
    copyrightString += "\u00A9 Copyright eComSystems, Inc. All rights reserved. Protected by U.S. Patent No. 7,315,983.  Patents pending. Visit us at www.eComSystems.com ";
    copyrightString += "</TextBlock>";
    this.copyrightText = this.plugIn.content.createFromXaml(copyrightString);
    this.copyrightInfoPanel.add(this.copyrightText);


    //Page1 has been downloaded..the AdPageCanvas
    //and UI have been created and added.  Now 
    //start downloading the product images.


    this.downloadProductImages();

}
AdViewer.prototype.jumpToPage = function(sender) {
    var adPage = this.adViewerAd.adPages[sender.Name - 1];
    
    this.adPageCanvas.setAdPage(adPage, true);
    this.toggleNavButtons(this.adPageCanvas.currentPageNumber);
    
    this.thumbnailPanel.setAllInactive();
    this.thumbnailPanel.setActiveThumbnail(sender.Name - 1);
    
    var sizeRatio = this.adPageCanvas.width / adPage.pageWidth;
    this.previewPanel.setAreas(adPage, sizeRatio);
    this.shoppingListPanel.setAreas(adPage);
}
AdViewer.prototype.setNextAdPage = function() {
    try {
        var adPage = this.adViewerAd.adPages[this.adPageCanvas.currentPageNumber + 1];
        this.adPageCanvas.setAdPage(adPage, true);
        this.toggleNavButtons(this.adPageCanvas.currentPageNumber);
        this.thumbnailPanel.setAllInactive();
        this.thumbnailPanel.setActiveThumbnail(this.adPageCanvas.currentPageNumber);
       
        var sizeRatio = this.adPageCanvas.width / adPage.pageWidth;
        this.previewPanel.setAreas(adPage, sizeRatio);
        this.shoppingListPanel.setAreas(adPage);
    }catch(err) {
    //do nothing
    }
    
}

AdViewer.prototype.setPreviousAdPage = function() {
    try {
        var adPage = this.adViewerAd.adPages[this.adPageCanvas.currentPageNumber - 1];
        this.adPageCanvas.setAdPage(adPage, true);
        this.toggleNavButtons(this.adPageCanvas.currentPageNumber);
        this.thumbnailPanel.setAllInactive();
        this.thumbnailPanel.setActiveThumbnail(this.adPageCanvas.currentPageNumber);
        
        var sizeRatio = this.adPageCanvas.width / adPage.pageWidth;
        this.previewPanel.setAreas(adPage, sizeRatio);
        this.shoppingListPanel.setAreas(adPage);
    }catch(err) {
        //do nothing
    }
    
}

AdViewer.prototype.toggleNavButtons = function(currentPageNumber) {

    switch(currentPageNumber) {
        case 0:
            this.thumbnailPanel.hidePreviousButton();
            this.thumbnailPanel.showNextButton();
            
        break;
        
        case this.adViewerAd.adPages.length - 1:
            this.thumbnailPanel.hideNextButton();
            this.thumbnailPanel.showPreviousButton();
            
            
        break;
        
        default:
            this.thumbnailPanel.showNextButton();
            this.thumbnailPanel.showPreviousButton();
            
           
    }
}


AdViewer.prototype.redirect = function(){
    //window.location = "http://www.adstudio.com";
}

function hideHTMLElements() {
    document.getElementById("listDiv").style.visibility = "hidden";
    document.getElementById("addMyOwn").style.visibility = "hidden";

}

function showHTMLElements() {
    
    document.getElementById("listDiv").style.visibility = "visible";
    document.getElementById("addMyOwn").style.visibility = "visible";

}

