/**
 * @author Daniel
 * 
 * The JavaScript file for dtsn.co.uk watch and learn baby!
 */

document.observe('dom:loaded', function() {
    if ($('s')) {
        $('s').observe('keydown', function(e) {
                if (e.KeyCode == 13) {
                        $('searchform').submit();
                }
        });
    }
});

function deliciouscount(data) {
    //get the number of saves
    var urlinfo = data[0];
    if (urlinfo == undefined) return;
    //if none, do nothing
    if(!urlinfo.total_posts) return;
    $('delicious').update('Delicious (' + urlinfo.total_posts + ' Saves)');
}

function weather(data) {
    var code = data.value.items[0].content;
    document.write('Its currently <strong>' + code + '</strong> in Reading, UK');
}

var Ghost = Class.create({
    initialize: function() {
        // create the element
        this.ghost = new Element('div', {'class' : 'ghost'});
        // hide the element
        this.ghost.hide();
        // add the element to the body
        $(document.body).insert({top: this.ghost});
        // get the size of the document
        this.size = document.viewport.getDimensions();
        // set up the timer
        new PeriodicalExecuter(this.show.bindAsEventListener(this), 2);
    },

    show: function() {
        // randomly set where the ghost is going
        var x = Math.floor(Math.random()*(this.size['width']-200));
        var y = Math.floor(Math.random()*(this.size['height']-200));
        // set the style
        this.ghost.setStyle({
                'left': x + 'px',
                'top': y + 'px'
        });
        // show the object
        this.ghost.appear({
                afterFinish: function() {
                        this.ghost.fade();
                }.bind(this)
        });
    }
});