var myConferencehotel = {
    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 () {
        // Set autosuggest options with all plugins activated & response in xml
        var options = {
            script: 'ajax/request.php?type=groups&',
            varname: 'q',
            shownoresults: true,
            noresults: myConferencehotel.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);

                myConferencehotel.submitTimeout = setTimeout(function () { myConferencehotel.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 (myConferencehotel.submitTimeout) != 'undefined') {
                clearTimeout(myConferencehotel.submitTimeout);
            }
            myConferencehotel.doSubmitRequestForm = false;

            if ($(this).val() == $(this).attr('defaultValue')) {
                $(this).css('color', '#000000');
                $(this).val('');
            }
        }).bind('blur', function () {
            myConferencehotel.doSubmitRequestForm = true;
            if ($(this).val() == '') {
                $(this).css('color', '#818284');
                $(this).val($(this).attr('defaultValue'));
            }
        }).bind('keydown', function (event) {
            if (event.which != 13) {
                myConferencehotel.doSubmitRequestForm = false;

                $('#request_destination_id').val('0');
            }
            $(this).css('color', '#000000');
        });

        $('#request_startpage').bind('submit', function () {
            $('#f_destination').css('backgroundColor', '#ffffff');

            var submitForm = true;
            if (!myConferencehotel.doSubmitRequestForm || $('#f_destination').val() == $('#f_destination').attr('defaultValue') || $('#f_destination').val() == '') {
                if ($('#f_destination').val() == $('#f_destination').attr('defaultValue') || $('#f_destination').val() == '') {
                    myConferencehotel.highlightInput($('#f_destination'), '#f9c4c4', true);
                }

                submitForm = false;
            }

            return submitForm;
        });

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