(function($) {
/**
 * Class Situation
 */
	Situation = function() {
		this._visibleInput = null;
	};
	
	$.extend(Situation.prototype, {
		
		/**
		 * Genère le menu de sélection de la situation.
		 */
		_getSelector: function(user_id, radioBoxes, checkBoxes) {
			var situationDiv = $('<div id="situation-div"></div>');
			this._radioBoxes = [];
			this._checkBoxes = [];
			
			var i = 1;
			$.each(radioBoxes, function(index, radioBox) {
				var real_id = '#situation_' + user_id;
				var fake_id = 'situ_' + i + '_' + user_id;
				var fake_name = 'situ_' + user_id;
				
				var input = $('<input id="' + fake_id + '" name="' + fake_name + '" type="radio" />')
					.val(radioBox['value'])
					.attr('title', radioBox['label'])
					.click(function(event) {
						$('option', real_id).removeAttr('selected');
						$('option[value=' + $(this).val() + ']', real_id).attr('selected', 'selected');
						situation._update();
					}
				);
				situation._radioBoxes.push(input);
				situationDiv.append($('<label for="' + fake_id + '"></label>').append(input, radioBox['label']));
				i = i + 1;
			});
			
			var i = 1;
			var multiDiv = $('<div class="multi-div"><span class="title">Complément(s) d\'information :</span></div>');
			$.each(checkBoxes, function(index, checkBox) {
				var real_id = '#complement_' + i + '_' + user_id;
				var fake_id = 'compl_' + i + '_' + user_id;
				
				var input = $('<input id="' + fake_id + '" type="checkbox" />')
					.val(checkBox['value'])
					.attr('title', checkBox['label'])
					.click(function(event) {
						if ( $(real_id).attr('checked') ) {
							$(real_id).removeAttr('checked');
						} else {
							$(real_id).attr('checked', 'checked');
						}
						situation._update();
					}
				);
				situation._checkBoxes.push(input);
				multiDiv.append($('<label for="' + fake_id + '"></label>').append(input, checkBox['label']));
				i = i + 1;
			});
			situationDiv.append(multiDiv);
			return situationDiv;
		},
		
		_inArray: function(array, value) {
			for (var i = 0; i < array.length; i++) {
				if (array[i] == value) {
					return true;
				}
			}
			return false;
		},

		/**
		 * Met à jour la valeur affichée dans le champ de saisie.
		 */
		_update: function() {
			var value = '';
			
			$.each(this._radioBoxes, function() {
				if (this.attr('checked')) {
					value = this.attr('title');
				}
			});
			$.each(this._checkBoxes, function() {
				if (this.attr('checked')) {
					value += (value != '' ? ', ' + this.attr('title').toLowerCase() : this.attr('title')) ;
				}
			});
			this._visibleInput.val(value).attr('title', value);
		},
		
		/**
		 * Affiche le menu de sélection de la situation.
		 */
		display: function(user_id, visibleInput, container, radioBoxes, checkBoxes) {
			this._visibleInput = visibleInput;
			$(container).append(this._getSelector(user_id, radioBoxes, checkBoxes));
			
			$.each(radioBoxes, function(i, radioBox) {
				var real_id = '#situation_' + user_id;
				var fake_id = '#situ_' + (i + 1) + '_' + user_id;
				if ( $(real_id).val() == radioBox['value'] ) {
					$(fake_id).attr('checked', 'checked');
				}
			});
			
			$.each(checkBoxes, function(i, checkBox) {
				var real_id = '#complement_' + (i + 1) + '_' + user_id;
				var fake_id = '#compl_' + (i + 1) + '_' + user_id;
				if ( $(real_id).attr('checked') ) {
					$(fake_id).attr('checked', 'checked');
				}
			});
		}
	});
	
	$.fn.situation = function() {
		return this.each(function() {
			var id = $('select', this).attr('id').split('_');
			var id = id[1];
			var radioBoxes = [];
			var checkBoxes = [];
			var visibleInputValue = '';
			
			/* Tranforme les options du select en boutons radio */
			var singleValue = '';
			$('option', this).each(function() {
				var value = $(this).val();
/*				if ( value ) { */
					var label = $.trim($(this).text());
					var checked = ( $(this).attr('selected') ? 1 : 0 );
					if ( checked ) {
						visibleInputValue = label;
						singleValue = value;
					}
					radioBoxes.push({'value':value, 'label':label});
/*				} */
			});
			
			/* */
			var multiValue = '';
			$('label', this).each(function() {
				var input = $('input', this);
				var value = input.val();
				var label = $.trim($(this).text());
				var checked = ( input.attr('checked') ? 1 : 0 );
				if ( checked ) {
					visibleInputValue += (visibleInputValue == '' ? label : ', ' + label.toLowerCase());
					multiValue += (multiValue == '' ? value : ',' + value);
				}
				checkBoxes.push({'value':value, 'label':label});
			});

			if ( !visibleInputValue ) {
				visibleInputValue = "S\u00e9lectionnez votre situation...";
			}
			var visibleInput = $('<input "type="text" id="visiblesituation_' + id + '" />').val(visibleInputValue).attr('title', visibleInputValue);

			$(this).children().hide();
			$(this).prepend(visibleInput);
			
			var imagesPath = $('script[type="text/javascript"][src$="js/edv.js"]').attr('src');
			imagesPath = imagesPath.substring(0, imagesPath.length - '/js/edv.js'.length);
			visibleInput.popControl({
				'buttonText': 'Modifier la situation',
				'buttonImage': imagesPath + '/img/popcontrol-button.gif',
				'className': 'situation-popup',
				'closeboxImage': imagesPath + '/img/popcontrol-closebox.gif',
				'directInput': false,
				'setContent': function(input, container) {
					var user_id = $(input).attr('id').split('_');
					user_id = user_id[1];
					situation.display(user_id, visibleInput, container, radioBoxes, checkBoxes);
				}
			});
		});
	};
	
	$(document).ready(function() {
		situation = new Situation(); // Singleton
	});
})(jQuery);
