leafm.plugins.ajax_window = new Class({
	_el: '',
	_ajax_uri: '',
	_id: '',
	_class: 'ajax_window',
	_styles: {
		position:'absolute',
		border:'1px solid gray',
		top:100,
		left:10
	},
	_draggable: true,
	_coord: {x:10, y:100},
	_size: {x:0, y:0},
	_state: '', // min, max, full
	
	/*
		Function: initialize
			Initialization that takes sets the window. You can set the source to
			be HTML or a URI location.
			
			Automatically add events to anything that has a class of ctrl_close.
		
		Parameters:
			id
			html
			options - (object / NULL)
						{
							'class': [], // array or string of new classes to add,
							draggable: true, // turns on if draggable
							events: {
								before_load: function() {
								},
								
								before_close: function() {
								},
								
								after_close: function() {
								}
							},
							ajax_uri: '' // uri source for refresh, can be optional
						}
	 */
	initialize: function(id, html, options) {
		var obj = this;
		
		options = $H(options);
		
		// If ajax_uri is not required
		if(options && options.get('ajax_uri')) {
			obj._ajax_uri = options.get('ajax_uri');
		}
		
		// If id is specified, set it
		if($type(id) == 'string') {
			obj._id = id;
		}		
		
		// If class options are set, add it
		if(options && $type(options.get('class') == 'string')) {
			obj._class += ' ' + options.get('class');
		}		
		
		// If events are specified
		obj.events = $H(obj.events);
		if(options && options.get('events')) {
			var new_events = $H(options.get('events'));
			new_events.each(function(new_event, key) {
				obj.events.set(key, new_event);
			});
		}
		
		obj.load.bind(obj, html)();
	},
	
	center: function() {
		var obj = this;
		parent_el = obj._el;
	},
	
	close: function() {
		var obj = this;
		parent_el = obj._el;

		obj.events.get('before_close')();		
		parent_el.destroy();
		obj.events.get('after_close')();
	},
	
	events: {
		after_close: function() {			
		},
		
		before_close: function() {			
		},
		
		after_load: function() {			
		},
		
		before_load: function() {			
		},
		
		after_refresh: function() {			
		},
		
		before_refresh: function() {			
		}
	},
	
	load: function(html) {
		var obj = this;
		
		if(!$(obj._id)) {
			var new_el = new Element('div')
				.setProperty('class', obj._class)
				.setStyles(obj._styles);
			
			obj._el = new_el;
			
			if(html != '') {
				new_el.set('html', html);
				
				if(obj._draggable) {
					new Drag(new_el, {
						handle: new_el.getElement('div.header')
					});					
				}
				
				new_el.inject(document.body);
				obj.update.bind(obj)();
				obj.events.get('after_load')();	
			} else if(obj._ajax_uri != '') {
				obj.refresh.bind(obj)();
			}
		} 
	},
	
	// Only works if the ajax_uri is set
	refresh: function() {		
		var obj = this;
		var parent_el = obj._el;
		
		if(obj._ajax_uri != '') {
			new Request({url: obj._ajax_uri})
				.addEvent('complete', function(resp) {
					try {
						if($type(JSON.decode(resp)) == 'object') {
							var results = JSON.decode(resp);
							
							switch(results.status.key) {
								case 'FORM_LOADED':
									var html = results.status.data.html;
									parent_el.set('html', html);

									if(obj._draggable) {
										new Drag(parent_el, {
											handle: parent_el.getElement('div.header')
										});					
									}
									
									parent_el.inject(document.body);
									obj.update.bind(obj)();				
									obj.events.get('after_load')();	
									break;
									
								default:
									break;
							}
						}
					} catch(e) {
						alert(e);
					}
				})
				.send();
		}
	},
	
	minimize: function() {		
	},
	
	update: function() {
		var obj = this;
		var parent_el = obj._el;
		
		parent_el.getElements('.ctrl_close').addEvent('click', function() {
			obj.close.bind(obj)();
		});
	}
});
