(function($) {
    var defaults = {
        day_select_class   :   'day_select',
        month_select_class  :   'month_select',
        year_select_class   :   'year_select'
    };
    var options;

    var iYear;
    var iMonth;
    var el;

    var methods = {
        init:function(params) {
            options = $.extend({}, defaults, params);

            return this.each(function(){
               el = $(this);
               iYear =  $(this).find('.'+options.year_select_class).val();
               iMonth =  $(this).find('.'+options.month_select_class).val();
               //console.warn('iYear = '+'.'+options.year_select_class+':selected');

               $(this).find('.'+options.month_select_class).change(function(){
                   iMonth = $(this).val();
                   setDays();
               });

               $(this).find('.'+options.year_select_class).change(function(){
                   iYear = $(this).val();
                   setDays();
               });
            });
        },
        color:function(color) {
           
        }
    };

    function setDays(iYear,iMonth){
        var selected_day = el.find('.'+options.day_select_class).val();
        var days = getDays();
        var cont = '<select class="day_select">'
        for ( var i=1; i<=days; i++ ){
            cont +=  '<option value="'+i+'">'+i+'</option>';
        }
        cont += '</select>';
        el.find('.'+options.day_select_class).remove();
        el.find('.'+options.month_select_class).before(cont);
        el.find('.'+options.day_select_class).val(selected_day);

    }

//    function daysInMonth(month, year) {
//        return new Date(year, month, 0).getDate();
//    }

    function getDays(){
        var _iMonth = iMonth-1;
        return 32 - new Date(iYear, _iMonth, 32).getDate();
    }

    $.fn.dateSelect = function(method){
        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Метод "' +  method + '" не найден в плагине jQuery.mySimplePlugin' );
        }
    };
})(jQuery);

