var win_width = 600;
var win_height = 510;

var index_width = 585;
var index_height = 400;

var panel_width = 585;
var panel_height = 400;

var info_width = 585;
var info_height = 400;

var record_width = 585;
var record_height = 471;

var sea_width = 585;
var sea_height = 315;

var sky_width = 585;
var sky_height = 85;

var over_width = 585;
var over_height = 85;

var command_width = 585;
var command_height = 85;

var winDIV;
var indexDIV;
var panelDIV;
var infoDIV;
var recordDIV;
var commandDIV;
var gameOverDIV;
var shipDIV;
var scoreDIV;
var clockDIV;
var torpedoDIV;
var liveDIV;

var recordBut, closeBut, gameBut;
var mm1 = 0, mm2 = 0, ss1 = 0, ss2 = 0;

var turnLeft = 37;
var turnRight = 39;
var turnfire = 34;

var fire = new Array();
var fireFlag = 0;


var playerScore = 0;
var playerLive = 3;

var submarine = new Array();

var subFire = new Array();
var subFireFlag = 0;

var menu;

var fireTimer;
var subFireTimer;
var subTimer;
var shipMoveTimer;
var timeTimer;
var shipTempTimer;
var ms;

var changeFlag = 0;

function game(){
	init();
//	window.location.reload();  刷新
//	var i = document.createElement('img');
//	i.src = 'images/submarine1.gif';
//	i.style.position = 'absolute';
//	i.style.left = 500;
//	i.style.top = 200;
//	document.body.appendChild(i);
}

