var PopupDiv = Class.create();
PopupDiv.obj = null;
PopupDiv.hide = function() {
	if (PopupDiv.obj != null)
		PopupDiv.obj.hide();
}
PopupDiv.prototype = {
  	initialize: function(url) {
		PopupDiv.hide();
		PopupDiv.obj = this;
		
		if ($('activity_indicator') != null)
			$('activity_indicator').title = 'hidden';
		
		this.toggleSelects('hidden');
		this.hhide = this.hide.bindAsEventListener(this);
		
		if ($('popup_div') == null)
			new Insertion.Top(document.body, '<div id="popup_div" style="position: absolute; top: 200px;"><img src=/themes/images/ajax_popupdiv.gif alt=loading width=126 height=22></div>');
		else {
			$('popup_div').update('<img src=/themes/images/ajax_popupdiv.gif alt=loading>');
			$('popup_div').show();
		}
		this.center('_no_trans');
		
		this.hcenter = this.center.bindAsEventListener(this);
		new Event.observe(window, 'resize', this.hcenter);
		new Event.observe(window, 'scroll', this.hcenter);

		var obj = this;
		new Ajax.Updater('popup_div', url, {
			method: 'get', 
			evalScripts: true,
			onComplete: function() {
				$('popup_div').hide();
				obj.center('_no_trans');
				$('popup_div').show();
				new Draggable('popup_div', { handle: 'popup_div_handle' });
			}
		});
	},
	
	toggleSelects: function(state) {
		if (Prototype.Browser.IE && document.getElementsByTagName) {
			var selects = document.getElementsByTagName('select');
			
			for (i = 0; i < selects.length; i++)
				Element.setStyle(selects[i], { visibility: state });
		}
	},
	
	hide: function() {
		if ($('popup_div') != null) {
			new Event.stopObserving(window, 'resize', this.hcenter);
			new Event.stopObserving(window, 'scroll', this.hcenter);
			new Event.stopObserving('popup_div_mask', 'click', this.hhide);
			
			$('popup_div').hide();
			$('popup_div').update('');
			$('popup_div_mask').hide();
			this.toggleSelects('visible');
			
			PopupDiv.obj = null;
			
			if ($('activity_indicator') != null)
				$('activity_indicator').title = '';
		}
	},
	
	center: function() {
		if ($('popup_div') == null) return;
		
		this.showMask();
		
		var dim_view = document.viewport.getDimensions();
		var dim_win = $('popup_div').getDimensions();
		if (dim_view.width < dim_win.width)
			x = this.getScrollX() <= dim_win.width - dim_view.width ? 0 : this.getScrollX() - (dim_win.width - dim_view.width);
		else
			x = Math.round(dim_view.width / 2 - dim_win.width / 2) + this.getScrollX();
		if (dim_view.height < dim_win.height)
			y = this.getScrollY() <= dim_win.height - dim_view.height ? 0 : this.getScrollY() - (dim_win.height - dim_view.height);
		else
			y = Math.round(dim_view.height / 2 - dim_win.height / 2) + this.getScrollY();
		
		if (arguments.length == 1 && arguments[0] == '_no_trans')
			$('popup_div').setStyle({ left: x + 'px', top: y + 'px' });
		else {
			var queue = Effect.Queues.get('popup_div');
			queue.each(function(e) { e.cancel() });
			new Effect.Move('popup_div', { duration: 0.5, 'x': x, 'y': y, mode: 'absolute', queue: { position: 'end', scope: 'popup_div' } })
		}
	},
	
	showMask: function() {
		if ($('popup_div_mask') == null)
			new Insertion.Top(document.body, '<div id="popup_div_mask" style="display: none"></div>');
		if (!$('popup_div_mask').visible())
			new Event.observe('popup_div_mask', 'click', this.hhide);

		var dim_body = $(document.body).getDimensions();
		var dim_view = $(document.viewport).getDimensions();
		$('popup_div_mask').setStyle({ width: dim_view.width > dim_body.width ? dim_view.width : dim_body.width + 'px' });
		$('popup_div_mask').setStyle({ height: dim_view.height > dim_body.height ? dim_view.height : dim_body.height + 'px' });
		
		$('popup_div_mask').show();
	},
	
	getScrollY: function() {
		var sy = 0;
		if (document.documentElement && document.documentElement.scrollTop)
			sy = document.documentElement.scrollTop;
		else if (document.body && document.body.scrollTop) 
			sy = document.body.scrollTop; 
		else if (window.pageYOffset)
			sy = window.pageYOffset;
		else if (window.scrollY)
			sy = window.scrollY;
		return sy;
	},
	
	getScrollX: function() {
		var sx = 0;
		if (document.documentElement && document.documentElement.scrollLeft)
			sx = document.documentElement.scrollLeft;
		else if (document.body && document.body.scrollLeft) 
			sx = document.body.scrollLeft; 
		else if (window.pageXOffset)
			sx = window.pageXOffset;
		else if (window.scrollX)
			sx = window.scrollX;
		return sx;
	}
}
