// Console ///////////////////////////////////////

Console = Class.create();
Console.prototype = {
    initialize : function (startScreen, tickSpeed) {
	this.tickSpeed = 70;
        this.screen = startScreen;
    },
    start : function() {
        _con = this;
        window.setInterval(function () { _con.tick() }, this.tickSpeed);
    },
    tick : function () {
        this.screen.tick();
    },
}

// Screen ////////////////////////////////////////

Screen = Class.create();
Screen.prototype = {
    initialize : function (id) {
        this.id = id;
    },
    tick : function () {
    },
}

// Sprite ////////////////////////////////////////

var Sprite = Class.create();
Sprite.prototype = {
    initialize : function(id) {
        this.id = id;
        this.node = $(id);
	this.myFirst = 1;
	this.myX = 0;
	this.myY = 0;
    },
    
    getX : function() {
	return this.myOx + this.myX;
    },
    
    setX : function(x) {
	this.myX = x - this.myOx;
    },


    getY : function () {
        return this.node.offsetTop;
    },
    
    setY : function(y) {
	this.myY = y - this.myOy;
    },    
    
    moveBy: function(dx, dy) {
	if (this.myFirst)
	{
	    this.myOx = this.node.offsetLeft;
	    this.myOy = this.node.offsetTop;
	    this.myFirst = 0;
	}
	ox = Math.floor(this.myOx + this.myX);
	oy = Math.floor(this.myOy + this.myY);
	this.myX += dx;
	this.myY += dy;
	nx = Math.floor(this.myOx + this.myX);
	ny = Math.floor(this.myOy + this.myY);
	if (nx != ox || ny != oy) {
	    this.node.setStyle({'left' : nx + 'px'});
	    this.node.setStyle({'top'  : ny + 'px'});
	}
    },

    getW : function() {
        return this.node.offsetWidth;
    },
    
    getH : function() {
        return this.node.offsetHeight;
    },
};

// --------------------------------------------------------------------
var cloud1;
var cloud2;
var cloud3;
var thesky;
var skyfield;

function startMoving() {
    cloud1 = new Cloud('cloud1');
    cloud2 = new Cloud('cloud2');
    cloud3 = new Cloud('cloud3');
    thesky = new Console(THE_SKY);
    thesky.start();
    skyfield = new Sprite();
    skyfield.id="skyfield";
    skyfield.node=$("skyfield");
}

// Cloud ////////////////////////////////////////

var Cloud = Class.create();
Cloud.prototype = Object.extend(new Sprite(), {
    initialize: function(id) {
        this.id = id;
        this.node = $(id);
	this.setV();
    },
    center: function () {
        this.setX((skyfield.getW()+this.getW())/2);
        this.velocity = 0;
    },
    setV: function() {
	this.velocity = (-.4 - Math.random())*.5;
    },
    startAgain: function() {
	this.setV();
	this.setX(skyfield.getW());
	this.setY((skyfield.getH()-this.getH())*Math.random());
    },
    tick: function() {
        if (this.getX() <= -this.getW())
	    this.startAgain();
	else if (this.getX() > skyfield.getW() + 20)
	    this.startAgain();
	this.moveBy(this.velocity, 0); 
    }
});

var THE_SKY = Object.extend(new Screen('skyfield'), {
    tick : function() {
        cloud1.tick();
        cloud2.tick();
        cloud3.tick();
    }
});
