/*-------------------------GLOBALS-------------------------*/

var currentRecord = 0;
var pagingSize 	= 1;
var recordCount;
var previousPage;
var currentPage;
var nextPage;
var active = false;
/*---------------------------------------------------------------*/

//Event.observe(window, 'load', init, false);

function init(dest) {

	showNavigation();
	//getRecordCount();
	getTableData(dest);
	setTimeout(
		function() {
			getNextData(dest);
		}, 500
	);
}

function getTableData(dest) {
	var myAjax = new Ajax.Request(
			'http://www.hotelrentalgroup.com/flow.php?action=page&current='+currentRecord+'&size='+pagingSize+'&dest='+dest, 
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
					
					currentPage = eval('(' + response.responseText + ')');
					drawTable(currentPage, $('view'));
				}
			});
}

function getNextData(dest) {
	var myAjax = new Ajax.Request(
			'http://www.hotelrentalgroup.com/flow.php?action=page&current='+(currentRecord+pagingSize)+'&size='+pagingSize+'&dest='+dest, 
			{
				method: 'get', 
				parameters: '', 
				onComplete: function(response) {
				
					nextPage = eval('(' + response.responseText + ')');
					active = false;
					
			//		drawTable(nextPage, $('next'));
				}
			});
}

function getPreviousData(dest) {
	if((currentRecord - pagingSize) >= 0) {
	
		var myAjax = new Ajax.Request(
				'http://www.hotelrentalgroup.com/flow.php?action=page&current='+(currentRecord-pagingSize)+'&size='+pagingSize+'&dest='+dest, 
				{
					method: 'get', 
					parameters: '', 
					onComplete: function(response) {
						
						previousPage = eval('(' + response.responseText + ')');
						active = false;
				//		drawTable(previousPage,$('previous'));
					}
				});
	}

}

/*---------------------------------------------------------------*/

function drawTable(page, contain) {
	
	

	table = 	'<table width="100%">';
	alt = '';
	for(i = 0; i < page['hotels'].length; i++) {

table +=		'<tr bgcolor="#FFFFFF">' +
						'<td align="center"><a href="http://www.hotelrentalgroup.com/'  + page['hotels'][i].HotelCode + '/' + page['hotels'][i].Name  + ' hotel.htm" class="bluelinks9" target="_blank">' + page['hotels'][i].Name 	+ '</a></td></tr><tr><td bgcolor="#DDDDDD"></td></tr><tr><td align="center">' +
						'<a href="http://www.hotelrentalgroup.com/'  + page['hotels'][i].HotelCode + '/' + page['hotels'][i].Name  + ' hotel.htm" class="bluelinks9"  target="_blank"><img src="http://www.hotelbeds.com/giata/small/'+ page['hotels'][i].ImagePath +'" border=0 /></a></td></tr><tr><td bgcolor="#DDDDDD"></td></tr><tr><td align="center">' + page['hotels'][i].CategoryCode	+ '</td></tr>';


		
								
		(alt == '')
			?	alt = 'alt'
			:	alt = '';
	}
	
	table += '</table>';

	contain.innerHTML = table;
}

function getNextPage(dest) {
if(!active) {
		currentRecord += pagingSize;
		showNavigation();
		previousPage = currentPage;
		currentPage = nextPage;
		drawTable(currentPage, $('view'));
		getNextData(dest);

	}
}

function getPreviousPage(dest) {
	if(!active) {
		currentRecord -= pagingSize;
		showNavigation();
		nextPage = currentPage;
		currentPage = previousPage;
		drawTable(currentPage, $('view'));
		getPreviousData(dest);
	}
}

function showNavigation() {
	$('currentRec').innerHTML = "Record " + currentRecord; 
	(currentRecord == 0)
		?	$('previousLink').style.visibility = 'hidden'
		:	$('previousLink').style.visibility = 'visible';
	((currentRecord + pagingSize) >= recordCount)
		?	$('nextLink').style.visibility = 'hidden'
		:	$('nextLink').style.visibility = 'visible';
		
}

