var max_height = 16
var max_width = 31

//Free JavaScripts on http://www.ScriptBreaker.com
var level = new Array()
//name,width,height,bombs
level[0] = new Array("Beginner",9,9,10)
level[1] = new Array("Intermediate",16,16,40)
level[2] = new Array("Expert",30,16,99)

var color = new Array()
color[0] = "#AAAAAA"
color[1] = "blue";
color[2] = "green"
color[3] = "red";
color[4] = "darkblue";
color[5] = "darkred";
color[6] = "lightgreen";
color[7] = "black";
color[8] = "gray";

var w = 16
var h = 16
var b
var vlevel = 1
var bomb = "¤"
var start = false;
var ga = new Array();
var intdate;
document.write("<center>");
document.write("<br><br>");
document.write("<table border=4><tr><td colspan=4 bgcolor=darkblue><img src='mine/title.gif'></td></tr><tr><td bgcolor=gray style='vertical-align:middle;'><table border=0 bgcolor=gray cellspacing=0 cellpadding=0 width=100%><tr><td>")
document.write("<div class=txtdiv id=count>040</div></td><td align=middle>")
document.write("<input type=button onclick=playinit() value='Play' class=button id=playbutton></td><td align=right>")
document.write("<div class=txtdiv id=timer>000</div></td></tr></table>")
document.write("</td></tr><tr><td align=center><div id=header2 style='position:relative;width:"+((w*16)+1)+";height:"+((h*16)+1)+"' >")
document.write("<div  style='position:absolute;overflow:hidden;width:"+((w*16)+1)+";top:0;left:0;' id=header><table border=1 BORDERCOLOR='#0000FF' cellspacing=0 cellpadding=0 style='border-collapse:collapse'>")

for(i=0;i<max_height;i++)
{
 document.write("<tr>")
  for(j=0;j<max_width;j++)
  {
    id = "h_"+i+"w_"+j
    between = "";
    if((i>w) || (j> h)) between = "style='display:none;'"
    document.write("<td><div id="+id+" class=sdiv onmousedown=\"clickon(event,"+i+","+j+",'"+id+"')\" ipos="+i+" jpos="+j+" onmouseout=uncheck() onmouseover=checkc('"+id+"',"+i+","+j+") "+ between +"></div></td>")
  }
  document.write("</tr>")
}
document.write("</table></div></div>")
document.write("</td></tr><tr><td bgcolor=gainsboro><table border=0 cellspacing=0 cellpadding=0 width=100%><tr><td><select class=button onchange='clevel(this.value)'><option value=0>Principiante<option value=1 selected>Intermedio<option value=2>Esperto</select></td><td align=right><input type=button class=button value='Flag' class=button onclick='flag()'></td></tr></table></td></tr>")
document.write("</td></tr></table>")

function checkc(id,ci,cj)
{ 
  if((vcheat == false) || (start == false)) return false;
  if(ga[ci][cj][1] == true) return false;
  if(ga[ci][cj][0] == bomb){
  document.getElementById("playbutton").style.backgroundColor = "#9999CC"
  }
}

function uncheck()
{
 document.getElementById("playbutton").style.backgroundColor = "white"
}

var T1
function settimer()
{
 temp = new Date()
 between = Math.floor((temp - intdate)/(1000));
 if(between < 10) between = "00"+between;
 else if(between < 100) between = "0"+ between;
 document.getElementById("timer").innerHTML = between
 if(start == true) T1 = setTimeout("settimer()",1000);
}

function clevel(val)
{
 if(ie){
 vlevel = val
 w = level[val][1]
 h = level[val][2]
 b = level[val][3]
 playinit();}
 else{
 alert("Sorry, your browser does not support this function!")
 }
}

function dead()
{
 clearTimeout(T1);
 start = false
 for(i=0;i<h;i++)
{
  for(j=0;j<w;j++)
  {
   if(ga[i][j][0] == bomb)
   {
     id = "h_"+i+"w_"+j
     obj = document.getElementById(id)
     obj.innerHTML = "<font color=red><b>"+bomb+"</b></font>";
     obj.style.backgroundColor="gainsboro";
   }
  }
}
}

