﻿function ResizeLargeImages() {
    var maxheight = 580;
    var maxwidth = 480;
    if ($("div_Story_Content")) {
        var imgs = $("div_Story_Content").find("img");
        for (var p = 0; p < imgs.length; p++) {
            var w = parseInt(imgs[p].offsetWidth);
            var h = parseInt(imgs[p].offsetHeight);
            var hasBigger = false;

            if (w > maxwidth) {
                hasBigger = true;
                h = (maxwidth / w) * h;
                w = maxwidth;
                imgs[p].height = h;
                imgs[p].width = w;
                imgs[p].style.height = h + "px";
                imgs[p].style.width = w + "px";
            }

            if (h > maxheight) {
                hasBigger = true;
                imgs[p].width = (maxheight / h) * w;
                imgs[p].height = maxheight;
                imgs[p].style.height = maxheight + "px";
                imgs[p].style.width = (maxheight / h) * w + "px";
            }

            if (hasBigger) {
                imgs[p].style.cursor = "pointer";
                imgs[p].title = "Nhấn để xem ảnh to hơn";
                if (imgs[p].getAttribute('class')) {
                    if (imgs[p].getAttribute('class') != 'noautolink') {
                        imgs[p].onclick = function () {
                            var iw = window.open(this.src, 'ImageViewer', 'resizable=1,scrollbars=1,width=600,height=600');
                            iw.title = 'Nhấn giữa ảnh để phóng to xem ảnh đúng cỡ';
                            iw.focus();
                        };
                    }
                }
                else {
                    imgs[p].onclick = function () {
                        var iw = window.open(this.src, 'ImageViewer', 'resizable=1,scrollbars=1,width=600,height=600');
                        iw.title = 'Nhấn giữa ảnh để phóng to xem ảnh đúng cỡ';
                        iw.focus();
                    };
                }

            }
        }
    }
}

function initialize() {

    ResizeLargeImages();
}

$(window).load(function () {
    initialize();
});
