// Programiraje:
// Bojan Plankar
// www.simetric.si

var Barva='red'; // Barva okvirja, ko je celica označena
var ZadnjiOz = null; // Nazadnje označen košček
var StPotez = 0; // Število premikov
var Vrstice=6; // Število vrstic
var Stolpci=8; // Število stolpcev
var Kosi=48; // Število koščkov
var Start; // Začetek igre
var Konec; // Konec igre
var porabljeno; // Porabljen čas
var scas=0; // Pomožna spremenljivka za štetje časa

window.onload = Inicializacija;

function Zmaga()
{
  clearTimeout(scas);
  vnos();
}

function Zacetek()
{
  StejCas();
}  

function StejCas()
{
  PorabljenCas.innerText=parseInt(PorabljenCas.innerText)+1;
  scas=setTimeout("StejCas()",1000);
}

function Inicializacija()
{
  var oRow;
  var iRow, iCol;
  for (iRow = 0; iRow < Vrstice; iRow++) {
	oRow = oTable.insertRow();
    for (iCol = 0; iCol < Stolpci; iCol++) {
	  oRow.insertCell();
    }
  } 
  PremesajCelice();
}

// Generiranje naključnih števil (odvisno od datuma in ure)

function GenerirajNaklj(maxVred) {
  var now = new Date();
  var num = now.getTime() * now.getSeconds() * Math.random();
  return Math.round(num % maxVred);
}


// Mešanje koščkov

function PremesajCelice()
{ 
  var arr, nIndex, nRnd, arrCount, bFound, iRow, iCol;
  arr = new Array(Kosi);
  for (nIndex = 0; nIndex < Kosi; nIndex++) {
    bFound = 1;
    while (bFound == 1) {
      bFound = 0;
      nRnd = GenerirajNaklj(Kosi-1);
      for (arrCount = nIndex; arrCount >= 0; arrCount--) {
        if (arr[arrCount] == nRnd) {
          bFound = 1;
        }
      }
    }
    arr[nIndex] = nRnd;
  }
  nIndex = 0;
  for (iRow = 0; iRow < Vrstice; iRow++) {
    theRow=oTable.rows(iRow);
	for (iCol = 0; iCol < Stolpci; iCol++) {
      theRow.cells(iCol).innerHTML = "<img src=puzzle/" + arr[nIndex] + ".gif>"
	  nIndex++;
    }
  }
  nIndex = 0;
  for (iRow = 0; iRow < Vrstice; iRow++) {
    theRow=oTable.rows(iRow);
  	for (iCol = 0; iCol < Stolpci; iCol++) {
      OdznaciCelico(oTable.rows(iRow).cells(iCol));
      nIndex++;
    }
  }
  KoncniText.innerText = "";
  StPotez = 0;
  SteviloPotez.innerText = StPotez;
  PorabljenCas.innerText=0;
  document.forma.poteze.value=StPotez;
  if (scas>0) clearTimeout(scas);
}

// Klikanje na slikice

function ObKlikuCelice()
{
  var e, c;  
  if (KoncniText.innerText != "") {
    window.event.cancelBubble = true;
	return;
  }
  e = window.event.srcElement;
  c = NajdiCelico(e);
  if (c != null) {
    if (ZadnjiOz == null) {
      OznaciCelico(c);
      ZadnjiOz = c;
	}
	else {
	  if (c == ZadnjiOz) {
	    OdznaciCelico(ZadnjiOz);
	  }
	  else {
	    c.swapNode(ZadnjiOz);
            OdznaciCelico(ZadnjiOz);
	    StPotez += 1;
            if (StPotez==1) Zacetek();
	    SteviloPotez.innerText = StPotez;
            document.forma.poteze.value=StPotez;
	    PreveriKonec();
	  }
	  ZadnjiOz = null;
    }
  }
  window.event.cancelBubble = true;
} 

// Iskanje koščka

function NajdiCelico(e)
{
  if (e.tagName == "TD") {
    return e;
  }
  else if (e.tagName == "BODY") {
    return null;
  }
  else {
    return NajdiCelico(e.parentElement);
  }
}

// Označevanje koščka

function OznaciCelico(c)
{
  c.runtimeStyle.backgroundColor = Barva;
  c.runtimeStyle.color = "#66CCED";
}

// Odznačevanje koščka

function PrekliciOznacbo()
{
  if (ZadnjiOz != null) {
    OdznaciCelico(ZadnjiOz);
    ZadnjiOz = null;
  }
}

// Odznačevanje koščka

function OdznaciCelico(c)
{
  c.runtimeStyle.backgroundColor = "#66CCED";
  c.runtimeStyle.color = "#66CCED";
}

// Preverjanje rešenosti

function PreveriKonec()
{
  var bWinner = 1;
  var nIndex = 0;
  var sFilename;
  for (iRow = 0; iRow < Vrstice; iRow++) {
    theRow=oTable.rows(iRow);
	for (iCol = 0; iCol < Stolpci; iCol++) {
      sFilename = theRow.cells(iCol).innerHTML;
	  sFilename = sFilename.substring(sFilename.length - 8, sFilename.length - 6);
	  if (sFilename.substring(0,1)=="/")
	  {
	   sFilename=sFilename.substring(1,2);
	  }
      if (sFilename != nIndex) {
	    bWinner = 0;
	  }
	  nIndex++;
    }
  }  
  if (bWinner == 1) {
    document.forma.cas.value=PorabljenCas.innerText;
    KoncniText.innerHTML="<font color='#66CCED'>N</font>";
	nIndex = 0;
    for (iRow = 0; iRow < Vrstice; iRow++) {
      theRow=oTable.rows(iRow);
  	  for (iCol = 0; iCol < Stolpci; iCol++) {
	    OznaciCelico(oTable.rows(iRow).cells(iCol));
	    nIndex++;
      }
    }
   Zmaga();
  }
}

oTable.onclick = ObKlikuCelice;
document.onclick = PrekliciOznacbo;