$(document).ready(function(){

//    слайд-шоу
    $("#slideImg").cycle({
        fx:      'fade', 
        speed:    2000,
        timeout:  5000,
        random: 1
    });

    var country = $("#countrySelect").val();
    $("#regionSelect").load( "index.php?id=41&countryId="+country, function() {
        var region = $("#regionSelect").val();
//        alert(region);

        if ( region > 0 ) {
            $("#makerSelect").load( "index.php?id=533&regionId="+region );
            $("#yearSelect").load( "index.php?id=45&regionId="+region );

        } else {
            $("#makerSelect").load( "index.php?id=533&countryId="+country );
            $("#yearSelect").load( "index.php?id=45&countryId="+country );
        }
    } );
    

    //  выбираем регион в зависимости от страны
    $("#countrySelect").change(function(){
        var country = $("select#countrySelect").val();
        $("#regionSelect").load( "index.php?id=41&countryId="+country, function(){
            var region = $("#regionSelect").val();
            if ( region > 0 ) {
                $("#makerSelect").load( "index.php?id=533&regionId="+region );
                $("#yearSelect").load( "index.php?id=45&regionId="+region );

            } else{
                $("#makerSelect").load( "index.php?id=533&countryId="+country );
                $("#yearSelect").load( "index.php?id=45&countryId="+country );
            }
        } );
    });

    //  выбираем год урожая
    $("#regionSelect").change(function(){
        var region = $("#regionSelect").val();
//        alert(region);
        if ( region > 0 ) {
            $("#makerSelect").load( "index.php?id=533&regionId="+region, function(){
                var maker = $("#makerSelect").val();
                if ( maker > 0 ) {
                    $("#yearSelect").load( "index.php?id=45&makerId="+maker );
                } else {
                    $("#yearSelect").load( "index.php?id=45&regionId="+region );
                }
            });
        } else {
            var country = $("select#countrySelect").val();
            $("#makerSelect").load( "index.php?id=533&countryId="+country, function(){
                var maker = $("#makerSelect").val();
//            alert(maker);
                if ( maker > 0 ) {
                    $("#yearSelect").load( "index.php?id=45&makerId="+maker );
                } else {

                    $("#yearSelect").load( "index.php?id=45&countryId="+country );
                }

            });
        }
    });

    $("#makerSelect").change(function(){
        var maker = $(this).val();
//        alert(maker);
        if ( maker != "" && maker != "-1" ) {
            $("#yearSelect").load( "index.php?id=45&makerId="+encodeURI(maker) );

        } else {
            var region = $("#regionSelect").val();

            if ( region > 0 ) {
                $("#yearSelect").load( "index.php?id=45&regionId="+region );

            } else {
                var country = $("#countrySelect").val();
                $("#yearSelect").load( "index.php?id=45&countryId="+country );
            }
        }
    });

    $("#yearSelect").change(function(){
        $.post( "index.php?id=45&clear" );
    });
});
