ImageResizer = {
    picsOA                  : new Array(),  //associative array of the picture objects
    enlargeContWidthOffset  : null,           //enlargement container width minus enlarged image width

    //Constructor
    init: function(picsA)    {
        //cache the images into an array
        //picsA is an associative array of the pic src paths
        //i is the id of the pics img element
        for(var i in picsA) {
            this.picsOA[i]       = new Image;
            this.picsOA[i].src   = picsA[i];
        }

        this.enlargeContWidthOffset = 13;
    },
    /*---------------------------------------------------------------------------------
     * Accessor methods
    ----------------------------------------------------------------------------------*/
    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(maxWidth, maxHeight) {
        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 this.picsOA) {
                if(document.getElementById(i)) { //check if the image element exists
                    //store this pic's original width and height
                    width  = this.picsOA[i].width;
                    height = this.picsOA[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     = this.picsOA[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)  {
        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  = 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(imgId);
            imgCont.width   = newDims["width"];
            imgCont.height  = newDims["height"];
            imgCont.src     = this.picsOA[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";
    }
};



