﻿function imageShower(browserRef, zIndex, minWidth, minHeight, parentPath)
{
    this.zIndex = zIndex ? zIndex : 10;
    this.minWidth = minWidth ? minWidth : 115;
    this.minHeight = minHeight ? minHeight : 175;
    this.parentPath = parentPath ? parentPath : "../";
    var loadingImagePath = "images/imageShower/loading.gif";
    var loadingGifWidth = 32, loadingGifHeight = 32;
    var rightArrowPath = "images/imageShower/rightArrow.png";
    var rightArrowWidth = 15, rightArrowHeight = 15;

    if (browserRef.isIE && browserRef.version < 7)
        rightArrowPath = "images/imageShower/rightArrow.gif";

    this.divImageShowerMain;
    this.divISImageContainer;
    this.divISImageRightArrow;
    this.imgISShower
    this.imgISRightArrow;

    this.imgISRightArrow = document.createElement('img');
    this.imgISRightArrow.setAttribute('id', 'imgISRightArrow');
    this.imgISRightArrow.setAttribute('src', this.parentPath + rightArrowPath);

    this.showImage = showImage;
    this.showImageByMouseEvent = showImageByMouseEvent;
    this.closeImageBox = closeImageBox;
    this.createImageBox = createImageBox;
    this.resizeDivs = resizeDivs;

    function showImage(imageShowerVariable, imageURL, positionX, positionY)
    {
        this.createImageBox(positionX, positionY);

        var imgPreloader = new Image();
        imgPreloader.onload = function()
        {
            imageShowerVariable.imgISShower.src = imgPreloader.src;
            imageShowerVariable.resizeDivs(positionX, positionY, imgPreloader.width, imgPreloader.height);
        };
        imgPreloader.src = imageURL;
    }

    function showImageByMouseEvent(imageShowerVariable, imageURL, evt)
    {
        if (!evt)
            evt = window.event;

        this.showImage(imageShowerVariable, imageURL, evt.clientX + getScrollCoordinatesX(), evt.clientY + getScrollCoordinatesY());
    }

    function createImageBox(positionX, positionY)
    {
        this.closeImageBox();
        
        this.divImageShowerMain = document.createElement('div');
        this.divImageShowerMain.setAttribute('id', 'divImageShowerMain');
        
        this.divISImageContainer = document.createElement('div');
        this.divISImageContainer.setAttribute('id', 'divISImageContainer');
        
        this.divISImageRightArrow = document.createElement('div');
        this.divISImageRightArrow.setAttribute('id', 'divISImageRightArrow');
        
        this.imgISShower = document.createElement('img');
        this.imgISShower.setAttribute('id', 'imgISShower');
        this.imgISShower.setAttribute('src', this.parentPath + loadingImagePath);

        this.resizeDivs(positionX, positionY, loadingGifWidth, loadingGifHeight);

        this.divISImageContainer.appendChild(this.imgISShower);
        this.divISImageRightArrow.appendChild(this.imgISRightArrow);
        this.divImageShowerMain.appendChild(this.divISImageContainer);
        this.divImageShowerMain.appendChild(this.divISImageRightArrow);
        this.divImageShowerMain.style.display = 'none';
        this.divImageShowerMain.style.zIndex = this.zIndex;
        document.body.appendChild(this.divImageShowerMain);
    }

    function closeImageBox()
    {
        try
        {
            document.body.removeChild(this.divImageShowerMain);
        }
        catch (err)
        {
        }
    }
    
    function resizeDivs(positionX, positionY, imgWidth, imgHeight)
    {
        var width = Math.max(imgWidth, this.minWidth);
        var height = Math.max(imgHeight, this.minHeight);
        var rightArrowPaddingTop = (height - rightArrowHeight - 20);
        var imageContainerPaddingLeft = Math.round((width - imgWidth) / 2);
        var imageContainerPaddingTop = Math.round((height - imgHeight) / 2);
        var left = positionX - width - rightArrowWidth - 10;
        var top = positionY - rightArrowPaddingTop
        
        this.imgISShower.setAttribute('width', imgWidth);
        this.imgISShower.setAttribute('height', imgHeight);

        this.divISImageContainer.style.width = (width - imageContainerPaddingLeft) + 'px';
        this.divISImageContainer.style.height = (height - imageContainerPaddingTop) + 'px';
        this.divISImageContainer.style.paddingLeft = imageContainerPaddingLeft + 'px';
        this.divISImageContainer.style.paddingTop = imageContainerPaddingTop + 'px';
        this.divISImageRightArrow.style.paddingTop = rightArrowPaddingTop + 'px';

        this.divImageShowerMain.style.width = (width + rightArrowWidth + 10) + 'px';
        this.divImageShowerMain.style.height = height + 'px';
        this.divImageShowerMain.style.left = left < 0 ? '0px' : left + 'px';
        this.divImageShowerMain.style.top = top < 0 ? '0px' : top + 'px';
    }
}
