/* de.js - Client-side JS code used by the page layout
 *
 * Copyright (C) 2006, 2007, 2008 Kevin Read, Simone Schaefer
 *
 * This file is part of Selador, a browser-based fantasy strategy game
 *
 * This program is distributed under the terms of the GNU Affero General Public License.
 *
 *
 *   Selador is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   Selador is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with Selador.  If not, see <http://www.gnu.org/licenses/>.
 **/

var hours, mins, secs;
var timers, itimers, max, uhrelem;
chours = new Array();
cmins = new Array();
csecs = new Array();
ichours = new Array ();
icmins = new Array ();
icsecs = new Array ();
counters = new Array();
icounters = new Array();
r = new Array();
resselem = new Array();
t = new Array();

gaGlobal = null;

/* Optimize me: three seperate boxes, so we only need to update secs (most of the time) and don't need
   to split */
function start()
{
	var split_arr, groupctr, numgroups;
  uhrelem = document.getElementById('uhr');
  if(uhrelem != null)
  {
    part = uhrelem.innerHTML.split(":");
    secs = parseInt(part[2], 10);
    mins = parseInt(part[1], 10);
    hours = parseInt(part[0], 10);
  }

  timers = 1;
  var debugstr = "";
  while (document.getElementById("timer"+timers) != null)
  {
    counters[timers] = document.getElementById("timer"+timers);
    if(counters[timers] != null)
    {
      part = counters[timers].innerHTML.split(":");
      csecs[timers] = parseInt(part[2], 10);
      cmins[timers] = parseInt(part[1], 10);
      chours[timers] = parseInt(part[0], 10);
    }
    timers++;
  }

	itimers = 1;
  while (document.getElementById("itimer"+itimers) != null)
  {
    icounters[itimers] = document.getElementById("itimer"+itimers);
    if(icounters[itimers] != null)
    {
      part = icounters[itimers].innerHTML.split(":");
      icsecs[itimers] = parseInt(part[2], 10);
      icmins[itimers] = parseInt(part[1], 10);
      ichours[itimers] = parseInt(part[0], 10);
    }
    itimers++;
  }

  var maxelem = document.getElementById("l");
  if (maxelem)
  {
		splitarr = maxelem.innerHTML.split (".");
		numgroups = splitarr.length;
		max = 0;
		for (groupctr = 0; groupctr < numgroups; groupctr++)
		{
			// alert ("doing run " + groupctr + ", parseint is " + parseInt (splitarr[groupctr], 10) + ", factor is " + Math.pow (10, (numgroups - groupctr - 1)*3 ));
			max += parseInt (splitarr[groupctr], 10) * Math.pow (10, (numgroups - groupctr - 1)*3);
		}
	}
	else
		max = 3500;

	// alert ("max is " + max);

  for (temp = 1; temp <= 4; temp++)
  {
  	r[temp] = 0;
    resselem[temp] = document.getElementById("r"+temp);
    splitarr = resselem[temp].innerHTML.split (".");
    numgroups = splitarr.length;
    if (numgroups == 0)
    	numgroups = 1;
    for (groupctr = 0; groupctr < numgroups; groupctr++)
    {
    	// alert ("doing run " + groupctr + ", parseint is " + parseInt (splitarr[groupctr], 10) + ", factor is " + Math.pow (10, (numgroups - groupctr - 1)*3 ));
    	r[temp] += parseInt (splitarr[groupctr], 10) * Math.pow (10, (numgroups - groupctr - 1)*3);
    }

    // alert ("val is " + r[temp]);
    // r[temp] = parseInt(resselem[temp].innerHTML);

    ielem = document.getElementById("i"+temp);
    splitarr = ielem.innerHTML.split (".");
    numgroups = splitarr.length;

    income = 0;
    for (groupctr = 0; groupctr < numgroups; groupctr++)
    {
    	// alert ("doing run " + groupctr + ", parseint is " + parseInt (splitarr[groupctr], 10) + ", factor is " + Math.pow (10, (numgroups - groupctr - 1)*3 ));
    	income += parseInt (splitarr[groupctr], 10) * Math.pow (10, (numgroups - groupctr - 1)*3);
    }
//    alert ("income is " + income);

		if (income > 0)
		{
			if (income < 3600)
				t[temp] = window.setInterval("income1("+temp+")", Math.abs(3600/income*1000));
			else
				t[temp] = window.setInterval("income2("+temp+", "+(income/3600)+")", 1000);
		}
		else
		{
			if (income < 0)
			{
				if (income < 3600)
					t[temp] = window.setInterval("dec1("+temp+")", Math.abs(3600/income*1000));
				else
					t[temp] = window.setInterval("dec22("+temp+", "+(income/3600)+")", 1000);
			}
		}
  }
  clock_c = window.setInterval("draw_clock()", 1000);
}

function Thousand (str) {
    var X = "", s = String (str), L, num = 3;

    while (s != "") {
        L = s.length - 3;
        if (L  < 0)
        {
        	L = 0;
        	num = 3 + L;
        }
        X = s.substr (L, num) + (X > "" ? "." + X : "");
        s = s.substr(0, L);
    }

    return X;
}

function income1(type)
{
  if (r[type] < max)
  {
    r[type]++;
    resselem[type].innerHTML = Thousand (r[type]);
  }
  else
    window.clearInterval (t[type]);
}

