function vNewsEnableTitle()
{
	apostrophe.formUpdates({ 'selector': '#v-news-item-title-interface', 'update': 'v-news-title-and-slug' });

	// Form has class, input element has id
	var titleInterface = $('#v-news-item-title-interface');
	var tControls = titleInterface.find('ul.a-controls');
	var tInput = titleInterface.find('.a-title');
	var originalTitle = tInput.val();
	tInput.keyup(function(event) {
		if (tInput.val().trim() != originalTitle.trim())
		{
			titleInterface.addClass('has-changes');
			tControls.fadeIn();
		}
		return false;
	});
	titleInterface.find('.a-cancel').click(function() {
		tInput.val(originalTitle);
		tControls.hide();
		return false;
	});
}

function vNewsEnableSlug()
{
	apostrophe.formUpdates({ 'selector': '#v-news-item-permalink-interface', 'update': 'v-news-title-and-slug' });

	// Form has class, input element has id
	var slugInterface = $('#v-news-item-permalink-interface');
	var tControls = slugInterface.find('ul.a-controls');
	var tInput = slugInterface.find('.a-slug');
	var originalSlug = tInput.val();
	tInput.keyup(function(event) {
		if (tInput.val().trim() != originalSlug.trim())
		{
			slugInterface.addClass('has-changes');
			tControls.fadeIn();
		}
		return false;
	});
	slugInterface.find('.a-cancel').click(function() {
		tInput.val(originalSlug);
		tControls.hide();
		return false;
	});
}

function vNewsEnableNewForm()
{
	var newForm = $('.v-news-admin-new-form');
	newForm.submit(function() {
		var form = $(this);
		apostrophe.updating('.v-news-admin-new-ajax');
		$.post(form.attr('action'), $(this).serialize(), function(data) {
			$(document).append(data);
		});
		return false;
	});
}

function vNewsEnableForm(options)
{
	var changed = false;
	var savedState = null;
	var form = $('#a-admin-form');
	apostrophe.formUpdates({ selector: '#a-admin-form', update: 'a-admin-form' });
	// Due to the way our markup is structured this is a better place for the little
	// 'updating' message
	$('.a-subnav-wrapper').addClass('a-ajax-attach-updating');
	var status = form.find('[name=v_news_item[publication]]');
	var init = true;

	// A convenience within this closure to keep us from getting lazy and
	// using selectors that aren't specific to the form
	function find(sel)
	{
		return form.find(sel);
	}

	status.change(function() {
		var c = form.find('.a-published-at-container');
		var s = status.val();
		if (s === 'schedule')
		{
			c.show();
		}
		else
		{
			c.hide();
		}
		if (!init)
		{
			find('.a-save-news-main .label').text(options['update-labels'][s]);
		}
		init = false;
	});
	status.change();

	find('.template.section select').change(function() {
		alert(options['template-change-warning']);
		// Let the form submit as a full refresh
		$(form).unbind('submit.aFormUpdates');
	});

	p = { 'choose-one': options['categories-choose-label'] };
	if (options['categories-add'])
	{
		p['add'] = options['categories-add-label'];
	}
	aMultipleSelect(form.find('#categories-section'), p);

	function toggleNoEnd(checkbox) {
		$(checkbox).toggleClass('no_end_enabled');
		find('.end_time').toggleClass('time_disabled').toggle();
		find('.end_date').toggleClass('date_disabled').toggle();
	}

	find('.no_end input[type=checkbox]').bind('click', function() {
		toggleNoEnd($(this));
	});

	if (find('.no_end input[type=checkbox]:checked').length)
	{
		toggleNoEnd($(this));
	}
}

function vNewsCheckboxToggle(checkbox)
{ // Toggle any checkbox you want with this one
	checkbox.attr('checked', !checkbox.attr('checked'));
}


function vNewsGetPostStatus()
{
	var postStatus = $('#v_news_item_status');
	return postStatus.val();
}

// Starting to assemble the vNewsConstructor -- eventually all of the JS functions above can be migrated into this space

function vNewsConstructor()
{
	this.sidebarEnhancements = function(options)
	{
		var debug = options['debug'];

		debug ? apostrophe.log('vNews.sidebarEnhancements -- debug') : '';

		$('.a-tag-sidebar-title.all-tags').click(function(){
			$('.a-tag-sidebar-list.all-tags').slideToggle();
			$(this).toggleClass('open');
		});

		$('.a-tag-sidebar-title.all-tags').hover(function(){
			$(this).toggleClass('over');
		},
		function(){
			$(this).toggleClass('over');
		});
	}

	this.slotEditView = function(options)
	{
		var formName = options['formName'];
		var autocompleteUrl = options['autocompleteUrl'];
		var className = options['class'];
		var selfLabelSelector = options['selfLabelSelector'];
		var labelSelector = options['labelSelector'];

		var debug = (options['debug']) ? options['debug'] : false;

		(debug) ? apostrophe.log('vNews.slotEditView -- formName: ' + formName) : '';
		(debug) ? apostrophe.log('vNews.slotEditView -- autocompleteUrl: ' + autocompleteUrl) : '';
		(debug) ? apostrophe.log('vNews.slotEditView -- class: ' + className) : '';

//    aMultipleSelect('#a-' + formName + ' .' + className, { 'autocomplete': autocompleteUrl });
    aMultipleSelect('#a-' + formName + ' .' + className, { 'choose-one': 'Ajouter les titres' });
    aMultipleSelect('#a-' + formName + ' .categories', { 'choose-one': labelSelector });

		var slotEditForm = $('#a-'+formName)
		var editStates = slotEditForm.find('.a-form-row.by-type input[type="radio"]');
		var editState = slotEditForm.find('.a-form-row.by-type input[type="radio"]:checked').val();
		slotEditForm.addClass('a-options dropshadow editState-' + editState );
			editStates.live('click', function(){
			 	editState = slotEditForm.find('.a-form-row.by-type input[type="radio"]:checked').val();
				slotEditForm.removeClass('editState-title').removeClass('editState-tags').addClass('editState-'+editState);
		});
	}

}

window.vNews = new vNewsConstructor();
