var geocoder2;
var map;
var country;
var addressd;
var zip;
var locationd;
var city;
var state;
var thumb;
var hotel_name;

var $j = jQuery.noConflict();


function switchImage(src)
{
	document.getElementById("bigimg").src = src;
	$("#bigimg").css({
		width: "200px",
		height: "200px"
	});
}

function filtreCollection(){
	inptv = jQuery('#destination_completion').val();	
	inptv = '^' + inptv.toUpperCase();
	result = "<select id=\"selected_destination\" name=\"destination\" style=\"width: 230px;height:119px\" size=\"7\">";
	for(i in collection){
		city = collection[i];			
		re = new RegExp(inptv);				
		if(re.test(city)){
			result = result + "\n<option value=\""+ city +"\" style=\"border-bottom:1px solid #CFCFCF;color:Black;padding:1px 2px;\">" + city + "</option>";
		}
	}
	result += "</select>";
	jQuery('#destination').html(result);
	jQuery('#selected_destination').change(function(){
			jQuery('#destination_completion').val(jQuery('#destination select').val());
			jQuery('#destination').slideUp();
		});
}

function loopCheck()
{	
	if(press == 0 && endFC == 0){
		filtreCollection();
		endFC = 1;
	}
}

var press = 0;
var endFC = 0;

jQuery(document).ready(function(){
	if (document.getElementById("moteurDeRecherche")) {						
		jQuery('#destination_completion').keyup(function(e){
			if(e.which < 65 && e.which != 8)
				return;
			if(jQuery('#destination_completion').val().length < 1)
				return;
			jQuery('#destination').slideDown();
			jQuery('#destination').html('<br /><img src="Images/citysearch.gif" alt="" />');
			press = 1;
			endFC = 0;
			setInterval("loopCheck()",1000);
			setTimeout(function(){press = 0},500);
		});
		
		jQuery("#nights").change(function(){
			changeCheckOut();
		});

		jQuery("#nbRooms").change(function(){
			roomschanged();
		});

		jQuery("#Room1Childs").change(function(){
			childschanged(1)
		});

		jQuery("#Room2Childs").change(function(){
			childschanged(2)
		});

		jQuery("#Room3Childs").change(function(){
			childschanged(3)
		});
		roomschanged();
		childschanged(1);
		childschanged(2);
		childschanged(3);
	}
	
	if (document.getElementById("passenger_age")) {
		jQuery('#passenger_age').hide();
		
		jQuery('#passenger_type').change(function(){
			if (jQuery('#passenger_type').attr('value') == 'CH') {
				jQuery('#passenger_age').show();
			}
			else {
				jQuery('#passenger_age').hide();
			}
		});
	
		jQuery('#addPassenger').click(function(){
			data = jQuery("#addPassengerForm").serialize();
			setCursor(1);
			action = jQuery("#addPassengerForm").attr("action");
			jQuery.post(action, data, function(data, textStatus){
				jQuery('#active-booking-p').html(data);
				setCursor(0);
				switchButtonCP();
			});
		});
		switchButtonCP();		
	}
			
	if (document.getElementById('addPassengerForm')) {
	
		jQuery('.add-passenger-img').click(function(){
			var tap = $(this).attr("alt");
			jQuery('#' + tap).attr('checked', 'checked');
			
			data = jQuery("#addPassengerForm").serialize();
			setCursor(1);
			action = jQuery("#addPassengerForm").attr("action");
			$.post(action, data, function(data, textStatus){
				jQuery('#active-booking-p').html(data);
				removePassengerC();
				setCursor(0);
			});
			
			jQuery('#' + tap).attr('checked', '');
			
		});						
		setCursor(0);
		removePassengerC();
		
	}
	
	jQuery('.hide').css('display','none');
	jQuery('.hide-fields').click(function(){
		hideFields(this);
	});
	
	jQuery('.sPassengers').change(function(){
		hideFields(this);
	});
	
	jQuery('.list-passengers').click(function(){
		hideFields(this);	
	});
	
	jQuery('.set-child').click(function(){
		val = jQuery(this).attr('value');
		cible = '#' + jQuery(this).attr('alt');
		cible2 = '.' + jQuery(this).attr('alt');		
		if (val == "CH") {
			jQuery(cible).show();
			jQuery(cible2).hide();
		}
		else {
			jQuery(cible).hide();
			jQuery(cible2).show();
		}	
	});			
});

