$(document).ready(function() {



	if ($("table.sortable tbody").length){

		$("table.sortable tbody").sortable({
			handle		: '',
			cursor		: "move",

			axis		: "y",

			helper		: function(e, ui) {
							ui.children().each(function() {
								$(this).width($(this).width());
							});
							return ui;
						},

			update		: function () {

							var order = sort.sortable(
								'serialize',
								$('#forms.reorder.link').val()
							);

							$.ajax({
								url:$('#forms-reorder-link').val(),
								type:'post',
								data:order,
								success: function(msg){
									
								
								}
							});

						},

			create		: function() {
								
			}
		}).disableSelection();
	}



	//tables with drag sort
	
	var sort = $("#STBFormSort tbody");

	sort.sortable({
		handle : '',
		update : function () {

			var order = sort.sortable(
				'serialize',
				$('#forms.reorder.link').val()
			);

			$.ajax({
				url:$('#forms-reorder-link').val(),
				type:'post',
				data:order,
				success: function(msg){
					
				
				}
			});

		},

		cursor		: "move",

		axis		: "y",

		helper		: function(e, ui) {
						ui.children().each(function() {
							$(this).width($(this).width());
						});
						return ui;
					},

		create	: function() {
		}
	});


	$("#STBFormSort").disableSelection();


	//load more records
	var oxbLoadMore = $('#OXB-load-more');
	oxbLoadMore.find("a").click(function(){
		//$(this)
		//alert($(this).html());
		
		var oxbLoadMorePage = oxbLoadMore.find("input[name='ajax.load.more.page']")
		
		//increase the page number		
		var page = 	parseInt(oxbLoadMorePage.val()) + 1;
		oxbLoadMorePage.val(page);

		$.ajax({
			url: oxbLoadMore.find("input[name='ajax.load.more.url']").val()  + '&page=' + page,
			type:'post',
			data:"",
			success: function(msg){

				if (msg != "empty"){
					sort.append(msg); 
				} else {


					//i have no more pages hide the load more
					$('#OXB-load-more').closest("tfoot").remove();
					//$('.paging').remove();

				}				
			}
		});

	});



});