var curIdx = 0;
var curMode = 0;
var curInfo = 0;
var oldMode = 0;
var oldThumb = -1;
var oldIM = "";

function changeInfo() {
  var ltext;
  if (curInfo) {
    curInfo = 0;
    jQuery("#pinfo").slideUp(600);
  } else {
    curInfo = 1;
    jQuery("#pinfo").slideDown(600);
  }
  document.getElementById("plink1").firstChild.data = infotext[curInfo];
}

function changeMode() {
  if (extended) {
    if (curMode)
      phInit(0);
    else
      phInit(1);
  } else {
    curMode = curMode ? 0 : 1;
    changePic();
  }
}

function changePic() {
  if (phnav_on && (oldMode!=curMode)) {
    document.getElementById("plink2").firstChild.data = modetext[curMode];
  }
  oldMode = curMode;
  var newIM = String(curIdx) + String(curMode);
  if (oldIM && (oldIM!=newIM))
    jQuery("#phimg"+oldIM).fadeOut("slow");
  jQuery("#phimg"+newIM).fadeIn("slow");
  oldIM = newIM;
}

function phPrev() {
  if (curIdx>0)
    phInit(curIdx-1);
  else
    phInit(maxpic-1);
}

function phNext() {
  if (curIdx<(maxpic-1))
    phInit(curIdx+1);
  else
    phInit(0);
}

function phInit(idx) {
  var defmode = 0;
  if (idx>=maxpic) {
    idx -= maxpic;
    defmode = 1;
  }
  curIdx = idx;
  curMode = extended ? ((curIdx>0) ? 1 : 0) : defmode;
  changePic();
  if (phnav_on && (oldThumb!=idx)) {
    var thumb0 = "phthumb" + String(oldThumb);
    var thumb1 = "phthumb" + String(idx);
    oldThumb = idx;
    if (document.getElementById(thumb0))
      document.getElementById(thumb0).className = "phinactive";
    if (document.getElementById(thumb1))
      document.getElementById(thumb1).className = "phactive";
  }
  if (phnav_on && (maxpic>1))
    document.getElementById("plink3").firstChild.data = String(curIdx+1) + "/" + String(maxpic);
  if (document.getElementById("pinfo")) {
    var p = document.getElementById("pinfo");
    while (p.childNodes.length)
      p.removeChild(p.lastChild);
    var t = infos[curIdx];
    var l = t[0].length;
    p.appendChild(document.createTextNode(t[0]));
    for (var i=1;i<t.length;i++) {
      l += t[i].length;
      p.appendChild(document.createElement("br"));
      p.appendChild(document.createTextNode(t[i]));
    }
  }
  if (!extended || (curIdx==0)) {
    if (document.getElementById("pname"))
      document.getElementById("pname").firstChild.data = titles[curIdx];
    curInfo = 0;
    if (document.getElementById("plink1"))
      document.getElementById("plink1").firstChild.data = infotext[curInfo];
    if (document.getElementById("pinfo"))
      jQuery("#pinfo").hide();
  }
  if (document.getElementById("plink1")) {
    if (l==0)
      document.getElementById("plink1").firstChild.data = "";
    else
      document.getElementById("plink1").firstChild.data = infotext[curInfo];
  }
}

jQuery(document).ready(function(){
  phInit(0);
});