function hideFields(ele)
{
	val = '.' + $(ele).attr('value');
	if(val.length > 3)
	{		
		jQuery(ele).parent().hide();
		jQuery(val).css('display','table-row');
	}
}

function removePassengerC()
{
	jQuery('.deletePassengerC').click(function(){
		index = jQuery(this).attr('rel');
		action = jQuery("#addPassengerForm").attr("action");
		data = 'doSomeWork=removePassenger&index=' + index;
		setCursor(1);
		jQuery.post(action,data,function(data,textStatus){
			jQuery('#active-booking-p').html(data);
			setCursor(0);
		});
	});
}

function setCursor(sw)
{
	if(sw == 0)
	{
		jQuery('body *').css('cursor','default');
		jQuery('body a *, body a, body form input, body form select').css('cursor','pointer');
		jQuery('.add-passenger-img').css('cursor','pointer');
		jQuery('.deletePassengerC').css('cursor','pointer');
	}else{
		jQuery('body *').css('cursor','wait');
	}
}

function removePassenger(webservice,id,elem,room)
{	
	setCursor(1);
	jQuery.get('/v3/recherche/passengers.php?delete=' + id + '&room=' + room + '&webservice=' + webservice,null,function(data){
		id = '#' + id + '-' + room;
		jQuery(id).remove();
		setCursor(0);	
	});
}


function resetBook(webservice)
{
	setCursor(1);
	jQuery.get('/v3/recherche/passengers.php?resetBook=1&webservice=' + webservice,null,function(data){		
		jQuery('#bookingInfo').html('<tr><td colspan="4">No rooms booked for this hotel</td></tr>');
		jQuery('#formSPassengers').html('You must add a least one passenger');		
		//document.location.reload();return(false);
		setCursor(0);	
	});
}

function selectRoom(webservice,room){
	jQuery.get('/v3/recherche/passengers.php?chooseRoom=' + room + '&webservice=' + webservice,null,function(data){
		/*document.location.reload();return(false);*/	
	});
}

function deleteBook(webservice,room){
	jQuery.get('/v3/recherche/passengers.php?deleteBook=' + room + '&webservice=' + webservice,null,function(data){
		document.location.reload();return(false);	
	});
}

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=640,height=480,left = 320,top = 272');");
}

function switchButtonCP()
{		
	jQuery('#confirm_passengers').after('<input type="button" id="ajax_button" name="confirm" value="Confirm" />');
	jQuery('#confirm_passengers').remove();	
	
	jQuery('#ajax_button').click(function(){
		setCursor(1);
		action = jQuery('#formSPassengersForm').attr('action');
		data = jQuery('#formSPassengersForm').serialize();
		jQuery.post(action,data,function(data,textStatus){
			jQuery('#bookingInfo').html(data);
			setCursor(0);
			jQuery('#cancel_booking').after('<input type="submit" name="goBook" value="Save this booking" />\
						<input type="hidden" name="doSomeWork" value="goBook" />');
		});
	});	
}

var activemod = null;
function modify(destination,elem)
{
	if(activemod != null)
	{
		hideActiveMod();
	}
	var elem = document.getElementById(elem);
	jQuery(elem).html('<img src="/v3/recherche/Images/progress.gif" alt="" />');
	data = {destination: destination};
	jQuery.get('/v3/recherche/getCorrespondances.php',data,function(response,textStatus){
		var buff = '<td colspan="3"><form action="/v3/recherche/index.php?page=admin&action=table-correspondance" method="post"><table class="codes-coress">';
		var code_destination;	
		response.each(function(val,index){
			buff = buff + '<tr><td>' + val['webservice'] + '</td><td><input type="text" name="code_correspondance[]" value="' + val['code_correspondance'] + '" /><input type="hidden" name="id[]" value="' + val['id_code_correspondance'] + '" /></td></tr>';
			code_destination = val['code_destination'];						
		});			
		buff = buff + '</table><p><input type="submit" value="Save" /></p><input type="hidden" name="code_destination" value="' + code_destination + '" /></form></td>';
		jQuery(elem).html(buff);
	},'json');	
	activemod = elem;
}

function hideActiveMod()
{
	jQuery(activemod).html('');
}