var myTagungshotel = {
    doSubmitRequestForm: true,
    requestType: 'groups',
    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 () {
        $('#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(myTagungshotel.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: myTagungshotel.locale.calendar.days,
                daysShort: myTagungshotel.locale.calendar.daysShort,
                daysMin: myTagungshotel.locale.calendar.daysMin,
                months: myTagungshotel.locale.calendar.months,
                monthsShort: myTagungshotel.locale.calendar.monthsShort,
                weekMin: myTagungshotel.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(myTagungshotel.settings.dateTomorrow);
                }
                $('#f_end').DatePickerSetDate($('#f_end').val(), true);
            },
            onChange: function(formated, dates){
                $('#f_end').val(formated);
                $('#f_end').DatePickerHide();
            },
            locale: {
                days: myTagungshotel.locale.calendar.days,
                daysShort: myTagungshotel.locale.calendar.daysShort,
                daysMin: myTagungshotel.locale.calendar.daysMin,
                months: myTagungshotel.locale.calendar.months,
                monthsShort: myTagungshotel.locale.calendar.monthsShort,
                weekMin: myTagungshotel.locale.calendar.weekMin
            }
        });

        // Set autosuggest options with all plugins activated & response in xml
        var options = {
            script: 'ajax/request.php?type=' + $('input[name=t]:checked').val() + '&',
            varname: 'q',
            shownoresults: true,
            noresults: myTagungshotel.locale.search.noResults,
            maxresults: 8,
            cache: false,
            delay: 0,
            minchars: 2,
            timeout: 100000,
            callback: function (obj) {
                // => TO submit form (general use)
                $('#request_destination_id').val(obj.id);

                myTagungshotel.submitTimeout = setTimeout(function () { myTagungshotel.doSubmitRequestForm = true; }, 200);
            }
        };

        // Init autosuggest
        var as_json = new bsn.AutoSuggest('f_destination', options);

        $('input[name=t]').bind('change', function () {
            as_json.oP.script = 'ajax/request.php?type=' + $(this).val() + '&';
            as_json.clearSuggestions();
            $('#f_destination').val('').focus();
            $('#request_destination_type').val('');
            $('#request_destination_id').val('0');

            myTagungshotel.radioButtonChangeSearch($(this).val());
        });
        // Change der Radiobuttons initialisieren
        myTagungshotel.radioButtonChangeSearch($('input[name=t]:checked').val());

        $('#f_destination').attr('defaultValue', $('#f_destination').val());
        $('#f_destination').css('color', '#818284');

        $('#f_destination').bind('focus', function () {
            if (typeof (myTagungshotel.submitTimeout) != 'undefined') {
                clearTimeout(myTagungshotel.submitTimeout);
            }
            myTagungshotel.doSubmitRequestForm = false;

            if ($(this).val() == $(this).attr('defaultValue')) {
                $(this).css('color', '#000000');
                $(this).val('');
            }
        }).bind('blur', function () {
            myTagungshotel.doSubmitRequestForm = true;
            if ($(this).val() == '') {
                $(this).css('color', '#818284');
                $(this).val($(this).attr('defaultValue'));
            }
        }).bind('keydown', function (event) {
            if (event.which != 13) {
                myTagungshotel.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 (!myTagungshotel.doSubmitRequestForm || $('#f_destination').val() == $('#f_destination').attr('defaultValue') || $('#f_destination').val() == '') {
                if ($('#f_destination').val() == $('#f_destination').attr('defaultValue') || $('#f_destination').val() == '') {
                    myTagungshotel.highlightInput($('#f_destination'), '#f9c4c4', true);
                }

                submitForm = false;
            }

            if (myTagungshotel.requestType == 'individual') {
                if($('#f_start').val().match(/\d{1,2}.\d{1,2}.\d{2,4}/) == null) {
                    myTagungshotel.highlightInput($('#f_start'), '#f9c4c4', true);
                    submitForm = false;
                }

                if($('#f_end').val().match(/\d{1,2}.\d{1,2}.\d{2,4}/) == null) {
                    myTagungshotel.highlightInput($('#f_end'), '#f9c4c4', true);
                    submitForm = false;
                }
            }

            return submitForm;
        });

        // Inputs
        myTagungshotel.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);
                            });
                        });
                    });
                });
            });
        }
    },

    radioButtonChangeSearch: function (current) {
        if (current == 'individual') {
            $('.individual_elements').show();
            $('.meetings_groups_elements').hide();
            myTagungshotel.settings.requestType = 'individual';
        } else if (current == 'groups') {
            $('.meetings_groups_elements').show();
            $('.individual_elements').hide();
            myTagungshotel.settings.requestType = 'groups';
        }
    }
};
