//- Version 2009-02-04

/*
 * Usage:
 * new JacAjax.Html ({ mode: 'modeless' }).send (targetID, url, cmd, arg, data) ;
 * new JacAjax.Json ({ mode: 'modeless' }).send (url, cmd, arg, data) ;
 * new JacAjax.Json ({ mode: 'modeless' }).sendForm (myForm) ;
 
 	none
 	modal (default)
 	modal-valid
 	modeless
 	modeless-valid
 */
var JacAjax =new Class ({
	Implements: [ Options ],

	options: {
		mode: 'modal',
		autoClose: 1.5
	},

	dlgReports: {
		running: 'Merci de patienter,<br />traitement en cours...',
		runningTitle: 'Traitement en cours...',
		completed: 'Requête terminée.',
		success: 'Votre demande a abouti.',
		failure: 'Votre demande a échoué :(',
		cancel: 'Demande annulée.',
		'success-valid': 'Votre demande a abouti.',
		'failure-valid': 'Votre demande a échoué :(',
		'cancel-valid': 'Demande annulée.'
	},
	
	initialize: function (options) {
		this.setOptions (options) ;
		this.req =null ;
		this.targetId =null ;
	},
	
	send: function (url, cmd, arg, data) {
		switch ( this.options.mode ) {
			default:
			case 'none':
				break ;
			case 'modal':
			case 'modal-valid':
				this.dlg =new JacDialogs ({
					modal: true, hideTitle: true, close: false
				}).MessageBox (this.dlgReports.running, this.dlgReports.runningTitle, 'no-button', 'wait') ;
				break ;
			case 'modeless':
			case 'modeless-valid':
				this.dlg =new JacDialogs ({
					modal: false, hideTitle: false
				}).MessageBox (this.dlgReports.running, this.dlgReports.runningTitle, 'cancel-button', 'wait') ;
				break ;			
		}
		this._internalSend (url, cmd, arg, data) ;
	},

	postForm: function (form) {
		switch ( this.options.mode ) {
			default:
			case 'none':
				break ;
			case 'modal':
			case 'modal-valid':
				this.dlg =new JacDialogs ({
					modal: true, hideTitle: true, close: false
				}).MessageBox (this.dlgReports.running, this.dlgReports.runningTitle, 'no-button', 'wait') ;
				break ;
			case 'modeless':
			case 'modeless-valid':
				this.dlg =new JacDialogs ({
					modal: false, hideTitle: false
				}).MessageBox (this.dlgReports.running, this.dlgReports.runningTitle, 'cancel-button', 'wait') ;
				break ;			
		}
		this._internalSendForm (form) ;
	},
	
	report: function (result) {
		var mode ={ modal: false, hideTitle: false } ;
		var resMsg =result ;
		switch ( this.options.mode ) {
			default:
			case 'none':
				return ;
			case 'modal':
			case 'modeless':
				mode.autoClose =this.options.autoClose ;
				break ;
			case 'modal-valid':
			case 'modeless-valid':
				mode.modal =true ;
				resMsg +='-valid' ;
				break ;
		}
		this.dlg.close () ;
		this.dlg =new JacDialogs (mode).MessageBox (
			this.dlgReports [resMsg], this.dlgReports.completed, 'ok-button', result
		) ;
	}

}) ;

JacAjax.Text = new Class({

	Extends: JacAjax,

	send: function (url, cmd, arg, data) {
		this.parent (url, cmd, arg, data) ;
	},
	
	postForm: function (form) {
		this.parent (form) ;
	},
		
	_internalSend: function (url, cmd, arg, data) {
		this.req =new Request ({
			'url': url, 
			onComplete: function (text) {
				this.report ('success') ;
	  		}.bind (this),
			onFailure: function () {
				this.report ('failure') ;
			}.bind (this),
			onCancel: function () {
				this.report ('cancel') ;
			}.bind (this)
		}).post ({ 'cmd': cmd, 'arg': arg, 'data': JSON.encode (data) }) ;
	},
	
	_internalSendForm: function (form) {
		this.req =new Request ({
			url: form.get ('action'),
			onComplete: function (text) {
				this.report ('success') ;
	  		}.bind (this),
			onFailure: function () {
				this.report ('failure') ;
			}.bind (this),
			onCancel: function () {
				this.report ('cancel') ;
			}.bind (this)
		}).post (form) ;
	}
	
}) ;

