nmdgf.widgets.Lightbox = {

	id : 'lightbox-panel',

	panelBody : null,

	panel : null,

	activeLink : null,

	initialize : function() {
		var links = nmdgf.query('.sc-lightbox');
		var that = this;
		for ( var idx in links) {
			nmdgf.addListener(links[idx], 'click', function(scopedLink) {
				return function(e) {
					nmdgf.stopEvent(e);
					that.click(scopedLink);
				};
			}(links[idx]));
		}
	},

	click : function(link) {
		nmdgf.widgets.LoadingPanel.show();
		this.activeLink = link;
		if(this.panel === null) {
			this.createPanel();
		}
		this.setContext();
		this.loadContent();
	},

	loadContent : function() {
		nmdgf.ajax('GET', this.activeLink.href, {
			success: function(r) {
				nmdgf.widgets.LoadingPanel.hide();
				this.panel.setBody(r.responseText);
				this.panel.show();
			}, scope: this
		});
	},

	setContext : function() {
		this.panel.setHeader(this.activeLink.title);
		var height = this.activeLink.getAttribute('lbheight') || '400';
		var width = this.activeLink.getAttribute('lbwidth') || '500';
		this.panel.cfg.setProperty('height', height+'px');
		this.panel.cfg.setProperty('width', width+'px');
		this.panel.center();
	},

	createPanel : function() {
		var modal = this.activeLink.getAttribute('modal') === 'true' || false;

		this.panel = new YAHOO.widget.Panel(this.id, {
			fixedcenter : true,
			constraintoviewport : true,
			underlay : 'shadow',
			close : true,
			visible : false,
			draggable : true,
			dragOnly : true,
			modal : modal,
			effect: {effect:YAHOO.widget.ContainerEffect.SLIDE,duration:0.25}
		});

		this.panel.cfg.queueProperty("keylisteners", new YAHOO.util.KeyListener(document, { keys:27 },
			{
				fn:this.panel.hide,
				scope:this.panel,
				correctScope:true
			}
		));
		this.panel.setHeader('');
		this.panel.setBody('');
		this.panel.render(document.body);
	}

};
nmdgf.registerWidget(nmdgf.widgets.Lightbox);

