var Dialog = Class.create();

Dialog.prototype = {
	
	dialog: false,
	bground: false,
	open: false,
	
	initialize: function() {
    
    // Set up dialog and grab contents
    this.dialog = document.createElement('div');
    Element.extend( this.dialog );
    this.dialog.setStyle( {
      position: 'absolute',
      top: '100px',
      zIndex: 10 
    } );
    this.dialog.addClassName('dialog');
    
  	// Setup background
    this.bground = document.createElement("div");
		Element.extend( this.bground );
		this.bground.id = "alert-message";
		this.bground.setStyle( {
			position: 'absolute',
			top: '0px',
			left: '0px',
			zIndex: 5,
			backgroundColor: '#000',
			opacity: '.80'
		});
		
  },
	
	show: function() {
		if( ! this.open ) {
			document.body.appendChild(this.dialog);
			document.body.appendChild(this.bground);
			this.center();
			this.open = true;
			Event.observe(window,'resize', function() { dialog.center(); });
		}
	},
	
	hide: function() {
		if( this.open ) {
			this.dialog.remove();
			this.bground.remove();
			this.open = false;
			Event.stopObserving(window,'resize',function() { dialog.center(); });
		}
	},
	
	center: function() {
		
		// get center of page
  	var pageCenter = ( document.body.clientWidth + document.body.scrollLeft ) / 2;
  	if ( navigator.userAgent.toLowerCase().indexOf("safari")!=-1 ) {
    	  pageCenter = ( document.body.offsetWidth / 2 );
    }
    
    // position dialog
    /*var ticks = 0;
    var width = parseInt( this.dialog.getWidth() );
    while( width <= 0 && ticks < 10000 ) {
    	width = parseInt(this.dialog.getWidth());
      ticks++;
    }*/
    var width = 675;
    var scroll = getScrollXY();
    this.dialog.style.left = pageCenter - ( width / 2 ) + 'px';
    this.dialog.style.top = (100 + scroll.y)+'px';
    this.bground.style.width = ( document.body.clientWidth + scroll.x )+'px';
    this.bground.style.height = ( document.body.clientHeight + scroll.y )+'px';
    
  },
  
  update: function(url,options) {
  	
  	if( !options ) { options = new Object(); }
  	
  	if(! options.method ) {
  		options.method = 'GET';
		}
  	
  	var onComplete = options.onComplete || Prototype.emptyFunction;
    options.onComplete = function(transport, param) {
      this.dialog.show();
      onComplete(transport, param);
    }
  	
  	new Ajax.Updater(this.dialog,url);
  	
  }
	
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return { x: scrOfX, y: scrOfY };
}