//this is a global variable to have only one instance of the calendar
var calendar = null;

function wysiwyg(theme)
{
	if (theme == 'simple')
	{
		tinyMCE.init({
			mode: "textareas",
			language: "fr",
			entity_encoding: "raw",
			theme: theme
		});
	}
	else
	{
		tinyMCE.init({
			mode: "textareas",
			language: "fr",
			direction: "ltr",
			convert_urls: true,
			entity_encoding: "raw",
			theme: "advanced",
			
			theme_advanced_toolbar_location: "top",
			theme_advanced_toolbar_align: "center",
			theme_advanced_path_location: "bottom",
			
			paste_use_dialog: true,
			plugin_preview_width: "500",
			plugin_preview_height: "600",
			theme_advanced_disable: "help,strikethrough,fontselect,fontsizeselect",
			theme_advanced_buttons2_add_before: "image,pastetext,pasteword,search,replace,separator,FormManager,Media"
		});
	}
}

function resize()
{
	if ($('contenu') != null)
	{
		var barre_infos = $('barre_infos').offsetHeight;
		var page = $('page').offsetHeight;
		var hauteur = 0;
		
		if (barre_infos > page)
		{
			hauteur = barre_infos;
		}
		else
		{
			hauteur = page;
		}
				
		$('page').style.height = hauteur - 10 + 'px';
		$('barre_infos').style.height = hauteur - 10 + 'px';
		$('contenu').style.height = hauteur + 'px';
	}
}

function authentification()
{
	if ($F('login') != '' && $F('pass') != '')
	{
		var xmlHttp = new Ajax.Request('privee/login.php',
							{
								method: 'post',
								asynchronous: false,
								parameters: 'login=' + $F('login') + '&pass=' + $F('pass')
							});
		
		if (xmlHttp.transport.responseText != '')
		{
			window.location.href = '/' + xmlHttp.transport.responseText + '/';
		}
		else
		{
			afficher_message('message_login', 'echec', 'Erreur d\'identifiant ou mot de passe.');
		}
	}
	else
	{
		afficher_message('message_login', 'echec', 'Veuillez saisir votre identifiant et mot de passe.');
	}
}

function deconnexion()
{
	new Ajax.Request('privee/logout.php',
					{
						method: 'post',
						asynchronous: false
					});
	
	window.location.href = '/login/';
}

function envoyer_email_inscription(destinataire)
{
	var xmlHttp = new Ajax.Request('rubriques/login/email_inscription.php',
					{
						method: 'post',
						asynchronous: false,
						parameters: 'destinataire=' + destinataire
					});
}

function afficher_message(id, type, message)
{
	$(id).style.display = 'block';
	$(id).className = type;
	$(id).innerHTML = message;
}

//@element   => is the <div> where the calender will be rendered by Scal.
//@input     => is the <input> where the date will be updated.
//@container => is the <div> for dragging.
//@source    => is the img/button which raises up the calender, the script will locate the calenar over this control.
function showCalendar(element, input, container, source)            
{
	if (!calendar)
	{
		container = $(container);
		//the Draggable handle is hard coded to "rtop" to avoid other parameter.
		new Draggable(container, {handle: "rtop", starteffect: Prototype.emptyFunction, endeffect: Prototype.emptyFunction});
		
		//The singleton calendar is created.
		calendar = new scal(element, $(input), 
		{
			updateformat: 'dd/mm/yyyy', 
			closebutton: 'x', 
			wrapper: container
		}); 
	}
	else
	{
		calendar.updateelement = $(input);
	}
	
	var date = $F(input).split('/');
	date = new Date(date[2], date[1]-1, date[0]);
	
	calendar.setCurrentDate(isNaN(date) ? new Date() : date);
	
	//Locates the calendar over the calling control  (in this example the "img").
	if (source = $(source))
	{
		Position.clone($(source), container, {setWidth: false, setHeight: false, offsetLeft: source.getWidth() + 2});
	}
	
	//finally show the calendar =)
	calendar.openCalendar();
}