function init(){
	document.body.style.fontSize = 12;
	winDIV = document.createElement('div');
	indexDIV = document.createElement('div');
	panelDIV = document.createElement('div');
	infoDIV = document.createElement('div');
	recordDIV = document.createElement('div');
	shipDIV = document.createElement('div');
	gameOverDIV = document.createElement('div');

	recordBut = document.createElement('div');
	closeBut = document.createElement('div');
	gameBut = document.createElement('div');

	initWinDIV();
	initIndexDIV();
	
}
//主窗口
function initWinDIV(){
	winDIV.style.position = 'absolute';
	winDIV.style.width = win_width;
	winDIV.style.height = win_height;
	winDIV.style.left = (document.body.offsetWidth - win_width) / 2;
	winDIV.style.top  = (document.body.offsetHeight - win_height) / 2;
	winDIV.style.backgroundImage = 'url(images/background.bmp)';
	winDIV.style.backgroundRepeat = 'no-repeat';
	//winDIV.style.backgroundPosition = 'center';
	winDIV.style.border = 'solid 1px black';
	document.body.appendChild(winDIV);

	
//关闭按钮
	
	closeBut.style.position = 'absolute';
	closeBut.style.width = 21;
	closeBut.style.height = 21;
	closeBut.style.left = 568;
	closeBut.style.top  = 8;
	//closeBut.style.border = 'solid 1px white';
	closeBut.style.backgroundImage = 'url(images/closeBut1.gif)';
	closeBut.style.cursor = 'pointer';
	closeBut.onmouseenter = function(){
		closeBut.style.backgroundImage = 'url(images/closeBut2.gif)';
	}
	closeBut.onmouseout = function(){
		closeBut.style.backgroundImage = 'url(images/closeBut1.gif)';
	}
	closeBut.onmouseup = function(){
		window.close();
	}	
	winDIV.appendChild(closeBut);
//游戏按钮
	
	gameBut.style.position = 'absolute';
	gameBut.style.width = 80;
	gameBut.style.height = 27;
	gameBut.style.left = 391;
	gameBut.style.top  = 4;
	//gameBut.style.border = 'solid 1px white';
	gameBut.style.cursor = 'pointer';
	gameBut.onmouseup = function(){
		gameStop();
		initIndexDIV();
	}	
	winDIV.appendChild(gameBut);
//排行按钮
	
	recordBut.style.position = 'absolute';
	recordBut.style.width = 80;
	recordBut.style.height = 27;
	recordBut.style.left = 474;
	recordBut.style.top  = 4;
	//recordBut.style.border = 'solid 1px white';
	recordBut.style.cursor = 'pointer';
	recordBut.onmouseup = function(){
		gameStop();
		initRecordDIV();
	}	
	winDIV.appendChild(recordBut);
}
//首页
function initIndexDIV(){
	indexDIV.style.position = 'absolute';
	indexDIV.style.width = index_width - 1;
	indexDIV.style.height = index_height;
	indexDIV.style.left = 7;
	indexDIV.style.top  = 34;
	indexDIV.style.backgroundImage = 'url(images/index.bmp)';
	indexDIV.style.backgroundRepeat = 'no-repeat';
	indexDIV.style.backgroundPosition = 'center';
	indexDIV.style.border = 'solid 1px black';
	winDIV.appendChild(indexDIV);
//开始游戏 按钮
	var startBut = document.createElement('div');
	startBut.style.position = 'absolute';
	startBut.style.width = 135;
	startBut.style.height = 45;
	startBut.style.left = 45;
	startBut.style.top  = 285;
	//startBut.style.border = 'solid 1px black';
	startBut.style.cursor = 'pointer';
	startBut.onmouseup = function(){
		if(event.button == 1){
			winDIV.removeChild(indexDIV);
			initPanelDIV();
			addListener();
			createFire();
			createSubmarine();
			createSubFire();
			fireTimer = setInterval('fireMove()', 150);
			//subFireTimer = setInterval('subFireMove()', 150);
			subTimer = setInterval('submarineMove()', 100);
			shipMoveTimer = setInterval('shipMove()',15);
			timeTimer = setInterval('clock()', 1000);
			}
	}
	indexDIV.appendChild(startBut);
//游戏说明 按钮
	var infoBut = document.createElement('div');
	infoBut.style.position = 'absolute';
	infoBut.style.width = 135;
	infoBut.style.height = 45;
	infoBut.style.left = 45;
	infoBut.style.top  = 233;
	//infoBut.style.border = 'solid 1px black';
	infoBut.style.cursor = 'pointer';
	infoBut.onmouseup = function(){
		if(event.button == 1){
			winDIV.removeChild(indexDIV);
			initInfoDIV();
		}
	}
	indexDIV.appendChild(infoBut);
//排行榜 按钮
	var recordBut = document.createElement('div');
	recordBut.style.position = 'absolute';
	recordBut.style.width = 135;
	recordBut.style.height = 45;
	recordBut.style.left = 45;
	recordBut.style.top  = 339;
	//recordBut.style.border = 'solid 1px black';
	recordBut.style.cursor = 'pointer';
	recordBut.onmouseup = function(){
		if(event.button == 1){
			winDIV.removeChild(indexDIV);
			winDIV.style.backgroundImage = 'url(images/background_record.bmp)';
			initRecordDIV();
		}
	}
	indexDIV.appendChild(recordBut);
}
//游戏 面板
function initPanelDIV(){
	panelDIV.style.position = 'absolute';
	panelDIV.style.overflow = 'hidden';
	panelDIV.style.width = panel_width - 1;
	panelDIV.style.height = panel_height;
	panelDIV.style.left = 7;
	panelDIV.style.top  = 34;
	panelDIV.style.backgroundImage = 'url(images/gameBg.bmp)';
	panelDIV.style.backgroundRepeat = 'no-repeat';
	panelDIV.style.cursor = 'crosshair';
	//panelDIV.style.backgroundPosition = 'center';
	panelDIV.style.border = 'solid 1px black';
	winDIV.appendChild(panelDIV);
//暂停 按钮
	var pauseBut = document.createElement('div');
	pauseBut.style.position = 'absolute';
	pauseBut.style.width = 46;
	pauseBut.style.height = 21;
	pauseBut.style.left = 369;
	pauseBut.style.top  = 8;
	//pauseBut.style.border = 'solid 1px black';
	pauseBut.style.cursor = 'pointer';
	pauseBut.onmouseup = function(){
		if(event.button == 1){
			alert('暂停');
		}
	}
	panelDIV.appendChild(pauseBut);
//主菜单 按钮
	var menuBut = document.createElement('div');
	menuBut.style.position = 'absolute';
	menuBut.style.width = 53;
	menuBut.style.height = 21;
	menuBut.style.left = 420;
	menuBut.style.top  = 8;
	//menuBut.style.border = 'solid 1px black';
	menuBut.style.cursor = 'pointer';
	menuBut.onmouseup = function(){
		if(event.button == 1){
			gameStop();
			initIndexDIV();
		}
	}
	panelDIV.appendChild(menuBut);
//帮助 按钮	
	var helpBut = document.createElement('div');
	helpBut.style.position = 'absolute';
	helpBut.style.width = 45;
	helpBut.style.height = 21;
	helpBut.style.left = 478;
	helpBut.style.top  = 8;
	//helpBut.style.border = 'solid 1px black';
	helpBut.style.cursor = 'pointer';
	helpBut.onmouseup = function(){
		if(event.button == 1){
			gameStop();
			initInfoDIV();
		}
	}
	panelDIV.appendChild(helpBut);
	
//总分 显示
	scoreDIV = document.createElement('div');
	scoreDIV.style.position = 'absolute';
	scoreDIV.style.width = 30;
	scoreDIV.style.height = 10;
	scoreDIV.style.left = 5;
	scoreDIV.style.top  = 22;
	//scoreDIV.style.border = 'solid 1px black';
	scoreDIV.style.fontFamily = 'Arial Bold';
	scoreDIV.style.fontSize = 12;
	scoreDIV.style.color = '#fdc801';
	scoreDIV.style.textAlign = 'center';
	//scoreDIV.style.paddingTop = 1;
	scoreDIV.innerHTML = playerScore;
	panelDIV.appendChild(scoreDIV);
//时间 显示
	clockDIV = document.createElement('div');
	clockDIV.style.position = 'absolute';
	clockDIV.style.width = 50;
	clockDIV.style.height = 20;
	clockDIV.style.left = 67;
	clockDIV.style.top = 8;
	//clockDIV.style.border = 'solid 1px black';
	clockDIV.style.fontFamily = 'Arial Bold';
	clockDIV.style.fontSize = 14;
	clockDIV.style.color = '#92dded';
	clockDIV.style.textAlign = 'center';
	clockDIV.innerHTML = mm1 + '' + mm2 + ':' + ss1 + '' + ss2;
	panelDIV.appendChild(clockDIV);
//鱼雷数 显示
	torpedoDIV = document.createElement('div');
	torpedoDIV.style.position = 'absolute';
	torpedoDIV.style.width = 55;
	torpedoDIV.style.height = 29;
	torpedoDIV.style.left = 180;
	torpedoDIV.style.top = 4;
	//torpedoDIV.style.border = 'solid 1px black';
	torpedoDIV.style.backgroundImage = 'url(images/torpedo2.bmp)';
	torpedoDIV.style.backgroundRepeat = 'no-repeat';
	panelDIV.appendChild(torpedoDIV);
//生命 显示
	liveDIV = document.createElement('div');
	liveDIV.style.position = 'absolute';
	liveDIV.style.width = 20;
	liveDIV.style.height = 10;
	liveDIV.style.left = 321;
	liveDIV.style.top  = 10;
	//liveDIV.style.border = 'solid 1px black';
	liveDIV.style.fontFamily = 'Arial Bold';
	liveDIV.style.fontSize = 14;
	liveDIV.style.color = '#fed73a';
	liveDIV.style.textAlign = 'center';
	//liveDIV.style.paddingTop = 1;
	liveDIV.innerHTML = playerLive;
	panelDIV.appendChild(liveDIV);
	//initSkyDIV();
	//initSeaDIV();
	initShipDIV();
}
//游戏说明 面板
function initInfoDIV(){
	infoDIV.style.position = 'absolute';
	infoDIV.style.width = panel_width - 1;
	infoDIV.style.height = panel_height;
	infoDIV.style.left = 7;
	infoDIV.style.top  = 34;
	infoDIV.style.backgroundImage = 'url(images/info.bmp)';
	infoDIV.style.backgroundRepeat = 'no-repeat';
	//infoDIV.style.backgroundPosition = 'center';
	infoDIV.style.border = 'solid 1px black';
	winDIV.appendChild(infoDIV);
//返回游戏 按钮
	var backBut = document.createElement('div');
	backBut.style.position = 'absolute';
	backBut.style.width = 110;
	backBut.style.height = 34;
	backBut.style.left = 185;
	backBut.style.top  = 318;
	//backBut.style.border = 'solid 1px white';
	backBut.style.cursor = 'pointer';
	backBut.onmouseup = function(){
		winDIV.removeChild(infoDIV);
		initIndexDIV();
	}
	infoDIV.appendChild(backBut);
}
//排行榜 面板
function initRecordDIV(){
	recordDIV.style.position = 'absolute';
	recordDIV.style.width = record_width - 1;
	recordDIV.style.height = record_height;
	recordDIV.style.left = 7;
	recordDIV.style.top  = 34;
	recordDIV.style.backgroundImage = 'url(images/record.bmp)';
	recordDIV.style.backgroundRepeat = 'no-repeat';
	//recordDIV.style.backgroundPosition = 'center';
	recordDIV.style.border = 'solid 1px black';
	winDIV.appendChild(recordDIV);
//继续游戏 按钮
	var continueBut = document.createElement('div');
	continueBut.style.position = 'absolute';
	continueBut.style.width = 75;
	continueBut.style.height = 21;
	continueBut.style.left = 480;
	continueBut.style.top  = 418;
	//continueBut.style.border = 'solid 1px black';
	continueBut.style.cursor = 'pointer';
	continueBut.onmouseup = function(){
		winDIV.style.backgroundImage = 'url(images/background.bmp)';
		gameStop();
		initIndexDIV();
	}
	recordDIV.appendChild(continueBut);
}

