/**
 * UI popup window library with AJAX 
 */
(function(prefix){
	var zindex;	
	var button_id;
	var div_id;
	var URI;
	var parent;

	var content;
	
	
	
	function uiWindow(button_id, div_id, URI, parent, content)
	{
		this.button_id 	= button_id;
		this.div_id 	= div_id;
		this.URI	= URI;
		this.parent = parent;
		this.content = content;
	
		this.create = create; 
		this.show = show;
	}

	
	function create()
	{		
		var self = this;
		
		$('#'+self.button_id).live("click", function()
		{	
			$('<div id="'+self.div_id+'" class="popup2"><a class="dialog_close"></a>' +
				'<div id="'+self.div_id+'_content"></div></div>').insertBefore('#'+self.parent);

			$('<div id="'+self.div_id+'_black" class="black_wnd"></div>').insertBefore('#'+self.div_id);
			
			this.zindex = parseInt($('#'+self.div_id).css('z-index'));
			$('#'+self.div_id+'_black').css('z-index', self.zindex - 1);
		
			if (self.content)
			{
				$('#' + self.div_id + '_content').html(self.content);
				return;
			}
			else	
			$.ajax({
				url: self.URI,
				success: function(result){
					$('#' + self.div_id + '_content').html(result);
				},
				error: function(err){
					alert(err);
				}
			});

		});
		
		function closeWindow()
		{
			
			$('#' + self.div_id + '>.dialog_close' ).die();
			
			$('#' + self.div_id + '_black' ).remove();
			$('#' + self.div_id ).remove();
		}
		
		
		$('#'+self.div_id+"_black").live("click", closeWindow);				
		$('#'+self.div_id+">.dialog_close").live("click", closeWindow);
	}
	
	
	function show()
	{		
		var self = this; 
		
		$('<div id="'+self.div_id+'" class="popup2">'+
				'<a class="dialog_close" href="#"></a>' +
				'<div id="'+self.div_id+'_content"></div>'+
		  '</div>').insertBefore('#'+self.parent);
		$('<div id="'+self.div_id+'_black" class="black_wnd"></div>').insertBefore('#'+self.div_id);
	
		this.zindex = parseInt($('#'+self.div_id).css('z-index'));
		$('#'+self.div_id+'_black').css('z-index', self.zindex - 1);
		
		if (self.content)
		{
			$('#' + self.div_id + '_content').html(self.content);
		}
		else		
		$.ajax({
			url: self.URI,
			success: function(result){
				$('#' + self.div_id + '_content').html(result);
			},
			error: function(err){
				alert(err);
			}
		});
		
		function closeWindow()
		{
			
			$('#' + self.div_id + '>.dialog_close' ).die();
			
			$('#' + self.div_id + '_black' ).remove();
			$('#' + self.div_id ).hide('fast', function(){$('#' + self.div_id ).remove();});
		}
		
				
		$('#'+self.div_id+">.dialog_close").live("click", closeWindow);
		$('#'+self.div_id+"_black").live("click", closeWindow);
	}
	

	if (!window[prefix]) 
		window[prefix] = uiWindow;	
})("uiWindow");
