ImageResizer = {
    picsMgrOA                : new Array(),  //associative array of the picture objects
    picsAdminOA              : new Array(),  //associative array of the picture objects
    picsGeneralOA             : new Array(),  //associative array of the picture objects
    arrayOfPicArrays         : new Array(),  //array of the above pic arrays
    enlargeContWidthOffset  : null,           //enlargement container width minus enlarged image width

    //Constructor
    init: function(picPathsA,picsOA)    {
        //cache the images into an array
        //picPathsA is an associative array of the pic src paths
        //picsOA is a string containing the name of the image object array
        //i is the id of the pics img element
        imgOA = this.getPicArrayObj(picsOA);
        for(var i in picPathsA) {
            imgOA[i]       = new Image;
            imgOA[i].src   = picPathsA[i];
        }
        this.arrayOfPicArrays[picsOA] = imgOA;

        this.enlargeContWidthOffset = 13;
    },
    /*---------------------------------------------------------------------------------
     * Accessor methods
    ----------------------------------------------------------------------------------*/
    getPicArray: function(name) {
        return this.getPicArrayObj(name)
    },
    setEnlargeContWidthOffset: function(num) {
        this.enlargeContWidthOffset = num;
    },
    //--------------------------------------------------------------------------------
    getEnlargeContWidthOffset: function(num) {
        return this.enlargeContWidthOffset;
    },
    /*---------------------------------------------------------------------------------
     * Modifier methods
    ----------------------------------------------------------------------------------*/
    /*---------------------------------------------------------------------------------------------
       This function rescales the images in picsOA and then loads them into the id of value key
    -----------------------------------------------------------------------------------------------*/
    loadScaledImages: function(picsOA,maxWidth, maxHeight) {
        imgOA = this.getPicArrayObj(picsOA);
        if(!isNaN(maxWidth) && !isNaN(maxHeight) && maxWidth > 0 && maxHeight > 0) {
            var newDims = new Array();
            var width;
            var height;
            var imgCont;

            //cycle through the images cache and rescale each image
            for(var i in imgOA) {
                if(document.getElementById(i)) { //check if the image element exists
                    //store this pic's original width and height
                    width  = imgOA[i].width;
                    height = imgOA[i].height;

                    //calculate the rescaled dimensions for this pic to fit within maxWidth and maxHeight
                    newDims = this.calcNewDimensions(width, height, maxWidth, maxHeight);

                    //load the rescaled image
                    imgCont         = document.getElementById(i);
                    imgCont.width   = newDims["width"];
                    imgCont.height  = newDims["height"];
                    imgCont.src     = imgOA[i].src;
                }
            }
        }
    },
    //------------------------------------------------------------------------------
    /*---------------------------------------------------------------------------------------------
       This function rescales the image in picsOA[picId] and loads it into id = picId
    -----------------------------------------------------------------------------------------------*/
    loadScaledImage: function(picId, maxWidth, maxHeight){
        if(document.getElementById(picId) && !isNaN(maxWidth) && !isNaN(maxHeight) && maxWidth > 0 && maxHeight > 0) {
            var newDims = new Array();

            //get this pic's original width and height
            var width  = this.picsOA[picId].width;
            var height = this.picsOA[picId].height;

            //calculate the rescaled dimensions for this pic to fit within maxWidth and maxHeight
            newDims = this.calcNewDimensions(width, height, maxWidth, maxHeight);

            //load the rescaled image
            var imgCont     = document.getElementById(picId);
            imgCont.width   = newDims["width"];
            imgCont.height  = newDims["height"];
            imgCont.src     = this.picsOA[picId].src;
        }
    },
    //------------------------------------------------------------------------------
    /*---------------------------------------------------------------------------------------------
       This function rescales the image in picsOA[picId] and loads it into id = imgId
    -----------------------------------------------------------------------------------------------*/
    
    loadScaledImageToImgId: function(imgId,picId, maxWidth, maxHeight)  {
        //find which pic array picId is in
        var arrayName = '';
        for(var j in this.arrayOfPicArrays) {
            for(var i in this.arrayOfPicArrays[j]) {
                if(picId == i)  arrayName = j;
            }
        }
        //get the array containing this picId
        imgOA = this.getPicArrayObj(arrayName);


        if(document.getElementById(imgId) && !isNaN(maxWidth) && !isNaN(maxHeight) && maxWidth > 0 && maxHeight > 0) {
            var newDims = new Array();

            //get this pic's original width and height
            var width  = imgOA[picId].width;
            var height = imgOA[picId].height;

            //calculate the rescaled dimensions for this pic to fit within maxWidth and maxHeight
            newDims = this.calcNewDimensions(width, height, maxWidth, maxHeight);

            //load the rescaled image
            var imgCont     = document.getElementById(imgId);
            imgCont.width   = newDims["width"];
            imgCont.height  = newDims["height"];
            imgCont.src     = imgOA[picId].src;
        }
    },
   
    //------------------------------------------------------------------------------

    calcNewDimensions: function(width, height, maxWidth, maxHeight){
        newDims = new Array();

        //scaling factors
        var xRatio = maxWidth / width;
        var yRatio = maxHeight / height;

        //calculate the new width and height
        if(width <= maxWidth && height <= maxHeight)  {	//image does not need resizing
            newDims["width"] 	= width;
            newDims["height"] 	= height;
        }
        else if(xRatio * height < maxHeight) {
            newDims["height"] = Math.round(xRatio * height);
            newDims["width"]  = maxWidth;
        }
        else {
            newDims["width"]  = Math.round(yRatio * width);
            newDims["height"] = maxHeight;
        }
        return newDims;
    },
    //------------------------------------------------------------------------------

    showVetEnlargement: function (thumbnail, enlargementContId, enlargeImgId) {
        var enlargementCont     = document.getElementById(enlargementContId);
        var enlargementObj      = document.getElementById(enlargeImgId);
        //var enlargementLbl    = document.getElementById("staff_enlargement_label");
        var picId               = thumbnail.id;

        //display the enlargement
        enlargementObj.alt                  = thumbnail.alt;
        enlargementObj.title                = thumbnail.title;
        ImageResizer.loadScaledImageToImgId(enlargementObj.id, picId, 350, 350);
        //reset the enlargement container width to enlarged image's width + 20px
        enlargementCont.style.width         = newDims["width"] + this.enlargeContWidthOffset + "px";
        enlargementCont.style.display       = "block";
        enlargementCont.style.visibility    = "visible";
    },
    //-----------------------------------------------------------------------------

    hideVetEnlargement: function(enlargementContId) {

        var enlargementCont = document.getElementById(enlargementContId);

        //hide the enlargement
        enlargementCont.style.display     = "none";
        enlargementCont.style.visibility  = "hidden";
    },
    //---------------------------------------------------------

    getPicArrayObj: function(name) {
        switch (name) {
            case 'picsMgrOA':
                imgOA = this.picsMgrOA;
                break;
            case 'picsAdminOA':
                imgOA = this.picsAdminOA;
                break;
            case 'picsGeneralOA':
                imgOA = this.picsGeneralOA;
                break;
        }
        return imgOA;
    }
};




