//	-----------------------------------------------------------------------------------------
//	Questa funzione viene chiamata all'apertura e alla chiusura del bookmark.
//	-----------------------------------------------------------------------------------------
function toggleBookmarks(pageShift)
{
//	Recupero l'elemento del DOM del selettore, ovvero l'immagine nel link di apertura/chiusura
	var selector = document.getElementById(bookmarkParams['selector_id']);
	
//	Operazioni in caso di bookmark aperto
	if ( bookmarkParams['visibility'] )
	{
	//	Nascondo l'area del bookmark
		hideElement(bookmarkParams['bookmark_id']);
	
	//	Aggiorno l'immagine del selettore
		if ( selector )
			selector.src = bookmarkParams['selector_close_src']
		
	//	Aggiorno lo stato di visibilità
		bookmarkParams['visibility'] = false;
	}

//	Operazioni in caso di bookmark chiuso
	else
	{
	//	Mostro l'area del bookmark
		showElement(bookmarkParams['bookmark_id']);
		
	//	Aggiorno gli hotel presenti
		showBookmarks(pageShift)
		
	//	Aggiorno l'immagine del selettore
		if ( selector )
			selector.src = bookmarkParams['selector_open_src']

	//	Aggiorno lo stato di visibilità
		bookmarkParams['visibility'] = true;
	}
}


//	-----------------------------------------------------------------------------------------
//	Questa funzione viene chiamata all'apertura del bookmark o negli scorrimenti di pagina.
//	In ingresso c'è la pagina richiesta (caso scorrimento); 
//	se non è presente (caso apertura) viene inizializzata.
//	-----------------------------------------------------------------------------------------
function showBookmarks(pageShift)
{
//	Calcolo la pagina iniziale, se non specificata
	if ( !bookmarkParams['page'] && bookmarkParams['position'] !== undefined )
		bookmarkParams['page'] = parseInt( ( bookmarkParams['position'] - 1 ) / bookmarkParams['page_length'] ) + 1;
		
//	Esegui lo scorrimento della pagina
	if ( pageShift )
	{
		bookmarkParams['page'] += pageShift;
		
		if ( bookmarkParams['page'] < 1 )
			bookmarkParams['page'] = 1;
			
		/*
		if ( pageShift > 0 && !bookmarkParams['hotels'][(bookmarkParams['page']-1)*bookmarkParams['page_length']] )
	//	Aumento della pagina
		if ( pageShift>0 && !bookmarkParams['last_page'] )
		
	//	Diminuzione della pagina
		if ( pageShift<0 && !bookmarkParams['first_page'] )
			bookmarkParams['page'] += pageShift;
		*/
	}
	
//	Recupero la lista degli hotel con ajax
//	Specifico la pagina, se nota, altrimento l'id dell'hotel da ricercare
	var myUrl = '/webservice/getHotelsXML/city_id='+bookmarkParams['city_id']+'&checkin_monthday='+bookmarkParams['checkin_monthday']+'&checkin_year_month='+bookmarkParams['checkin_year_month']+'&checkout_monthday='+bookmarkParams['checkout_monthday']+'&checkout_year_month='+bookmarkParams['checkout_year_month']+'&do_availability_check='+bookmarkParams['do_availability_check']+'&order_by='+bookmarkParams['order_by']+'&landing='+bookmarkParams['landing']+'&ts='+(new Date()).getTime();
	if ( bookmarkParams['page'] )
		myUrl += '&offset='+((bookmarkParams['page']-1)*bookmarkParams['page_length'])+'&limit='+(bookmarkParams['page_length']+1);
	else
		myUrl += '&hotel_id='+bookmarkParams['hotel_id']+'&page_length='+bookmarkParams['page_length'];
	new Ajax.Request( myUrl, { method:'post', onSuccess: loadHotels, onFailure: failure });
}

function loadHotels(transport, json)
{
//	Recupero gli elementi hotel dall'xml di risposta
	var hotels = transport.responseXML.documentElement.getElementsByTagName('hotel');
	
//	Inserisco gli elementi nell'array principale degli hotel
	for ( i=0; i<hotels.length; i++ )
	{
		var hotel = {
			hotel_id		: hotels[i].attributes.getNamedItem("hotel_id").value,
			hotel_name	: hotels[i].attributes.getNamedItem("hotel_name").value,
			hotel_class	: hotels[i].attributes.getNamedItem("hotel_class").value,
			url			: hotels[i].attributes.getNamedItem("url").value,
			thumb_url	: hotels[i].attributes.getNamedItem("thumb_url").value,
			currency		: hotels[i].attributes.getNamedItem("currency").value,
			minrate		: hotels[i].attributes.getNamedItem("minrate").value,
			position		: hotels[i].attributes.getNamedItem("position").value
		}
		
	//	Aggiorno il valore della pagina, se necessario
		if ( bookmarkParams['page'] == undefined )
			bookmarkParams['page'] = parseInt( hotel['position'] / bookmarkParams['page_length'] ) + 1;

	//	Memorizzo gli hotel recuperati divisi per pagina
		bookmarkParams['hotels'][(bookmarkParams['page']-1)*bookmarkParams['page_length']+i] = hotel;
	}
	
//	Aggiorno la visualizzazione
	showHotels(bookmarkParams['page']);
}

function showHotels(page)
{
//	Recupero l'elemento contenitore della lista hotel
	var bookmarkElement	= document.getElementById(bookmarkParams['container_id']);
	
//	Reset del contenuto
	bookmarkElement.innerHTML = '';
	
//	Loop sugli hotel
	for ( i=0; i<bookmarkParams['page_length']; i++ )
	{
	//	Recupero i dati dell'hotel
		var hotel = bookmarkParams['hotels'][(parseInt(bookmarkParams['page'])-1)*bookmarkParams['page_length']+i];
		
	//	Inserisco i dati dell'hotel nella lista
		if ( hotel )
			bookmarkElement.innerHTML += '<p><a href="' + unescape(hotel['url']) +  '"><img class="picture" src="' + unescape(hotel['thumb_url']) + '" alt="' + hotel['hotel_name'] + '"><br><strong>' + hotel['hotel_name'] +'</strong></a><br><img src="' + imgDir + '/class-' + hotel['hotel_class'] + '.gif"><br><span>'+bookmarkParams['from']+' '+hotel['currency']+' '+hotel['minrate']+'</span></p>';
	}
	//alert(bookmarkParams['page'])

//	Stabilisco se sono agli estremi delle pagine ed aggiorno la visibilità dei link di conseguenza
	bookmarkParams['first_page']	= bookmarkParams['page'] == 1;
	bookmarkParams['last_page']	= !bookmarkParams['hotels'][bookmarkParams['page']*bookmarkParams['page_length']];
	document.getElementById(bookmarkParams['prev_id']).style.visibility = bookmarkParams['first_page'] ? 'hidden' : 'visible';
	document.getElementById(bookmarkParams['next_id']).style.visibility = bookmarkParams['last_page'] ? 'hidden' : 'visible';
}

function failure()
{
	//alert('errore')
}