function initGameOverDIV(){
	gameOverDIV.style.position = 'absolute';
	gameOverDIV.style.width = panel_width - 1;
	gameOverDIV.style.height = panel_height;
	gameOverDIV.style.left = 7;
	gameOverDIV.style.top  = 34;
	gameOverDIV.style.backgroundImage = 'url(images/gameOver.bmp)';
	gameOverDIV.style.backgroundRepeat = 'no-repeat';
	//gameOverDIV.style.backgroundPosition = 'center';
	gameOverDIV.style.border = 'solid 1px black';
	winDIV.appendChild(gameOverDIV);
//好友排行榜 按钮
	var overRecordBut = document.createElement('div');
	overRecordBut.style.position = 'absolute';
	overRecordBut.style.width = 108;
	overRecordBut.style.height = 35;
	overRecordBut.style.left = 163;
	overRecordBut.style.top  = 198;
	//overRecordBut.style.border = 'solid 1px white';
	overRecordBut.style.cursor = 'pointer';
	overRecordBut.onmouseup = function(){
		winDIV.removeChild(gameOverDIV);
		initRecordDIV();
		playerScore = 0;
	}
	gameOverDIV.appendChild(overRecordBut);

//返回游戏 按钮
	var overContinueBut = document.createElement('div');
	overContinueBut.style.position = 'absolute';
	overContinueBut.style.width = 107;
	overContinueBut.style.height = 35;
	overContinueBut.style.left = 165;
	overContinueBut.style.top  = 239;
	//overContinueBut.style.border = 'solid 1px white';
	overContinueBut.style.cursor = 'pointer';
	overContinueBut.onmouseup = function(){
		winDIV.removeChild(gameOverDIV);
		initIndexDIV();
		playerScore = 0;
	}
	gameOverDIV.appendChild(overContinueBut);
//您的得分 显示
	var overScore = document.createElement('div');
	overScore.style.position = 'absolute';
	overScore.style.width = 30;
	overScore.style.height = 30;
	overScore.style.left = 286;
	overScore.style.top  = 162;
	//overScore.style.border = 'solid 1px white';
	overScore.style.fontFamily = 'Arial Bold';
	overScore.style.fontSize = 24;
	overScore.style.fontWeight = 'bold';
	overScore.style.color = '#ffffff';
	overScore.style.textAlign = 'center'; 
	overScore.innerHTML = playerScore;
	overScore.onmouseup = function(){
		winDIV.removeChild(gameOverDIV);
		initIndexDIV();
		playerScore = 0;
	}
	gameOverDIV.appendChild(overScore);
}