function playinit()
{
 b = level[vlevel][3]
 document.getElementById("count").innerHTML = "0"+b
 init();
 ga = new Array();
for(i=0;i<h;i++)
{
  ga[i] = new Array()
  for(j=0;j<w;j++)
  {
   ga[i][j] = new Array()
   ga[i][j][0] = 0
   ga[i][j][1] = 0
}}

 for(i=0;i<b;i++)
 {
   ok = false
   while (ok == false)
   {
    x = Math.floor((h*Math.random()))
    y = Math.floor((w*Math.random()))
    if(ga[x][y][0] != bomb)
    {
     ga[x][y][0] = bomb;
     ok = true;
    }
   }
 }

for(i=0;i<h;i++)
{
  for(j=0;j<w;j++)
  {
    counter = "";
    if(ga[i][j][0] == bomb) {counter = bomb}
    else
    {
      counter=0;
      if(ga[i][j+1]){if(ga[i][j+1][0] == bomb) counter++;}
      if(ga[i][j-1]){if(ga[i][j-1][0] == bomb) counter++;}
      if((i+1)<h){if(ga[i+1][j][0] == bomb) counter++;}
      if((i-1)>=0){if(ga[i-1][j][0] == bomb) counter++;}
      if(((i+1)<h) && ((j+1)<w)){if(ga[i+1][j+1][0] == bomb) counter++;}
      if(((i+1)<h) && ((j-1)>=0)){if(ga[i+1][j-1][0] == bomb) counter++;}
      if(((i-1)>=0) && ((j+1)<w)){if(ga[i-1][j+1][0] == bomb) counter++;}
      if(((i-1)>=0) && ((j-1)>=0)){if(ga[i-1][j-1][0] == bomb) counter++;}
    }
   ga[i][j][0] = counter;
  }
}

 start = true
 intdate = new Date();
 settimer();
}
var vflag = false
function flag()
{
	if(vflag)
	{
		vflag = false;
        document.body.style.cursor='default'
	}
	else
	{
		vflag = true;
		document.body.style.cursor = "help";
	}

}

function clickon(event,i,j,id)
{
 if(!start)
 {alert("Clicca 'Play' per iniziare la partita!");
  return false
 }
 if(ga[i][j][1] == true) return false
	 var nav=navigator.appName;
  var IE="Microsoft Internet Explorer";
  var NS="Netscape";
  if ((nav==NS && event.which==3) || (nav==IE && event.button==2))  {setflag(i,j);return false}
	else proces(id)

}

function setflag(i,j)
{
	 id = "h_"+i+"w_"+j
     obj = document.getElementById(id)
     if(ga[i][j][1] == "?")
     {
          ga[i][j][1] = false
          obj.innerHTML = "";
	  b++;
     }
     else{
     if(ga[i][j][1] == "flag")
     {
          ga[i][j][1] = "?"
          obj.innerHTML = "<font color=black><b>?</b></font>";
     }
     else
     {
	     ga[i][j][1] = "flag"
	     obj.innerHTML = "<font color=red><b>P</b></font>";
     b--;
     }}
     document.getElementById("count").innerHTML  = b;
     winner()

}

function winner()
{
win = true;
 for(i=0;i<h;i++)
   {
  for(j=0;j<w;j++)
  {
	  if((ga[i][j][0] == bomb) && (ga[i][j][1] != "flag"))
	  {win = false;i=h;j=w}
  }
  }
  if(win)
  {
	  end_win()
  }
}
function winner2()
{
swin = true;
 for(i=0;i<h;i++)
   {
  for(j=0;j<w;j++)
  {
	  if((ga[i][j][0] != bomb) && (ga[i][j][1] != true))
	  {swin = false;}
  }
  }
  if(swin)
  {
	  end_win()
  }
}

function end_win()
{
	clearTimeout(T1);
	alert("Congratulazioni, hai VINTO!");
	start = false
}

function init()
{
 h = level[vlevel][2]
 w = level[vlevel][1]
head = document.getElementById("header").style
head2 = document.getElementById("header2").style
head.height = (((h)*16)+1) +"px";
head.width = (((w)*16)+1) +"px";
head2.height = (((h)*16)+1) +"px";
head2.width = (((w)*16)+1) +"px";
for(i=0;i<max_height;i++)
{
 for(j=0;j<max_width;j++)
 {
  id = "h_"+i+"w_"+j
 obj = document.getElementById(id)
 if((i>=h)||(j>=w))obj.style.display  = "none";
 else
  {obj.style.display = "";
   obj.innerHTML = "";
   obj.style.backgroundColor= "#AAAAAA";}
 }
}
}

