﻿(function ($) {

    $.fn.extend({

        //pass the options variable to the function
        createMonthPagerWithinYear: function (options,
        onLeftPagerClick, onRightPagerClick,
        onItemChanged) {


            //Set the default values, use comma to separate the settings, example:  
            var defaults = {
                leftToken: '<',
                rightToken: '>',
                topLeftPagerId: 'topLeftPager',
                topRightPagerId: 'topRightPager',
                leftPagerId: 'leftPager',
                rightPagerId: 'rightPager',
                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);
                var startYear = options.currentDate.getFullYear();

                $('#yearlyNewsDropdown', obj).change(
		            function () {
		                if ($.isFunction(onItemChanged)) {
		                    var val = $(this).attr('value');
		                    onItemChanged(val);
		                }
		            }
	            );
		        
                var tmpText

                var $leftPager = $('#' + opt.leftPagerId, obj);
                var $topLeftPager = $('#' + opt.topLeftPagerId, obj);
                tmpText = getLeftPagerString(opt);
                $leftPager.text(tmpText);
                $topLeftPager.text(tmpText);

                if (opt.currentDate.getMonth() == 0) {
                    $leftPager.hide();
                    $topLeftPager.hide();
                }

                var $rightPager = $('#' + opt.rightPagerId, obj);
                var $topRightPager = $('#' + opt.topRightPagerId, obj);
                tmpText = getRightPagerString(opt);
                $rightPager.text(tmpText);
                $topRightPager.text(tmpText);

                if (opt.currentDate.getMonth() == 11) {
                    $rightPager.hide();
                    $topRightPager.hide();
                }


                setCurrentMonthTitle(opt, 0);

                $leftPager.click(
                    function () {
                        leftPagerClickHandler();
                    }
                );
                $rightPager.click(
                    function () {
                        rightPagerClickHandler();
                    }
                );
                $topLeftPager.click(
                    function () {
                        leftPagerClickHandler();
                    }
                );
                $topRightPager.click(
                    function () {
                        rightPagerClickHandler();
                    }
                );

                function leftPagerClickHandler() {

                    $rightPager.show();

                    if (isYearLeftScope(opt, startYear)) {
                        $leftPager.hide();
                        changeOptionDate(opt, -1);

                        if ($.isFunction(onLeftPagerClick)) {
                            onLeftPagerClick(opt.currentDate);
                        }

                    }

                    else {
                        changeOptionDate(opt, -1);
                        if ($.isFunction(onLeftPagerClick)) {
                            onLeftPagerClick(opt.currentDate);
                        }
                    }
                }
                function rightPagerClickHandler() {
                    $leftPager.show();

                    if (isYearRightScope(opt, startYear)) {
                        $rightPager.hide();
                        changeOptionDate(opt, 1);

                        if ($.isFunction(onRightPagerClick)) {
                            onRightPagerClick(opt.currentDate);
                        }

                    }

                    else {
                        changeOptionDate(opt, 1);

                        if ($.isFunction(onRightPagerClick)) {
                            onRightPagerClick(opt.currentDate);
                        }
                    }
                }
            });


            function getLeftPagerString(options) {

                var currentMonth = parseInt(options.currentDate.getMonth());
                var currentYear = parseInt(options.currentDate.getFullYear());

                if (currentMonth == 0) {
                    return options.leftToken + ' ' + options.monthes[11] + ' ' + (currentYear - 1);
                }

                return options.leftToken + ' ' + options.monthes[currentMonth - 1] + ' ' + currentYear;
            }
            function getRightPagerString(options) {

                var currentMonth = parseInt(options.currentDate.getMonth());
                var currentYear = parseInt(options.currentDate.getFullYear());

                if (currentMonth == 11) {
                    return options.monthes[0] + ' ' + (currentYear + 1) + ' ' + options.rightToken;
                }

                return options.monthes[currentMonth + 1] + ' ' + currentYear + ' ' + options.rightToken;
            }
            function getCurrentMonthString(options) {
                var currentMonth = parseInt(options.currentDate.getMonth());
                var currentYear = parseInt(options.currentDate.getFullYear());
                return options.monthes[currentMonth] + ' ' + currentYear;
            }
            function setCurrentMonthTitle(options) {
                var title = getCurrentMonthString(options);
                $('div.find_box p').eq(0).text(title);
            }
            function changeOptionDate(oOptions, iFlag) {

                var currentMonth = parseInt(oOptions.currentDate.getMonth());
                var currentYear = parseInt(oOptions.currentDate.getFullYear());
                if (iFlag == -1) {
                    if (currentMonth == 0) {
                        oOptions.currentDate.setFullYear(currentYear - 1);
                        oOptions.currentDate.setMonth(11);
                    }
                    else {
                        oOptions.currentDate.setFullYear(currentYear);
                        oOptions.currentDate.setMonth(currentMonth - 1);
                    }
                    return;
                }

                if (currentMonth == 11) {
                    oOptions.currentDate.setFullYear(currentYear + 1);
                    oOptions.currentDate.setMonth(0);
                }
                else {
                    oOptions.currentDate.setFullYear(currentYear);
                    oOptions.currentDate.setMonth(currentMonth + 1);
                }
            }
            /*
            function isLeftPagerDateInPast(oOptions) {
            return oOptions.currentDate < new Date() ? true : false;
            }
            */
            function isCurrentDateInPast(oOptions, startYear) {
                return oOptions.currentDate.getFullYear() < startYear ? true : false;
            }
            function isCurrentDateInFuture(oOptions, startYear) {
                return oOptions.currentDate.getFullYear() > startYear ? true : false;
            }

            function isYearLeftScope(oOptions, startYear) {
                return oOptions.currentDate.getMonth() == 1 ? true : false;
            }
            function isYearRightScope(oOptions, startYear) {
                return oOptions.currentDate.getMonth() == 10 ? true : false;
            }
        }
    });

})(jQuery); 
