var categories={};

categories.init=function() {
	

	$('table.datas').dataTable();
	$('table.datas tbody').sortable(
		{
			helper: "clone",
			handle: "span.handle",
			update: function(event, ui) {
				categories.reorder(ui.item);
			}
		}
	);
	
		
}

categories.editCategories=function() {
	
}

categories.editData=function(categoryId) {
	
	$.post('/index.php',{'jfunc':'categories.adminEdit','contentId':$('#contentId').val(),'categoryId':categoryId},function(data) {
		//alert(data);
		data=$.parseJSON(data);
		
		$('#dataEdit').show();
		$('#dataListing').hide();
		$('#categoryId').val(data.categoryId);
		$('#parentId').val(data.parentId);
		$('#title_fr').val(data.title_fr);
		$('#title_en').val(data.title_en);
		
	});
}


categories.deleteData=function(categoryId) {
	if(confirm('Are you sure ?')) {
		$.post('/index.php',{'jfunc':'categories.adminDelete','categoryId':categoryId},function(data) {
		window.location.reload();
	});
	}
}

categories.saveData=function(categoryId) {
	datas=$('#form').serialize();
    $.post('/index.php',{'jfunc':'categories.adminSave','categoryId':categoryId,'datas':datas},function(data) {
		//alert(data);
		//window.location.reload();
		admin.addEditWindow('categories');
	});
	
	
}


categories.newData=function() {
	
		$.post('/index.php',{'jfunc':'categories.adminNew'},function(dataId) {
			//alert(dataId);
			categories.editData(dataId);
		});
	
}

categories.reorder=function(obj) {
	var ids=[];
	var table=$(obj).closest('.datas');
	
	table.find('tr').each(function() {
		ids.push($(this).attr('rel'));
	});
	 $.post('/index.php',{'jfunc':'categories.reorder','categories':ids.join(',')},function(data) {
		//alert(data);
		
	});
}

