function imageHandler()
{
    this.ResizeImage = function (img, parentSelector)
    {
        if (img)
        {
            var $img = $(img);
            if ($img.size() == 1)
            {
                var imgWidth = $img.width();
                var imgHeight = $img.height();
                var $container;
                if (parentSelector)
                {
                    $container = $img.parents(parentSelector + ":first");
                }
                else
                {
                    $container = $img.parent();
                }

                var containerWidth = $container.width();
                var containerHeight = $container.height();

                _resize($img, imgWidth, imgHeight, containerWidth, containerHeight);
                $img.removeClass('hidden');
                $img.css("cursor", "pointer");
                if(!SkipXAlign($img)) {
                    $container.css("text-align", "center");
                }
                $container.css("background-image", "none");
            }
        }
        return this;
    }

    function _resize($img, imgWidth, imgHeight, containerWidth, containerHeight)
    {
        if (imgWidth <= containerWidth && imgHeight <= containerHeight)
        {
            $img.css("margin-top", (containerHeight - imgHeight) / 2);
        }
        else
        {
            $img.css("margin-top", "0");
            if (imgWidth > containerWidth && imgHeight < containerHeight)
            {
                $img.css("width", "100%");
            }
            if (imgHeight > containerHeight && imgWidth < containerWidth)
            {
                $img.css("height", "100%");
            }
            if (imgWidth > containerWidth && imgHeight > containerHeight)
            {
                if (imgWidth / containerWidth > imgHeight / containerHeight)
                {
                    $img.css("width", "100%");
                    var d = containerWidth / imgWidth;
                    imgHeight = imgHeight * d;
                    var marginTop = (containerHeight - imgHeight) / 2;
                    if (!SkipYAlign($img))
                    {
                        $img.css("margin-top", marginTop);
                    }
                }
                else
                {
                    $img.css("height", "100%");
                }
            }
            if (imgWidth <= containerWidth && imgHeight <= containerHeight) {
                if (!SkipXAlign($img)) {
                    $img.css("margin-left", "auto").css("margin-right", "auto");
                }
                var d = containerWidth / imgWidth;

                imgHeight = imgHeight * d;
                var marginTop = (containerHeight - imgHeight) / 2;
                if (!SkipYAlign($img))
                {
                    $img.css("margin-top", marginTop);
                }
            }
        }
    }

    function SkipYAlign($img)
    {
        var attr = $img.attr("disableVerticalAlign");
        return isTrue(attr);
    }

    function SkipXAlign($img) {
        var attr = $img.attr("disableXAlign");
        return isTrue(attr);
    }

    function isTrue(attr) {
        return attr && attr.toLowerCase() == "true";
    }
}
ImageHandler = new imageHandler();
