var ImageViewer = new Object();
ImageViewer.db = document.documentElement || document.body;

function monitorImageViewer() {
    var docURL = window.location.toString();
    var hashIndex = docURL.indexOf('#');
    if (hashIndex != -1) {
        var anchor = docURL.substring(hashIndex + 1);
        if (anchor == ImageViewer.closingHashCode) {
            closeImageViewer();
        }
    }
}

function startImageViewerMonitor() {
    ImageViewer.closingHashCode = 'closeImageViewer' + Math.floor(Math.random() * 1000000);
    ImageViewer.monitorHandler = setInterval('monitorImageViewer()', 500);
}

function registerScrollPosition() {
    if (document.documentElement && document.body.scrollTop) {
        ImageViewer.db = document.body;
    }
    with (ImageViewer) {
        ImageViewer.oldBodyScrollTopValue = db.scrollTop;
        ImageViewer.oldBodyScrollLeftValue = db.scrollLeft;
        layer.style.top = oldBodyScrollTopValue + "px";
        layer.style.left = oldBodyScrollLeftValue + "px";
    }
}

function restoreScrollPosition() {
    with (ImageViewer) {
        db.scrollTop = oldBodyScrollTopValue;
        db.scrollLeft = oldBodyScrollLeftValue;
    }
}

function resizeImageViewer() {
    var w, h;
    if (window.innerHeight || window.innerWidth) {
        w = window.innerWidth;
        h = window.innerHeight;
    } else {
        with (ImageViewer) {
            w = db.offsetWidth;
            h = db.offsetHeight;
        }
    }
    with (ImageViewer) {
        layer.style.width = w + 'px';
        layer.style.height = h + 'px';
    }
    restoreScrollPosition();
};

function startImageViewerResize() {
    registerScrollPosition();
    ImageViewer.oldBodyScrollValue = document.body.scroll;
    document.body.scroll = 'no';
    with (ImageViewer) {
        ImageViewer.oldBodyOverflowValue = db.style.overflow;
        db.style.overflow = 'hidden';
    }
    resizeImageViewer();
    ImageViewer.resizeHandler = setInterval('resizeImageViewer()', 50);
}

function openImageViewer(title, url) {
    if (initializeImageViewer()) {
        startImageViewerResize();
        startImageViewerMonitor();
        var flashVars = 'imageTitle=' + escape(title) + '&imageURL=' + escape(url) + '&closingHashCode=' + escape(ImageViewer.closingHashCode);
        var content = '<object id="imageViewer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
            + 'width="100%" height="100%"'
            + 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
            + '<param name="movie" value="/flash/imageviewer.swf" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="high" /><param name="scale" value="noscale" /><param name="salign" value="lt" /><param name="wmode" value="transparent" />'
            + '<param name="FlashVars" value="' + flashVars + '" />'
            + '<embed src="/flash/imageviewer.swf" loop="false" menu="false" quality="high" scale="noscale" salign="lt" wmode="transparent" '
            + 'width="100%" height="100%" name="popup" align="middle"'
            + 'name="imageViewer"'
            + 'play="true"'
            + 'loop="false"'
            + 'quality="high"'
            + 'FlashVars="' + flashVars + '"'
            + 'type="application/x-shockwave-flash"'
            + 'pluginspage="http://www.macromedia.com/go/getflashplayer">'
            + '</embed>'
            + '</object>';
        with (ImageViewer) {
            layer.innerHTML = content;
            layer.style.visibility = '';
        }
    } else {
        window.open(url);
    }
}

function closeImageViewer() {
    clearInterval(ImageViewer.resizeHandler);
    clearInterval(ImageViewer.monitorHandler);
    if (ImageViewer.layer) {
        with (ImageViewer) {
            layer.style.visibility = 'hidden';
            layer.innerHTML = '';
            layer.style.width = "0px";
            layer.style.height = "0px";
            layer.style.top = "0px";
            layer.style.left = "0px";
            document.body.scroll = oldBodyScrollValue;
            db.style.overflow = oldBodyOverflowValue;
        }
    }
}

function initializeImageViewer() {
    if (ImageViewer.layer) {
        return ImageViewer.layer;
    } else if (DetectFlashVer(7, 0, 0)) {
        ImageViewer.layer = document.createElement('div');
        with (ImageViewer) {
            layer.id = 'imageViewerLayer';
            layer.style.visibility = 'hidden';
            layer.style.position = 'absolute';
            layer.style.zIndex = 1000;
            document.body.appendChild(layer);
            return layer;
        }
    } else {
        return null;
    }
}

window.onload = initializeImageViewer;