var save_mode = ['regular_save', 'ajax_save', 'autosave'];

$(function(){
	$("#act").val(save_mode[1]);

	// START SUBMIT FORM VIA AJAX
	$("#article_data").submit( function() {
		$("#article_data").ajaxSubmit( {
			data: {
				ajax: 1
			},
			beforeSerialize: function() {
				CKEDITOR.instances.body.updateElement();
			},
			dataType: 'json',
			error: function(a, b, c) {
				alert('data error');
				$("#act").val(save_mode[1]);
			},
			success: function(data) {
				if (save_mode[2] == $("#act").val())
				{
					$("#save_notif").fadeIn().fadeOut(3000);
				}
				else if (data['err'])
				{
					$("#report").html(data['msg']);
					$("#report").addClass('error');
				}
				else if ('' == data['err'])
				{
					location.href = sbr['SBR_URL'];
				}
				$("#act").val(save_mode[1]);
			}
		});
		return false;
	});
	// END SUBMIT FORM VIA AJAX

	/* Setting up the tree categories */
	var treeCat = new esyndicat.tree({
		id: 'tree',
		type: 'radio',
		state: '',
		url: 'controller.php?package=publishing&file=get_categories',
		hideRoot: true,
		expandableLocked: true,
		callback: function()
		{
			$('#category_id').val(this.value);
			$('#change_cat>span').html($(this).attr('title'));
		}
	});
	/* Initialization tree categories */
	treeCat.init();
	/* Event handler for displaying tree categories */
	$('#change_cat').click(function()
	{
		$('#tree').toggle();
		return false;
	});
});

function autosave_handler()
{
	$("#act").val(save_mode[2]);
	$("#article_data").submit();
}
