// JavaScript Document

//I like this "feature", makes the interface a bit more usable without the hassle for the coder ;)
$.fn.clearField = function() {
    return this.focus(function() {
        if( this.value == this.defaultValue ) {
            this.value = "";
        }
    }).blur(function() {
        if( !this.value.length ) {
            this.value = this.defaultValue;
        }
    });
	
};	

$().ready(function() {

	//Change this to the ID of the country input you want to be autocompleted
	//make sure to update the CSS for this ID as well
	var ac_country = "#ac_country";
	

	//options are the same as the JQuery Autocomplete plugin
	$(ac_country).autocomplete(countries, {
		minChars: 2,
		width: 220,
		matchContains: true,
		scroll: true,
		max:0,
		formatItem: function(row, i, max, term) {
			return "<img src='images/flags/" + row.code.toLowerCase() + ".gif'/> " + row.name;
		},
		formatResult: function(row) {
			return row.name;
		},
		formatMatch: function(row, i, max) {
			return row.name;
		}
	});

	$(ac_country).after($(ac_country).clone().attr('value','').attr('id', $(ac_country).attr('id') + '_hidden'));
	$(ac_country).removeAttr('name', '').clearField();
	$(ac_country).result(function(event, data, formatted) {
		if(data) {
			$(ac_country + '_hidden').val(data.code.toLowerCase());
		}
		var src = 'images/flags/' + data.code.toLowerCase() + '.gif';
		$(ac_country).css('backgroundImage', 'url(' + src + ')');
	});
	
	$('.datepicker').datepicker({dateFormat:'dd/mm/yy'});
	
	$('.datetimepicker').datepicker({ 
		dateFormat:'dd/mm/yy',
		duration: '',  
        showTime: true,  
        constrainInput: false,  
        stepMinutes: 1,  
        stepHours: 1,  
        altTimeField: '',  
        time24h: true
		
		
		});
	
	$('#air_form, #hotel_form').validate();
	$(".radio").dgStyle();
	$(".checkbox").dgStyle();
	
	
	$(".point a").hover(map_show_info, function(){ 
		//$(this).next().hide(); 
	});
	
	$('#city_more_link > a, .point > a').click(show_city);
	$('#back_to_the_map, #places_tab a').click(show_map);
	
});


function map_show_info(){
	$(this).next().show();
	var a = $(this);
	$('#city_more_link a').attr('href', a.attr('href') ).attr( 'rel', a.attr('rel') );
	$('#slider').stop().animate({'top':'120px'}, 500, 'swing', function(){
			$('#slider').html( a.next().next().html()).animate({'top':'0px'}, 500, 'swing');
		});	
}
function show_city(event){
	event.preventDefault();
	var a = this;
	$.post('js/ajx.php', {'id':a.rel, 'type':'place'}, function(data){
			//data = eval(data);
			//alert(data);
			var d =eval(data);
			$('#city_details_container h2').text(d[1]);
			$('#city_details_container p').html(d[2]);
			$('#city_details_container > img').attr('src', 'images/places/big/'+d[0]+'.jpg');
			setTimeout("$('.right_column_wide').animate({'height': $('#city_details_container').outerHeight() - 30 + 'px'}, 500, 'swing')",1250);
			
		});
	$('#map_container').animate({'left':'530px'}, 800, 'swing', function(){
				$('#map_container').hide();
				$('#city_details_container').css('top', '400px').animate({'top': '3px'}, 400, 'swing', function(){
						setTimeout("$('#places_tab').css('top', -($('#city_details_container').outerHeight() - 70) + 'px').animate({'left':'521px'}, 300, 'swing')", 1200);
					
					}) ;
				
			}
		);
}

function show_map(event){
	event.preventDefault();
	
	
	$('#places_tab').animate({'left':'544px'}, 200, 'swing', function(){
	
		$('#city_details_container').animate({'top': '403px'}, 400, 'swing', function(){
							$('#map_container').show();
							$('#map_container').animate({'left':'0px'}, 800, 'swing', function(){
								$('.right_column_wide').animate({'height': '430px'}, 400, 'swing') ;
						}
					);	
				
				}
			) ;
	});
}