//游戏 面板 船
function initShipDIV(){
	shipDIV.style.position = 'absolute';
	shipDIV.style.top = 50;
	shipDIV.style.left = 100;
	shipDIV.style.width = 71;
	shipDIV.style.height = 41;
	shipDIV.style.backgroundImage = 'url(images/ship.gif)';
	panelDIV.appendChild(shipDIV);	
}

function addListener(){
	document.body.onkeydown = function (){
		if(event.keyCode == turnfire){
			openFire();
			return;
		}
	}
	panelDIV.onmousemove = function(){
		ms = event.x;
	}

	document.body.onmousedown = function(){
		if(event.button == 1){
			openFire();
			return;
		}
	}
}

function clock(){
	ss2++;
	
	if(mm1 > 9){
		ss2 = 0;
		ss1 = 0;
		mm2 = 0;
		mm1 = 0;
	}
	if(ss2 > 9 && ss1 == 5 && mm2 == 9){
		mm1++;
		ss2 = 0;
		ss1 = 0;
		mm2 = 0;
	}
	if(ss2 > 9 && ss1 == 5){
		mm2++;
		ss2 = 0;
		ss1 = 0;
	}
	
	if(ss2 > 9){
		ss1++;
		ss2 = 0;
	}
	clockDIV.innerHTML = mm1 + '' + mm2 + ':' + ss1 + '' + ss2;
}

