$.fn.init_datepicker = function(options) {
    $.fn._init_datepicker = function(options) {
        $(this).datepicker(
            $.extend({       
              showOn: 'button', // 'focus' for popup on focus,
                // 'button' for trigger button, or 'both' for either
              showAnim: 'show', // Name of jQuery animation for popup
              showOptions: {}, // Options for enhanced animations
              defaultDate: null, // Used when field is blank: actual date,
                // +/-number for offset from today, null for today
              appendText: '', // Display text following the input box, e.g. showing the format
              buttonText: '', // Text for trigger button
              buttonImage: '', // URL for trigger button image
              buttonImageOnly: false, // True if the image appears alone, false if it appears on a button
              closeAtTop: true, // True to have the clear/close at the top,
                // false to have them at the bottom
              mandatory: false, // True to hide the Clear link, false to include it
              hideIfNoPrevNext: false, // True to hide next/previous month links
                // if not applicable, false to just disable them
              navigationAsDateFormat: false, // True if date formatting applied to prev/today/next links
              showBigPrevNext: false, // True to show big prev/next links
              gotoCurrent: false, // True if today link goes back to current selection instead
              changeMonth: true, // True if month can be selected directly, false if only prev/next
              changeYear: true, // True if year can be selected directly, false if only prev/next
              showMonthAfterYear: false, // True if the year select precedes month, false for month then year
              yearRange: '-10:+10', // Range of years to display in drop-down,
                // either relative to current year (-nn:+nn) or absolute (nnnn:nnnn)
              changeFirstDay: true, // True to click on day name to change, false to remain as set
              highlightWeek: false, // True to highlight the selected week
              showOtherMonths: false, // True to show dates in other months, false to leave blank
              showWeeks: false, // True to show week of the year, false to omit
              calculateWeek: this.iso8601Week, // How to calculate the week of the year,
                // takes a Date and returns the number of the week for it
              shortYearCutoff: '+10', // Short year values < this are in the current century,
                // > this are in the previous century, 
                // string value starting with '+' for current year + value
              showStatus: false, // True to show status bar at bottom, false to not show it
              statusForDate: this.dateStatus, // Function to provide status text for a date -
                // takes date and instance as parameters, returns display text
              minDate: null, // The earliest selectable date, or null for no limit
              maxDate: null, // The latest selectable date, or null for no limit
              duration: 'normal', // Duration of display/closure
              beforeShowDay: null, // Function that takes a date and returns an array with
                // [0] = true if selectable, false if not, [1] = custom CSS class name(s) or '', 
                // [2] = cell title (optional), e.g. $.datepicker.noWeekends
              beforeShow: null, // Function that takes an input field and
                // returns a set of custom settings for the date picker
              onSelect: null, // Define a callback function when a date is selected
              onChangeMonthYear: null, // Define a callback function when the month or year is changed
              onClose: null, // Define a callback function when the datepicker is closed
              numberOfMonths: 1, // Number of months to show at a time
              showCurrentAtPos: 0, // The position in multipe months at which to show the current month (starting at 0)
              stepMonths: 1, // Number of months to step back/forward
              stepBigMonths: 12, // Number of months to step back/forward for the big links
              rangeSelect: false, // Allows for selecting a date range on one date picker
              rangeSeparator: ' - ', // Text between two dates in a range
              altField: '', // Selector for an alternate field to store selected dates into
              altFormat: '' // The date format to use for the alternate field
          }, options)
        )
    }
    
    if ($._datepicker_translations) {
        $(this)._init_datepicker(options);    
    } else { 
        var element = this;
        $.json('general', 'translate',
            {path : 'datepicker', shared: true, words : 'clear,close,prev,next,today,monthJanuary,monthFebruary,monthMarch,monthApril,monthMay,monthJune,monthJuly,monthAugust,monthSeptember,monthOctober,monthNovember,monthDecember,monthShortJanuary,monthShortFebruary,monthShortMarch,monthShortApril,monthShortMay,monthShortJune,monthShortJuly,monthShortAugust,monthShortSeptember,monthShortOctober,monthShortNovember,monthShortDecember,daySunday,dayMonday,dayTuesday,dayWednesday,dayThursday,dayFriday,daySaturday,dayShortSunday,dayShortMonday,dayShortTuesday,dayShortWednesday,dayShortThursday,dayShortFriday,dayShortSaturday'}, 
            function(r) {
                $._datepicker_translations = r.translations
                var t = r.translations;
                var datepicker_regional = {                        
                    		clearText: t.clear, // Display text for clear link
                    		clearStatus: '', // Status text for clear link
                    		closeText: t.close, // Display text for close link
                    		closeStatus: '', // Status text for close link
                    		prevText: t.prev, // Display text for previous month link
                    		prevStatus: '', // Status text for previous month link
                    		prevBigText: '', // Display text for previous year link
                    		prevBigStatus: '', // Status text for previous year link
                    		nextText: t.next, // Display text for next month link
                    		nextStatus: '', // Status text for next month link
                    		nextBigText: '', // Display text for next year link
                    		nextBigStatus: '', // Status text for next year link
                    		currentText: t.today, // Display text for current month link
                    		currentStatus: '', // Status text for current month link
                    		monthNames: [t.monthJanuary, t.monthFebruary, t.monthMarch, t.monthApril, t.monthMay, t.monthJune, t.monthJuly, t.monthAugust, t.monthSeptember, t.monthOctober, t.monthNovember, t.monthDecember], // Names of months for drop-down and formatting
                    		monthNamesShort: [t.monthShortJanuary, t.monthShortFebruary, t.monthShortMarch, t.monthShortApril, t.monthShortMay, t.monthShortJune, t.monthShortJuly, t.monthShortAugust, t.monthShortSeptember, t.monthShortOctober, t.monthShortNovember, t.monthShortDecember], // For formatting
                    		monthStatus: '', // Status text for selecting a month
                    		yearStatus: '', // Status text for selecting a year
                    		weekHeader: '', // Header for the week of the year column
                    		weekStatus: '', // Status text for the week of the year column
                    		dayNames: [t.daySunday, t.dayMonday, t.dayTuesday, t.dayWednesday, t.dayThursday, t.dayFriday, t.daySaturday], // For formatting
                    		dayNamesShort: [t.dayShortSunday, t.dayShortMonday, t.dayShortTuesday, t.dayShortWednesday, t.dayShortThursday, t.dayShortFriday, t.dayShortSaturday], // For formatting
                    		dayNamesMin: [t.dayShortSunday, t.dayShortMonday, t.dayShortTuesday, t.dayShortWednesday, t.dayShortThursday, t.dayShortFriday, t.dayShortSaturday], // Column headings for days starting at Sunday
                    		dayStatus: '', // Status text for the day of the week selection
                    		dateStatus: '', // Status text for the date selection
                    		dateFormat: 'dd.mm.yy', // See format options on parseDate
                    		firstDay: 1, // The first day of the week, Sun = 0, Mon = 1, ...
                    		initStatus: '', // Initial Status text on opening
                    		isRTL: false // True if right-to-left language, false if left-to-right                        
                };         
                $.datepicker.setDefaults(datepicker_regional);            
                $(element)._init_datepicker(options);        
            }
        );
    }
}
$(document).ready(function() {
    if ($('.periodinput').length) { 
        $.datepicker.setDefaults($.datepicker.regional[$('meta[name=lang]').attr('content')]);
        currentTime = new Date()
        var yearrange =  (currentTime.getFullYear() - 90) +  ':' + (currentTime.getFullYear() - 15);
        currentTime.setFullYear(currentTime.getFullYear() - 25);
        $(".periodinput").init_datepicker({
          defaultDate : currentTime, 
          yearRange: yearrange
        });
    }
});
