var CStar=Class.create(
{
		initialize: function()
		{
			var loop;
			var div;
			this.detectCollision=true;
			this.doubleUpdate=true;
			this.maxLives=3;
			this.timer=null;
			this.stars=null;
			this.isPaused=false;
			this.lifeIndicators=new Array();
			for (loop=0; loop<this.maxLives; loop++)
			{
				div=document.createElement('div');
				div.className='life_indicator';
				div.style.left=(loop*30) + 'px';
				$('container').appendChild(div);
				this.lifeIndicators.push(div);
			}
			Event.observe($('pause_div'),"mousedown",this.pause.bindAsEventListener(this));
			Event.observe($('play_button'),"click",this.play.bindAsEventListener(this));
			Element.setOpacity('level_number',0.15);
			Element.setOpacity('paused',0.8);
			Event.observe($('button'),"mousedown",this.onButtonClick.bindAsEventListener(this));
			this.renderShipPositions();
			this.gameStarted=false;
			this.pause();
//			this.newGame();
//			this.beginLevel();
		},
		renderShipPositions: function()
		{
			var loop;
			var position;
			this.shipPositions=new Array();
//			for (loop=0; loop<320; loop++)
			for (loop=0; loop<10; loop++)
			{
				position=document.createElement('div');
				position.className='ship_position';
				position.style.left='0px';
				position.style.left=loop + 'px';
				position.style.top='120px';
//				position.style.display='none';
				$('container').appendChild(position);
				this.shipPositions.push(position);
			}
		},
		renderStar: function(x,y)
		{
			var star=document.createElement("div");
			var opacity=Math.round(Math.random()*75)+25;
			star.className='star';
			star.style.left=x + 'px';
			star.style.top=y + 'px';
//			Element.setOpacity(star,opacity/100);
			$('container').appendChild(star);
			this.stars.push(star);
		},
		addStars: function()
		{
			var loop;
			var x;
			var y;
			for (loop=0; loop<this.level*5; loop++)
			{
				x=Math.round(Math.random()*300-20)+4+32;
				y=Math.round(Math.random()*232)+4;
				this.renderStar(x,y);
			}
		},
		hideShipPositions: function()
		{
			var loop;
//			for (loop=0; loop<this.shipPositions.length; loop++)
//			{
//				this.shipPositions[loop].style.display='none';
//			}
			for (loop=0; loop<this.shipPositions.length; loop++)
			{
				this.shipPositions[loop].style.left='0px';
			}
		},
		showShipPosition: function()
		{
			
			// this.shipPositions[this.x].style.top=this.y + 'px';
			// this.shipPositions[this.x].style.display='';
			// return;
						
			var loop;
			var first=this.shipPositions[0];
			for (loop=1; loop<this.shipPositions.length; loop++)
			{
				this.shipPositions[loop-1]=this.shipPositions[loop];
			}
			first.style.left=this.x + 'px';
			first.style.top=this.y + 'px';
			this.shipPositions[this.shipPositions.length-1]=first;
		},
		updateLifeIndicators: function()
		{
			var loop;
			var display;
			for (loop=1; loop<=this.lifeIndicators.length; loop++)
			{
				if (this.lives>=loop)
				{
					display='';
				}
				else
				{
					display='none';
				}
				this.lifeIndicators[loop-1].style.display=display;
			}
		},
		newGame: function()
		{
			this.gameStarted=true;
			$('game_over').style.display='none';
			this.direction=1;
			this.level=0;
			this.lives=this.maxLives;
			this.updateLifeIndicators();
			this.nextLevel();
		},
		restartLevel: function()
		{
			if (this.timer)
			{
				clearInterval(this.timer);
				this.timer=null;
			}
			setTimeout(this.beginLevel.bind(this),2000);
		},
		nextLevel: function()
		{
			var loop;
			if (this.timer)
			{
				clearInterval(this.timer);
				this.timer=null;
			}
			this.level++;
			if (this.stars)
			{
				for (loop=0; loop<this.stars.length; loop++)
				{
					$('container').removeChild(this.stars[loop]);
				}
			}
			this.stars=new Array();
			this.addStars();
			this.beginLevel();
		},
		startLevel: function()
		{
			$('level').style.display='none';
			this.hideShipPositions();
			this.x=0;
			this.y=120;
			this.startPreviewLevel();
			$('level_number').innerHTML=this.level;
			if (this.timer)
			{
				clearInterval(this.timer);
				this.timer=null;
			}
			this.timer=setInterval(this.update.bind(this),25);
		},
		beginLevel: function()
		{
			$('level').style.display='';
			setTimeout(this.startLevel.bind(this),2000);
		},
		startPreviewLevel: function()
		{
			this.previewLevel=true;
			$('level_number').style.display='';
			setTimeout(this.endLevelPreview.bind(this),2000);
		},
		onButtonClick: function(event)
		{
			Event.stop(event);
			this.direction*=-1;
			if (this.direction==-1)
			{
				$('button').className='down_button';
			}
			else
			{
				$('button').className='up_button';
			}
		},
		endLevelPreview: function()
		{
			this.previewLevel=false;
			$('level_number').style.display='none';
			$('up_indicator').style.display='none';
			$('down_indicator').style.display='none';
		},
		update: function()
		{
			if (this.paused)
			{
				return;
			}
			if (this.previewLevel)
			{
				if (this.direction==-1)
				{
					$('up_indicator').style.display='';
					$('down_indicator').style.display='none';
				}
				else
				{
					$('down_indicator').style.display='';
					$('up_indicator').style.display='none';
				}
				return;
			}
			this.showShipPosition();
			if (this.doubleUpdate)
			{
				this.x++;
				this.y+=this.direction;
				this.showShipPosition();
			}
			this.doubleUpdate=!this.doubleUpdate;
			this.x++;
			this.y+=this.direction;
			if (this.x>=320)
			{
				if ((this.y>=90) && (this.y<=150))
				{
					this.nextLevel();
				}
				else
				{
					this.looseLife();
					if (this.lives)
					{
						this.restartLevel();
						return;
					}
				}
			}
			if (this.hitSomething())
			{
				this.looseLife();
				if (this.lives)
				{
					this.restartLevel();
					return;
				}
			}
		},
		pause: function()
		{
			this.paused=true;
			$('paused').style.display='';
			$('play_button').style.display='';
		},
		play: function()
		{
			if (!this.gameStarted)
			{
				this.newGame();
				this.beginLevel();
			}
			this.startPreviewLevel();
			$('paused').style.display='none';
			$('play_button').style.display='none';
			this.paused=false;
		},
		showExplosion: function()
		{
			$('explosion').style.left=this.x + 'px';
			$('explosion').style.top=this.y + 'px';
			$('explosion').style.display='';
		},
		hideExplosion: function()
		{
			$('explosion').style.display='none';
		},
		looseLife: function()
		{
			this.showExplosion();
			setTimeout(this.hideExplosion.bind(this),2000);
			this.lives--;
			this.updateLifeIndicators();
			if (!this.lives)
			{
				if (this.timer)
				{
					clearInterval(this.timer);
					this.timer=null;
				}
				setTimeout(this.gameOver.bind(this),2000);
			}
		},
		gameOver: function()
		{
			if (this.timer)
			{
				clearInterval(this.timer);
				this.timer=null;
			}
			this.hideShipPositions();
			$('game_over').style.display='';
			setTimeout(this.newGame.bind(this),3000);
		},
		hitSomething: function()
		{
			var loop;
			this.detectCollision=!this.detectCollision;
			if ((this.y<=0) || (this.y>=238))
			{
				return true;
			}
			if (!this.detectCollision)
			{
				return false;
			}
			for (loop=0; loop<this.stars.length; loop++)
			{
				if ((this.x>parseInt(this.stars[loop].style.left)-3) && (this.x<parseInt(this.stars[loop].style.left)-3+7))
				{
					if ((this.y>parseInt(this.stars[loop].style.top)-3) && (this.y<parseInt(this.stars[loop].style.top)-3+7))
					{
						return true;
					}
					
				}
			}
			return false;
		}
});