AjaxPopups = {

	timers: {},
	visible: {},
	loaded: {},
	
	getRequest: function() {
		return window.ActiveXObject
			? new ActiveXObject( "Microsoft.XMLHTTP" )
			: new XMLHttpRequest()
		;
	},

	loadGET: function( url, data, param ) {
		var req = this.getRequest();
		req.onreadystatechange = this.getHandler( req, param );
		var params = [];
		for ( var x in data )
			params.push( encodeURIComponent( x ) + "=" + encodeURIComponent( data[ x ] ) );
		req.open( "GET", url + "?" + params.join("&" ), true );
		req.send( "" );
	},

	load: function( url, data, param ) {
		var req = this.getRequest();
		req.onreadystatechange = this.getHandler( req, param );
		req.open( data ? "POST" : "GET", url, true );
		if ( data ) {
			req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
			var params = [];
			for ( var x in data )
				params.push( encodeURIComponent( x ) + "=" + encodeURIComponent( data[ x ] ) )
			req.send( params.join( "&" ) );
		} else {
			req.send();
		}
	},
	
	getHandler: function( req, param ) {
		return function() {
			if ( req.readyState == 4 )
				document.getElementById( param.id ).innerHTML = req.responseText;
		}
	},
	
	showPopup: function( id, url, data ) {
		if ( this.timers[ id ] )
			window.clearTimeout( this.timers[ id ] );
		if ( !this.visible[ id ] ) {
			document.getElementById( id ).style.display = 'block';
			this.visible[ id ] = true;
			if ( !this.loaded[ id ] ) {
				this.loadGET( url, data, { id: id } );
				this.loaded[ id ] = true;
			}
		}	
	},
	
	delayedHidePopup: function( id ) {
		if ( this.timers[ id ] )
			window.clearTimeout( this.timers[ id ] );
		this.timers[ id ] = window.setTimeout( "AjaxPopups.hidePopup( '" + id + "' )", 250 );				
	},
	
	hidePopup: function( id ) {
		document.getElementById( id ).style.display = 'none';
		this.visible[ id ] = false;
	}

}
