$.extend({
	hookExecute: function (function_name){
		if (eval('typeof '+function_name+' == "function"')){
			eval(function_name+'();')
			return true;
		}else{
			return false;
		}
	}
});

$(document).ready(function(){
$.fn.ajaxTable	= function(options){
	var	tableId	= $(this).attr('id');
	var	total	= 0;
	var	options	= $.extend({
		url: '',
		data: {},
		page: 0,
		per_page: 20,
		sortname: false,
		sortorder: 'ASC',
		prehook: false,
		hook: false,
		hook_new: new Function()
	}, options);
	if (!options.url)
		return	false;

	if (options.prehook)
		eval('options = '+options.prehook+'(options);');

	options.data['page']		= options.page;
	options.data['per_page']	= options.per_page;
	options.data['sortname']	= options.sortname;
	options.data['sortorder']	= options.sortorder;

	var	$table		= $('#'+tableId);						//	Ajax Table
	var	$loading	= $('#'+tableId+'_loading');			//	Loading DIV
	var	colCount	= $('#'+tableId+' thead th').length;	//	Table Colummns Count

	if (!$loading.length)
	{
		$table.after('<div id="'+tableId+'_loading">Загрузка ...<br /><img src="/img/ajax-loader.gif" /></div>');
		var	$loading	= $('#'+tableId+'_loading');
		o	= $table.offset();
		$loading.css({
			display: 'none',
			position: 'absolute',
			backgroundColor: '#FFF',
			width: $table.width(),
			height: $table.height(),
			left: o.left,
			top: o.top,
			textAlign: 'center'
		});
	}
	$loading.css('height',$table.height()).css('padding-top',Math.round($table.height()/2));

	if (!$('#'+tableId).hasClass('initialised'))
	{
		$('#'+tableId).addClass('initialised');
		if (options.sortname)
		{
			sortorder	= (options.sortorder=='ASC')?'asc':'desc';
			$('#'+tableId+' thead th[name='+options.sortname+']').addClass(sortorder);
		}
		$('#'+tableId+' thead th[@name]').click(function(){
			var	sortorderAsc	= $(this).hasClass('asc');
			$('#'+tableId+' thead th').removeClass('asc').removeClass('desc');
	
			options.sortname	= $(this).attr('name');
			if (sortorderAsc)
			{
				$(this).removeClass('asc').addClass('desc');
				options.sortorder	= 'DESC';
			}
			else
			{
				$(this).removeClass('desc').addClass('asc');
				options.sortorder	= 'ASC';
			}
			$('#'+tableId).ajaxTable(options);
		});
	}

	$.ajax({
		url: options.url,
		type: 'POST',
		dataType:'xml',
		data: options.data,
		success: function(xmlResponse){
			total	= $('total',xmlResponse).text();
			page	= $('page',xmlResponse).text();
			var	total_pages	= Math.ceil(total / options.per_page);
			var	table_rows	= new Array();
			$('row',xmlResponse).each(function(id) {
				row = $('row',xmlResponse).get(id);
				var	row_html	= '';
				$('cell',row).each(function(cell_id){
					cell = $('cell',row).get(cell_id);
					celltext	= $(cell).text();
					if (!celltext)
						celltext	= '&nbsp;';
					row_html	+= '<td>'+celltext+'</td>';
				});
				table_rows.push('<tr>'+row_html+'</tr>');
			});
			trows	= table_rows.join('')
			if (!trows)
				trows	= '<tr><td colspan="'+colCount+'" class="nodata">нет записей</td></tr>';
			$('#'+tableId+' tbody').empty().html(trows);
			$('#'+tableId+' tbody tr:nth-child(odd)').addClass('even');
			$('#'+tableId+' tfoot span.page').html(parseInt(page)+1);
			$('#'+tableId+' tfoot span.total').html(total_pages);
			$('#'+tableId+' tfoot input.total_items').val(total);
			if (total)
			{
				from	= page*options.per_page+1;
				to		= (page+1)*options.per_page;
				if (to>total)
					to	= total;
				$('#'+tableId+' tfoot span.status').html('Записи с '+from+' по '+to+' из '+total);
			}
			else
				$('#'+tableId+' tfoot span.status').html('Нет записей');

			if (options.hook)
				$.hookExecute(options.hook);
			options.hook_new();	//	new hook system
		}
	});

	if (!$('#'+tableId+' tbody').length)
	{
		$('#'+tableId).append('<tbody></tbody>');
	}

	if (!$('#'+tableId+' tfoot').length)
	{
		$('#'+tableId).append('<tfoot></tfoot>');
		$('#'+tableId+' tfoot').html('<tr><td colspan="'+colCount+'"><a class="icon first" title="first"></a><a class="icon prev" title="prev"></a>Страница <span class="page">0</span> из <span class="total">1</span><a class="icon next" title="next"></a><a class="icon last" title="last"></a><a class="icon refresh" title="refresh"></a> <span class="status"></span><input type="hidden" class="total_items" /></td></tr>');
	}

	if (!$('#'+tableId+' tfoot').hasClass('initialised'))
	{
		$('#'+tableId+' a.refresh').ajaxStart(function(){
			/*
			$(this).css('background-image','url(/img/ajaxtable/load.gif)');
			$('#'+tableId+' tfoot span.status').html('Загрузка ...');
			$('#'+tableId+' tfoot a').block({ message: null }); 
			*/
			$loading.show();
		}).ajaxStop(function(){
			/*
			$(this).css('background-image','url(/img/ajaxtable/load.png)');
			$('#'+tableId+' tfoot a').unblock();
			*/
			$loading.hide();
		});

		$('#'+tableId+' tfoot').addClass('initialised');
		$('#'+tableId+' tfoot').find('a.first').click(function(){
			options.page	= 0;
			$('#'+tableId).ajaxTable(options);
		});
		$('#'+tableId+' tfoot').find('a.prev').click(function(){
			options.page--;
			if (options.page<0)
				options.page	= 0;
			$('#'+tableId).ajaxTable(options);
		});
		$('#'+tableId+' tfoot').find('a.next').click(function(){
			var	total_pages	= $('#'+tableId+' tfoot span.total').html();
			options.page++;
	//console.log(options.page+'/'+total_pages);
			if (options.page > total_pages - 1)
				options.page	= total_pages - 1;
			$('#'+tableId).ajaxTable(options);
		});
		$('#'+tableId+' tfoot').find('a.last').click(function(){
			var	total_pages	= $('#'+tableId+' tfoot span.total').html();
			options.page	= total_pages - 1;
			$('#'+tableId).ajaxTable(options);
		});
		$('#'+tableId+' tfoot').find('a.refresh').click(function(){
			$('#'+tableId).ajaxTable(options);
		});
	}
};
});