var gallery_current_page = 0;

var countryId = 0;
var regionId = 0;
var townId = 0;

function setSearchCountryId(countryId, advanced) {
    var adv = '';
    if (advanced) {
        adv = 'Advanced';
    }
    $("select." + adv + "PropertyCountryId").val(countryId);
	$("select." + adv + "PropertyRegionId").html('<option value="">[ loading regions ]</option>');
	$("select." + adv + "PropertyTownId").html('<option value=""></option>');
	var json_url = "/properties/ajax_get_regions/" + countryId + "/true";
	if ($(this).attr('title') == 'restricted') {
        json_url = json_url + '/true';
	}
	$.getJSON(json_url, function(j){
		var options = '<option value="">-- any --</option>';
		for (var i = 0; i < j.length; i++) {
			if (j[i].optionValue == regionId) {
                options += '<option value="' + j[i].optionValue + '" selected="selected">' + j[i].optionDisplay + '</option>';
			} else {
			    options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
		}
		$("select." + adv + "PropertyRegionId").html(options);
	});
}

function setSearchRegionId(regionId, advanced) {
    var adv = '';
    if (advanced) {
        adv = 'Advanced';
    }
    //$("select." + adv + "PropertyRegionId").val(regionId);
    $("select." + adv + "PropertyRegionId option").each(function(){

        if ($(this).attr('value') == regionId) {
            $(this).attr('selected') == 'selected';
        } else {
            $(this).attr('selected') == '';
        }

    });
    $("select." + adv + "PropertyTownId").html('<option value="">[ loading towns and resorts ]</option>');
	var json_url = "/properties/ajax_get_towns/" + regionId + "/true";
	if ($(this).attr('title') == 'restricted') {
        json_url = json_url + '/true';
	}
	$.getJSON(json_url, function(j){
		var options = '<option value="">-- any --</option>';
		for (var i = 0; i < j.length; i++) {
			if (j[i].optionValue == townId) {
			    options += '<option value="' + j[i].optionValue + '" selected="selected">' + j[i].optionDisplay + '</option>';
			} else {
			    options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}
		}
		$("select." + adv + "PropertyTownId").html(options);
	});
}

$(document).ready(function(){

	// highlight fields on focus
	$(function(){$("fieldset input").focus(function(){$(this).parent('div.input').addClass("focus");});});
	$(function(){$("fieldset input").blur(function(){$(this).parent('div.input').removeClass("focus");});});
	$(function(){$("fieldset select").focus(function(){$(this).parent('div.input').addClass("focus");});});
	$(function(){$("fieldset select").blur(function(){$(this).parent('div.input').removeClass("focus");});});
	$(function(){$("fieldset textarea").focus(function(){$(this).parent('div.input').addClass("focus");});});
	$(function(){$("fieldset textarea").blur(function(){$(this).parent('div.input').removeClass("focus");});});

	// datepicker fields
	$("input.datepicker_single").datepicker({dateFormat: 'dd/mm/yy'});

	// ajax function to load regions into dropdown based on country selection
	$("select.PropertyCountryId").change(function(){
		setSearchCountryId($(this).val(), false);
	});

	// ajax function to load towns into dropdown based on region selection
	$("select.PropertyRegionId").change(function(){
        setSearchRegionId($(this).val(), false);
	});

	// ajax function to load regions into dropdown based on country selection
	$("select.AdvancedPropertyCountryId").change(function(){
		setSearchCountryId($(this).val(), true);
	});

	// ajax function to load towns into dropdown based on region selection
	$("select.AdvancedPropertyRegionId").change(function(){
		setSearchRegionId($(this).val(), true);
	});

	// clear the area code fields on focus
	$("input#OwnerTelCountry").focus(function(){
		if ($(this).val() == 'country code') {
			$(this).val('');
		}
	});
	$("input#OwnerTelArea").focus(function(){
		if ($(this).val() == 'area code') {
			$(this).val('');
		}
	});
	$("input#OwnerTelNumber").focus(function(){
		if ($(this).val() == 'number') {
			$(this).val('');
		}
	});

	// tab nav system
	$('div#tabnav ul li a').click(function(){
		$('div.section').hide();
		$('div#tabnav ul li a').removeClass('on');
		$('div.' + $(this).attr('class')).fadeIn('normal');
		$(this).addClass('on');
	});

	// gallery system
	$('div#property-gallery div.next').click(function(){
		// hide all images on this page
		for (var i = 1; i <= 4; i++) {
			$('div#property-gallery div.photo' + (gallery_current_page * 4 + i + 1)).hide();
		}
		gallery_current_page++;
		for (var i = 1; i <= 4; i++) {
			$('div#property-gallery div.photo' + (gallery_current_page * 4 + i + 1)).fadeIn();
		}
	});

	// gallery system
	$('div#property-gallery div.previous').click(function(){
		if (gallery_current_page > 0) {
			// hide all images on this page
			for (var i = 1; i <= 4; i++) {
				$('div#property-gallery div.photo' + (gallery_current_page * 4 + i + 1)).hide();
			}
			gallery_current_page--;
			for (var i = 1; i <= 4; i++) {
				$('div#property-gallery div.photo' + (gallery_current_page * 4 + i + 1)).fadeIn();
			}
		}
	});


});

function show_all_options(id) {
	$('#viewall-' + id).hide();
	$('#options-' + id).fadeIn('fast');
}
