﻿// UI_ImprintCanvas.js
// Ryan van der Kooy
// 02/20/08
// eComSystems, Inc.

// Classes:
// ImprintCanvas

ImprintCanvas = function(plugIn, storeName) {

    this.plugIn = plugIn;
    this.imprintPrefix;
    this.storeNumber;
    this.storeName = storeName;
    this.dist;
    
    this.imprintNames = new Array();
    
    this.imprintNames[0] = "_storelogo";
    this.imprintNames[1] = "_front";
    this.imprintNames[2] = "_back";
    this.imprintNames[3] = "TEXT";
   
    
    this.downloadedImprint;
    this.currentTry = 0;
    
    
    
    
    this.left = 0;
    this.top = 0;
    this.width = 570;
    this.height = 114;
    this.cursor = "Arrow";

    var mainCanvasXamlString = "<Canvas ";
                mainCanvasXamlString += "xmlns='http://schemas.microsoft.com/client/2007' ";
                mainCanvasXamlString += "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ";
                mainCanvasXamlString += "x:Name='" + this.pageNumber + "' ";
                mainCanvasXamlString += "Canvas.Left='" + this.left + "' ";
                mainCanvasXamlString += "Canvas.Top='" + this.top + "' ";
                mainCanvasXamlString += "Height='" + this.height + "' ";
                mainCanvasXamlString += "Width='" + this.width + "' ";
                mainCanvasXamlString += "Background='White' ";
        mainCanvasXamlString += " />";
    this.mainCanvas = this.plugIn.content.createFromXaml(mainCanvasXamlString);

}



ImprintCanvas.prototype.setTop = function(top) {
    this.top = top;
    this.mainCanvas["Canvas.Top"] = top;
}

ImprintCanvas.prototype.setLeft = function(left) {
    this.left = left;
    this.mainCanvas["Canvas.Left"] = left;
}

ImprintCanvas.prototype.setHeight = function(height) {
    this.height = height;
    this.mainCanvas.Height = height;
}

ImprintCanvas.prototype.setWidth = function(width) {
    this.width = width;
    this.mainCanvas.Width = width;
}

ImprintCanvas.prototype.setStore = function(distributor, storeNumber) {
    this.mainCanvas.children.clear();
    this.dist = distributor;
    this.storeNumber = storeNumber;
    var dl = this.plugIn.createObject("downloader");
    dl.addEventListener("completed", Silverlight.createDelegate(this, this.handleImprintDownloadComplete));
    dl.addEventListener("downloadFailed", Silverlight.createDelegate(this, this.tryDownload));
    this.tryDownload(dl);
        
    
    

}


ImprintCanvas.prototype.tryDownload = function(dl) {

    
    var imprintPrefix = Settings.setting1 + this.dist + "/images/";
    if (this.imprintNames[this.currentTry] == "TEXT") {
        this.setTextImprint();
    } else {
        
        //debug info
        if (gblDebugMode) {
            if (this.currentTry != 0) {
            document.getElementById("debugger").value = "\nImprint:   ...Imprint image not found: " + imprintPrefix + this.storeNumber + this.imprintNames[this.currentTry - 1] + ".jpg" + document.getElementById("debugger").value;
            }
            document.getElementById("debugger").value = "\nImprint:   ...Attempting to set imprint: " + imprintPrefix + this.storeNumber + this.imprintNames[this.currentTry] + ".jpg" + document.getElementById("debugger").value;
       }
       
       
        var url = imprintPrefix + this.storeNumber + this.imprintNames[this.currentTry] + ".jpg";
       
        dl.open("GET", url);
        dl.send();
        
        
        
        this.currentTry++;
    }
    
}

/*ImprintCanvas.prototype.setImprintSource = function(url) {
    var imageImprintXamlString = "<Image ";
            imageImprintXamlString += "Height='114' />";
            this.imageImprint = this.plugIn.content.createFromXaml(imageImprintXamlString);
            this.imageImprint.Source = url;
            this.mainCanvas.children.add(this.imageImprint);
            alert("set url: " + this.imageImprint.Source);
}*/
ImprintCanvas.prototype.handleImprintDownloadComplete = function(sender) {
    //debug info
        if (gblDebugMode) {
            document.getElementById("debugger").value = "\nImprint:   ...Found: " + sender.uri + document.getElementById("debugger").value;
       }
    var imageImprintXamlString = "<Image ";
            imageImprintXamlString += "Height='" + this.height + "' ";
            imageImprintXamlString += "Width='" + this.width + "' ";
            imageImprintXamlString += "Stretch='Uniform' ";
            imageImprintXamlString += "/>";
    this.imageImprint = this.plugIn.content.createFromXaml(imageImprintXamlString);
    this.imageImprint.setSource(sender, "");
    
    ShoppingList.imprintLocation = sender.uri.toString();
    
    this.width = 570;
    this.height= 114;
    
    this.mainCanvas.children.add(this.imageImprint);
    
}

ImprintCanvas.prototype.setTextImprint = function() {
    
    var textImprintXamlString = "<TextBlock ";
            textImprintXamlString += "Text='" + this.storeName + "' ";
            textImprintXamlString += "Foreground='White' ";
            textImprintXamlString += "FontSize='20' ";
            textImprintXamlString += " />";
    this.mainCanvas.children.add(this.plugIn.content.createFromXaml(textImprintXamlString));

}

ImprintCanvas.prototype.getXamlElement = function() {
    return this.mainCanvas;
}
