$(window).load(function(){

						

	$('#headerSearchForm #PropertyQuery').focus(function(){

		if($(this).val() == 'Search for a holiday home'){

			$('#headerSearchForm #PropertyQuery').val('').addClass('on');

		}		

	});

	$('#headerSearchForm #PropertyQuery').blur(function(){

		if($(this).val() == ''){

			$('#headerSearchForm #PropertyQuery').val('Search for a holiday home').removeClass('on');

		}		

	});			

	$('#AdvancedQuery').focus(function(){

		if($(this).val() == 'Property ID or State...'){

			$('#AdvancedQuery').val('').addClass('on');

		}		

	});

	$('#AdvancedQuery').blur(function(){

		if($(this).val() == ''){

			$('#AdvancedQuery').val('Property ID or State...').removeClass('on');

		}		

	});

	

	

	// Property view main images switcher

	// 05/02/2011

	if($('#propertyImages').length > 0){

		$('#propertyImages img').click(function(){

			$('img#mainImage').attr('src', '/img/properties/big/'+$(this).attr('title'));

		});

	}

	

						

	// Property list view main images switcher

	// 06/02/2011

	if($('.tinyImages').length > 0){

		$('.tinyImages img').click(function(){

			$id = $(this).attr('title');

			$('.imageArea img#mainImage_'+$id).attr('src', '/img/properties/medium/'+$(this).attr('alt'));

		});

	}

	

	// Waypoints for Find a Property page

	// 28/02/11

	if($('.waypoint').length > 0){

		$('#footer').waypoint(function(event, direction) {

			 if (direction === 'down') {

					//alert('hi');

			 }

			 else {

					//alert('back up');

			 }

		});

	}



	

	//Shadowbox init

	// 05/02/2011

	Shadowbox.init();

	



	// State/city/address property edit feature

	// 04/02/2011

	if($('#PropertyEditForm #PropertyState').length > 0){

		if($('#PropertyEditForm #PropertyState option:selected').val() == ''){

			// If no state is selected hide the city select

			$('#PropertyEditForm #PropertyCity').parent().hide();

		} else {

			$('#PropertyCity').parent().hide();

			$.get('/properties/propertyCity/'+$('#PropertyEditForm #PropertyState option:selected').val()+'/'+$('#PropertyOriginalCity').val(), function(data){

				$('#PropertyCity').parent().html(data).slideDown();

			});			

		}

		$('#PropertyEditForm #PropertyState').change(function(){

			$('#PropertyCity').parent().slideUp();

			$.get('/properties/propertyCity/'+$('option:selected', this).val(), function(data){

				$('#PropertyCity').parent().html(data).slideDown();

			});

		});

	}

	

	// Hide auth/flash message

	if($('#authMessage').length > 0){

		setTimeout("hideAuth();", 5000);

	}

	if($('#flashMessage').length > 0){

		setTimeout("hideFlash();", 5000);

	}

	// Property view page

	// 29/01/2011

	if($('#propertyNav').length > 0){

		$('#propertyNav li a').not('#loadVideo').click(function(){

			$('.all').hide();

			$id = $(this).attr('id').split('_');

			$id = $id[1];

			$('.'+$id).show();			

			if($id == 'map'){

				var myOptions = {

					zoom: 8,

					center: latlng,

					mapTypeId: google.maps.MapTypeId.ROADMAP

				};

				map = new google.maps.Map(document.getElementById("googleMap"), myOptions);

			}

		});

		var myOptions = {

			zoom: 8,

			center: latlng,

			mapTypeId: google.maps.MapTypeId.ROADMAP

		};

		map = new google.maps.Map(document.getElementById("googleMap"), myOptions);

	}

	

	// Add property - when a user clicks on a input with the text Empty, it clears it

	// 24/01/11

	if($('.advert_list_buy').length > 0){

		$('.advert_list_buy input').focus(function(){

			if($(this).val() == 'Empty'){

				$(this).val('');

			}

		});

		$('.advert_list_buy input').blur(function(){

			if($(this).val() == ''){

				$(this).val('Empty');

			}

		});

	}

	

	// Top Nav User Login

	if($('#HeaderUserLoginForm').length > 0){

		if($('#HeaderUserLoginForm #UserUsername').val() == ''){

			$('#HeaderUserLoginForm #UserUsername').val('E-mail...');

		}

		

		$('#HeaderUserLoginForm #UserUsername').focus(function(){

			if($('#HeaderUserLoginForm #UserUsername').val() == 'E-mail...'){

				$('#HeaderUserLoginForm #UserUsername').val('');

			}

		});

		$('#HeaderUserLoginForm #UserUsername').blur(function(){

			if($('#HeaderUserLoginForm #UserUsername').val() == ''){

				$('#HeaderUserLoginForm #UserUsername').val('E-mail...');

			}

		});

		

		

		if($('#HeaderUserLoginForm #UserPassword').val() == ''){

			$('#HeaderUserLoginForm #UserPassword').hide();

			$('#HeaderUserLoginForm #UserShowPassword').show().val('Password...');

		}

		

		$('#HeaderUserLoginForm #UserShowPassword').focus(function(){

	 		$('#HeaderUserLoginForm #UserShowPassword').hide();

			$('#HeaderUserLoginForm #UserPassword').show().focus();

	 	});

		

		$('#HeaderUserLoginForm #UserPassword').blur(function(){

			if($('#HeaderUserLoginForm #UserPassword').val() == ''){

				$('#HeaderUserLoginForm #UserPassword').hide();

				$('#HeaderUserLoginForm #UserShowPassword').show();				

			}

	 	});

		

	}

	

	

	// Confirm when clicking on an a tag

	if($('#body_edit_property .confirmExit').length > 0){

		$('.confirmExit a').click(function(){

			if(!confirm('Please note that any unsaved data will be lost if you leave this page\nAre you sure you want to leave this page?')){

				return false;

			}

		});

	}

	

	if($('.editProperty').length > 0){

		$('.editProperty').click(function(){

			if(!confirm('Please note that changing your property will result in your property becoming inactive until it is submitted for review by the Indialets team.')){

				return false;

			}																	

		});

	}

	

	

	//Messages

	

	if($('.enquiryMessage'). length > 0){

		$('.enquiryMessage').hide();

		$('.readEnquiry').toggle(function(){

			$(this).parent().parent().removeClass('unread').addClass('read');	

			$(this).parent().parent().next('.enquiryMessage').show().removeClass('unread').addClass('read');	

			$.get('/enquiries/markAsRead/'+$(this).attr('id'));

			$(this).html('Hide &#187;');									 

		}, function(){

			$(this).parent().parent().next('.enquiryMessage').hide();		

			$(this).html('Read &#187;');

		});

		

		$('#delete_all').click(function(){

			if($('#delete_all:checked').length > 0){

				$('tr input').attr('checked', true);

			} else {

				$('tr input').attr('checked', false);

			}

		});

		

		$('#markAsReadChecked').click(function(){

			if(confirm('Are you sure you want to mark as read all marked enquiries?')){

				var ids = $('tr input:checked');

				$.each(ids, function(){

					$(this).parent().parent().removeClass('unread').addClass('read');	

					$(this).parent().parent().next('.enquiryMessage').show().removeClass('unread').addClass('read');	

					$.get('/enquiries/markAsRead/'+$(this).attr('id'));											 

				});				 

			}

		});

		

		$('#deleteChecked').click(function(){

			if(confirm('Are you sure you want to delete all marked enquiries?')){

				var ids = $('tr input:checked');

				$.each(ids, function(){

					$(this).parent().parent().remove();

					$(this).parent().parent().next('.enquiryMessage').show().remove();

					$.get('/enquiries/delete/'+$(this).attr('id'));											 

				});				 

			}

		});

	}

	

	if($('#submitToPaypal').length > 0){

		$('form').submit();	

	}

	

	// Homepage signup form

	if($('#submitNewsLetter').length > 0){

		$('#submitNewsLetter').click(function(){

			var name = $('#signUpForm #name').val();

			var email = $('#signUpForm #email').val();

			

			if(name == ''){

				alert('Please enter your name');

				$('#signUpForm #name').focus();

				return false;

			}

			if(!validateEmail(email)){

				alert('Please enter a valid email address');

				$('#signUpForm #email').focus();

				return false;

			}

			

			$.post('/users/news_letter_sign_up', { ajax: 1, name: name, email: email, math: Math.random()}, function(data){

				if(data == 'success'){

					$('#signUpForm').hide();

					$('#signUpFormSuccess').fadeIn();

				} else {

					alert('There was an error, please try again.');

				}

			});

		});

	}

	

	// Apply hover state to table rows

	if($('tbody tr').length > 0){

		$('tbody tr').hover(function(){

			$(this).addClass('on');

	 	}, function(){

			$(this).removeClass('on');

	 	});

	}

	

	if($('#PropertyAddress').length > 0){

		var geocoder = new google.maps.Geocoder();

		$('#PropertyAddress').blur(function(){

		 	getLatLng();

		});

		$('#PropertyEditForm #PropertyState').change(function(){

		 	getLatLng();

		});

		$('#PropertyEditForm #PropertyCity').change(function(){

		 	getLatLng();

		});

	}

	

	function getLatLng(){

		var address = $('#PropertyAddress').val()+', '+$('#PropertyEditForm #PropertyCity option:selected').val()+', '+$('#PropertyEditForm #PropertyState option:selected').val()+', India';

		geocoder.geocode( { 'address': address}, function(results, status) {

			if (status == google.maps.GeocoderStatus.OK) {

				$('#PropertyLatLng').val(results[0].geometry.location);

			} else {

				$('#PropertyLatLng').val('');

			}

		});

	}

	// Manage property bedroom show/hide

	if($('.bedrooms').length > 0){

		$('.bedrooms').hide();

		$('.bedrooms:first').show();

		var bedrooms = $('.bedrooms');

		$.each(bedrooms, function(){

			var parent = $(this);

			if($('.bedroomName', parent).val() != ''){

				parent.show();

				var prev = parent.prev();

				$('.removeBedroom', prev).hide();

				$('.addBedroom', prev).hide();

			}

		});

		$('.bedrooms:first .removeBedroom').hide();

		$('.addBedroom').click(function(){

			var bedroom = $(this).parent();

			bedroom.next().show();

			$('.addBedroom', bedroom).hide();

			$('.removeBedroom', bedroom).hide();

		});

		$('.removeBedroom').click(function(){

			var parent = $(this).parent();

			parent.hide();

			$('input', parent).val('');

			$('select', parent).find('option:first').attr('selected', 'selected');

			$('.addBedroom', parent.prev()).show();

			$('.removeBedroom', parent.prev()).show();

		});

	}

		

	

	// Manage property bathrooms show/hide

	if($('.bathrooms').length > 0){

		$('.bathrooms').hide();

		$('.bathrooms:first').show();

		var bathrooms = $('.bathrooms');

		$.each(bathrooms, function(){

			var parent = $(this);

			if($('.bathroomName', parent).val() != ''){

				parent.show();

				var prev = parent.prev();

				$('.removeBathroom', prev).hide();

				$('.addBathroom', prev).hide();

			}

		});

		$('.bathrooms:first .removeBathroom').hide();

		$('.addBathroom').click(function(){

			var bathroom = $(this).parent();

			bathroom.next().show();

			$('.addBathroom', bathroom).hide();

			$('.removeBathroom', bathroom).hide();

		});

		$('.removeBathroom').click(function(){

			var parent = $(this).parent();

			parent.hide();

			$('input, textarea', parent).val('').attr('checked', false);

			$('.addBathroom', parent.prev()).show();

			$('.removeBathroom', parent.prev()).show();

		});

	}

		

		

	// User password confirm

	if($('#UserAddForm').length > 0){

		$('#UserAddForm').submit(function(){

			if($('#UserAddForm #UserPassword').val() == ''){

				alert('Passwords do not match');

				$('#UserAddForm #UserConfirmPassword').val('');

				$('#UserAddForm #UserPassword').val('').focus();

				return false;

			}

			if($('#UserAddForm #UserPassword').val() != $('#UserAddForm #UserConfirmPassword').val()){

				alert('Passwords do not match');

				$('#UserAddForm #UserConfirmPassword').val('');

				$('#UserAddForm #UserPassword').val('').focus();

				return false;

			}

			if(!$('#UserAddForm #UserTerms:checked').length > 0){

				alert('You must agree to our terms and conditions');

				$('#UserAddForm #UserTerms').focus();

				return false;

			}

		});

	}

	if($('#UserChangePasswordForm').length > 0){

		$('#UserChangePasswordForm').submit(function(){

			if($('#UserNewPassword').val() != $('#UserConfirmPassword').val()){

				alert('Passwords do not match');

				$('#UserNewPassword').val('').focus();

				$('#UserConfirmPassword').val('');

				return false;

			}

			if($('#UserNewPassword').val() == ''){

				alert('Password cannot be empty');

				$('#UserNewPassword').val('').focus();

				return false;

				

			}

		});

	}

	

	if($('#searchLimit').length > 0){

		$('#searchLimit').change(function(){

			$('#searchLimitSort').submit();

		});

		$('#searchSort').change(function(){

			$('#searchLimitSort').submit();

		});

	}

	

	if($('#purchasePackages').length > 0){

		alert('hi');

		$('#purchasePackages').submit(function(){

			if($('input').val() == 'Empty'){

				alert('Please ensure that you have named your advert');

				return false;

			}

		});

	}

	// User account details

	

	if($('#vatNo').length > 0){

		$('#outsideEU').hide();

		if($('#UserUkResidentYes:checked').length > 0){

			$('#outsideEU').hide();

			$('#outsideEU')

		}

		if($('#UserUkResidentNo:checked').length > 0){

			$('#outsideEU').show();

		}

		if($('#UserOutsideEuNo:checked').length > 0 && $('#UserInBusinessYes:checked').length > 0){

			$('#vatNo').show();

		}		

		if($('#UserVatNo').val() != '' || $('#UserVatExempt:checked').length > 0){

			$('#vatNo').show();

		}

		if($('#UserOutsideEuYes:checked').length > 0 || $('#UserInBusinessNo:checked').length > 0){

			$('#vatNo').hide();

		}		

		$('#UserUkResidentNo').click(function(){

			if($('#UserUkResidentNo:checked').length > 0){

				$('#outsideEU').show();

			}

		});

		$('#UserUkResidentYes').click(function(){

			if($('#UserUkResidentYes:checked').length > 0){

				$('#outsideEU').hide();

			}

		});

		

		

		$('#UserOutsideEuNo').click(function(){

			if($('#UserInBusinessYes:checked').length > 0){

				$('#vatNo').show();

			}

		});		

		$('#UserInBusinessYes').click(function(){

			if($('#UserOutsideEuNo:checked').length > 0){

				$('#vatNo').show();

			}

		});	
/*
		$('#UserInBusinessNo').click(function(){

			if($('#UserOutsideEuNo:checked').length > 0){

				$('#vatNo').hide();
				$('#UserVatNo').val('');
				$('#UserVatExempt').attr('checked', false);
			}

		});
*/
	}

});



	

function hideAuth(){

	$('#authMessage').slideUp();

}



function hideFlash(){

	$('#flashMessage').slideUp();

}



function validateEmail(email) {

   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

   if(!reg.test(email)) {

      return false;

   }

	 return true;

}
