/**
 * Comment preview
 */

var Preview = {

	line : null,

	init : function()
	{
		if (!document.getElementById || !document.createElement
			|| !document.appendChild || Ajax.getTransport() === false) return false;
		
		button = document.createElement('input');
		Element.extend(button);
		
		button.setAttribute('type', 'button');
		button.setAttribute('value', 'Prévisualiser');
		Event.observe(button, 'click', Preview.show);
		
		$('li_submit').appendChild(document.createTextNode(' '));
		$('li_submit').appendChild(button);
		
		Preview.line = document.createElement('li');
		Element.extend(Preview.line);
		
		$('li_comment').parentNode.insertBefore(Preview.line, $('li_comment').nextSibling);
		Preview.line.hide();
	},
	
	show : function()
	{
		new Ajax.Updater(Preview.line, '/comment/preview', {
		
			parameters : { comment: $('comment').getValue() },
			
			requestHeaders : ['Accept', 'application/xhtml+xml'],
			method : 'post',
			
			onComplete : function(transport) {
				if(transport.responseText == '') {
					Preview.line.hide();
				} else {
					Preview.line.show();
				}
			}
			
		});
	}
	
}

Event.observe(window, 'load', Preview.init);

/**
 * Authentication selector
 */

var AuthSelector = {

	selector : null,

	init : function()
	{
		if (!document.getElementById || !document.createElement || !document.appendChild) return false;
		if (!$('li_name')) return false;
		
		AuthSelector.selector = document.createElement('select');
		Element.extend(AuthSelector.selector);
		
		AuthSelector.selector.setAttribute('size', '1');
		Event.observe(AuthSelector.selector, 'change', AuthSelector.change);
		
		visitorOption = document.createElement('option');
		visitorOption.setAttribute('value', 'visitor');
		visitorOption.setAttribute('selected', 'selected');
		visitorOption.appendChild(document.createTextNode('Visiteur'));
		AuthSelector.selector.appendChild(visitorOption);
		
		registerOption = document.createElement('option');
		registerOption.setAttribute('value', 'registered');
		registerOption.appendChild(document.createTextNode('Utilisateur enregistré'));
		AuthSelector.selector.appendChild(registerOption);
		
		label = document.createElement('label');
		Element.extend(label);
		label.addClassName('floated');
		label.appendChild(document.createTextNode('Mode d’identification : '));
		
		line = document.createElement('li');
		line.appendChild(label);
		line.appendChild(AuthSelector.selector);
		
		$('li_name').parentNode.insertBefore(line, $('li_name').parentNode.firstChild);
		
		AuthSelector.change();
	},
	
	change : function()
	{
		$('li_name').hide();
		$('li_email').hide();
		$('li_url').hide();
		$('li_password').hide();
		
		mode = AuthSelector.selector.value;
		
		switch(mode) {
			case 'visitor':
				$('li_name').show();
				$('li_email').show();
				$('li_url').show();
				break;
			case 'registered':
				$('li_name').show();
				$('li_password').show();
				break;
			default:
				$('li_name').show();
				$('li_email').show();
				$('li_url').show();
				$('li_password').show();
		}
	}
	
}

Event.observe(window, 'load', AuthSelector.init);

