var Portfolio = function(parent) {

    var body = document.documentElement || document.body,
        box = document.getElementById('lightbox'),
        image = document.createElement('IMG'),
        prevButton  = box.getElementsByTagName('A')[0],
        nextButton  = box.getElementsByTagName('A')[1],
        closeButton = box.getElementsByTagName('A')[2],
        next = null,
        prev = null;

    for (n = parent.firstChild; n != null; n = n.nextSibling)
        if (n.nodeName == 'A') {
            n._href = n.href;
            n.href = 'javascript:void(0);';
            n.onclick = show;
        }

    function show() {
        var y = window.pageYOffset || body.scrollTop;
        box.style.top = y + 'px';
        box.style.height = (window.innerHeight || body.clientHeight) + 'px';

        body.style.overflow = 'hidden';
        if (window.scroll)
            window.scroll(0, y);

        load(this);
    }

    function load(target) {
        box.className = 'loading';

        next = target;
        do next = next.nextSibling; while (next && next.nodeName != 'A');
        nextButton.style.display = next ? 'block' : 'none';

        prev = target;
        do prev = prev.previousSibling; while (prev && prev.nodeName != 'A');
        prevButton.style.display = prev ? 'block' : 'none';

        image.onload = onload;
        image.onreadystatechange = function() {
            if (this.readyState && this.readyState.match(/complete|loaded/))
                this.onload();
        }

        image.src = target._href;
    }

    function onload() {
        box.appendChild(image);
        box.className = 'loaded';
        image.style.width = 'auto';
        image.style.height = 'auto';
        image.style.marginLeft = '-' + (image.clientWidth / 2) + 'px';
        image.style.marginTop = '-' + (image.clientHeight / 2) + 'px';
        closeButton.style.top = image.offsetTop - 23 + 'px';
        closeButton.style.left = image.offsetLeft + image.clientWidth + 10 + 'px';
    }

    nextButton.onclick = function() {
        if (next)
            load(next);
    }

    prevButton.onclick = function() {
        if (prev)
            load(prev);
    }

    closeButton.onclick = function() {
        box.className = '';
        box.removeChild(image);

        var y = window.pageYOffset || body.scrollTop;
        body.style.overflow = 'visible';
        if (window.scroll)
            window.scroll(0, y);
    }
}