var picked = new Array(4);
picked[0] = new Array(4);
picked[1] = new Array(4);
picked[2] = new Array(4);
picked[3] = new Array(4);
picked[4] = new Array(4);
var split = 3;
var poza = 1;
var picki = 0, pickj = 0;
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function openhelp() {
  document.location.href = 'help2.htm';
  window.resizeTo(765,630);
}
function openhelp2() {
  document.location.href = 'help.htm';
  window.resizeTo(765,630);
}
function cl(a) {
  if (a == 1) {
    window.opener.close();
    window.close();
  } else {
    window.close();
  }
}
function scrimg(a) {
  if (a == 1) {
    if (document.all['view1'].style.pixelTop > -200){
      document.all['view1'].style.pixelTop = document.all['view1'].style.pixelTop - 100;
    }
  } else {
    if (document.all['view1'].style.pixelTop < 0){
      document.all['view1'].style.pixelTop = document.all['view1'].style.pixelTop + 100;
    }
  }
}
function over() {
  for (i = 0; i <= 4; i++)
    for (j = 0; j <= 4; j++)
      picked[i][j] = 0;
  var content = "";
  for (j = 0; j < split; j++) {
    for (i = 0; i < split; i++) {
      content = content + "<img id='" + i + j +"' onmouseover='check(" + i + ","+ j +")' src='img/" + poza + "/" + split + "x" + split + "/b" + i + j + ".jpg'>";
    }
    content = content + "<br>";
  }
  content = content + "<span id='selected'></span>";
  document.getElementById("view2").innerHTML = content;
  pickOne();
  border();
  setLenghts();
}
function lclick() {
  for (i = 0; i <= 4; i++)
    for (j = 0; j <= 4; j++)
      picked[i][j] = 0;
  var content = "";
  for (j = 0; j < split; j++) {
    for (i = 0; i < split; i++) {
      content = content + "<img id='" + i + j +"' onclick='check(" + i + ","+ j +")' src='img/" + poza + "/" + split + "x" + split + "/b" + i + j + ".jpg'>";
    }
    content = content + "<br>";
  }
  content = content + "<span id='selected'></span>";
  document.getElementById("view2").innerHTML = content;
  pickOne();
  border();
  setLenghts();
}
function rclick() {
  for (i = 0; i <= 4; i++)
    for (j = 0; j <= 4; j++)
      picked[i][j] = 0;
  var content = "";
  for (j = 0; j < split; j++) {
    for (i = 0; i < split; i++) {
      content = content + "<img id='" + i + j +"' oncontextmenu='checkr(" + i + ","+ j +")' src='img/" + poza + "/" + split + "x" + split + "/b" + i + j + ".jpg'>";
    }
    content = content + "<br>";
  }
  content = content + "<span id='selected'></span>";
  document.getElementById("view2").innerHTML = content;
  pickOne();
  border();
  setLenghts();
}
function dblclick() {
  for (i = 0; i <= 4; i++)
    for (j = 0; j <= 4; j++)
      picked[i][j] = 0;
  var content = "";
  for (j = 0; j < split; j++) {
    for (i = 0; i < split; i++) {
      content = content + "<img id='" + i + j +"' ondblclick='check(" + i + ","+ j +")' src='img/" + poza + "/" + split + "x" + split + "/b" + i + j + ".jpg'>";
    }
    content = content + "<br>";
  }
  content = content + "<span id='selected'></span>";
  document.getElementById("view2").innerHTML = content;
  pickOne();
  border();
  setLenghts();
}
function mRandom(lbound,ubound){
// return a random number >= lbound and < upper bound
	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}
function pickOne() {
  var test = 0;
  for (i = 0; i < split; i++)
    for (j = 0; j < split; j++){
      if (picked[i][j] == 0) test = 1;
    }
  if (test != 0) {
    picki = mRandom(0,split);
    pickj = mRandom(0,split);
    if (picked[picki][pickj] == 1) {
      pickOne();
    } else {
      picked[picki][pickj] = 1;
      if (picki == 0) newleft = 0 + 23;
      if (picki == 1) newleft = parseInt(460 / split) + 23;
      if (picki == 2) newleft = parseInt(460 / split) * 2 + 23;
      if (picki == 3) newleft = parseInt(460 / split) * 3 + 23;
      if (picki == 4) newleft = parseInt(460 / split) * 4 + 23;
      if (pickj == 0) newtop = 0 + 82;
      if (pickj == 1) newtop = parseInt(360 / split) + 82;
      if (pickj == 2) newtop = parseInt(360 / split) * 2 + 82;
      if (pickj == 3) newtop = parseInt(360 / split) * 3 + 82;
      if (pickj == 4) newtop = parseInt(360 / split) * 4 + 82;
      document.all['selected'].style.pixelTop = newtop;
      document.all['selected'].style.pixelLeft = newleft;
    }
  }
}
function border() {
  if (split == 5){
    document.getElementById("cinci").style.borderColor = '#000000';
    document.getElementById("patru").style.borderColor = '#800000';
    document.getElementById("trei").style.borderColor = '#800000';
    document.getElementById("trei").src = 'img/3x3.jpg';
    document.getElementById("patru").src = 'img/4x4.jpg';
    document.getElementById("cinci").src = 'img/5x5o.jpg';
  } else if (split == 4) {
    document.getElementById("cinci").style.borderColor='#800000';
    document.getElementById("patru").style.borderColor='#000000';
    document.getElementById("trei").style.borderColor='#800000';
    document.getElementById("trei").src = 'img/3x3.jpg';
    document.getElementById("patru").src = 'img/4x4o.jpg';
    document.getElementById("cinci").src = 'img/5x5.jpg';
  } else {
    document.getElementById("cinci").style.borderColor='#800000';
    document.getElementById("patru").style.borderColor='#800000';
    document.getElementById("trei").style.borderColor='#000000';
    document.getElementById("trei").src = 'img/3x3o.jpg';
    document.getElementById("patru").src = 'img/4x4.jpg';
    document.getElementById("cinci").src = 'img/5x5.jpg';
  }
}
function   setLenghts() {
  document.all['selected'].style.width = parseInt(460 / split);
  document.all['selected'].style.height = parseInt(360 / split);
}
function check(a,b) {
  if ((a == picki) && (b == pickj)) {
    changeImg(a,b);
    document.all['selected'].style.pixelTop = 4000;
    document.all['selected'].style.pixelLeft = 4000;
    pickOne();
  }
}
function checkr(a,b) {
  if (event.button != 1) {
    if ((a == picki) && (b == pickj)) {
      changeImg(a,b);
      document.all['selected'].style.pixelTop = 4000;
      document.all['selected'].style.pixelLeft = 4000;
      pickOne();
    }
  }
}
function changeImg(a,b) {
  poz = document.getElementById(a + "" + b);
  poz.src = "img/" + poza + "/" + split + "x" + split + "/" + a + b + ".jpg";
}
document.oncontextmenu = function() {return false;}

