var config_url = 'http://www.gauvion.com/canuckrealty';

$(document).ready(function() {
	
	$('.search .input input').focus(function() {	
		if ($(this).val() == 'Enter a city/town name to search in...')
			$(this).val('');
	});
	
	$('.search .input input').blur(function() {	
		if ($(this).val().length == 0)
			$(this).val('Enter a city/town name to search in...');
	});
	
	if ($('#map')) 
	{
		$('#map area').each(function() {		
			$(this).mouseover(function() {
				$prov = $(this).attr('id').replace('area-', '');
				$('#li-'+$prov).show();
				$('#select-'+$prov).addClass('active');
			});			
			$(this).mouseout(function() {
				$prov = $(this).attr('id').replace('area-', '');
				$('#li-'+$prov).hide();
				$('#select-'+$prov).removeClass('active');
			});
		});
		
		$('.select-province a').each(function() {		
			$(this).mouseover(function() {
				$prov = $(this).attr('id').replace('select-', '');
				$('#li-'+$prov).show();
			});			
			$(this).mouseout(function() {
				$prov = $(this).attr('id').replace('select-', '');
				$('#li-'+$prov).hide();
			});			
		});
	}
	
	$('.filter-city input').focus(function() {	
		if ($(this).val() == 'Enter a city/town name...')
			$(this).val('');
	});
	
	$('.filter-city input').blur(function() {	
		if ($(this).val().length == 0)
			$(this).val('Enter a city/town name...');
	});
	
	$('.filter-city #query').keyup(function() {	
		if ($(this).val().length > 0)
		{
			$.ajax({
				type: "POST",
				url: config_url+"/ajax-filter-city.php",
				data: { 
						query : $(this).val(), 
						province: $('#f_province').val(),
						range: $('#f_range').val(),
						type: $('#f_type').val(),
						contract: $('#f_contract').val(),
						price: $('#f_price').val(),
						bedrooms: $('#f_bedrooms').val(),
						bathrooms: $('#f_bathrooms').val()
				},
				dataType: "text",
				success: function(data) {
					if (data != 'error')
						$('.filter-city ul').html(data);
				}
			});
		}	
	});
	
	$('.filter-city #query').blur(function() {
		$('.filter-city #query').keyup();
	});
	
	$( "#slider" ).slider({
		range: "min",
		value: 0,
		min: 0,
		max: 200,
		slide: function( event, ui ) {
			$( ".filter-range  span" ).html( ui.value );
			$( "#filter_range" ).val( ui.value );
		},
		change: function() {
			$( "#filter_range" ).closest('form').submit();
		}
	});
	
	$( ".filter-range span" ).html( $( "#slider" ).slider( "value" ) );
	
	$('.filter-price #price_from').focus(function() {	
		if ($(this).val() == 'From')
			$(this).val('');
	});
	
	$('.filter-price #price_from').blur(function() {	
		if ($(this).val().length == 0)
			$(this).val('From');
	});
	
	$('.filter-price #price_to').focus(function() {	
		if ($(this).val() == 'To')
			$(this).val('');
	});
	
	$('.filter-price #price_to').blur(function() {	
		if ($(this).val().length == 0)
			$(this).val('To');
	});
	
	$('.filter-price').submit(function(e) {
	
		var alert_message = '';    
		var price_from = $('.filter-price #price_from').val();
		var price_to = $('.filter-price #price_to').val();

		var isNumberRegExp = new RegExp(/^[-+]?[0-9]+(\.[0-9]+)*$/);

		if (!isNumberRegExp.test(price_from) || !isNumberRegExp.test(price_to))
		{
			if (!isNumberRegExp.test(price_from))
			{
				alert_message += "Price from must be a number, i.e. 500\n";
				$('.filter-price #price_from').val('From');
			}

			if (!isNumberRegExp.test(price_to))
			{
				alert_message += "Price to must be a number, i.e. 500\n";
				$('.filter-price #price_to').val('To');
			}
		}
		else
		{
			price_from = parseFloat(price_from);
			price_to = parseFloat(price_to);

			if (price_from >= price_to)
			{
				alert_message += "Price from must be less than price to\n";
				$('.filter-price #price_from').val('From');
				$('.filter-price #price_to').val('To');
			}
		}

		if (alert_message != '')
		{
			e.preventDefault();
			alert(alert_message);
		}
		
	});
	
	$('#id_province').change(function() {	
		if ($(this).val().length > 0)
		{
			$.ajax({
				type: "POST",
				url: config_url+"/ajax-change-province.php",
				data: { 
						id_province : $(this).val()
				},
				dataType: "text",
				success: function(data) {
					if (data != 'error')
						$('#id_city').html(data);
				}
			});
		}	
	});
	
	$('#s_province').change(function() {
		$.ajax({
			type: "POST",
			url: config_url+"/ajax-change-province.php",
			data: { 
					id_province : $(this).val(),
					search : 'yes'
			},
			dataType: "text",
			success: function(data) {
				if (data != 'error')
					$('#s_city').html(data);
			}
		});
	});
	
	$('.listing-page .right #your_email').focus(function() {	
		if ($(this).val() == 'Your Email Address')
			$(this).val('');
	});
	
	$('.listing-page .right #your_email').blur(function() {	
		if ($(this).val().length == 0)
			$(this).val('Your Email Address');
	});
	
	$('.listing-page .right #your_message').focus(function() {	
		if ($(this).val() == 'Your Message')
			$(this).val('');
	});
	
	$('.listing-page .right #your_message').blur(function() {	
		if ($(this).val().length == 0)
			$(this).val('Your Message');
	});
	
	$('.listing-page .right #captcha, .form-page #captcha').focus(function() {	
		if ($(this).val() == 'Enter the six letters shown above')
			$(this).val('');
	});
	
	$('.listing-page .right #captcha, .form-page #captcha').blur(function() {	
		if ($(this).val().length == 0)
			$(this).val('Enter the six letters shown above');
	});
	
	$('.photo-thumbs ul li a').click(function(e) {
	
		e.preventDefault();
		
		var next = $(this).parent('li').index();
		
		$('.photo-main ul .active').fadeOut(1000).removeClass('active');
		$('.photo-main ul li:eq('+next+')').fadeIn(1000).addClass('active');		
		$('.photo-thumbs ul .active').removeClass('active');
		$('.photo-thumbs ul li:eq('+next+')').addClass('active');
	
	});
	
	$('.delete-ad').click(function(e) {
		
		var answer = confirm('Are you sure you want to delete this ad?');
		
		if (!answer)
			e.preventDefault();
		
	});
	
});
