﻿// AdComponentLib.js
// Ryan van der Kooy
// 09/11/07
// eComSystems, Inc.

// Classes:
// AdViewerAdvertisement
// AdPage
// Product Area
// Store
// Settings   (static)  




//AdViewerAdvertisement Class*********************************
AdViewerAdvertisement = function(){
    this.serverName = "";
    this.dist = "";
    this.storeNumber = "";
    this.advertID = "";
    this.title = "";
    this.pageImages = new Array();
    this.adPages = new Array();
    this.sizeRatio = "";
    this.goodThru = "";
    this.store;
    
}

AdViewerAdvertisement.prototype.getMaxNumPages = function() {
    return this.adPages.length;
}

AdViewerAdvertisement.prototype.getMaxPageHeight = function() {
    var currMaxHeight = 0;
    for (i=0;i<this.getMaxNumPages();i++) {
        if (this.adPages[i].pageHeight > currMaxHeight) {
        currMaxHeight = this.adPages[i].pageHeight;
            } //end if
        } //end for
 
        return currMaxHeight;
 }
 
 AdViewerAdvertisement.prototype.getStoreLogo = function() {
    var myImage = new Image();
    var logoName = "_storelogo.jpg";
    var frontName = "_front.jpg";
    var backName = "_back.jpg";
    var locationName = Settings.setting1 + this.dist + "/images/" + this.storeNumber;
    
   //TRY STORE LOGO
    myImage.src = locationName + logoName;
    if (myImage.width > 10) {
    return myImage.src
    } else {
        //TRY FRONT IMPRINT
        myImage.src = locationName + frontName;
        if (myImage.width > 10) {
        return myImage.src;
            } else {
                //TRY BACK IMPRINT
                myImage.src = locationName + backName;
                if (myImage.width > 10) {
                return myImage.src;
                } else {
                    return '';
                } //end back else
            } //end front else        
    }//end logo else
}
 
 AdViewerAdvertisement.prototype.getPageImageLocation = function(imageName) {
  if (imageName == '') {
    return '';
    } else {
    return Settings.setting1 + this.dist + "/background_templates/finished/download/" + imageName;
    }
}
AdViewerAdvertisement.prototype.getProductImageLocation = function(imageName) {
    
    if (imageName == '') {
    return '';
    } else {

    return Settings.setting1 + this.dist + "/ads/" + Left(imageName, 1) + "/" + imageName + ".jpg";
    //return Settings.setting1 + this.dist + "/thumbs/" + Left(imageName, 1) + "/" + imageName + "_thumb.jpg";
    }
    function Left(str, n) {
                if (n <= 0)     // Invalid bound, return blank string
                        return "";
                else if (n > String(str).length)   // Invalid bound, return
                        return str;                // entire string
                else // Valid bound, return appropriate substring
                        return String(str).substring(0,n);
        }

}

AdViewerAdvertisement.prototype.getProductThumbLocation = function(imageName) {
    
    if (imageName == '') {
    return '';
    } else {

    //return Settings.setting1 + this.dist + "/ads/" + Left(imageName, 1) + "/" + imageName + ".jpg";
    return Settings.setting1 + this.dist + "/thumbs/" + Left(imageName, 1) + "/" + imageName + "_thumb.jpg";
    }
    function Left(str, n) {
                if (n <= 0)     // Invalid bound, return blank string
                        return "";
                else if (n > String(str).length)   // Invalid bound, return
                        return str;                // entire string
                else // Valid bound, return appropriate substring
                        return String(str).substring(0,n);
        }

}

AdViewerAdvertisement.prototype.getIndexByAreaID = function(areaID) {
    var currAreaID = 0;
    for (i=0;i<this.getMaxNumPages();i++) {
        for (p=0;p<this.adPages[i].getMaxNumProducts();p++) {
            if (this.adPages[i].productAreas[p].areaID == areaID) {
            return p;
            }
        }
        
    }
}

AdViewerAdvertisement.prototype.getProductAreaByAreaID = function(areaID) {
    var currAreaID = 0;
    for (i=0;i<this.getMaxNumPages();i++) {
        for (p=0;p<this.adPages[i].getMaxNumProducts();p++) {
            if (this.adPages[i].productAreas[p].areaID == areaID) {
            return this.adPages[i].productAreas[p];
            }
        }
        
    }
}
 
 
 //AdPage Class****************************************************
AdPage = function() {
    this.advertisementID = "";
    this.name = "";
    this.serverName = "";
    this.dist = "";
    this.pageImageName = "";
    this.pageWidth = "";
    this.pageHeight = "";
    this.pageNumber = "";
    this.productAreas = new Array();
    this.downloadedImage = "";
}

AdPage.prototype.getMaxNumProducts = function() {
    return this.productAreas.length;
}

AdPage.prototype.getMaxNumProductImages = function() {
    var numProductImages = 0;
    for (p=0;p<this.getMaxNumProducts();p++) {
        if (this.productAreas[p].productImageName != "") { numProductImages++; }
       
    }
    
    return numProductImages;
}

AdPage.prototype.getPageImageLocation = function() {

    return Settings.setting1 + this.dist + "/background_templates/finished/download/" + this.pageImageName; 
}

AdPage.prototype.getProductImageLocation = function(imageName) {
    
    if (imageName == '') {
    return '';
    } else {

    return Settings.setting1 + this.dist + "/ads/" + Left(imageName, 1) + "/" + imageName + ".jpg";
    //return "../../TOM_OCPS/" + this.dist + "/thumbs/" + Left(imageName, 1) + "/" + imageName + "_thumb.jpg";
    }
    function Left(str, n) {
                if (n <= 0)     // Invalid bound, return blank string
                        return "";
                else if (n > String(str).length)   // Invalid bound, return
                        return str;                // entire string
                else // Valid bound, return appropriate substring
                        return String(str).substring(0,n);
        }

}

AdPage.prototype.getIndexByAreaID = function(areaID) {
    var currAreaID = 0;
   
        for (p=0;p<this.getMaxNumProducts();p++) {
            if (this.productAreas[p].areaID == areaID) {
            return p;
        }
        
    }
}




//Product Area Class***********************************************
ProductArea = function() {
    this.areaID = "";
    this.productImageName = "";
    this.downloadedImage = "";
    this.price = "";
    this.unit = "";
    this.size = "";
    this.brand = "";
    this.title = "";
    this.description = "";
    this.department = "";
    this.upperLeftX = "";
    this.upperLeftY = "";
    this.width = "";
    this.height = "";
}


//Store Class***********************************************
Store = new Object()

    Store.number;
    Store.name;
    Store.address1;
    Store.address2;
    Store.city;
    Store.state;
    Store.zip;
    Store.phone;
    
 


Settings = new Object()

Settings.setting1 = "";

