function photoButtons() {
    if (!$('.propertyThumbnails ul li:visible').length) {
        $('.propertyThumbnails p.buttonDesc').hide();
    }
    else {
        $('.propertyThumbnails p.buttonDesc').show();
    }
}
$(function() {
	/* Firefox scroll fix */
	$('html').css('overflow-y', 'scroll');

	/* IE 5.5+ Pngfix */
	if ($.browser.msie && $.browser.version >= 5.5 && $.browser.version <= 6.0) {
		$('html').pngFix();
	}

	/* Open in new page (target="_blank") */
	$('._blank').click(function() {
		window.open($(this).attr('href'));
		return false;
	});

	/* Textarea vertical resize */
	$('textarea').css('resize', 'vertical');

	/* Form elements focus highlight disable (Chrome, Safari) */
	$('input, select, textarea').focus(function() {
		$(this).css('outline', 0);
	});

	/* Lightbox */
	if ($('.propertyThumbnail a').length) {
		$('.propertyThumbnail a').lightBox();
	}

    if ($('#town').length) {
        $('#town').dependent({ parent:'subregion' });
    }
    if ($('#subregion').length) {
        $('#subregion').dependent({ parent:'region', child:'town'});
    }
    
    if ($('#region').length) {
        $('#region').dependent({ parent:'country' });
    }
                                                          
    /* Toolbar */
    if ($.cookie('toolbarHidden') == 'true')
        $('.adminToolbar ul').addClass('hidden');
    $('.adminToolbar .l').click(function() {
        if ($.cookie('toolbarHidden') == 'true')
            $('.adminToolbar ul').removeClass('hidden');
        else
            $('.adminToolbar ul').addClass('hidden');
        $.cookie('toolbarHidden', $('.adminToolbar ul').hasClass('hidden'), {expires: 14});
    });

    /* Property photo */
    if ($('#propertyPhoto').length) {
        photoButtons();
        var propertyId = $('#propertyPhotosA').attr('rel');
        new AjaxUpload('#propertyPhoto', {
            action: '/properties/photo/' + propertyId,
            autoSubmit: true,
            onComplete: function(file, response) {
                $.ajax({
                    url: '/properties/photo/' + propertyId,
                    cache: false,
                    success: function(html){
                        if ($('.propertyThumbnails ul').length)
                            $('.propertyThumbnails ul').replaceWith(html);
                        else
                            $('.propertyThumbnails div.buttonDesc').after(html);
                        photoButtons();
                    }
                });
    
            }
        });
    }
    $('#property-thumbnail-delete').click(function() {
        if (confirm('Are you sure?')) {
            $('input[name="property-thumbnail-delete"]:checked').parent().fadeOut(500);
            setTimeout("$('input[name=\"property-thumbnail-delete\"]:checked').parent().remove(); photoButtons();", 500);
            $.ajax({
                url: '/properties/photo/false/delete/' + $('input[name="property-thumbnail-delete"]:checked').attr('value'),
                cache: false
            });
        }
        return false;
    });
    $('#property-thumbnail-default').click(function() {
        $('.propertyThumbnails .default').removeClass('default');
        $('input[name="property-thumbnail-delete"]:checked').parent().addClass('default');
        $.ajax({
            url: '/properties/photo/false/default/' + $('input[name="property-thumbnail-delete"]:checked').attr('value'),
            cache: false
        });
        return false;
    });
    
    
    var selectedRegion;
    var selectedSubRegion;
    $('#state').change(function() {
        selectedRegion = $('#state option:selected').attr('value');
        $('.subregion').hide();
        $('.r' + selectedRegion).attr('name', 'data[Subregion][id]');
        $('.r' + selectedRegion).show();
        $('.r' + selectedRegion + ' option:first').attr('selected', 'selected');

        selectedSubRegion = $('.subregion:visible option:selected').attr('value');
        $('.town').hide();
        if ($('.s' + selectedSubRegion).length == 0) {
            $('#townLabel').hide();
        } else {
            $('#townLabel').show();
        }
        $('.s' + selectedSubRegion).attr('name', 'data[Town][id]')
        $('.s' + selectedSubRegion).show();
    });
    
    
    $('.subregion').change(function() {
        selectedSubRegion = $('.subregion:visible option:selected').attr('value');
         if ($('.s' + selectedSubRegion).length == 0) {
            selectedSubRegion = '0';
            $('.town').hide();
            $('.s' + selectedSubRegion).attr('name', 'data[Town][id]')
            $('.s' + selectedSubRegion).show();
        } else {
            $('#townLabel').show();
            $('.town').hide();
            $('.s' + selectedSubRegion).attr('name', 'data[Town][id]')
            $('.s' + selectedSubRegion).show();
        }
    });
});