function income2(type, inc)
{
  if (r[type]+inc < max)
  {
    r[type]=Math.min (max, r[type]+inc);
    resselem[type].innerHTML = Thousand (Math.floor (r[type]));
  }
  else
    window.clearInterval (t[type]);
}

function dec1(type)
{
  if (r[type] > 0)
  {
    r[type]--;
    resselem[type].innerHTML = Thousand (r[type]);
  }
  else
    window.clearInterval (t[type]);
}

function dec2(type, inc)
{
  if (r[type]+inc > 0)
  {
    r[type]=Math.min (max, r[type]+inc);
    resselem[type].innerHTML = Thousand (Math.floor (r[type]));
  }
  else
  {
    window.clearInterval (t[type]);
    r[type]=0;
  }
}

function draw_clock ()
{
  secs++;
  var clockstr;
  if (secs > 59)
  {
    secs = 0;
    mins++;
    if (mins > 59)
    {
      mins = 0;
      hours++;
      if (hours > 23)
        hours = 0;
    }
  }
  if (hours < 10)
    clockstr = "0" + hours;
  else
    clockstr = hours;
  if (mins < 10)
    clockstr = clockstr + ":0" + mins;
  else
    clockstr = clockstr + ":" + mins;
  if (secs < 10)
    clockstr = clockstr + ":0" + secs;
  else
    clockstr = clockstr + ":" + secs;

  uhrelem = document.getElementById('uhr');

  uhrelem.innerHTML =  clockstr;

  for (var temp=1; temp < timers; temp++)
  {
    csecs[temp]--;
    if (csecs[temp] < 0)
    {
      csecs[temp] = 59;
      cmins[temp]--;
      if (cmins[temp] < 0)
      {
        cmins[temp] = 59;
        chours[temp]--;
        if (chours[temp] < 0)
        {
        	chours[temp] = cmins[temp] = csecs[temp] = 0;
        	counters[temp].innerHTML = "--:--:--";
        	if (!no_reload)
		{
        		document.location.reload();
			window.clearInterval (clock_c);
		}
        	else
        		continue;
        }
      }
    }
    if (chours[temp] < 10)
      clockstr = "0" + chours[temp];
    else
      clockstr = chours[temp];
    if (cmins[temp] < 10)
      clockstr = clockstr + ":0" + cmins[temp];
    else
      clockstr = clockstr + ":" + cmins[temp];
    if (csecs[temp] < 10)
      clockstr = clockstr + ":0" + csecs[temp];
    else
      clockstr = clockstr + ":" + csecs[temp];
    counters[temp].innerHTML = clockstr;
  }

  for (var temp=1; temp < itimers; temp++)
  {
    icsecs[temp]++;
    if (icsecs[temp] > 59)
    {
      icsecs[temp] = 0;
      icmins[temp]++;
      if (icmins[temp] > 59)
      {
        icmins[temp] = 0;
        ichours[temp]++;
        if (ichours[temp] > 23)
        {
        	ichours[temp] = icmins[temp] = icsecs[temp] = 0;
        }
      }
    }
    if (ichours[temp] < 10)
      clockstr = "0" + ichours[temp];
    else
      clockstr = ichours[temp];
    if (icmins[temp] < 10)
      clockstr = clockstr + ":0" + icmins[temp];
    else
      clockstr = clockstr + ":" + icmins[temp];
    if (icsecs[temp] < 10)
      clockstr = clockstr + ":0" + icsecs[temp];
    else
      clockstr = clockstr + ":" + icsecs[temp];
    icounters[temp].innerHTML = clockstr;
  }

}

function help(topic)
{
  helpelem = document.getElementById("help");
  if(helpelem != null)
  {
    var helpcont = "<div class=\"popup3\"><iframe frameborder=\"0\" id=\"msgframe\" src=\"hilfe.php?t="+ topic + "\" width=\"710\" height=\"460\" border=\"0\"></iframe></div><a href=\"#\" onClick=\"helpclose()\"><img src=\"gfx/close.gif\" border=\"1\" class=\"helpclose\" alt=\"Hilfe Schlie�enn\" title=\"Hilfe Schlie�en\"></a><div class=\"helptitle\"><b>Selador Onlinehilfe</b><div>";
    helpelem.innerHTML=helpcont;
  }
}

function helpclose()
{
  helpelement = document.getElementById("help");
  if(helpelement != null)
  {
    helpelement.innerHTML='';
  }
}

function special(x,y,name,player,score,villagescore, tag)
{
  document.getElementById('X').firstChild.nodeValue = x + " | ";
  document.getElementById('Y').firstChild.nodeValue = y;
  document.getElementById('SCORE').firstChild.nodeValue = " ("+ score+ " Punkte)";
  document.getElementById('VNAME').firstChild.nodeValue = name;
  document.getElementById('VSCORE').firstChild.nodeValue = " ("+villagescore+ " Einwohner) ";
  document.getElementById('PLAYER').firstChild.nodeValue = player;
  document.getElementById('TAG').firstChild.nodeValue = tag;
}

function special_clear()
{
  document.getElementById('X').firstChild.nodeValue = '';
  document.getElementById('Y').firstChild.nodeValue = '';
  document.getElementById('VNAME').firstChild.nodeValue = '';
  document.getElementById('VSCORE').firstChild.nodeValue = '';
  document.getElementById('SCORE').firstChild.nodeValue = '';
  document.getElementById('PLAYER').firstChild.nodeValue = '';
  document.getElementById('TAG').firstChild.nodeValue = '';

}