/* /static/js/search.js */ /* UTF8 COOKIE éà */

var	searchHotelHash = null;

function	searchInit()
{
	var	i, j;
	var	dest = vkDom.el('SEARCH_DEST');
	var	hotels = vkDom.el('SEARCH_HOTEL');
	var	themes = vkDom.el('SEARCH_THEME');

	if(!dest || !hotels || !themes)
		return false;

	// Build searchHotelHash

	searchHotelHash = {};
	for(i = 0; i < searchAllHotels.length; i++)
		searchHotelHash['H'+searchAllHotels[i].I] = i;

	dest.options.length = 0;
	dest.options[0] = new Option('-- Choix --', 0);
	for(i = 0; i < searchDest.length; i++)
	{
		var last;
		dest.options[last = dest.options.length] = new Option(searchDest[i].N, i);
		dest.options[last].style.fontWeight = 'bold';

		for(j = 0; j < searchDest[i].CT.length; j++)
			dest.options[dest.options.length] = new Option('\xA0\xA0'+searchDest[i].CT[j].N, ''+i+':'+j);
	}
	dest.selectedIndex = 0;

	if(searchDefContinent != null)
	{
		var	idx = 1;

		if(searchDefCountry != null)
		{
			for(i = 0; i < searchDest.length; i++)
			{
				if(searchDest[i].I == searchDefContinent)
				{
					idx++;

					for(j = 0; j < searchDest[i].CT.length; j++)
					{
						if(searchDest[i].CT[j].I == searchDefCountry)
						{
							dest.selectedIndex = idx;
							break;
						}

						idx++;
					}

					if(dest.selectedIndex)
						break;

				}
				else
					idx += 1 + searchDest[i].CT.length;
			}
		}
		else
		{
			for(i = 0; i < searchDest.length; i++)
			{
				if(searchDest[i].I == searchDefContinent)
				{
					dest.selectedIndex = idx;
					break;
				}

				idx += 1 + searchDest[i].CT.length;
			}
		}
	}

	onSearchDestChange();
	// searchWriteAllHotels(hotels);

	themes.options.length = 0;
	themes.options[0] = new Option('-- Choix --', 0);
	for(i = 0; i < searchThemes.length; i++)
		themes.options[themes.options.length] = new Option(searchThemes[i].N, searchThemes[i].I);
	themes.selectedIndex = 0;

	if(window.addEventListener)
	{
		dest.addEventListener('change', onSearchDestChange, false);
		themes.addEventListener('change', onSearchThemeChange, false);
	}
	else if(window.attachEvent)
	{
		dest.attachEvent('onchange', onSearchDestChange);
		themes.attachEvent('onchange', onSearchThemeChange);
	}


	return true;
}

function	searchWriteAllHotels(hotels)
{
	var	i;

	searchEmpty(hotels);

	hotels.options[0] = new Option('-- Choix --', 0);

	for(i = 0; i < searchAllHotels.length; i++)
		hotels.options[hotels.options.length] = new Option(searchAllHotels[i].N, searchAllHotels[i].I);

	hotels.selectedIndex = 0;
}


function	onSearchDestChange()
{
	var	dest = vkDom.el('SEARCH_DEST');
	var	hotels = vkDom.el('SEARCH_HOTEL');

	if(dest.selectedIndex)
	{
		var	tmp, hid;

		searchEmpty(hotels);

		tmp = dest.options[dest.selectedIndex].value.split(':', 2);

		if(tmp.length == 1)
			hid = searchDest[parseInt(tmp[0])].H;
		else if(tmp.length == 2)
			hid = searchDest[parseInt(tmp[0])].CT[parseInt(tmp[1])].H;
		else
			return;

		hotels.options[0] = new Option('-- Choix --', 0);

		for(var i = 0; i < hid.length; i++)
		{
			tmp = searchHotelHash['H'+hid[i]];
			hotels.options[hotels.options.length] = new Option(searchAllHotels[tmp].N, searchAllHotels[tmp].I);
		}
			
		hotels.selectedIndex = 0;
	}
	else
		searchWriteAllHotels(hotels);
}

function	onSearchThemeChange()
{
	var	themes = vkDom.el('SEARCH_THEME');

	window.location = searchThemes[themes.selectedIndex-1].U;
}



function	searchEmpty(el)
{
	var	i;

	for(i = el.options.length-1; i >= 0; i--)
		el.options[i] = null;
	el.options.length = 0;
}

function	onSearchClick()
{
	var	dest = vkDom.el('SEARCH_DEST');
	var	hotels = vkDom.el('SEARCH_HOTEL');
	var	tmp;

	if(!dest.selectedIndex && !hotels.selectedIndex)
	{
		setTimeout(function() { alert('Veuillez sélectionner une destination ou un hôtel pour lancer la recherche.') }, 100 );
		return false;
	}

	if(hotels.selectedIndex)
	{
		window.location = searchAllHotels[searchHotelHash['H'+hotels.options[hotels.selectedIndex].value]].U;
		return false;
	}

	tmp = dest.options[dest.selectedIndex].value.split(':', 2);

	if(tmp.length == 1)
		window.location = searchDest[parseInt(tmp[0])].U;
	else if(tmp.length == 2)
		window.location = searchDest[parseInt(tmp[0])].CT[parseInt(tmp[1])].U;

	return false;
}



if(!searchInit())
	vkDom.onLoad(searchInit);