JacAjax.Html = new Class({

	Extends: JacAjax,

	send: function (id, url, cmd, arg, data) {
		this.targetId =id ;
		this.parent (url, cmd, arg, data) ;
	},

	postForm: function (id, form) {
		this.targetId =id ;
		this.parent (form) ;
	},
		
	_internalSend: function (url, cmd, arg, data) {
		this.req =new Request.HTML ({
			'url': url, 
			onSuccess: function (html) {
				//$(this.targetId).set ('text', '') ;	// Clear the text currently inside the results div.
				//$(this.targetId).adopt (html) ;		// Inject the new DOM elements into the results div.
				//alert (html.length ) ;
				//alert (arguments [2] ) ;
				//alert (arguments [3] ) ;
				//$exec (arguments [3]) ;				// Execute scripts
				//alert (html [0].nodeValue) ;
				this.report ('success') ;
	  		}.bind (this),
			onFailure: function () {
				$(this.targetId).set ('text', this.dlgReports.failure) ;
				this.report ('failure') ;
			}.bind (this),
			onCancel: function () {
				$(this.targetId).set ('text', this.dlgReports.cancel) ;
				this.report ('cancel') ;
			}.bind (this),
			update: this.targetId,		// Inject the new DOM elements into the results div.
			evalScripts: true			// Execute scripts
		}).post ({ 'cmd': cmd, 'arg': arg, 'data': JSON.encode (data) }) ;
	},
	
	_internalSendForm: function (form) {
		this.req =new Request.HTML ({
			url: form.get ('action'),
			onSuccess: function (html) {
				//$(this.targetId).set ('text', '') ;	// Clear the text currently inside the results div.
				//$(this.targetId).adopt (html) ;		// Inject the new DOM elements into the results div.
				//alert (html.length ) ;
				//alert (arguments [2] ) ;
				//alert (arguments [3] ) ;
				//$exec (arguments [3]) ;				// Execute scripts
				//alert (html [0].nodeValue) ;
				this.report ('success') ;
	  		}.bind (this),
			onFailure: function () {
				$(this.targetId).set ('text', this.dlgReports.failure) ;
				this.report ('failure') ;
			}.bind (this),
			onCancel: function () {
				$(this.targetId).set ('text', this.dlgReports.cancel) ;
				this.report ('cancel') ;
			}.bind (this),
			update: this.targetId,		// Inject the new DOM elements into the results div.
			evalScripts: true			// Execute scripts
		}).post (form) ;
	}
	
}) ;

JacAjax.Json = new Class({

	Extends: JacAjax,

	send: function (url, cmd, arg, data) {
		this.parent (url, cmd, arg, data) ;
	},
	
	postForm: function (form) {
		this.parent (form) ;
	},
		
	_internalSend: function (url, cmd, arg, data) {
		this.req =new Request.JSON ({
			'url': url,
			onComplete: function (jsonObj) {
				this.report ('success') ;
	  		}.bind (this),
			onFailure: function () {
				this.report ('failure') ;
			}.bind (this),
			onCancel: function () {
				this.report ('cancel') ;
			}.bind (this)
		}).post ({ 'cmd': cmd, 'arg': arg, 'data': JSON.encode (data) }) ;
	},
	
	_internalSendForm: function (form) {
		this.req =new Request.JSON ({
			url: form.get ('action'),
			onComplete: function (jsonObj) {
				this.report ('success') ;
	  		}.bind (this),
			onFailure: function () {
				this.report ('failure') ;
			}.bind (this),
			onCancel: function () {
				this.report ('cancel') ;
			}.bind (this)
		}).post (form) ;
	}
	
}) ;