var dopointer;
var doopen = new Array();
function proces(id)
{


 mobj = document.getElementById(id)
 jpos = mobj.getAttribute("jpos",false)
 ipos = mobj.getAttribute("ipos",false)
 if(ga[ipos][jpos][1] == true) return false
 if(vflag) {setflag(ipos,jpos);return false;}
 mtext = ga[ipos][jpos][0]
 mttext = mtext
 if(mttext == bomb){
 dead();
 return false
 }
 if(mttext == 0)
 {
  doopen = new Array()
  doopen[0] = new Array()
  doopen[0][0] = parseInt(ipos);
  doopen[0][1] = parseInt(jpos);
  forceopen(ipos,jpos);
  mttext = "&nbsp"
  procesopen();
 }
 else{
 mobj.innerHTML = "<font color="+color[mtext]+"><b>"+mttext+"</b></font>";
 mobj.style.backgroundColor="gainsboro";
 ga[ipos][jpos][1] = true;
 winner2();
 }
}

function procesopen()
{
 for(k=0;k<doopen.length;k++)
 {
      i = doopen[k][0];
      j = doopen[k][1];

      if((j+1)<w){
      if(ga[i][j+1][1] != true)
      {forceopen(i,(j+1))
      if(ga[i][j+1][0] == 0) push(i,(j+1))}
      if((i-1)>=0){
     if(ga[i-1][j+1][1] != true)
     {forceopen((i-1),(j+1))
     if(ga[i-1][j+1][0] == 0)push((i-1),(j+1))}}}

      if((j-1)>=0){
      if(ga[i][j-1][1] != true)
      {forceopen(i,(j-1))
      if(ga[i][j-1][0] == 0)push(i,(j-1))}
      if((i+1)<h){
     if(ga[i+1][j-1][1] != true)
     {forceopen((i+1),(j-1))
     if(ga[i+1][j-1][0] == 0)push((i+1),(j-1))}}}

     if((i+1)<h){
     if(ga[i+1][j][1] != true)
     {forceopen((i+1),j)
     if(ga[i+1][j][0] == 0)push((i+1),j);}
     if((j+1)<w){
     if(ga[i+1][j+1][1] != true)
     {forceopen((i+1),(j+1))
     if(ga[i+1][j+1][0] == 0)push((i+1),(j+1))}}}

     if((i-1)>=0){
     if(ga[i-1][j][1] != true){
     forceopen((i-1),j)
     if((ga[i-1][j][0] == 0))push((i-1),j)}
     if((j-1)>=0){
     if(ga[i-1][j-1][1] != true)
     {forceopen((i-1),(j-1))
     if(ga[i-1][j-1][0] == 0)push((i-1),(j-1))}}}
}
winner2();
}

function push(hi,hj)
{
 pointer = doopen.length
 doopen[pointer] = new Array()
 doopen[pointer][0] = hi
 doopen[pointer][1] = hj
}

function forceopen(ii,jj)
{
 ga[ii][jj][1] = true;
 id = "h_"+ii+"w_"+jj
 obj = document.getElementById(id)
 text = ga[ii][jj][0]
 ttext = text
 if(ttext == 0) ttext = "&nbsp;"
 obj.innerHTML = "<font color="+color[text]+"><b>"+ttext+"</b></font>";
 obj.style.backgroundColor="gainsboro";
}

var nav=navigator.appName;
var ie=(nav.indexOf("Microsoft")!=-1);
var ns=(nav.indexOf("Netscape")!=-1);

function nrcIE(){
return false;
}

function nrcNS(e){
if(e.which==2 || e.which==3){
return false;
}
 }

if(ie){
document.oncontextmenu=nrcIE;
document.onkeypress = cheat;
}


if(ns){
if(document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=nrcNS;
}
if(document.getElementById){
document.onmouseup=nrcNS;
document.onkeypress = cheat;
}
 }

var SpecialWord = "scriptbreaker.com";
var SpecialLetter = 0;
var vcheat = false
function cheat(keyStroke)
{
 var eventChooser = (ns)?keyStroke.which: event.keyCode;
 var which = String.fromCharCode(eventChooser).toLowerCase();
 if(which == SpecialWord.charAt(SpecialLetter)){
   SpecialLetter++;
   if (SpecialLetter == SpecialWord.length) vcheat = true
}
else {SpecialLetter = 0;vcheat = false}

}