// --------
// Params
// --------
var _papi = 'http://c-www.net/precog/precog.php';
var _pwidth = 185;
var _pheight = 115;
var _first = new Array();

// --------
// HTML Tag
function divsingle(id, css)
{
  var div = document.createElement("div");
  if (id) {
    div.setAttribute("id", id);
  }
  if (navigator.userAgent.indexOf("MSIE") != -1) {
    div.style.cssText = css;
  } else {
    div.setAttribute("style", css);
  }
  return div;
}

function divclick(id, css, pid, proc)
{
  var div = divsingle('', css);
  var a = document.createElement("a");
  a.setAttribute("id", id);
  a.setAttribute("href", "#");
  a.style.color = "white";
  if (navigator.userAgent.indexOf("MSIE") != -1) {
    a.setAttribute('onclick', function() { selection(proc,pid); return false; });
  } else {
    a.setAttribute("onClick", "selection(" + proc + "," + pid + "); return false;");
  }
  div.appendChild(a);
  return div;
}

function divlink(id, css, url, text)
{
  var div = divsingle(id, css);
  var a = document.createElement("a");
  a.setAttribute("href", url);
  a.style.color = "white";
  a.appendChild(document.createTextNode(text));
  div.appendChild(a);
  return div;
}

// Gauge Write
function hundler_precog(data)
{
  var _base = 'zettai-precog-' + data.id;
  for (i=0; i<=2; ++i) {
    var item = document.getElementById(_base + '-gauge-item-'+i);
    item.style.width = data.gauge[i] + 'px';
  }
  for (i=0; i<=2; ++i) {
    var item = document.getElementById(_base + '-number-'+i);
    var text = data.percent[i] + "% (" + data.point[i] + ")";
    item.appendChild(document.createTextNode(text));
  }

  if (_first[data.id] == true) {
    document.getElementById(_base + '-title').appendChild(document.createTextNode(data.title));
    document.getElementById(_base + '-description').appendChild(document.createTextNode(data.description));
    for (i=0; i<=2; ++i) {
      document.getElementById(_base + '-select-'+i).appendChild(document.createTextNode(data.select[i]));
    }
    _first[data.id] = false;
  }
  oJsr.removeScriptTag();
}

// Gauge Select
function selection(s, pid)
{
  precog_reset(pid);
  oJsr = new JSONscriptRequest(_papi + '?s=' + s + '&id=' + pid);
  oJsr.buildScriptTag();
  oJsr.addScriptTag();
}

// Gauge Reset
function precog_reset(pid)
{
  var _base = 'zettai-precog-' + pid;
  var item;
  for (i=0; i<=2; ++i) {
    item = document.getElementById(_base + '-number-'+i).firstChild;
    document.getElementById(_base + '-number-'+i).removeChild(item);
  }
}

// HTML Write
function _pwrite(pid)
{
  var _base = 'zettai-precog-' + pid;
  var root = document.getElementById(_base);
  var rootcss = "width:" + _pwidth + "px; height:" + _pheight + "px; padding:2px; padding-left:4px; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:white; background-color:black; text-align:center;";
  if (navigator.userAgent.indexOf("MSIE") != -1) {
    root.style.cssText = rootcss;
  } else {
    root.setAttribute("style", rootcss);
  }
  root.appendChild(divsingle(_base + "-title", "width:100%; height:20px; font-size:12px;"));
  root.appendChild(divsingle(_base + "-description", "width:100%; height:20px; font-size:10px;"));
  var sel = divsingle(_base + "-select", "width:100%; height:16px; font-size:10px; float:left;");
  sel.appendChild(divclick(_base + "-select-0", "width:25%; text-align:left; margin-left:4px; float:left;", pid, 1));
  sel.appendChild(divclick(_base + "-select-1", "width:50%; text-align:center; float:left;", pid, 2));
  sel.appendChild(divclick(_base + "-select-2", "width:20%; text-align:right; float:left;", pid, 3));
  root.appendChild(sel);
  root.appendChild(divsingle('', "clear: both;"));

  var gauge = divsingle(_base + "-gauge", "width:100%; height:30px; float:left;");
  gauge.appendChild(divsingle(_base + "-gauge-item-0", "height:30px; border:1px solid yellow; border-right:0px; background-color:#FF33FF; float:left;"));
  gauge.appendChild(divsingle(_base + "-gauge-item-1", "height:30px; border:1px solid yellow; border-right:0px; background-color:#0066FF; float:left;"));
  gauge.appendChild(divsingle(_base + "-gauge-item-2", "height:30px; border:1px solid yellow; background-color:#FF3300; float:left;"));
  root.appendChild(gauge);
  root.appendChild(divsingle('', "clear: both;"));

  var num = divsingle(_base + "-number", "width:100%; height:12px; font-size:9px; font-family:monospace; padding-top:2px;");
  num.appendChild(divsingle(_base + "-number-0", "width:30%; text-align:left; float:left;"));
  num.appendChild(divsingle(_base + "-number-1", "width:40%; text-align:center; float:left;"));
  num.appendChild(divsingle(_base + "-number-2", "width:30%; text-align:right; float:left;"));
  root.appendChild(num);

  root.appendChild(divlink(_base + "-footer", "width:100%; height:15px; font-size:8px; text-align:right;", "http://c-www.net/diary/", "Powered by C-WWW"));
}

//---------
// MAIN
//---------
// Tag Search
var divs = document.getElementsByTagName("div");
var ids = new Array();
for (var i=0; i<divs.length; i++) {
  var s = divs[i];
  var id = s.getAttribute("id");
  if (id) {
    var reg = id.match(/zettai-precog-([0-9]*)?$/);
    if (reg) {
      var id = reg[1];
      ids.push(id);
      _first[id] = true;
    }
  }
}

for (var i=0; i<ids.length; ++i) {
  // Tag Write
  _pwrite(ids[i]);

  // JSONP
  var oJsr;
  oJsr = new JSONscriptRequest(_papi + '?s=0&id=' + ids[i]);
  oJsr.buildScriptTag();
  oJsr.addScriptTag();
}
