var Lotate = Class.create();
Lotate.prototype = {
    initialize: function(element, url, timeout) {
        this.element = $(element);
        this.url = url;
        this.timeout = timeout;
        this.data = new Array();
        this.counter = 0;
        this.change_flag = 0;
    },
    getInnerHtml: function() {
        return '';
    },
    updateData: function() {
        new Ajax.Request(this.url, {
            method: 'get',
            onComplete: function(r) {
                this.data = eval('('+r.responseText+')');
                this.change();
            }.bind(this)
        });
    },
    change: function() {
        if (this.change_flag == 0) {
            Effect.Fade(this.element, {duration:1.0});
            this.change_flag = 1;
            setTimeout(this.change.bind(this), 1500);
            return;
        }

        this.element.innerHTML = this.getInnerHtml();

        this.counter ++;
        if (this.counter >= this.data.length) this.counter = 0;

        this.change_flag = 0;

        Effect.Appear(this.element);
        setTimeout(this.change.bind(this), this.timeout);
    }
}

/*
var Thumb = Class.create();
Thumb.prototype = Object.extend(new Lotate, {
    initialize: function() {
        this.element = $('thumb');
        this.url = '/ajax.php?action=get_thumbs';
        this.timeout = 7000;
    },
    getInnerHtml: function() {
        return this.element.innerHTML = '<a href="'+this.data[this.counter]['link']+'" title="'+this.data[this.counter]['title']+'"><img src="'+this.data[this.counter]['image_url']+'" /></a>';
    }
});
*/

var Koto = Class.create();
Koto.prototype = Object.extend(new Lotate, {
    initialize: function(element, timeout) {
        this.element = $('koto');
        this.url = '/ajax.php?action=get_kotonoha';
        this.timeout = 6000;
    },
    getInnerHtml: function() {
        return '<a href="'+this.data[this.counter]['link']+'">'+this.data[this.counter]['title']+'</a>';
    }
});

function fadeCover() {
    new Effect.Fade('cover', {duration: 3.0});
    $('topillust').style.visibility = 'visible';
//  Element.show('thumb');
    Element.show('koto');
}

function startLotate() {
    var k = new Koto;
    k.updateData();

//  var t = new Thumb;
//  t.updateData();
}

function initTop() {
    setTimeout('fadeCover()', 2000);
    setTimeout('startLotate()', 5000);
}

Event.observe(window, 'load', initTop);
