var loadTime = 0;
var isie = false;
var finished = false;
var started = false;

function zeitInit()
{
  var zeit = $('zeit');
  var offset = 0;
  
  if (zeit && zeit.value) offset = zeit.value;
  var jetzt = new Date;
  loadTime = Math.floor(jetzt.getTime()/1000)-offset;
  zeitUpdate();
}

function zeitUpdate()
{
  var zeitHuman = $('zeitHuman');
  var zeit = $('zeit');
  var jetzt = new Date;
  var seconds = Math.floor(jetzt.getTime()/1000) - loadTime;
  if (zeitHuman) {
    var secondsHuman = '';
    secondsHuman = (seconds%60)+'';
    if (secondsHuman.length == 1) secondsHuman = '0'+secondsHuman;
    
    secondsHuman = (Math.floor(seconds/60)%60)+':'+secondsHuman;
    if (secondsHuman.length == 4) secondsHuman = '0'+secondsHuman;
    
    secondsHuman = (Math.floor(seconds/3600)%60)+':'+secondsHuman;
    if (secondsHuman.length == 7) secondsHuman = '0'+secondsHuman;
    
    zeitHuman.innerHTML = secondsHuman;
  }
  if (zeit) {
    zeit.value = seconds;
  }
  if (!finished)
  {
	  if (seconds % 60 == 0)
	  	  new Ajax.Request('/ajax/keepsession.php');
	  window.setTimeout('zeitUpdate()',1000);
  }
}

function showpruefen(t)
{
  var s = t.headerJSON;
  if (s) {
    if (s[0]) {
		finished = true;
		Effect.Appear('done',{ duration: 1.5 });
    }
    for (var i=1;i<=9;i++) {
      for (var j=1;j<=9;j++) {
        var field = i+'_'+j;
        
        switch (s[i][j]) {
          case 1:
            // richtiger Eintrag
            $('td'+field).style.backgroundImage = $('td'+field).style.backgroundImage.replace(/\_a/,'');
            if ($('tbl'+field)) $('tbl'+field).parentNode.removeChild($('tbl'+field));
            break;
            
          case 0:
            // keiner oder vorgegebener Eintrag
            break;
            
          case -1:
            // falscher Eintrag
            $('td'+field).style.backgroundImage = $('td'+field).style.backgroundImage.replace(/\_a/,'_f');
            break;
          default:
            //alert(s[i][j]);
            break;
        }
      }
    }
  }
}

function pruefen()
{
  var s = $('sudoku');
  if (s) {
          new Ajax.Request( '/ajax/pruefen.php' , {
          onComplete:showpruefen,
          parameters:s.serialize()
          }
        );
  }
}

function speichern(o,a,t)
{
  if (o == null) {
    o = $('sudoku');
  }
  
  if (o) {
    if (a) o.action = a;
    if (t) o.target = t;
    o.submit();
  }
}

function scbg(o,i)
{
	if (i == 0)
	{
		o.style.backgroundImage = 'url(/layout/cell.gif)';	
	}
	else
	{
		o.style.backgroundImage = 'url(/layout/numbers/small_all.gif)';	
	}
}

function ssbg(o,i)
{
	if (i == 0)
	{
		o.style.backgroundImage = 'url(/layout/trans.gif)';	
	}
	else
	{
		o.style.backgroundImage = 'url(/layout/numbers/small_'+i+'.png)';	
	}
}

function initSudoku()
{
    isie = /msie\s(\d)/.test(navigator.userAgent.toLowerCase());
    for (var i=1;i<=9;i++) {
      for (var j=1;j<=9;j++) {
        for (var n=1;n<=9;n++) {
          var field = i+'_'+j;
          if ($('f'+field).value.search(','+n) != -1) {
            showAuswahl(field,n,true);
          }
        }
      }
    }
}

function rv(field,value)
{
	i = $('i'+field+'_'+value);
	if (i)
	{
		if ($('f'+field).value.search(','+value) != -1) {
		  showAuswahl(field,value,false);
		  $('f'+field).value = $('f'+field).value.replace(','+value,'');
		} else {
		  showAuswahl(field,value,true);
		  $('f'+field).value += ','+value;
		}
	}
	if (!started) 
	{
		zeitInit();
		started = true;
	}
}

function sf(e,field,value,init)
{
	if ($('tbl'+field) && $('tbl'+field).style.display == '')
	{
		$('f'+field).value = value;
		$('tbl'+field).style.display = 'none';
		$('td'+field).style.backgroundImage = 'url(/layout/numbers/'+value+'_a.gif)';
	}
	if (!started && !init) 
	{
		zeitInit();
		started = true;
	}
	if (e) Event.stop(e);
}

function showAuswahl(field,n,t)
{
  var	img = $('i'+field+'_'+n);
  if (t) {
    if (isie)
      img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/layout/numbers/small_"+n+".png', sizingMethod='scale')";
    else 
      img.style.backgroundImage = 'url(/layout/numbers/small_'+n+'.png)';
  } else {
    if (isie)
      img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/layout/trans.png', sizingMethod='scale')";
    else 
      img.style.backgroundImage = 'url(/layout/trans.png)';
  }
}

function usf(e,field)
{
	if ($('tbl'+field) && $('tbl'+field).style.display == 'none')
	{
		$('f'+field).value = 0;
		$('tbl'+field).style.display = '';
		$('td'+field).style.backgroundImage = '';
	}
	if (e) Event.stop(e);	
}
