if (!OB)
{
	var OB = {};
}
(function(){
OB.Cache = {};

OB.Diablogs = new Array();
/**
 * Diablog class
 * @constructor
 * @author Hadrien Lanneau
 */
OB.Diablog = function(params)
{
	this.dialog;
	
	this.init(params);
	
	this.noEnter = false;
	
	this.onAjaxLoaded = new YAHOO.util.CustomEvent(
		'onAjaxLoaded',
		this
	);
	
	OB.Diablogs.push(this);
};
// Text label by default
OB.Diablog.LABEL_CLOSE	= 'close';
// Closing dialog message type
OB.Diablog.TYPE_OK			= 'diablog_ok';
OB.Diablog.TYPE_ERROR		= 'diablog_error';

OB.Diablog.prototype =
{
	/**
	 * Initialize
	 */
	init: function(params)
	{
		if (!params)
		{
			params = {};
		}
		// modal
		this.modal = document.createElement('div');
		YAHOO.util.Dom.setStyle(
			this.modal,
			'background', 'black');
		YAHOO.util.Dom.setStyle(
			this.modal,
			'opacity', 0.0);
		YAHOO.util.Dom.setStyle(
			this.modal,
			'top', 0);
		YAHOO.util.Dom.setStyle(
			this.modal,
			'left', 0);
		YAHOO.util.Dom.setStyle(
			this.modal,
			'height', YAHOO.util.Dom.getDocumentHeight() + 'px');
		YAHOO.util.Dom.setStyle(
			this.modal,
			'width', YAHOO.util.Dom.getDocumentWidth() + 'px');
		YAHOO.util.Dom.setStyle(
			this.modal,
			'position', 'absolute');
		
		// Get higher z-index level
		var zindex = 0;
		YAHOO.util.Dom.batch(
			document.getElementsByTagName('*'),
			function(el)
			{
				var zi = parseInt(
					YAHOO.util.Dom.getStyle(
						el, 'z-index'
					)
				);
				
				if (zi > zindex)
				{
					zindex = zi;
				}
			}
		);
		
		YAHOO.util.Dom.setStyle(
			this.modal,
			'z-index', zindex);
		
		this.modal.dialog = this;
		document.body.appendChild(
			this.modal
		);
		
		this.dialog = document.createElement('div');
		this.dialog.dialog = this;
		if (params.width)
		{
			YAHOO.util.Dom.setStyle(
				this.dialog,
				'width',
				typeof params.width == 'string' ?
					params.width :
					parseInt(params.width) + 'px'
			);
		}
		YAHOO.util.Dom.setStyle(
			this.dialog,
			'z-index', zindex + 1);
		
		this.dialog.className = 'diablog';
		
		if (params.className)
		{
			this.dialog.className += ' ' + params.className;
		}
		
		// Title
		this.dialog.titleCtn = document.createElement('div');
		this.dialog.titleCtn.className = 'title';
		this.dialog.appendChild(
			this.dialog.titleCtn
		);
		// Content
		this.dialog.contentCtn = document.createElement('div');
		this.dialog.contentCtn.className = 'content';
		this.dialog.appendChild(
			this.dialog.contentCtn
		);
		// Buttons
		this.dialog.buttonsCtn = document.createElement('div');
		this.dialog.buttonsCtn.className = 'buttons';
		this.dialog.appendChild(
			this.dialog.buttonsCtn
		);
		this.dialog.buttons = new Array();
		
		if (!params.noButton)
		{
			this.addButton(
				OB.Diablog.LABEL_CLOSE,
				{
					click: function()
					{
						this.close();
					},
					className: '',
					scope: this
				}
			);
		}
		
		document.body.appendChild(
			this.dialog
		);
		
		// Center
		YAHOO.util.Dom.setStyle(
			this.dialog,
			'opacity', 0
		);
		this.center();
		
		// Effect
		(new YAHOO.util.Anim(
			this.modal,
			{
				opacity	: {to: 0.5}
			},
			0.25
		)).animate();
		(new YAHOO.util.Anim(
			this.dialog,
			{
				opacity	: {to: 1}
			},
			0.25
		)).animate();
		
		// Event
		if (!params.noEsc)
		{
			YAHOO.util.Event.addListener(
				window,
				'keydown',
				function(e, dialog)
				{
					if (e.keyCode == e.DOM_VK_ESCAPE ||
						e.keyCode == 27)
					{
						dialog.close();
					}
				},
				this
			);
		}
		YAHOO.util.Event.purgeElement(
			this.dialog,
			null,
			'keydown'
		);
		if (!params.noEnter)
		{
			YAHOO.util.Event.addListener(
				this.dialog,
				'keydown',
				function(e, dialog)
				{
					if (e.keyCode == 13 &&
						(!e.target ||
						e.target.nodeName.toLowerCase() != 'textarea') &&
						!this.dialog.noEnter)
					{
						YAHOO.util.Event.preventDefault(e);
					
						dialog.getDefaultButton().click();
					}
				},
				this
			);
		}
		
		// Hidding shitties objects
		YAHOO.util.Dom.batch(
			YAHOO.util.Dom.getElementsBy(
				function(el)
				{
					return	el.nodeName.toLowerCase() == 'embed' ||
							el.nodeName.toLowerCase() == 'object' ||
							/*el.nodeName.toLowerCase() == 'iframe' ||*/
							el.nodeName.toLowerCase() == 'select';
				}
			),
			function(el)
			{
				YAHOO.util.Dom.setStyle(
					el,
					'visibility', 'hidden'
				);
				/*YAHOO.util.Dom.setStyle(
					el,
					'display', 'none'
				);*/
			}
		);
		
		// params
		this.nocache = params.nocache;
	},
	/**
	 * getDefaultButton
	 */
	getDefaultButton: function()
	{
		var dftBtn = false;
		for (var i = 0; this.dialog.buttons[i]; i++)
		{
			if (this.dialog.buttons[i].isDefault)
			{
				dftBtn = this.dialog.buttons[i];
			}
		}
	
		if (!dftBtn)
		{
			dftBtn = this.dialog.buttons[i - 1];
		}
		
		return dftBtn;
	},
	/**
	 * center dialog
	 */
	center: function()
	{
		YAHOO.util.Dom.setStyle(
			this.dialog,
			'position', 'absolute');
		
		// max height
		YAHOO.util.Dom.setStyle(
			this.getCtnEl(),
			'height', 'auto'
		);
		var reg = YAHOO.util.Dom.getRegion(
			this.getCtnEl()
		);
		
		if ((reg.bottom - reg.top) > YAHOO.util.Dom.getViewportHeight())
		{
			var outerHeight = this.getOuterHeight();
			
			YAHOO.util.Dom.setStyle(
				this.getCtnEl(),
				'height',
				(
					YAHOO.util.Dom.getViewportHeight() - outerHeight - 80
				) + 'px'
			);
			YAHOO.util.Dom.setStyle(
				this.getCtnEl(),
				'overflow', 'auto'
			);
		}
		else
		{
			YAHOO.util.Dom.setStyle(
				this.dialog,
				'height',
				'auto'
			);
		}
		
		r = YAHOO.util.Dom.getRegion(this.dialog);
		YAHOO.util.Dom.setStyle(
			this.dialog,
			'left', parseInt(
				(YAHOO.util.Dom.getViewportWidth() / 2) -
				((r.right - r.left) / 2) +
				YAHOO.util.Dom.getDocumentScrollLeft()
			) + 'px');
		YAHOO.util.Dom.setStyle(
			this.dialog,
			'top', parseInt(
				(YAHOO.util.Dom.getViewportHeight() / 2) -
				((r.bottom - r.top) / 2) +
				YAHOO.util.Dom.getDocumentScrollTop()
			) + 'px');
	},
	/**
	 * Set dialog title
	 */
	setTitle: function(title)
	{
		if (typeof title == 'string')
		{
			this.dialog.titleCtn.innerHTML = title;
		}
		else
		{
			this.dialog.titleCtn.appendChild(
				title
			);
		}
		this.center();
	},
	/**
	 * Set dialog content
	 */
	setContent: function(content)
	{
		if (typeof content == 'string')
		{
			this.dialog.contentCtn.innerHTML += content;
		}
		else
		{
			this.dialog.contentCtn.appendChild(
				content
			);
		}
		
		// showwing shitties objects inside the diablog
		YAHOO.util.Dom.batch(
			YAHOO.util.Dom.getElementsBy(
				function(el)
				{
					return	el.nodeName.toLowerCase() == 'embed' ||
							el.nodeName.toLowerCase() == 'object' ||
							/*el.nodeName.toLowerCase() == 'iframe' ||*/
							el.nodeName.toLowerCase() == 'select';
				},
				null,
				this.dialog.contentCtn
			),
			function(el)
			{
				YAHOO.util.Dom.setStyle(
					el,
					'visibility', 'visible'
				);
				/*YAHOO.util.Dom.setStyle(
					el,
					'display', ''
				);*/
			}
		);
		
		this.center();
	},
	/**
	 * flush content
	 */
	flushContent: function()
	{
		while (this.dialog.contentCtn.firstChild)
		{
			this.dialog.contentCtn.removeChild(
				this.dialog.contentCtn.firstChild
			);
		}
	},
	/**
	 * set content from an ajax requet
	 */
	setContentFromAjax: function(uri, success, successParams)
	{
		this._uri = uri;
		this._success = success;
		this._successParams = successParams;
		
		this.flushContent();
		if (!this.nocache &&
			OB.Cache[this._uri])
		{
			this.setContent(OB.Cache[this._uri]);
			if (typeof this._success == 'function')
			{
				this._success.apply(
					this,
					[this._successParams]
				);
			}
			this._evalJSFromAjax(
				this.getEl().contentCtn
			);
			this.onAjaxLoaded.fire(this);
		}
		else
		{
			var wait = document.createElement('div');
			wait.className = 'wait';
			this.setContent(
				wait
			);
			
			if (this._ajax)
			{
				this._ajax.cancel();
			}
			this._ajax = YAHOO.util.Connect.asyncRequest(
				'get',
				this._uri,
				{
					success: function(o)
					{
						this.flushContent();
						var body = o.responseText.replace(/\s+/gm, ' ').match(/<body[^>]*>(.*)<\/body>/gi);
						if (body)
						{
							body = body[0].replace(
								/<body[^>]*>/gi,
								''
							);
							body = body.replace(
								/<\/body>/gi,
								''
							);
						}
						else
						{
							body = o.responseText;
						}
							
						this.setContent(
							body
						);
						if (typeof this._success == 'function')
						{
							this._success.apply(
								this,
								[this._successParams]
							);
						}
						this._evalJSFromAjax(
							this.getEl().contentCtn
						);
						this.onAjaxLoaded.fire(this);
						OB.Cache[this._uri] = body;
						this._ajax = null;
					},
					scope: this
				}
			);
		}
	},
	/**
	 * exec js after ajax request
	 */
	_evalJSFromAjax: function(ctn)
	{
		var scripts = ctn.getElementsByTagName('script');
		
		for (var i = 0; scripts[i]; i++)
		{
			eval(scripts[i].text);
		}
	},
	/**
	 * add a button and an event
	 */
	addButton: function(label, params)
	{
		var button = new OB.Blutton(
			label, params
		);
		button.dialog = this;
		
		this.dialog.buttonsCtn.appendChild(
			button.getEl()
		);
		this.dialog.buttons.push(button);
		
		if (params.isDefault)
		{
			for (var i = 0; this.dialog.buttons[i]; i++)
			{
				if (this.dialog.buttons[i].isDefault)
				{
					this.dialog.buttons[i].isDefault = false;
				}
			}
			button.isDefault = true;
		}
		
		// 
		
	},
	/**
	 * close dialog
	 */
	close: function(msg, params)
	{
		if (!params)
		{
			params = {};
		}
		if (msg)
		{
			params.closeicon = false;
			params.dontreopen = true;
			this.displayMessage(
				msg,
				params
			);
		}
		if (this.ajax)
		{
			this.ajax.cancel();
		}
		var anim = new YAHOO.util.Anim(
			this.dialog,
			{
				opacity	: {to: 0}
			},
			msg ? 2 : 0.25,
			msg ?
				YAHOO.util.Easing.easeInStrong :
				YAHOO.util.Easing.easeOut
		);
		if (params.fn)
		{
			anim.afterComplete = params.fn;
		}
		else
		{
			anim.afterComplete = new Function();
		}
		if (params.scope)
		{
			anim.afterCompleteScope = params.scope;
		}
		else
		{
			anim.afterCompleteScope = this;
		}
		anim.onComplete.subscribe(
			function()
			{
				this.getEl().parentNode.removeChild(
					this.getEl()
				);
				
				this.afterComplete.apply(this.afterCompleteScope);
			}
		);
		anim.animate();
		
		var anim2 = new YAHOO.util.Anim(
			this.modal,
			{
				opacity	: {to: 0}
			},
			msg ? 2 : 0.25,
			msg ?
				YAHOO.util.Easing.easeInStrong :
				YAHOO.util.Easing.easeOut
		);
		anim2.onComplete.subscribe(
			function()
			{
				if (this.getEl())
				{
					this.getEl().parentNode.removeChild(
						this.getEl()
					);
				}
				
				// Showing shitties objects
				YAHOO.util.Dom.batch(
					YAHOO.util.Dom.getElementsBy(
						function(el)
						{
							return	el.nodeName.toLowerCase() == 'embed' ||
									el.nodeName.toLowerCase() == 'object' ||
									/*el.nodeName.toLowerCase() == 'iframe' ||*/
									el.nodeName.toLowerCase() == 'select';
						}
					),
					function(el)
					{
						YAHOO.util.Dom.setStyle(
							el,
							'visibility', 'visible'
						);
						/*YAHOO.util.Dom.setStyle(
							el,
							'display', ''
						);*/
					}
				);
			}
		);
		anim2.animate();
	},
	/**
	 * Display a full dialog message
	 */
	displayMessage: function(msg, params)
	{
		if (!params)
		{
			params = {};
		}
		var reg = YAHOO.util.Dom.getRegion(
			this.getEl()
		);
		
		var panel = document.createElement('div');
		if (!params.type)
		{
			params.type = OB.Diablog.TYPE_OK;
		}
		YAHOO.util.Dom.addClass(
			panel,
			'fullmessage'
		);
		YAHOO.util.Dom.addClass(
			panel,
			params.type
		);
		
		if (typeof msg == 'string')
		{
			panel.appendChild(
				document.createTextNode(
					msg
				)
			);
		}
		else
		{
			panel.appendChild(msg);
		}
		
		if (params.closeicon)
		{
			panel.appendChild(
				document.createElement('br')
			);
			panel.appendChild(
				document.createElement('br')
			);
			panel.appendChild(
				new OB.Blutton(
					'Ok',
					{
						className: 'orange'
					}
				).getEl()
			);
		}
		
		// Fadein
		panel.dialog = this;
		this.noEnter = true;
		// Close element
		if (!params.close)
		{
			params.close = panel;
		}
		if (!params.dontreopen)
		{
			YAHOO.util.Event.addListener(
				params.close,
				'click',
				function(e, dialog)
				{
					var a = new YAHOO.util.Anim(
						this,
						{
							opacity	: {to: 0}
						},
						0.25,
						YAHOO.util.Easing.easeOut
					);
					a.onComplete.subscribe(
						function(a,b, dialog)
						{
							this.parentNode.removeChild(
								this
							);
						},
						this,
						true
					);
					a.animate();
					
					(new YAHOO.util.Anim(
						this.dialog.getEl(),
						{
							height: {
								to: this.dialog.getEl().originalHeight
							},
							top: {
								to: this.dialog.getEl().originalTop
							}
						},
						0.25,
						YAHOO.util.Easing.easeOut
					)).animate();
				
					if (params.onClose)
					{
						params.onClose.apply(this.dialog);
					}
				
					this.noEnter = false;
				},
				this,
				panel
			);
		}
		
		YAHOO.util.Dom.setStyle(
			panel,
			'opacity',
			0
		);
		
		YAHOO.util.Dom.setStyle(
			panel,
			'cursor',
			'pointer'
		);
		
		(new YAHOO.util.Anim(
			panel,
			{
				opacity	: {to: 1}
			},
			0.25,
			YAHOO.util.Easing.easeOut
		)).animate()
		
		this.getEl().contentCtn.appendChild(
			panel
		);
		
		// Downsize dialog if needed
		this.getEl().originalHeight = reg.bottom - reg.top;
		this.getEl().originalTop = parseInt(YAHOO.util.Dom.getStyle(
			this.getEl(), 'top'
		));
		
		reg = YAHOO.util.Dom.getRegion(
			panel
		);
		var panelHeight = reg.bottom - reg.top - 10;
		
		YAHOO.util.Dom.setStyle(
			this.getEl(),
			'overflow', 'hidden'
		);
		(new YAHOO.util.Anim(
			this.getEl(),
			{
				height: {
					to: panelHeight
				},
				top: {
					to: this.getEl().originalTop +
						(this.getEl().originalHeight - panelHeight) / 2
				}
			},
			0.25,
			YAHOO.util.Easing.easeOut
		)).animate();
	},
	/**
	 * get element
	 */
	getEl: function()
	{
		return this.dialog;
	},
	/**
	 * get container
	 */
	getCtnEl: function()
	{
		return this.dialog.contentCtn;
	},
	/**
	 * getOuterHeight
	 */
	getOuterHeight: function()
	{
		var reg1 = YAHOO.util.Dom.getRegion(
			this.getEl().titleCtn
		);
		var reg2 = YAHOO.util.Dom.getRegion(
			this.getEl().buttonsCtn
		);
		
		return (reg1.bottom - reg1.top) +
			(reg1.bottom - reg1.top);
	}
};

OB.Bluttons = new Array();
/**
 * OB.Blutton class
 * @constructor
 * @author Hadrien Lanneau
 */
OB.Blutton = function(label, params)
{
	this.label = label;
	this.el;
	this.active = true;
	
	if (!params)
	{
		params = {};
	}
	this.init(params);
};
OB.Blutton.LABEL_DEACTIVATED = "…";
OB.Blutton.prototype =
{
	/**
	 * init
	 */
	init: function(params)
	{
		if (typeof this.label == 'string')
		{
			this.el = document.createElement('span');
			this.el.className = 'ob-button';
			if (params.className)
			{
				this.el.className += ' ' + params.className;
			}
			this.el.firstchild = document.createElement('span');
			this.el.firstchild.className = 'first-child';
			this.el.firstchild.b = document.createElement('button');
			this.el.firstchild.b.appendChild(
				document.createTextNode(
					this.label
				)
			);
			this.el.firstchild.appendChild(
				this.el.firstchild.b
			);
			this.el.appendChild(
				this.el.firstchild
			);
		}
		else if (this.label.nodeName.toLowerCase() === 'button' ||
				 this.label.nodeName.toLowerCase() === 'input' ||
				 this.label.nodeName.toLowerCase() === 'a')
		{
			// Existant button
			this.el = this.label.parentNode.parentNode;
			this.el.firstchild = this.label.parentNode;
			this.el.firstchild.b = this.label;
			if (this.label.nodeName.toLowerCase() === 'input')
			{
				this.label = this.label.value;
			}
			else
			{
				this.label = this.label.innerHTML;
			}
		}
		else
		{
			throw 'label must be a string or an encapsuled input/button/a';
		}
		
		if (params.click)
		{
			this.fn = params.click;
		
			if (params.scope)
			{
				scope = params.scope;
			}
			else
			{
				scope = this;
			}
		
			YAHOO.util.Event.addListener(
				this.el,
				'click',
				function(e, obj, params)
				{
					this.fn.apply(obj.scope, [e, obj.params]);
				},
				{
					scope: scope,
					button: this,
					params: params.params
				},
				this
			);
		}
		
	},
	/**
	 * get element
	 */
	getEl: function()
	{
		return this.el;
	},
	/**
	 * display waiting label
	 */
	deactivate: function()
	{
		// Backup click events
		this._clickEvents = YAHOO.util.Event.getListeners(
			this.el,
			'click'
		);
		YAHOO.util.Event.purgeElement(
			this.el,
			false,
			'click'
		);
		
		// Set active class
		YAHOO.util.Dom.addClass(
			this.el,
			'active'
		);
		
		// Get original size
		var reg = YAHOO.util.Dom.getRegion(
			this.el.firstchild.b
		);
		
		YAHOO.util.Dom.setStyle(
			this.el.firstchild.b,
			'width',
			(reg.right - reg.left) + 'px'
		);
		YAHOO.util.Dom.setStyle(
			this.el.firstchild.b,
			'padding',
			'0'
		);
		
		
		// Change label
		if (this.el.firstchild.b.nodeName.toLowerCase() === 'input')
		{
			this.el.firstchild.b.value = OB.Blutton.LABEL_DEACTIVATED;
		}
		else
		{
			this.el.firstchild.b.removeChild(
				this.el.firstchild.b.firstChild
			);
			this.el.firstchild.b.appendChild(
				document.createTextNode(
					OB.Blutton.LABEL_DEACTIVATED
				)
			);
		}
		
		this.el.firstchild.b.blur();
		
		this.active = false;
	},
	/**
	 * recover original label
	 */
	reactivate: function()
	{
		// remove active class
		YAHOO.util.Dom.removeClass(
			this.el,
			'active'
		);
		
		// Reset label
		if (this.el.firstchild.b.nodeName.toLowerCase() === 'input')
		{
			this.el.firstchild.b.value = this.label;
		}
		else
		{
			this.el.firstchild.b.removeChild(
				this.el.firstchild.b.firstChild
			);
			this.el.firstchild.b.appendChild(
				document.createTextNode(
					this.label
				)
			);
		}
		
		// Retrieving old events
		if (this._clickEvents)
		{
			for (var i = 0; this._clickEvents[i]; i++)
			{
				YAHOO.util.Event.addListener(
					this.el,
					this._clickEvents[i].type,
					this._clickEvents[i].fn,
					this._clickEvents[i].obj,
					this._clickEvents[i].scope
				);
			}
		}
		
		this.active = true;
	},
	/**
	 * click
	 */
	click: function()
	{
		var evts = YAHOO.util.Event.getListeners(
			this.el,
			'click'
		);
		
		for (var i = 0; evts[i]; i++)
		{
			evts[i].fn.apply(evts[i].scope, [evts[i], evts[i].obj]);
		}
	}
};

})();
