//
// MoP - Mozilla de Presentation
// Main JavaScript Source
// (C)2005 by HIROSE, Yuuji [yuuji@yatex.org]
// $Id: mop.js,v 1.6 2007/06/28 02:15:51 yuuji Exp $
// Last modified Thu Jun 28 11:05:55 2007 on firestorm
// Update count: 206
//
MSIE = navigator.userAgent.indexOf("MSIE")!=-1;
document.onkeypress = keyExec;
self.focus();

function getMyNumber() {
  if (location.pathname.match(/(\d+)\.html?/i))
    return RegExp.$1-0;
  return 0;
}
function getMaxNumber() {
  var title = document.getElementsByTagName("title").item(0).innerHTML;
  if (title.match(/\/(\d+) /))
    return RegExp.$1-0;
  else
    return 2;
}

function gotoPage(n) {
  var cur = location.pathname;
  var file = cur.substring(cur.lastIndexOf("/")+1);
  var l, m, r, dest, dstr;
  l = "slide-", m="00", r=".html";
  if (n) numericarg = n;
  if (file.match(/(.*-)(\d+)(\.html?)/i)) {
    l = RegExp.$1;
    m = RegExp.$2;
    r = RegExp.$3;
  }
  dest = (numericarg==0 ? getMaxNumber() : numericarg);
  dest = "0"+dest;
  dest = dest.substring(dest.length-2);// substr(-2) doesn't work on IE :(
  dstr = l+dest+r;
  location.href = dstr;
  numericarg = 0;
}

function pageNext() {
  var n = (numericarg==0 ? 1 : numericarg);
  var goal = getMyNumber()+n;
  if (goal > 1 && goal <= totalpages)
    gotoPage(goal);
  numericarg = 0;
}

function getKEYCODE(e){  

  if (document.getElementById && !MSIE )  
    return  e.charCode||e.keyCode;
  else if(document.all)       return  event.keyCode;
  else if(document.layers)    return  e.which;
}
function keyExec(e) {
  key = getKEYCODE(e);
  mods = null;
  if (!MSIE) mods = e.shiftKey|e.ctrlKey|e.altKey;

  switch (key) {
  case 107:
    if (mods) break;
  case 38:
    dialogue(-1); break;	// up
  case 106:
    if (mods) break;
  case 40:
    stepOne(); break;		// down
  case 104:
    if (mods) break;
  case 37:
    if (document.getElementById("_prev"))
      history.back();
    break;	// left
  case 103:
    gotoPage();
    break;
  case 108:
    if (mods) break;
  case 39:
    pageNext(); break;		// right
  case 48: case 49: case 50: case 51: case 52:
  case 53: case 54: case 55: case 56: case 57:
    numericarg = 10*numericarg+key-48;
    break;
  case 45:  			// '-'
    decFontSize();
    break;
  case 43:  			// '+'
    incFontSize();
    break;
  default:
    numericarg = 0;
  }
}

function resetHandler(id) {
  target = document.getElementById(id);
  target.onmouseover = null;			// ハンドラ解除
  target.onclick = null;
  target.setAttribute("onMouseOver", null);
  target.setAttribute("onclick", null);
}

rcnt=new Array();
function rcutin(id) {
  w = 600;
  if (document.getElementById) {        //Moz,NN6,IE5用
    if (!rcnt[id]) {
      rcnt[id] = 0;
    }
    target = document.getElementById(id);
    target.style.borderLeft = "2px none";
    document.getElementById(id+"p").style.borderLeft = "2px none";
    target.style.visibility = "visible";
    target.style.paddingLeft=w*(1.0-rcnt[id]/10.0);
    target.style.visibility = 'visible';
    target.style.borderTop = 'none';
    resetHandler(id+"p");
    if (rcnt[id]<10) {
      rcnt[id]++;
      myname="rcutin('"+id+"')";
      setTimeout(myname, 50);
    }
  }
}
function emerge(id) {
  if (document.getElementById) {        //Moz,NN6,IE5用
    target = document.getElementById(id);
    target.style.visibility = 'visible';
    target.style.borderTop = 'none';
    resetHandler(id+"p");
    document.getElementById(id+"p").style.border = '1px none';
  }  
}

