addEvent = function(element, evt, handler)
{
    if (element.attachEvent) {
        element.attachEvent('on' + evt, handler);
    } else if (element.addEventListener) {
        element.addEventListener(evt, handler, true);
    }
}
checkPopups = function(evt)
{
    if (!evt) {
        evt = window.event;
    }
    oLinks = document.getElementsByTagName('a');
    l = oLinks.length;
    i = 0;
    while (i < l) {
        oLink = oLinks[i];
        if (rel = oLink.getAttribute('rel')) {
            if (rel.split(/ /)[0] == 'popup') {
                addEvent(oLink, 'click', doPopup);
            }
        }
        i++;
    }
    evt.cancelBubble = true;
    evt.returnValue = false;
    return false;
}
doPopup = function(evt) {
    if (!evt) {
        evt = window.event;
    }
    if (evt.cancelable) {
        evt.preventDefault();
    }
    if (!evt.shiftKey && !evt.ctrlKey) {
        trigger = evt.target || evt.srcElement;
        while (trigger.tagName.toLowerCase() != 'a') {
            trigger = trigger.parentNode;
        }
        img = {
            src: trigger.href,
            width: Number(trigger.getAttribute('rel').split(/ /)[1]),
            height: Number(trigger.getAttribute('rel').split(/ /)[2])
        }
        if (img.width > window.screen.availWidth) {
            img.height = img.height / img.width * window.screen.availWidth;
            img.width = window.screen.availWidth;
        }
        if (img.height > window.screen.availHeight) {
            img.width = img.width / img.height * window.screen.availHeight;
            img.height = window.screen.availHeight;
        }
        myImg = window.open('', 'myImg', 'status=no, width=' + img.width + ', height=' + img.height);
        myImg.resizeTo(img.width, img.height);
        if (myImg.innerHeight) {
            diff = img.height - myImg.innerHeight;
        } else if (myImg.document.body.clientHeight) {
            diff = img.height - myImg.document.body.clientHeight;
        } else {
            diff = 0;
        }
        myImg.resizeTo(img.width, img.height + diff);
        myImg.document.open();
        myImg.document.write('<html><head><title>' + document.title + '</title></head><body style="margin: 0;"><img src="' + img.src + '" width="' + img.width + '" height="' + img.height + '" /></body></html>');
        myImg.document.close();
        myImg.focus();
        evt.cancelBubble = true;
        evt.returnValue = false;
        return false;
    }
}
var myImg;
addEvent(window, 'load', checkPopups);
