function get_ajax_regional()
{	
	$("select.regional_select").change(
		function(e)
		{
			var id = $(this).attr("id");
			var level = id.replace('regional_','');
			
			if(level=='') return false;
			var val = $("#regional_"+level).val();
			var query_search = $("#regional_"+level).children("[@selected]").text();
			if(val=="") return false;
			
			$.ajax(
				{
					method: "post",
					url: site_domain+"ajax/regional/"+val,
					beforeSend: function(){ $("#ajax-loader").show(); },
					complete: function(){  $("#ajax-loader").hide(); },
					success: function(xml){
						var options = '<option value="">...</option>';
						$("node",xml).each(
							function()
							{
								options += '<option value="' +$(this).find("regional_id").text()+ '">' + $(this).find("title").text() + '</option>';
      						}
						);
						var next_level=level*1+1;
						//alert(next_level);
						$("select#regional_"+next_level).html(options);
					}
				 }
			);
		}
	);
}

//autocomplete location 
function lookup(inputString) 
{
	if(inputString.length == 0) {
		// Hide the suggestion box.
		$('#suggestions').hide();
	} else {
		$.ajax(
		{
			method: "post",
			url: site_domain+"ajax/locations/"+inputString,
			dataType:"text",
			success: function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#auto_suggestions_list').html(data);
				}
			}
		}
		);	
	}
} // lookup
	
function fill(thisValue) 
{
	$('#location').val(thisValue);
	setTimeout("$('#suggestions').hide();", 200);
}
//end autocomplete location 


$.fn.check = function(mode) {
	var mode = mode || 'on';
	return this.each(function() {
		switch(mode) {
			case 'on':
				this.checked = true;
				break;
			case 'off':
				this.checked = false;
				break;
			case 'toggle':
				this.checked = !this.checked;
				break;
			}
	});
};

function search_date_pickers()
{
	$(".arrival-date-pick")
		.datePicker({createButton:false})
		.bind(
			'click',
			function()
			{
				update_arrival($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		)
		.bind(
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				update_arrival(selectedDate);
			}
		)
		.bind(
			'dpClosed',
			function(e, selected)
			{
				update_arrival(selected[0]);
			}
		);
	$(".departure-date-pick")
		.datePicker({createButton:false})
		.bind(
			'click',
			function()
			{
				update_departure($(this).dpGetSelected()[0]);
				$(this).dpDisplay();
				return false;
			}
		)
		.bind(
			'dateSelected',
			function(e, selectedDate, $td, state)
			{
				update_departure(selectedDate);
			}
		)
		.bind(
			'dpClosed',
			function(e, selected)
			{
				update_departure(selected[0]);
			}
		);
	var update_arrival = function (selectedDate)
	{
		
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();
		//alert(y);
		($('#sday')[0]).selectedIndex = d;
		($('#smonth')[0]).selectedIndex = m+1;
		($('#syear')[0]).selectedIndex = (y-really_today.getFullYear())+1;
		if($("#eday").val()=="" && $("#emonth").val()=="" && $("#eyear").val()=="")
		{
			($('#eday')[0]).selectedIndex = d+1;
			($('#emonth')[0]).selectedIndex = m+1;
			($('#eyear')[0]).selectedIndex = (y-really_today.getFullYear())+1;
		}
	}
	var update_departure = function (selectedDate)
	{
		
		selectedDate = new Date(selectedDate);
		var d = selectedDate.getDate();
		var m = selectedDate.getMonth();
		var y = selectedDate.getFullYear();
		($('#eday')[0]).selectedIndex = d;
		($('#emonth')[0]).selectedIndex = m+1;
		($('#eyear')[0]).selectedIndex = (y-really_today.getFullYear())+1;	
	}
	
	
	$('#sday, #smonth, #syear')
		.bind(
			'change',
			function()
			{
				if($("#sday").val()!="" && $("#smonth").val()!="" && $("#syear").val()!="")
				{
					var d = new Date(
								$('#syear').val(),
								$('#smonth').val()-1,
								$('#sday').val()
							);
					$('.arrival-date-pick').dpSetSelected(d.asString());
				}
			}
		);
		
	$('#eday, #emonth, #eyear')
		.bind(
			'change',
			function()
			{
				if($("#eday").val()!="" && $("#emonth").val()!="" && $("esyear").val()!="")
				{
					var d = new Date(
								$('#eyear').val(),
								$('#emonth').val()-1,
								$('#eday').val()
							);
					$('.departure-date-pick').dpSetSelected(d.asString());
				}
			}
		);
	
		
	var today = new Date();
	var really_today = new Date();
	if($('#sday')[0] && $('#smonth')[0] && $('#syear')[0] && $('#eday')[0] && $('#emonth')[0] && $('#eyear')[0])
	{
		if($('#sday').val()!="" && $("#smonth").val()!="" && $("#syear").val()!="")
		{
			($('#sday')[0]).selectedIndex = $("#sday").val();
			($('#smonth')[0]).selectedIndex = $("#smonth").val();
			($('#syear')[0]).selectedIndex = ($("#syear").val()-really_today.getFullYear())+1;
		}
		else 
		{
			($('#sday')[0]).selectedIndex = 0;
			($('#smonth')[0]).selectedIndex = 0;
			($('#syear')[0]).selectedIndex = 0;
		}
		
		if($('#eday').val()!="" && $("#emonth").val()!="" && $("#eyear").val()!="")
		{
			($('#eday')[0]).selectedIndex = $("#eday").val();
			($('#emonth')[0]).selectedIndex = $("#emonth").val();
			($('#eyear')[0]).selectedIndex = ($("#eyear").val()-really_today.getFullYear())+1;
		}
		else
		{
			($('#eday')[0]).selectedIndex = 0;
			($('#emonth')[0]).selectedIndex = 0;
			($('#eyear')[0]).selectedIndex = 0;
		}
		
		$('#sday').trigger('change');
		$('#eday').trigger('change');
	}
}

function wishlist()
{
	$(".add_to_wishlist").click(
		function(e)
		{
			var property_id = $(this).attr("rel");
			if(property_id=="") return false;
			$.ajax(
				{
					method: "post",
					url: site_domain+"ajax/add_to_wishlist/"+property_id,
					//beforeSend: function(){ $("#ajax-loader").show(); },
					//complete: function(){  $("#ajax-loader").hide(); },
					success: function(data){
						//alert(data);
						$(".wishlist_number").html(data);
					}
				 }
			);
		}
	);
	$(".remove_from_wishlist").click(
		function(e)
		{
			var property_id = $(this).attr("rel");
			if(property_id=="") return false;
			$.ajax(
				{
					method: "post",
					url: site_domain+"ajax/remove_from_wishlist/"+property_id,
					//beforeSend: function(){ $("#ajax-loader").show(); },
					//complete: function(){  $("#ajax-loader").hide(); },
					success: function(data){
						//alert(data);
						$(".wishlist_number").html(data);
						window.location.href = site_domain+'search/wishlist';
					}
				 }
			);
		}
	);
}


$(document).ready(
function(){
	get_ajax_regional();
	$("#logo").pngFix();
	$(".item-featured").pngFix();
	search_date_pickers();
	wishlist();
});
