Element.implement('formalize', function (target, formName) {
    $(formName).getFirst().addEvent('submit', function(e) {
        e.stop();
        this.set('send', {
            onComplete: function(response) {
        }});
        this.send();
    });

});

var Notification = new Class ({
    show: function (text) {
        if ($("notification").style.display === 'none') {
           $("notification").getFirst().set('text', text);
           new Fx.Morph($("notification"), {
               duration: 'normal',
               onStart: function() { $('notification').style.display = 'block' },
               transition: Fx.Transitions.Sine.easeOut
           }).start(
               {
                   'top': [ -200, document.getScroll().y + 5 ],
                   'opacity' : [ 0, .9 ]
           });

           window.addEvent('scroll', this.scroll);
           this.delayedHide();
        }
    },
    delayedHide: function() {
       (function() {
           window.removeEvent('scroll', this.scroll );

           new Fx.Morph($("notification"), {duration: 'normal', transition: Fx.Transitions.Sine.easeOut, onComplete: function(){
               $("notification").style.display = 'none';
               $("notification").style.top = -200;
           } }).start({'opacity': [ .9, 0 ] });

       }).delay(2000);
    },
    scroll: function(e) {
       (function() {
           if (!Notification.moving) {
               Notification.moving = true;
               new Fx.Morph($("notification"),{
                   duration: 'short',
                   link: 'ignore',
                   onComplete: function () { Notification.moving = false; },
                   transition: Fx.Transitions.Sine.easeOut}).start(
                       { 'top': [ $("notification").style.top, document.getScroll().y + 5 ] }
                   );
           }
       }).delay(200);
    }
});
function notification (text) {
}


Element.implement('formalize_hide', function () {
    new Fx.Morph(this, {duration: 'short', transition: Fx.Transitions.Sine.easeOut}).start({'opacity': [ .9, 0 ], 'display': ['block', 'none'] });
});

Element.implement('formalize_show', function () {
    new Fx.Morph(this, {duration: 'short', transition: Fx.Transitions.Sine.easeOut}).start({'opacity': [ 0, .9 ], 'display': ['none', 'block']  });
});

Element.implement('default_text', function () {
    this.addEvent('focus', function () {
        if (this.value == this.getProperty("alt")){
            this.value = '';
        }
    });
    this.addEvent('blur', function () {
        if (this.value == '')
            this.value = this.getProperty("alt");
    });
    this.value = this.getProperty("alt");
});

var Voting = new Class ({});
Voting.Vote = function(id, up) {
    if (Cookie.read("voted-for-speech" + id) === "true")
    {
        new Notification().show("За каждый из докладов можно голосовать только однажды (в остальном мы надеемся на Вашу порядочность и сохранность наших Cookies)");
    }
    else
    {
        new Request.JSON({
            url: "/home/vote/",
            method: "post",
            data: { up: up, speech_id: id },
            onComplete: function(success) {
                new Notification().show("Спасибо! Мы обязательно учтем все пожелания!");
                Cookie.write("voted-for-speech" + id,  true);
            }
        }).send();
    }
};
