// For discussion and comments, see: http://remysharp.com/2009/01/07/html5-enabling-script/
(function(){if(!/*@cc_on!@*/0)return;var e = "abbr,article,aside,audio,canvas,datalist,details,figure,figcaption,footer,header,hgroup,mark,menu,meter,nav,output,progress,section,summary,time,video".split(','),i=e.length;while(i--){document.createElement(e[i])}})()

var Newsletter = {
        init: function (elementSelector) {
                this.element = $(elementSelector);
                this.form = this.element.parents('form');
                this._bindFocus();
                this._bindSubmit();
        },
        _bindFocus: function () {
                this.element.focus(function () {
                        if (this.value == 'E-Mail-Adresse') {
                                $(this).val('');
                        }
                }).blur(function () {
                        if (this.value.trim() == '') {
                                $(this).val('E-Mail-Adresse');
                        }
                });
        },
        _bindSubmit: function () {
                this.form.submit(function () {
                        $.post(this.action, $(this).serialize(), function (rsp) {
                                if (rsp.success === true) {
                                        $.showMsg('Yeah! Du bist dabei!');
                                        Newsletter.element.blur().val('E-Mail-Adresse');
                                }
                                else {
                                        (rsp.notify != '' ? $.showMsg(rsp.notify, true) :
                                                $.showMsg('D\'oh! Es ist ein Fehler aufgetreten.', true));
                                }
                        }, 'json');
                        return false;
                });
        }
}

$(function () { Newsletter.init('input'); });

(function($) {
        $.fn.extend({
                delay: function (ms) {
                        return $(this).animate({ 'height': $(this).height() }, ms);
                }
        });

        $.removeDiv = function () {
                $(this).remove();
        };

        $.showMsg = function (msg, isError, delay) {
                if ($('.notify-bar').length != 0) {
                        $('.notify-bar').remove();
                }

                if (typeof delay !== 'number') {
                        delay = 3000;
                }

                if (typeof isError != 'boolean') {
                        isError = false;
                }

                var $bar = $('<div\/>').addClass('notify-bar');
                $('<div\/>').addClass('notify-bg').appendTo($bar);
                $('<div\/>').addClass('notify-msg').html(msg).appendTo($bar);

                if (isError === true) {
                        $bar.find('.notify-bg').addClass('notify-error');
                }

                $bar.appendTo('body').animate({ 'top': '0px' }, 600).delay(delay).fadeOut('normal', $.removeDiv);
                return $bar;
        };
})(jQuery);