function puzzel() {
  var content = "";
  var t, l;
  for (j = 0; j < split; j++) {
    for (i = 0; i < split; i++) {
      t = mRandom(82,(442 - parseInt(360 / split)));
      l = mRandom(23,(483 - parseInt(460 / split)));
      content = content + "<span id='floater" + i + j +"' style=\"top: " + t + "px; left: " + l + "px\" class='draged'><span class='handle' handlefor='floater" + i + j +"'><img src='img/" + poza + "/" + split + "x" + split + "/" + i + j + ".jpg'></span></span>";
    }
  }
  document.getElementById("view2").innerHTML = content;
  border();
}

var checkZIndex = true;
var dragobject = null;
var tx;
var ty;
var ie5 = document.all != null && document.getElementsByTagName != null;
function getReal(el) {
 temp = el;
 while ((temp != null) && (temp.tagName != "BODY")) {
  if (temp.className == "handle"){
   el = temp;
   return el;
  }
  temp = temp.parentElement;
 }
 return el;
}
function moveme_onmousedown() {
  el = getReal(window.event.srcElement)
  if (el.className == "handle") {
    tmp = el.getAttribute("handlefor");
    if (tmp == null) {
      dragobject = null;
      return;
    } else dragobject = eval(tmp);
  } else dragobject = el;
  if (checkZIndex) makeOnTop(dragobject);
  ty = window.event.clientY - getTopPos(dragobject);
  tx = window.event.clientX - getLeftPos(dragobject);
  window.event.returnValue = false;
  window.event.cancelBubble = true;
}
function moveme_onmouseup() {
  if(dragobject) {
    rePos(dragobject);
    dragobject = null;
  }
}
function moveme_onmousemove() {
  minx = 23 + tx;
  miny = 82 + ty;
  maxx = 483 - parseInt(460 / split) + tx;
  maxy = 442 - parseInt(360 / split) + ty;
  if (dragobject) {
    if (window.event.clientX >= minx && window.event.clientX <= maxx ) {
      if(dragobject.className == "draged") 
        dragobject.style.left = window.event.clientX - tx;
    }
    if (window.event.clientY >= miny && window.event.clientY <= maxy ) {
      dragobject.style.top = window.event.clientY - ty;
    }
    window.event.returnValue = false;
    window.event.cancelBubble = true;
  }
}
function rePos(el) {
  mustx = parseInt(460 / split) * parseInt(el.id.toString().substring(7,8)) + 23;
  musty = parseInt(360 / split) * parseInt(el.id.toString().substring(8,9)) + 82;
  if (Math.abs(getLeftPos(el) - mustx) < 15 && Math.abs(getTopPos(el) - musty) < 15) {
    el.style.left = mustx;
    el.style.top = musty;
  }
}
function getLeftPos(el) {
 if (ie5) {
  if (el.currentStyle.left == "auto")
   return 0;
  else
   return parseInt(el.currentStyle.left);
 }
 else {
  return el.style.pixelLeft;
 }
}
function getTopPos(el) {
 if (ie5) {
  if (el.currentStyle.top == "auto")
   return 0;
  else
   return parseInt(el.currentStyle.top);
 }
 else {
  return el.style.pixelTop;
 }
}
function makeOnTop(el) {
 var daiz;
 var max = 0;
 var da = document.all;
 for (var i=0; i<da.length; i++) {
  daiz = da[i].style.zIndex;
  if (daiz != "" && daiz > max)
   max = daiz;
 }
 el.style.zIndex = max + 1;
}
function openpic() {
  var src = "img/" + poza + "/" + poza + ".jpg";
  window.open(src,'','width=480,height=380');
}
function openrez() {
}
function openopt() {
}