function openFire(){
	if(fireFlag < 5){
		while(fire[fireFlag].isUser == 'true'){
			fireFlag++;
			if(fireFlag > 4){
				fireFlag = 0;
				return;
			}
		}
		fire[fireFlag].style.left = shipDIV.offsetLeft + 20;
		fire[fireFlag].style.display = 'block';
		fire[fireFlag].isUser = 'true';
		fireFlag++;
		if(torpedoDIV.offsetWidth >= 11){
			torpedoDIV.style.width = torpedoDIV.offsetWidth - 11;
		}
		
	}else{
		fireFlag = 0;
	}
}

function openSubFire(obj, objIndex){
	if(obj.isFire == 'false'){
		subFire[objIndex].style.left = obj.offsetLeft + 10;
		subFire[objIndex].style.top = obj.offsetTop + subFire[objIndex].offsetHeight;
		subFire[objIndex].style.display = 'block';
		subFire[objIndex].isUser = 'true';
		subFireFlag++;	
		obj.isFire = 'true';
	}
	
	
}


function fireMove(){
	for(i = 0; i < 5; i++){
		if(fire[i].isUser == 'true'){
			fire[i].style.top = fire[i].offsetTop + 5;
			submarineDestroy(fire[i]);
		}
		if(fire[i].offsetTop >= panelDIV.offsetHeight){
			fire[i].style.display = 'none';
			fire[i].style.top = 85;
			fire[i].isUser = 'false';
			if(torpedoDIV.offsetWidth <= 44){
				torpedoDIV.style.width = torpedoDIV.offsetWidth + 11;
			}
			
			
		}
	}
	subFireMove();
}
function subFireMove(){
	for(i = 0; i < 10; i++){
		if(subFire[i].isUser == 'true'){
			subFire[i].style.top = subFire[i].offsetTop - 5;
			//submarineDestroy(subFire[i]);
		}
		if(subFire[i].offsetTop <= 85){
			if(subFire[i].isUser == 'true'){
				shipDestroy(subFire[i]);	
			}		
			subFire[i].style.display = 'none';
			subFire[i].style.top = panelDIV.offsetHeight;
			subFire[i].isUser = 'false';
			
		}
	}
}

