// upload file functions
var upload_nb = 1;
// add a source to articles
var source_nb = 1;

// function to clean autocomplete tag and add a coma
function tag_cleanup(field,li) {
	field.value = field.value + ', ';
}

function add_article_to_folder(field,li){
	var chxbox = new Template('<input type="radio"/><input type="checkbox" name="articles[]" value="#{id}" id="article_#{id}" checked="checked"/> <label for="article_#{id}">#{title}</label>');
	var article_id = $(li).getAttribute('value');
	var article_title = $F(field);

	if (!$('article_' + article_id)) {
		var content = chxbox.evaluate({id: article_id, title: article_title});
		new Insertion.Bottom($('articles'),content);
	} 
	else {
		alert('Cet article est deja dans ce dossier!');
	}
	$(field).value = '';
}

function add_agreement_to_folder(field,li){
	var chxbox = new Template('<input type="checkbox" name="agreement[]" value="#{id}" id="agreement_#{id}" checked="checked"/> <label for="agreement_#{id}">#{title}</label>');
	var doc_id = $(li).getAttribute('value');
	var doc_title = $F(field);

	if (!$('agreement_' + doc_id)) {
		var content = chxbox.evaluate({id: doc_id, title: doc_title});
		new Insertion.Bottom($('agreement_list'),content);
	} 
	else {
		alert('Cet element est deja dans ce dossier!');
	}
	$(field).value = '';
}


function add_article_alaune(field,li){
	var list = new Template('<li id="article_#{id}" class="sortable">#{title} <input type="text" name="alaune_title_#{id}"/> <input type="file" name="fp_img[]"/></li>');
	var article_id = $(li).getAttribute('value');
	var article_title = $F(field);

	if (!$('article_' + article_id)) {
		var content = list.evaluate({id: article_id, title: article_title});
		new Insertion.Bottom($('order'),content);
	} 
	else {
		alert('Cet article est deja a la une!');
	}
	$(field).value = '';
	Sortable.create('order', {hoverclass:'hovered', onUpdate:function(){new Ajax.Updater('feedback', '/frontend_dev.php/admin/articles_alaune', {asynchronous:true, evalScripts:false, parameters:Sortable.serialize('order')})}})
}

function add_article_recommended(field,li){
	var list = new Template('<li id="article_#{id}"><span id="article_star_#{id}"></span> #{title}</li>');
	var article_id = $(li).getAttribute('value');
	var article_title = $F(field);

	if (!$('article_' + article_id)) {
		var content = list.evaluate({id: article_id, title: article_title});
		new Insertion.Bottom($('stars'),content);
	} 
	else {
		alert('Cet article est deja recommendé!');
	}
	$(field).value = '';
	new Ajax.Updater({success:'article_star_'+article_id},'/article/starer/id/'+article_id);
}

function add_folder_recommended (field, li) {
	var list = new Template('<li id="folder_#{id}"><span id="folder_star_#{id}"></span> #{title}</li>');
	var folder_id = $(li).getAttribute('value');
	var folder_title = $F(field);

	if (!$('folder_' + folder_id)) {
		var content = list.evaluate({id: folder_id, title: folder_title});
		new Insertion.Bottom($('folder_stars'),content);
	} 
	else {
		alert('Ce dossier est deja recommendé!');
	}
	$(field).value = '';
	new Ajax.Updater({success:'folder_star_'+folder_id},'/folder/starer/id/'+folder_id);
}


function add_attachment(elt,dest) {
	newform = elt.clone(true);
	newform.attr('id','upload_'+upload_nb);
	newform.children('input.file_upload').attr('name','attachment_'+upload_nb);
	newform.children('input.file_title').attr('name','attachment_'+upload_nb+'_title');
	newform.children('a.delete').click(function(){
			delete_elt(this);
		});
	jQuery(dest).append(newform);
	newform.show();
	upload_nb++;	
}

function clone_elt (elt,dest) {
	newform = elt.clone(true);
	newform.attr('id','source_'+source_nb);
	newform.children('a.delete').click(function(){
			delete_elt(this);
		});
	jQuery(dest).append(newform);
	newform.show();
	source_nb++;
}

// delete an object and its direct parent (for source and file upload in articles)
function delete_elt (elt) {
	jQuery(elt).parent().remove();
}


function add_member (li) {
	var field = $('users');
	var chxbox = new Template('<li><label for="user_#{id}"><input type="checkbox" name="user[]" value="#{id}" id="user_#{id}" checked="checked"/> #{title}</label></li>');
	var doc_id = $(li).getAttribute('id');
	var doc_title = $(li).getAttribute('value');

	if (!$('user_' + doc_id)) {
		var content = chxbox.evaluate({id: doc_id, title: doc_title});
		new Insertion.Bottom($('member_list'),content);
	} 
	else {
		alert('Ce membre est deja dans cette discussion!');
	}
	$(field).value = '';
	$(field).focus();
}

function set_admin_article (url) {
	$('#admin').attr('action',url);
	$('#publish_comment').show();
}

function discussion_form(thread_id) {
	$('id').value = thread_id;
	$('thread_form').show();
}


jQuery().ready(function(){
	
	jQuery('select.autres').change(function(){
		showHide_userinput(jQuery(this),jQuery('#'+jQuery(this).attr('id') +' option:selected').text());
	}).trigger('change');

	jQuery(':checkbox.autres').change(function(){
		(jQuery(this).attr('checked'))?jQuery(this).next('input').show():jQuery(this).next('input').hide();
	}).trigger('change');

	jQuery('.discussion_add_member').click(function(){
		jQuery(this).load(this.href);
		return false;
	});
	
	jQuery('select#abonnement').change(function(){
		$('#abonnement option:selected').each( function(){
			if ($(this).attr('value') == 'communautaire') $('#period-form').show();
			else $('#period-form').hide(); 
		})
	}).trigger('change');
	
	$(':radio.payer-type').change(function(){
		if ($(this).val() == 'moral' && this.checked) {$('.moral').show(); $('div.physique').hide(); };
		if ($(this).val() == 'physique' && this.checked) {$('.moral').hide(); $('div.physique').show(); }
	}).trigger('change');

	$('select#abonnement_payer_email2').change(function(){
		$('#abonnement_payer_email2 option:selected').each(function(){
			if ($(this).attr('value') == 'other') $('.moral2').show();
			else $('.moral2').hide();
		});
	}).trigger('change');

	$('#go').click(function(){
		if ($('#query-public').attr('value') == '') return false;
	});

});


function showHide_userinput(elt,val)
{
	(val=='Autre')?jQuery(elt).next('input').show():jQuery(elt).next('input').hide();			
}


