/* MAP RESULTS
------------------------------------------------------------------ */
function mapResultsInit(marker_list) {
  jQuery('#map_results').map_results(marker_list);
  jQuery(document).bind('map:paneOpened', onPaneOpened);
  jQuery('.place_list a.locate').bind('click', onPaneOpen);
}

// Pane Opened
function onPaneOpened(e, marker) {
  jQuery('.place_list a.locate').removeClass('on');
  jQuery('#marker_'+marker.id).addClass('on');
}

// Pane Open
function onPaneOpen(e) {
  e.preventDefault();
  var map_offset = jQuery('.map').offset().top;
  var win_scroll = jQuery(window).scrollTop();
  if (win_scroll > map_offset) {
    jQuery('html,body').animate({scrollTop: map_offset}, 200);
  }
  marker_index = jQuery(e.target).attr('id').replace('marker_', '');
  jQuery(document).trigger('map:paneOpen', [marker_index])
}

// Pane Close
function paneClose() {
  jQuery(document).trigger('map:paneClose');
}

$(document).ready(function(){
	
	$('fieldset.premium').hide();
	
	if ($('#type_premium').is(':checked')) {
		$('fieldset.premium').show();
	}

	$("#type_free").click(function() {
		$('fieldset.premium').hide();
	});
	
	$("#type_premium").click(function() {
		$('fieldset.premium').show();
	});
	
    // FORMS
    $("input[type='button']").addClass('button');
    $("input[type='checkbox']").addClass('checkbox');
    $("input[type='file']").addClass('file');
    $("input[type='image']").addClass('image');
    $("input[type='password']").addClass('password');
    $("input[type='radio']").addClass('radio');
    $("input[type='submit']").addClass('submit');
    $("input[type='text']").addClass('text');

   
    
    // RSS ICON
    $("img[alt='Feed Icon']").addClass('rss_icon');
	
	$('a.external').click(function(){
	        window.open(this.href);
	        return false;
	 });
	
	$('#type_filter').change( function() {
		      location.href = $(this).val();
		});
    
    $('body.listing-5 ul#place_listing li:nth-child(5n)').addClass('end'); 
	$('body.listing-3 ul#place_listing li:nth-child(3n)').addClass('end');     
    $('div#footer dl:nth-child(5n)').addClass('end'); 

    //$('ul#type_listing li:nth-child(3n)').addClass('end'); 


     
	$("a[rel='gallery']").colorbox();
	
	$("#accommodation_contact_form").validate({
		rules: {
			name: "required",
		    email: {
		      required: true,
		      email: true
		    },
			contact_phone: "required",
			captcha: "required"
		},
		messages: {
			name: "You must include your name",
			email: {
				required: "You must include your email address", 
				email: "You must enter a valid email address"
			},
			contact_phone: "You must include a contact telephone number",
			captcha: "You must answer the spam/security question"
		},
		errorElement: "em"
	});
	
	$(".properties_form #entryform").validate({
		rules: {
			title: "required",
			field_id_6: "required", // short desscription
			field_id_7: "required", // address line 1
			field_id_14: "required", // town
			field_id_15: "required", // postcode
			field_id_16: "required", // telephone
			field_id_17: "required", // email
			field_id_19: "required" // description
		},
		messages: {
			title: "You must include your name",
			field_id_6: "You must add a short description",
			field_id_7: "The first line of the address is required",
			field_id_14: "You must enter the nearest town",
			field_id_15: "You must enter the property's postcode",
			field_id_16: "Please add the telephone number for customers to call",
			field_id_17: "Please add the email address for customers to use",
			field_id_19: "You must add a full description of the property"
		},
		errorElement: "em"
	});

	$("#member_form").validate({
		rules: {
			username: "required",
			email: "required", 
			screen_name: "required", 
			cpf_address1: "required", 
			cpf_town: "required", 
			cpf_county: "required", 
			cpf_postcode: "required", 
			cpf_telephone: "required" 
		},
		messages: {
			username: "You must enter a username",
			email: "You must provide an email address", 
			screen_name: "You must enter a contact name", 
			cpf_address1: "You must give us the first line of your address", 
			cpf_town: "You enter your postal town", 
			cpf_county: "You must enter your county", 
			cpf_postcode: "You must enter you postcode", 
			cpf_telephone: "You must submit a contact telephone number"
		},
		errorElement: "em"
	});
	
	$("#register_form").validate({
		rules: {
			username: "required",
			password: "required",
			password_confirm: {
							required: true,
							equalTo: "#reg_password"
			},
			email: "required", 
			screen_name: "required", 
			cpf_address1: "required", 
			cpf_town: "required", 
			cpf_county: "required", 
			cpf_postcode: "required", 
			cpf_telephone: "required" 
		},
		messages: {
			username: "You must enter a username",
			password: "You must enter a password",
			password_confirm: {
				required: "Please provide a password",
				equalTo: "Please enter the same password as above"
			},
			email: "You must provide an email address", 
			screen_name: "You must enter a contact name", 
			cpf_address1: "You must give us the first line of your address", 
			cpf_town: "You enter your postal town", 
			cpf_county: "You must enter your county", 
			cpf_postcode: "You must enter you postcode", 
			cpf_telephone: "You must submit a contact telephone number"
		},
		errorElement: "em"
	});
		
});