var proCONVENTION = {
    doSubmitRequestForm: true,
    submitTimeout: null,

    locale: {
        calendar: {
            days: ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"],
            daysShort: ["Son", "Mon", "Die", "Mit", "Don", "Fre", "Sam", "Son"],
            daysMin: ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"],
            months: ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"],
            monthsShort: ["Jan", "Feb", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"],
            weekMin: 'Wo'
        },
        search: {
            noResults: 'Keine Suchergebnisse'
        }
    },
    settings: {
        dateToday: 'not set',
        dateTomorrow: 'not set'
    },

    init: function () {
        $('.ttwe').tipsy({gravity: $.fn.tipsy.autoWE, offset: 5, opacity: 1});
        $('.ttns').tipsy({gravity: $.fn.tipsy.autoNS, offset: 5, opacity: 1});

        $('#f_start').DatePicker({
            format: 'd.m.Y',
            date: $('#f_start').val(),
            current: $('#f_start').val(),
            starts: 1,
            eventName: 'focus',
            position: 'r',
            onBeforeShow: function(){
                if ($('#f_start').val() == '') {
                    $('#f_start').val(proCONVENTION.settings.dateToday);
                }
                $('#f_start').DatePickerSetDate($('#f_start').val(), true);
            },
            onChange: function(formated, dates, el, tomorrow){
                $('#f_end').val(tomorrow);
                $('#f_start').val(formated);
                $('#f_start').DatePickerHide();
            },
            locale: {
                days: proCONVENTION.locale.calendar.days,
                daysShort: proCONVENTION.locale.calendar.daysShort,
                daysMin: proCONVENTION.locale.calendar.daysMin,
                months: proCONVENTION.locale.calendar.months,
                monthsShort: proCONVENTION.locale.calendar.monthsShort,
                weekMin: proCONVENTION.locale.calendar.weekMin
            }
        });

        $('#f_end').DatePicker({
            format: 'd.m.Y',
            date: $('#f_end').val(),
            current: $('#f_end').val(),
            starts: 1,
            eventName: 'focus',
            position: 'r',
            onBeforeShow: function(){
                if ($('#f_end').val() == '') {
                    $('#f_end').val(proCONVENTION.settings.dateTomorrow);
                }
                $('#f_end').DatePickerSetDate($('#f_end').val(), true);
            },
            onChange: function(formated, dates){
                $('#f_end').val(formated);
                $('#f_end').DatePickerHide();
            },
            locale: {
                days: proCONVENTION.locale.calendar.days,
                daysShort: proCONVENTION.locale.calendar.daysShort,
                daysMin: proCONVENTION.locale.calendar.daysMin,
                months: proCONVENTION.locale.calendar.months,
                monthsShort: proCONVENTION.locale.calendar.monthsShort,
                weekMin: proCONVENTION.locale.calendar.weekMin
            }
        });

        // Set autosuggest options with all plugins activated & response in xml
        var options = {
            script: 'ajax/request.php?type=group&',
            varname: 'q',
            shownoresults: true,
            noresults: proCONVENTION.locale.search.noResults,
            maxresults: 8,
            cache: false,
            delay: 0,
            minchars: 2,
            timeout: 100000,
            callback: function (obj) {
                $('#request_destination_id').val(obj.id);

                proCONVENTION.submitTimeout = setTimeout(function () { proCONVENTION.doSubmitRequestForm = true; }, 200);
            }
        };

        // Init autosuggest
        var as_json = new bsn.AutoSuggest('f_destination', options);

        $('#f_destination').attr('defaultValue', $('#f_destination').val());
        $('#f_destination').css('color', '#818284');

        $('#f_destination').bind('focus', function () {
            if (typeof (proCONVENTION.submitTimeout) != 'undefined') {
                clearTimeout(proCONVENTION.submitTimeout);
            }
            proCONVENTION.doSubmitRequestForm = false;

            if ($(this).val() == $(this).attr('defaultValue')) {
                $(this).css('color', '#000000');
                $(this).val('');
            }
        }).bind('blur', function () {
            proCONVENTION.doSubmitRequestForm = true;
            if ($(this).val() == '') {
                $(this).css('color', '#818284');
                $(this).val($(this).attr('defaultValue'));
            }
        }).bind('keydown', function (event) {
            if (event.which != 13) {
                proCONVENTION.doSubmitRequestForm = false;

                $('#request_destination_type').val('');
                $('#request_destination_id').val('0');
            }
            $(this).css('color', '#000000');
        });

        $('#request_startpage').bind('submit', function () {
            $('#f_destination').css('backgroundColor', '#ffffff');
            $('#f_start').css('backgroundColor', '#ffffff');
            $('#f_end').css('backgroundColor', '#ffffff');

            var submitForm = true;
            if (!proCONVENTION.doSubmitRequestForm || $('#f_destination').val() == $('#f_destination').attr('defaultValue') || $('#f_destination').val() == '') {
                if ($('#f_destination').val() == $('#f_destination').attr('defaultValue') || $('#f_destination').val() == '') {
                    proCONVENTION.highlightInput($('#f_destination'), '#f9c4c4', true);
                }

                submitForm = false;
            }

            return submitForm;
        });

        // Inputs
        proCONVENTION.setDefaultValueInputs();
    },

    setDefaultValueInputs: function (which) {
        var colorUnfocused = '#818284';

        $('input[rel="defaultValue"]').each(function (i) {
            $(this).attr('defaultValue', $(this).val());
            $(this).css('color', colorUnfocused);
        });

        $('input[rel="defaultValue"]').bind('focus', function () {
            if ($(this).val() == $(this).attr('defaultValue')) {
                $(this).css('color', '#000000');
                $(this).val('');
            }
        }).bind('blur', function () {
            if ($(this).val() == '') {
                $(this).css('color', colorUnfocused);
                $(this).val($(this).attr('defaultValue'));
            }
        }).bind('keydown', function () {
            $(this).css('color', '#000000');
        });
    },

    highlightInput: function (element, color, animate) {
        var inputColor = color || '#fff58a';

        $(element).css('backgroundColor', inputColor);

        if (animate) {
            $(element).animate({backgroundColor: '#ffffff'}, 1000, function () {
                $(element).animate({backgroundColor: inputColor}, 200, function () {
                    $(element).animate({backgroundColor: '#ffffff'}, 1000, function () {
                        $(element).animate({backgroundColor: inputColor}, 200, function () {
                            $(element).animate({backgroundColor: '#ffffff'}, 1000, function () {
                                $(element).animate({backgroundColor: inputColor}, 200);
                            });
                        });
                    });
                });
            });
        }
    }
};