function setOpacity(obj, op) {
  ua = navigator.userAgent;
  if (ua && ua.indexOf("Gecko") != -1) {
    obj.style.MozOpacity = op;
  } else if (document.all) {
    /* うまくいかん IE6 */
    obj.style.filter="alpha(opacity=0)";
    obj.filters.alpha.opacity  = (op * 100);
  } 
}
opaquecount=new Array();
function opaque(id) {
  obj = document.getElementById(id);
  pnt = document.getElementById(id+"p");
  if (!opaquecount[obj]) {
    resetHandler(id+"p");
    obj.style.border = "1px none";
    obj.style.visibility = 'visible';
    opaquecount[obj]=0;
    document.getElementById(id+"p")
    pnt.style.border = "1px none";
    if (true || document.all) {
      obj.style.position = "relative";
    }
  }
  if (opaquecount[obj]>10) return;	// for too many calls
  obj.style.top = 100*(1-opaquecount[obj]/10.0);
  //obj.style.posTop = 100*(1-opaquecount[obj]/10.0);
  setOpacity(obj, (opaquecount[obj]++)/10.0);
  if (opaquecount[obj] <= 10) {
    setTimeout("opaque('"+id+"')", 200);
  } else {
    if (document.all) {
      obj.style.position = "static";
      pnt.style.position = "static";
    }
  }
}

function revealOne() {
  top:
  for (var i=1; obj=document.getElementById("id"+i); i++) {
    targets = new Array("id"+i, "id"+i+"p");
    for (ts in targets) {
      obj=document.getElementById(targets[ts]);
      if (!obj) continue;
      for (j=0; j<obj.attributes.length; j++) {
	name = obj.attributes.item(j).name;
	value = obj.getAttribute(name);
	if (value && name.match(/^on(click|mouseover)/i)) {
	  //alert("i="+i+" obj="+obj.id+" n="+name+" v="+value);
	  obj.setAttribute(name, null);
	  if (document.all) {
	    if ((""+value).match(/{(.*)}/m)) {
	  alert("value===="+value);
	      value = RegExp.$1;
	    }
	  }
	  if (document.all)
	    value.call();
	  else
	    eval(value);
	  break top;
	}
      }
    }
  }
}

function stepOne() {
  if (!dialogue()) {
    revealOne();
  }
}

function searchTagByClass(tag, classval) {
  olist = document.getElementsByTagName(tag);
  //alert("class="+document.getElementById("foofoo").getAttribute("align"));
  for (var i =0; i<olist.length; i++) {
    obj = olist[i];
    vlist = obj.attributes;
    if (!vlist) continue;
    if (MSIE) {
      cl = vlist.getNamedItem("class").value;
    } else
      cl = obj.getAttribute("class");

    if (cl && cl == classval) {
      return obj;
    }
  }
  return null;
}
function removeAllChild(obj) {
  cs = obj.childNodes;
  var element_node = obj.nodeType;
  var i=0;
  while (i<cs.length) {
    if (cs[i].nodeType == element_node) {
      obj.removeChild(cs[i]);		// remove したらiはそのままでよい
    } else {
      i++;
    }
  }
}

dX = dY = 0;
function d_down(e) {
  var me = document.getElementById("id1"); // searchTagByClass("div", "dialog");
  if (!dragcheck) {
    dragcheck = true;
    // me.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP|Event.MOUSEMOVE);
    //me.setAttribute("onMouseUp", "d_up()");
    me.onmouseup = d_up;
    if (e.offsetX) {
      dX = e.offsetX; dY = e.offsetY;
    } else {
      dX = e.layerX; dY = e.layerY;
    }
  }
}

function d_up(e) {
  if (!dragcheck) return;
  var x, y;
  var me = document.getElementById("id1"); searchTagByClass("div", "dialog");

  dragcheck = false;
  if (document.all) {			// for IE?
    x=event.offsetX; y=event.offsetY;
  } else {
    x=e.layerX; y=e.layerY;
  }
  if (x == dX && y == dY) {
    dialogue();
  }
}

