var Skooot = new  Class ({
    initialize: function () {
        if ($$('.updates')){
            this.populateStream();
        } 
        if ($$('.updateForm')){
            this.sendStream();
            this.linkParser();
        }
    },
    populate: function (fade){
        var populate = new Request({
            url: '/ajax/stream/',
            method: 'get',
            cache: false,
            onFailure: function(response){
                $$('.updates').set('html', '<p>Failed to load stream</p>');
            },
            onSuccess: function(response) {
                $$('.updates').set('html', response);
                window.parent.Skooot.prototype.deleteListener();
            }
        }).send();
        
        setTimeout(function() {window.parent.Skooot.prototype.populateStream()}, 40000);
    },
    
    deleteListener: function () {
        $$('.update .delete a').each(function(e){
            e.addEvent('click', function (event) {
                event.stop();
                if(confirm('Are you sure you want to delete this?')){
                    window.parent.Skooot.prototype.update_delete(this.id);
                }
                else {
                    return false;
                }      
            });
        });
    },
    
    update_delete: function(update_id){
        var remove = new Request({
               url: '/ajax/stream/delete/' + update_id + '/',
               method: 'get',
               cache: false,
               onFailure: function(response){
                   console.log('fail');
               },
               onSuccess: function(response) {
                   var item = $(''+ update_id +'').getParent().getParent();

                   item.setStyles({
                       'overflow' : 'hidden',
                       'float' : 'left'
                   });
                   
                   var slide_out = new Fx.Morph(item, {
                       duration: 200, 
                       onComplete: function(){
                            socket.send('delete_post');
                            window.parent.Skooot.prototype.populate();
                       } // end onComplete

                   }).start({'height': '0'}); // end slide_out_old_page
               }
           }).send();
    },
    
    populateStream: function (){
        this.populate(); 
    },
      
    sendStream: function () {
        $$('.updateForm input[type=submit]').addEvent('click', function(event){
           event.stop();
           var message = $$('.updateForm textarea')[0].value
           var populateStream = new Request({
               url: $$('.updateForm').get('action'),
               method: 'POST',
               cache: false,
               data: {
                   'message' : message
               },
               onFailure: function(response){
                   $$('.updates').set('html', '<p>Failed to post to stream</p>');
               },
               onSuccess: function(response) {
                   socket.send('newpost');
                   $$('.updateForm textarea')[0].set('value', '');
               }
           }).send();
        });
        $$('.updateForm textarea').addEvent('keyup', function(key){
            if(key.key == 'enter'){
                   var message = $$('.updateForm textarea')[0].value;
                   var populateStream = new Request({
                       url: $$('.updateForm').get('action'),
                       method: 'POST',
                       cache: false,
                       data: {
                           'message' : message
                       },
                       onFailure: function(response){
                           $$('.updates').set('html', '<p>Failed to post to stream</p>');
                       },
                       onSuccess: function(response) {
                           socket.send('newpost');
                           $$('.updateForm textarea')[0].set('value', '');
                       }
                   }).send();
            }
        });
    },
    
    linkParser: function () {
        $$('.updateForm textarea')[0].addEvent('keyup', function(key){
           if (this.value.test('^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$\g')){
               
           }
        });
    }
});
var WindowActions = new Class({   
    initialize: function () {
        window.addEvent('focus', function () {
            document.title = 'Skooot';
        });
    }
});
