PigEgg = {
	setup: function() {
		$clear(myPig);
		this.left = 0;
		this.right = Window.getWidth();
		this.top = 0;
		this.bottom = Window.getHeight()+Window.getScrollTop();
		this.startPos = $random(0,99);
		this.startTop = $random(Window.getScrollTop()+60,this.bottom)-60;
		this.imgS = '<img src="img/pig_right.png" width="73" height="54" />';
		this.imgD = '<img src="img/pig_left.png" width="73" height="54" />';
		this.start();
	},
	start: function() {
		var img;
		this.leftpos=0;
		if (this.startPos<50) { img = this.imgS; this.leftpos = this.left-93; }
		else { img = this.imgD; this.leftpos = this.right; }
		
		var div = new Element('div').setProperty('id','piggy').setHTML(img).setStyles({
			'position': 'absolute',
			'margin-top': this.startTop+'px',
			'margin-left': this.leftpos+'px',
			'z-index': '10001',
			'opacity': 1
		}).injectBefore('header');
		
		this.horEff = new Fx.Style('piggy','margin-left',{
			duration: 3500,
			wait: false,
			fps: 60,
			onComplete: function() {
				$('piggy').remove();
				myPig = (function () {PigEgg.setup();}).periodical(120000,this);
			}
		});
		
		var oriTop = this.startTop;
		this.verEff = new Fx.Style('piggy', 'margin-top', {duration:90, wait:false, 
			onComplete: function() {
				if ($('piggy')) {
					var curTop = $('piggy').getStyle('margin-top').toInt();
					if (curTop>=oriTop+5) {
						this.start(curTop,oriTop-5);
					} else {
						this.start(curTop,oriTop+5);
					}
				}
			}
		});
		
		this.move();
	},
	move: function() {
		this.verEff.start(this.startTop,this.startTop+5);

		if (this.leftpos>0)
			this.horEff.start(this.leftpos,this.left-93);
		else 
			this.horEff.start(this.leftpos,this.right);		
	}
}

window.addEvent('load', function() {
	// 180000 => 3 mins
	myPig = (function () {PigEgg.setup();}).periodical(120000,this);
});