function submarineMove(){
	for(i = 0; i < 10; i++){
		if(submarine[i].isDestroy == 'false'){
			submarine[i].style.left = submarine[i].offsetLeft + 3;
		}else{
			submarine[i].style.top = submarine[i].offsetTop + 3;
		}
		if(submarine[i].offsetLeft > 0 && submarine[i].isFire == 'false' && submarine[i].isDestroy == 'false'){
			switch(Math.floor(Math.random() * 50)){
				case 5: openSubFire(submarine[i], i);break;
				default :break;
			}
			
		}
		if(submarine[i].offsetLeft >= panelDIV.offsetWidth || submarine[i].offsetTop >= panelDIV.offsetHeight){
			submarine[i].style.top = Math.random() * (panelDIV.offsetHeight - 250) + 150;
			submarine[i].style.left =  Math.random() * panelDIV.offsetWidth * 2 + 1 - panelDIV.offsetWidth * 2 - 55;
			submarine[i].src = 'images/submarine1.gif';
			submarine[i].isDestroy = 'false';
			submarine[i].isFire = 'false';
			
		}
	}
}
function shipMove(){
	if(ms - winDIV.offsetLeft > shipDIV.offsetLeft + shipDIV.offsetWidth / 2 + 5){
		shipDIV.style.left = shipDIV.offsetLeft + 2;
	}else if(ms - winDIV.offsetLeft < shipDIV.offsetLeft + shipDIV.offsetWidth / 2 - 5){
		shipDIV.style.left = shipDIV.offsetLeft - 2;
	}
}

function submarineDestroy(obj){
	for(j = 0; j < 10; j++){
		if(((obj.offsetLeft + obj.offsetWidth > submarine[j].offsetLeft && obj.offsetLeft < submarine[j].offsetLeft + submarine[j].offsetWidth) && (obj.offsetTop + obj.offsetHeight > submarine[j].offsetTop && obj.offsetTop  < submarine[j].offsetTop + submarine[j].offsetHeight))  && submarine[j].isDestroy == 'false'){
			obj.style.display = 'none';
			obj.style.top = 85;
			obj.isUser = 'false';
			if(torpedoDIV.offsetWidth <= 44 && submarine[j].isDestroy == 'false'){
				torpedoDIV.style.width = torpedoDIV.offsetWidth + 11;
			}
			submarine[j].isDestroy = 'true';
			submarine[j].src = 'images/submarine.gif';
			playerScore = playerScore + 10;
			scoreDIV.innerHTML = playerScore;
		}
	}	
}s
function shipDestroy(obj){
	if(obj.offsetLeft + obj.offsetWidth > shipDIV.offsetLeft && obj.offsetLeft < shipDIV.offsetLeft + shipDIV.offsetWidth){
		obj.style.display = 'none';
		obj.style.top = panelDIV.offsetHeight;
		obj.style.left = -55;
		obj.isUser = 'false';
		playerLive--;
		shipDIV.style.backgroundImage = 'url(images/shipDestroy.gif)';
		changeFlag = 0;
		clearInterval(shipTempTimer);
		shipTempTimer = setInterval('shipChangeBGimage()', 200);
		if(playerLive <= 0){						
				gameOver();
				//gamestart();
		}
		liveDIV.innerHTML = playerLive;
	}
}
function shipChangeBGimage(){
	if(changeFlag == 1){
		shipDIV.style.backgroundImage = 'url(images/ship.gif)';
		clearInterval(shipTempTimer);
	}
	changeFlag++;
}

//function stopGame(){
//	fireFlag = 0;
//	submarineFlag = 0;
//	for(k = document.body.childNodes.length - 1; k >= 0; k--){
//		document.body.removeChild(document.body.childNodes[k]);
//	}
//
//	document.writeln('<a href=\'www.hao123.com\'>更多资讯请登陆 www.hao123.com</a>');
//	
//}