dlog = 0;
dragcheck = false;
function dialogue() {
  qanda = false;
  dialog = searchTagByClass("div", "dialog");
  if (!dialog) {
    if (!(dialog=searchTagByClass("div", "qanda")))
      return null;
    qanda = true;
  }
  children = dialog.childNodes;
  element_node = dialog.nodeType;
  bq = dialog.getElementsByTagName("blockquote").item(0);
  bqs = bq.childNodes;
  if (arguments.length>0)
    dir = arguments[0];
  else
    dir = 1;

  /* if (dlog>=bqs.length)
     return null;
     // dialogのあとにアニメーションを入れたいならこれが必要
  */

  // a, bの位置
  area_a = searchTagByClass("div", "a");

  dlog=(dlog>=0? (dlog>=bqs.length?bqs.length-1:dlog) :0);
  while (dlog<bqs.length && dlog>=0 && bqs[dlog].nodeType != element_node)
    dlog+=dir;
  // numericarg 分だけスキップ。上のループといっしょにできん
  while (--numericarg > 0) {
    dlog+=dir;
    while (dlog<bqs.length && dlog>=0 && bqs[dlog].nodeType != element_node)
      dlog+=dir;
  }
  //alert("o="+odlog+", na="+na+", dlog="+dlog);
  numericarg=0;
  if (dlog>=0 && dlog<bqs.length) {
    area = bqs[dlog].getAttribute("area");
    if (area.match(/^a$/i)) {
      //area_a.innerHTML = bqs[dlog].innerText;
      removeAllChild(area_a);
      area_a.appendChild(bqs[dlog].cloneNode(true));
      if (qanda) {// quiz format
	var panes="bcdefg", pn;
	for (var p=0;
	     p<panes.length && (pn=searchTagByClass("div", panes.substr(p,1)));
	     p++) {
	  pn.innerHTML = "";
	}
      }
    } else {
      /* removeAllChild(area_b); */
      pane = searchTagByClass("div", area);
      if (pane) {
	removeAllChild(pane);
	pane.appendChild(bqs[dlog].cloneNode(true));
      }
      //area_b.innerHTML = bqs[dlog].innerHTML;
    }
    //alert("dlog="+dlog+", "+bqs[dlog].nodeName+"="+bqs[dlog].nodeType);
    dlog+=dir;
  }
  return dlog;
}

function getCSSRule(element) {
  // http://www.quirksmode.org/dom/changess.html
  var theRules = new Array();
  if (document.styleSheets[0].cssRules)
    theRules = document.styleSheets[0].cssRules;
  else if (document.styleSheets[0].rules)
    theRules = document.styleSheets[0].rules;
  rx = new RegExp("\\b"+element+"\\b", "i");
  for (s in theRules) {
    if (s && theRules[s].selectorText && theRules[s].selectorText.match(rx)) {
      return theRules[s];
    }
  }
  return null;
}

function incFontSize() {
  var n = (numericarg==0 ? 1 : numericarg);
  fs = parseInt(body.style.fontSize);
  for (i=0; i<Math.abs(n); i++) {
    fs += n*2;
    if (fs < 10) {
      fs = 10; break;
    } else if (fs > 500) {
      fs = 500; break;
    }
  }
  body.style.fontSize = fs+"%";
  numericarg = 0;
}

function decFontSize() {
  var n = (numericarg==0 ? 1 : numericarg);
  numericarg = -n;
  incFontSize();
}


function autoAdjust() {
  if (!document.getElementsByTagName("h1")) return;
  if (document.getElementById("noresize")) return; // id="noresize" ならしない
  h1 = document.getElementsByTagName("h1")[0];
  h1height = h1.offsetHeight;
  bottom = document.getElementById("_bottom");
  y = bottom.offsetTop;
  body = document.getElementsByTagName("body").item(0);
  height = (MSIE ? document.body.clientHeight : innerHeight);
  bodyrule=getCSSRule("body");
  fs = (bodyrule&&bodyrule.style.fontSize
	  ? parseInt(bodyrule.style.fontSize)
	: 200);
//alert("hh="+h1height+"y="+y+"/"+height+", fs="+fs);
  body.style.fontSize = fs+"%";
  for ( ; y>height && fs > 80; fs-=10) {
//alert("hh="+h1height+"y="+y+"/"+height+", fs="+fs);
    body.style.fontSize = fs+"%";
    y = bottom.offsetTop;
    h1height = h1.offsetHeight;
  }
}

var numericarg = 0;
var totalpages = 99;
function init(total) {
  /* はみ出たら字を小さく。余計なお世話?? */
  if (total) totalpages = total;
  autoAdjust();

  var pnt;
  numericarg = 0;
  for (i=1; child=document.getElementById("id"+i); i++) {
    pnt=document.getElementById("id"+i+"p");
    if (!pnt) continue;
    width = child.offsetWidth;
    height = child.offsetHeight;
    if (width==undefined || height==undefined)
      continue;
    pnt.style.width = width;
    if (!MSIE) pnt.style.height = height;
    child.style.position = "relative";

     //alert("i="+i+", "+width+"x"+height);
  }
}
