var Dialog = {
	el: $('#lightbox'),
	render: function(title) {
		// closeText is global
		this.el.dialog({
			dialogClass: '',
			closeText: closeText,
			position: 'center',
			resizable: false,
			draggable: false,
			height: 'auto',
			title: title,
			width: 'auto',
			modal: true,
			show: 'fade',
			hide: 'fade',
			zIndex: 1001
		});		      
	},
	center: function() {
		this.el.parent('div').position({
			my: "center center",
			at: "center center",
			of: window
		});
	},
	setHtml: function(data) {
		this.el.html(data);
	},
	setTitle: function(title) {
		this.el.dialog('option', 'title', title);
	},
	getTitle: function() {
		return	this.el.dialog('option', 'title');
	}
}


$(document).ready(function(){
	var msg = '';
	var requestCache = false;
	$('.popup').each(function(){
		var href = $(this).attr('href');
		if(href.match(/^(.*)\?(.*)$/)) href = href+'&type='+ajaxPageType;
		else href = href+'?type='+ajaxPageType;
		$(this).attr('href', href);

		$(this).click(function(ev){
			ev.preventDefault();
			if ($(this).parent().attr('id') === 'google') msg = googleMapLoadingMsg;
			else msg = '';
			var url = $(this).attr('href');
			var title = $(this).attr('title');

			if (url.match(/\.[jpgneif]{3}/)) {
				requestCache = url; 
				var data = '<div><img src="' + url + '" title="' + title + '" alt="' + title + '" /></div>';
				Dialog.setHtml(data);
				Dialog.render(title);
				Dialog.center();
			} else if (requestCache !== url){
				requestCache = url; 
				$.ajax({
					url: $(this).attr('href'),
					beforeSend: function(){
						$('#loading').remove();
						$('<div id="loading">' + msg + '</div>').appendTo('body');
						$('#loading').dialog({
							dialogClass: 'loading',
							closeText: closeText,
							position: 'center',
							resizable: false,
							draggable: false,
							height: 50,
							width: 230,
							modal: true,
							show: 'fade'
						});		      
						$('#loading').bind('dialogclose',function(){
							Dialog.render(title);
						});
					},
					success: function(data){
						Dialog.setHtml(data);
					},
					complete: function(){
						$('#loading').dialog('close');
					}
				});
			} else {
				$('#lightbox').dialog('open');
			}
		});
	});
});


