﻿(function($) {

    $.fn.extend({

        //pass the options variable to the function
        createDropdownMonthPager: function(options, onItemChanged) {


            //Set the default values, use comma to separate the settings, example:  
            var defaults = {
                countYearsAhead: 2,
                currentDate: new Date(),
                monthes: ['Januar', 'Februar',
                          'Marts', 'April',
                          'Maj', 'Juni',
                          'Juli', 'August',
                          'September', 'Oktober',
                          'November', 'December']
            }

            var options = $.extend(defaults, options);

            return this.each(function() {
                var opt = options;
                var obj = $(this);
                buidDropdownItems(opt, $(this));
                $(this).change(
		            function() {
		                if ($.isFunction(onItemChanged)) {
		                    var val = $(this).attr('value');
		                    var month = val.split(',')[0];
		                    var year = val.split(',')[1];
		                    var date = new Date();
		                    date.setFullYear(year);
		                    date.setMonth(month)
		                    onItemChanged(date);
		                }
		            }
	            );
            });
            function buidDropdownItems(oOptions, $selectBox) {
                for (var i = 0; i < oOptions.countYearsAhead; i++) {
                    var k = i == 0 ? parseInt(oOptions.currentDate.getMonth()) : 0;
                    for (var j = k; j < 12; j++) {
                        //<OPTION value="0,2010">Januar 2010</OPTION>
                        $selectBox.append('<option value="' + j + ',' + (parseInt(oOptions.currentDate.getFullYear()) + i) + '">' + oOptions.monthes[j] + ' ' + (parseInt(oOptions.currentDate.getFullYear()) + i) + '</option>');
                    }
                };
            }
        }
    });

})(jQuery); 
