
var Poll = {};

Poll.PollBox = Class.create( {

	/**
	 * constructeur de la classe
	 */
	initialize: function(idBox, options) {
		
		// Définition des parametres
		this.urlContent				= $globals['url_ajax_poll_box'];
		this.idBox					= idBox;
		this.idForm					= 'form-poll';
		this.container				= null;
		this.options				= {};
		
		Object.extend(this.options, options || {});
		
		// on affecte les propriétés des options
		for (var o1 in this) {
			for (var o2 in this.options) {
				if (o1==o2) {
					this[o1] = this.options[o2];
				}
			}
		}

		var obj = this;

		if ($(this.idBox)) {
			this.container = $(this.idBox);
			this.display();
		}

	}, // fin du constructeur
	
	display: function()
	{
		var obj = this;

		if (this.container.innerHTML != "")
			Effect.Fade(this.container, {to: 0.5, duration: 0.25} );

		new Ajax.Request(this.urlContent + '?tt=' + new Date().getTime(), {
			method: 'get',
			onSuccess: function(transport) {
				if (obj.container.innerHTML == "") {
					obj.container.hide();
					Effect.Appear(obj.container, {duration: 0.25});
				} else {
					Effect.Appear(obj.container, {from: 0.5, duration: 0.25});
				}
				obj.container.update(transport.responseText);
				initLinks();
				obj.configure();
			}
		} );
	}, // display
	
	configure: function()
	{
		var obj = this;	
		
		if ($(this.idForm))
			Event.observe(this.idForm, 'submit', this.submitForm.bindAsEventListener(this));
	}, // configure
		
	submitForm: function(event)
	{
		var obj = this;
		Event.stop(event);
		
		var form = $(this.idForm);
		
		Effect.Fade(this.container, {to: 0.5, duration: 0.25} );

		form.request( {
			onComplete: function(transport) {
				obj.container.update(transport.responseText);
				Effect.Appear(obj.container, {from: 0.5, duration: 0.25} );
				obj.configure();
			}
		});
	},
	
	displayMessage: function(str)
	{
		$globals['popup-error'] = new Display.Popup( {
			mode: 'div',
			divId: 'div-msg',
			autoClose: true,
			contentHTML: str,
			classDiv: 'div-msg'
		} );	
	}
	
} ); // fin de Poll.PollBox