/*
 * Usage:
 * 	new JacDialogs ({ modal: false }).MessageBox ('test', 'mytitle', 'okcancel-button', 'success') ;
 */
var JacDialogs =new Class ({
	Implements: [ Options ],

	options: {
		label: '&nbsp;',
		image: '/images/logo16.jpg',
		hideTitle: false,
		modal: true,
		horizontal: 'center center',
		vertical: 'center center',
		width: 300, // default is 250 in jxLib
		height: 100, // default is 250 in jxLib
		content: '',
		resize: false,
		close: true,
		marginTop: 12 //- This is to fix the jxLib bug with dialogs without caption
	},
	
	templates: {
		'no-button': '<div class="message"><table><tr><td class="%s"></td><td class="text">%s</td></tr></table></div>',
		'ok-button': '<div class="message"><table><tr><td class="%s"></td><td class="text">%s</td></tr><tr><td colspan="2" class="buttons"><button id="IDOK">Ok</button></td></tr></table></div>',
		'cancel-button': '<div class="message"><table><tr><td class="%s"></td><td class="text">%s</td></tr><tr><td colspan="2" class="buttons"><button id="IDCANCEL">Annuler</button></td></tr></table></div>',
		'okcancel-button': '<div class="message"><table><tr><td class="%s"></td><td class="text">%s</td></tr><tr><td colspan="2" class="buttons"><button id="IDOK">Ok</button> <button id="IDCANCEL">Annuler</button></td></tr></table></div>',
		'yesno-button': '<div class="message"><table><tr><td class="%s"></td><td class="text">%s</td></tr><tr><td colspan="2" class="buttons"><button id="IDYES">Ok</button> <button id="IDNO">Non</button></td></tr></table></div>'
	},
	
	heights: {
		'no-button': 80, 'ok-button': 120, 'cancel-button': 120, 'okcancel-button': 120, 'yesno-button': 120
	},
	
	initialize: function (options) {
		this.setOptions (options) ;
	},

	MessageBox: function (text, title, buttons, icon) {
		var def =this.options ;
		def.label =title ;
		def.content =this.templates [buttons] ;
		def.height =this.heights [buttons] ;
		def.content =sprintf (def.content, icon, text) ;
		this.dlg =new Jx.Dialog (def) ;
		if ( this.options.modal )
			this.dlg.blanket.setStyles ({ 'background-color': '#333333', 'opacity': .7, '-ms-filter': "Alpha(opacity=70)" }) ;
		if ( def.hideTitle == true )
			this.dlg.contentContainer.setStyles ({ 'margin-top': def.marginTop }) ;
		this.dlg.domObj.getElements ('button').each (function (elt) {
			elt.addEvent ('click', function (e) {
				var event =new Event (e) ;
				event.stop () ;
				this.retCode =event.target.id ;
				this.dlg.close () ;
			}.bind (this)) ;
		}.bind (this)) ;
		this.dlg.open () ;
		//- Just in case someone requested a delayed autoclose
		if ( $defined(this.options.autoClose) ) {
			this.dlg.domObj.addEvent ('shiftclick', function (e) {
				//this.dlg.domObj.fade ('out') ;
				var myEffects =new Fx.Morph (this.dlg.domObj, {
					duration: 500,
					transition: Fx.Transitions.Sine.easeOut,
					onComplete: function (elt) { this.close () ; }.bind (this)
				}).start ({ 'opacity': 0 }) ;
			}.bind (this)) ;
			this.dlg.domObj.fireEvent ('shiftclick', this.dlg.domObj, this.options.autoClose * 1000);
		}
		return (this) ;
	},
	
	close: function () {
		this.dlg.close () ;
	}
	
}) ;
