var ImagePop = Class.create();
ImagePop.prototype = {
    
    initialize: function(link) {
        this.link = link;
        this.url = link.href.toString().replace(/^http:\/\/[^\/]*/, '');
        this.hijackLink();
    },
    
    hijackLink: function() {
        if (!this.link || this.link.nodeType != 1) return;
        this.link.observe('click', Event.stop);
        this.link.observe('click', this.openOverlay.bind(this));
        new Insertion.Bottom(this.link, '<span class="view-large-image">View large image</span>');
    },
    
    openOverlay: function() {
	
        new Ajax.Request(this.url, {
            method: 'GET',
            onSuccess: function(transport) {
                this.content = transport.responseText.stripScripts();
                this.metaData = transport.responseText.extractScripts().map('evalJSON')[0] || {};
                this.renderOverlay();
            }.bind(this)
        });
    },
    
    renderOverlay: function() {
        if (!this.content || !this.metaData) return;
        this.window = new Glazing.Window({
            modal: true,
            closable: true, resizable: false,
            content: this.content,
            className: 'image-overlay',
            title: this.metaData.title,
            width: this.metaData.width + 2,
            height: this.metaData.hieght + 20,
            closeAnimation: 'fade'
        });
        this.window.center();
    }
};

ImagePop.hijackLinks = function() {
    if (this._done) return;
    this._done = true;
    $$('a.popup-image').each(function(link) {new this(link); }.bind(this));
    Glazing.Overlay.color = '#ffffff';
    Glazing.Overlay.opacity = 0.95;
};