function createFire(){
	for(i = 0; i < 5; i++){
		fire[i] = document.createElement('div');
		fire[i].style.position = 'absolute';
		fire[i].style.top = 85;
		fire[i].style.left = shipDIV.offsetLeft + 30;
		fire[i].style.width = 8;
		fire[i].style.height = 19;
		//fire[i].src = 'images/torpedo.gif';
		fire[i].style.backgroundImage = 'url(images/torpedo.gif)';
		//fire[i].style.backgroundPosition = 'center';
		fire[i].style.backgroundRepeat = 'no-repeat';
		fire[i].style.display = 'none';
		fire[i].isUser = 'false';
		panelDIV.appendChild(fire[i]);
	}
}

function createSubFire(){
	for(i = 0; i < 10; i++){
		subFire[i] = document.createElement('div');
		subFire[i].style.position = 'absolute';
		subFire[i].style.top = panelDIV.offsetHeight;
		subFire[i].style.left = 0;
		subFire[i].style.width = 6;
		subFire[i].style.height = 16;
		//subFire[i].src = 'images/torpedo.gif';
		subFire[i].style.backgroundImage = 'url(images/torpedo3.gif)';
		//subFire[i].style.backgroundPosition = 'center';
		subFire[i].style.backgroundRepeat = 'no-repeat';
		subFire[i].style.display = 'none';
		subFire[i].isUser = 'false';
		panelDIV.appendChild(subFire[i]);
	}
}

function createSubmarine(){
	for(i = 0; i < 10; i++){
		submarine[i] = document.createElement('img');
		submarine[i].style.position = 'absolute';
		submarine[i].style.top = Math.random() * (panelDIV.offsetHeight - 250) + 150;
		submarine[i].style.left =  Math.random() * panelDIV.offsetWidth * 2 + 1 - panelDIV.offsetWidth * 2 - 55;
		submarine[i].style.width = 55;
		submarine[i].style.height = 40;
		submarine[i].src = 'images/submarine1.gif';
		//submarine[i].style.background = 'black';
		//submarine[i].style.backgroundImage = 'url(images/submarine1.gif)';
		//submarine[i].style.backgroundPosition = 'center';
		//submarine[i].style.backgroundRepeat = 'no-repeat';
		submarine[i].isDestroy = 'false';
		submarine[i].isFire = 'false';
		panelDIV.appendChild(submarine[i]);
		
	}
}

function gameOver(){
	clearInterval(fireTimer);
	clearInterval(subFireTimer);
	clearInterval(subTimer);
	clearInterval(shipMoveTimer);
	clearInterval(timeTimer);
	winDIV.style.backgroundImage = 'url(images/background.bmp)';
	for(i = panelDIV.childNodes.length - 1; i >= 0; i--){
		panelDIV.removeChild(panelDIV.childNodes[i]);
	}
	
	for(i = winDIV.childNodes.length - 1; i >= 0; i--){
		winDIV.removeChild(winDIV.childNodes[i]);
	}
	winDIV.appendChild(closeBut);
	winDIV.appendChild(gameBut);
	winDIV.appendChild(recordBut);
	//initIndexDIV();
	mm1 = 0;
	mm2 = 0;
	ss1 = 0;
	ss2 = 0;
	fireFlag = 0;
	playerLive = 3;
	subFireFlag = 0;
	initGameOverDIV();
}

function gameStop(){
	clearInterval(fireTimer);
	clearInterval(subFireTimer);
	clearInterval(subTimer);
	clearInterval(shipMoveTimer);
	clearInterval(timeTimer);
	winDIV.style.backgroundImage = 'url(images/background.bmp)';
	for(i = panelDIV.childNodes.length - 1; i >= 0; i--){
		panelDIV.removeChild(panelDIV.childNodes[i]);
	}
	
	for(i = winDIV.childNodes.length - 1; i >= 0; i--){
		winDIV.removeChild(winDIV.childNodes[i]);
	}
	winDIV.appendChild(closeBut);
	winDIV.appendChild(gameBut);
	winDIV.appendChild(recordBut);
	mm1 = 0;
	mm2 = 0;
	ss1 = 0;
	ss2 = 0;
	fireFlag = 0;
	playerLive = 3;
	playerScore = 0;
	subFireFlag = 0;
}



function click(){
	if (event.button==2) {
		alert('o(∩_∩)o');
	}
}

document.onmousedown=click;


function gamestart(){
	game();
}
