var regionsCache;
var townsCache;
var lastBedroomsValue;

$(document).ready(function() {
    if ($("#search")) {
        regionsCache = $("#search-region").clone();
        townsCache = $("#search-town").clone();
        setRegions();
        setTowns();
        setBedrooms();
        $("#search-country").change(function() { setRegions(); setTowns(); });
        $("#search-region").change(function() { setTowns(); });
        $("#search-type").change(function() { setBedrooms(); });
    }

    if ($("#searchmoscow")) {
        setMoscow();
        $("#searchmoscow input[name='region']").click(function() { setMoscow(); });
    }

    if(jQuery().fancybox) {
        var fancy = {
    		'transitionIn'	:	'fade',
    		'transitionOut'	:	'fade',
    		'speedIn'		:	600, 
    		'speedOut'		:	200, 
    		'overlayShow'	:	true,
    	    'autoScale'     :   false
    	};
    	$(".gallery A").fancybox(fancy);
    	$("A.photo").fancybox(fancy);
    }  
});

function setRegions() {
    $("#search-region OPTION:gt(0)").remove();
    $("#search-region OPTGROUP").remove();
    var options = $(regionsCache).find("OPTGROUP[label=" + $("#search-country").children(":selected").html() + "]").html();
    if (options) {
        $("#search-region").append("<optgroup label=\"---\"></optgroup>").append(options);
        $("#search-region").parent("DD").show().prev("DT").show();
    } else {
        $("#search-region").parent("DD").hide().prev("DT").hide();
    }
}

function setTowns() {
    $("#search-town OPTION:gt(0)").remove();
    $("#search-town OPTGROUP").remove();
    var options = $(townsCache).find("OPTGROUP[label=" + $("#search-region").children(":selected").html() + "]").html();
    if (!options) {
        options = $(townsCache).find("OPTGROUP[label=" + $("#search-country").children(":selected").html() + "]").html();
    }

    if (options) {
        $("#search-town").append("<optgroup label=\"---\"></optgroup>").append(options);
        $("#search-town").parent("DD").show().prev("DT").show();
    } else {
        $("#search-town").parent("DD").hide().prev("DT").hide();
    }
}

function setBedrooms() {
    if ($("#search-type").val() == 6 || $("#search-type").val() == 7) {
        $("#search-bedrooms").parent("DD").hide().prev("DT").hide();
        lastBedroomsValue = $("#search-bedrooms").val();
        $("#search-bedrooms").val("");
    } else {
        $("#search-bedrooms").val(lastBedroomsValue);
        $("#search-bedrooms").parent("DD").show().prev("DT").show();
    }
}

function setMoscow() {
    $("#searchmoscow-metro").parent("DD").hide().prev("DT").hide();
    $("#searchmoscow-town").parent("DD").hide().prev("DT").hide();
    $("#searchmoscow-type").parent("DD").hide().prev("DT").hide();
    if ($("#searchmoscow input[name='region']:checked").val() == 1) {
        $("#searchmoscow-metro").parent("DD").show().prev("DT").show();
    } else {
        $("#searchmoscow-town").parent("DD").show().prev("DT").show();
        $("#searchmoscow-type").parent("DD").show().prev("DT").show();
    }
}
