//gettext parsed


if (eval("typeof(get" + "text) == 'undefined';")) {
	eval('var get' + 'text = function (str) { return str; };');
}

var Window = Class.create ({
	options : {
		id: 'wnd',
		isDraggable: false,
		top: 150,
		onCreate: function () {},
		onSubmit: function () { return true; },
		onDestroy: function () { return true; }
	},

	initialize: function (options) {
		this.setOptions(options);
	},
	
	setTitle: function (title) {
		Selector.findChildElements(this.wnd, ['.Window-title'])
			.first().update(title);
	},

	setOptions: function (options) {
		Object.extend (this.options, options || {});
	},
	
	openWindow: function (url, parameters, method) {
		var self = this;

		if (this.isLocked ())
			return false;

		parameters  = parameters || '';
		Object.isString (parameters)
			?	parameters += '&h='+new Date ().getTime ()
			:	parameters.h = new Date ().getTime ();
		
		new Ajax.Request (url, {
			method: method || 'get',
			parameters: parameters,
			
			onException: function (e) {
				alert ('openWindow.onException: '+ e.dispatchException ());
			},
			onCreate: function () {	
				self.lock ();
				self._createWindow ();
			},
			onSuccess: function (xhr) {
				setTimeout (function () {
					var response  = xhr.responseText.isJSON () ? xhr.responseText.evalJSON () : {errno: 99};
					self.response = {
						title: '',
						body: '',
						toolbar: '',
						styles: {},
						classes: '',
						errno: 0,
						errstr: false,
						ev_destroy: false,
						ev_create: false,
						ev_response: false
					};
					Object.extend (self.response, response || {});
					
					if (self.response.errno == 0) {
						self._generateContents ();
					} else {
						self._generateContentsError ();
					}
					self._initializeEvents ();
				}, 850);
				self.unlock ();
			}
		});

	},
	
	destroy: function () {
		this.wnd.remove ();
		this.wnd = null;
	},

	disable: function (type) {
		$$ ('.Window-' + type).each (function (element) {
			element.disable ();
		});
	},

	enable: function (type) {
		$$ ('.Window-' + type).each (function (element) {
			element.enable ();
		});
	},

	_initializeEvents: function () {
		$$ ('.Window-destroy').each (function (element) {
			Event.observe (element, 'click', function (event) {
				var destroy = true;
				if (this.response.ev_destroy)
					eval (this.response.ev_destroy);
				destroy &= this.options.onDestroy(this);
				if (destroy)
					this.destroy ();
			}.bind (this));
		}.bind (this));
		
		$$ ('.Window-response').each (function (element) {
			Event.observe (element, 'click', function (event) {
				var destroy = true;
				if (this.response.ev_response)
					eval (this.response.ev_response);
				destroy &= this.options.onSubmit(this);
				if (destroy)
					this.destroy ();
			}.bind (this));
		}.bind (this));
		
		$$ ('.Window-toolbar').each (function (element) {
			var elements = element.immediateDescendants ();
			elements.each (function (el) {
				el.observe ('click', function () {
					elements.each (function (el2) {
						el2.disable ();
					});
				}.bind (this));
			}.bind (this));
		}.bind (this));

		document.observe ('Window:enable', function (event) {
			this.enable (event.memo.type);
		}.bind (this));

		document.observe ('Window:disable', function (event) {
			this.disable (event.memo.type);
		}.bind (this));
		
	},

	_getScrollTop: function() {
		return (window.pageYOffset) ? window.pageYOffset : 
			(document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop:document.body.scrollTop;
	},
	
	_createWindow: function () {
		if (!this.wnd) {
			this.wnd = new Element ('div', {id: this.options.id, 'class':'Window-contener'});
			this.wnd.update ('<h2 style="margin-left:5px">' + 'Chargement...' + '</h2>');
			this.wnd.setOpacity (0.0);
			$ (document.body).insert (this.wnd);
		} else 
			this._createLoading ();
		this.wnd.setStyle ({'top': (this._getScrollTop () + this.options.top) + 'px'});
		this.wnd.fadeIn ();
	},
	
	_generateContentsError: function () {
		this.response.title   = 'Oups, ...';
		this.response.body    = new Element ('h3')
		.update (this.response.errstr || 'Erreur indéterminée,<br/>veuillez réessayer plus tard.');
		this.response.toolbar = '<input type="button" class="Window-destroy" value="Annuler/Fermer"/>';
		
		this._generateContents ();
	},
	
	_createLoading: function () {
		if (this.title)
			this.title.insert (this.loading = 
												 new Element ('p', {'style':'position:absolute; top:10px; right:6px;'}).
												 update ('Chargement...'));
	},
	
	_generateContents: function () { var self = this;
		this.wnd.setStyle (this.response.styles);
		$A(this.response.classes.split(' ')).each(function (e) {
			self.wnd.addClassName(e);
		});
		
		this.title   = new Element ('div', {'class':'Window-title'}).update (this.response.title);
		this.body    = new Element ('div', {'class':'Window-body'}).update (this.response.body);
		this.toolbar = new Element ('div', {'class':'Window-toolbar'}).update (this.response.toolbar);
		
		this.wnd.update (this.title);
		this.wnd.insert (this.body);
		this.wnd.insert (this.toolbar);
		if(this.options.isDraggable){
			this.draggable();
		}
		
		if (this.response.ev_create)
			eval (this.response.ev_create);
		
		this.options.onCreate(this);
	},

	lock: function () {	this.locked = true; },
	
	unlock: function () {	this.locked = false; },
	
	isLocked: function () {	if (!this.locked)	return false;	return true; },
	
	draggable: function () {
		new Draggable(this.options.id, {
			scroll: window,
			handle: 'Window-title'
		});
		this.title.setStyle({cursor: 'move'});
	}
});
