/**
 * @version 1.0
 *
 * @copyright Copyright 2009 WnG Solutions Sàrl, all rights reserved
 * @author David Ansermot <david [DOT] ansermot [AT] wng [DOT] ch>
 * @author Daniel Calderini <daniel [DOT] calderini [AT] wng [DOT] ch>
 * @package pittet-associes.ch
 */

/**
 * Modification du formulaire de contact.
 * Cette fonction place le contenu du label directement dans le champ et cache le label
 */
$(document).ready(formFilterLabel);

$(document).ready(function() {
	$('#mailformplus #submit').click(function(){
		$('#mailformplus ul li label').each(function(i) {
			
			var elementId = $(this).attr('for');
			var elementType = formGetElementType(elementId);
			
			switch (elementType) {
				case 'textarea':
					if ($('textarea#'+elementId).html() == $(this).html()) {
						$('textarea#'+elementId).html('') ;
					}
				break;
				case 'text':
					if ($('input#'+elementId).attr('value') == $(this).html()) {
						$('input#'+elementId).attr('value', '');
					}
				break;
			}
			
		});
	});
});

/**
 *
 */
function formFilterLabel() {
	$('ul li label').each(function(i) {
		var elementId = $(this).attr('for');
		var hide = false;
		var addAttr = false;
		
		// Type de l'élément HTML
		var elementType = formGetElementType(elementId);
		
		switch (elementType) {
			case 'textarea':
				// Label placé dans le champ
				if ($('textarea#' + elementId).html() == '')
					$('textarea#' + elementId).html($(this).html());
				
				hide = true;
				
		/*		// Création des actions "onfocus" et "onblur"
				var addAttrFocus = "if ($(this).html() == '" + $(this).html() + "') $(this).html('');";
				var addAttrBlur = "if ($(this).html() == '') $(this).html('" + $(this).html() + "');";
				
				// Faut-il ajouter ces attributs
				if (
					($('#' + elementId).attr('onfocus') == '' || typeof $('#' + elementId).attr('onfocus') == 'undefined') &&
					($('#' + elementId).attr('onblur') == '' || typeof $('#' + elementId).attr('onblur') == 'undefined')
				)
					addAttr = true;*/
			break;
			
			case 'text':
				// Création du label dans le champ
				if ($('input#' + elementId).attr('value') == '')
					$('input#' + elementId).attr('value', $(this).html());
				
				hide = true;
				
		/*		// Ajout des actions "onfocus" et "onblur"
				var addAttrFocus = "javascript:if(this.value=='" + $(this).html() + "'){this.value = ''}";
				var addAttrBlur = "javascript:if(this.value==''){this.value = '" + $(this).html() + "'}";
				
				// Faut-il ajouter ces attributs
				if (
					($('#' + elementId).attr('onfocus') == '' || typeof $('#' + elementId).attr('onfocus') == 'undefined') &&
					($('#' + elementId).attr('onblur') == '' || typeof $('#' + elementId).attr('onblur') == 'undefined')
				)
					addAttr = true;*/
			break;
			
			case 'select':
				hide = true;
			break;
		}
		
		// On cache le label
		if (hide)
			$(this).addClass('hideMe');
		
		// Ajout des attributs "onfocus" et "onblur"
		if (addAttr) {
			$('#' + elementId).attr('onfocus', addAttrFocus);
			$('#' + elementId).attr('onblur', addAttrBlur);
		}
	});
}

/**
 * Retourne le type d'un champ de formulaire
 *
 * @param string elementId Identifiant de l'élément HTML
 * @return string
 */
function formGetElementType(elementId) {
	if ($('textarea#' + elementId).length > 0) {
		return 'textarea';
	} else if ($('select#' + elementId).length > 0) {
		return 'select';
	} else if ($('input#' + elementId).length > 0) {
		return $('input#' + elementId).attr('type');
	} else {
		return '';
	}
}