var pix = new Array();

pix[pix.length] = (new Array("20050226-115619--IMG_9433.jpg", "20050226-115619--IMG_9433.jpg", "600", "800"));
pix[pix.length] = (new Array("20050226-115647--IMG_9434.jpg", "20050226-115647--IMG_9434.jpg", "800", "600"));
pix[pix.length] = (new Array("20050226-123435--IMG_9438.jpg", "20050226-123435--IMG_9438.jpg", "800", "600"));
pix[pix.length] = (new Array("20050226-131002--IMG_9440.jpg", "20050226-131002--IMG_9440.jpg", "800", "600"));
pix[pix.length] = (new Array("20050226-132243--IMG_9442.jpg", "20050226-132243--IMG_9442.jpg", "800", "600"));
pix[pix.length] = (new Array("20050226-151349--IMG_9446.jpg", "20050226-151349--IMG_9446.jpg", "800", "600"));
pix[pix.length] = (new Array("20050226-165017--STA_9471.jpg", "20050226-165017--STA_9471.jpg", "800", "600"));
pix[pix.length] = (new Array("20050227-122953--IMG_9491.jpg", "20050227-122953--IMG_9491.jpg", "600", "800"));
pix[pix.length] = (new Array("20050227-150014--IMG_9517.jpg", "20050227-150014--IMG_9517.jpg", "800", "600"));


  var template = new String("<"+"html> <"+"head> <"+"title>__PIXTITLE__<"+"/title> <"+"script type=\"text/javascript\" src=\"pictureviewer.js\"><"+"/script> <"+"style type=\"text/css\"> body {margin: 0; padding 0;} form {margin: 0; padding 0;} <"+"/style> <"+"/head> <"+"body> <"+"table align=\"center\" width=\"100%\"> <"+"tr> <"+"td align=\"left\" width=\"33%\"> <"+"a href=\"javascript:stepBackward(__NUMBER__)\" ><"+"img src=\"thumbs/indexerfiles/previous.png\" border=0 ><"+"/a><"+"a href=\"javascript:displayallpix()\" ><"+"img src=\"thumbs/indexerfiles/index.png\" border=0 ><"+"/a><"+"a href=\"javascript:stepForward(__NUMBER__)\" ><"+"img src=\"thumbs/indexerfiles/next.png\" border=0><"+"/a> <"+"/td><"+"td align=\"center\" width=\"33%\"> <"+"form> <"+"select onchange=\"displaypix(this.options[this.selectedIndex].value)\"> <"+"options value=\"-1\"> <"+"/option> <"+"/select> <"+"select onchange=\"displaypix(this.options[this.selectedIndex].value)\"> <"+"options value=\"-1\"> <"+"/option> <"+"/select> <"+"/form> <"+"/td> <"+"td align=\"right\" width=\"33%\"> <"+"form name=widthform> <"+"a href=\"javascript:zoom(-1)\" ><"+"img src=\"thumbs/indexerfiles/zoomminus.png\" border=0 align=\"middle\" ><"+"/a><"+"a href=\"javascript:zoom(+1)\" ><"+"IMG SRC=\"thumbs/indexerfiles/zoomplus.png\" border=0 align=\"middle\" ><"+"/a><"+"select name=width onChange=\"setCurrentWidth(document.widthform.width.selectedIndex); redisplay()\"> __WIDTHSELECT__ <"+"/select> <"+"/form> <"+"/tr> <"+"tr><"+"td align=\"center\" colspan=3> <"+"img src=\"__PIXFILE__\" __PIXSIZE__> <"+"/td><"+"/tr> <"+"tr><"+"td align=\"center\" colspan=3> __PIXTITLE__ <"+"/td><"+"/tr> <"+"/table> <"+"/body> <"+"/html>");

  var current      = 0;

  var currentWidth;
  var widthChoice = new Array;
  widthChoice[0] = new Array( 600,  400,   '640x480');
  widthChoice[1] = new Array( 760,  500,   '800x600');
  widthChoice[2] = new Array( 980,  650,  '1024x768');
  widthChoice[3] = new Array(1050,  750,  '1152x864');
  widthChoice[4] = new Array(1200,  650,  '1280x768');
  widthChoice[5] = new Array(1200,  800,  '1280x960');
  widthChoice[6] = new Array(1200,  850, '1280x1024');
  widthChoice[7] = new Array(1550,  900, '1600x1024');
  widthChoice[8] = new Array(1550, 1000, '1600x1200');
  widthChoice[9] = new Array(1800, 1000, '1920x1200');

  var expdate = new Date();
  expdate.setTime(expdate.getTime()+(365*24*60*60*1000));

  function InitCookie() {
    var widthset;
    widthset = EatCookie("displaywidth");
    //alert("widthset="+widthset);
    if (widthset == null || widthset == "null") {
      // Netscape 4 seems to return widthset as a string sometimes
      //alert("Doing autodetect");
      widthset = doAutoDetect();
    }
    //alert("widthset="+widthset);
    setCurrentWidth(parseInt(widthset));

    var fullScreenPref
    if (!(fullScreenPref = EatCookie("fullscreenpref", expdate))) {
    }
    if (fullScreenPref != null) {
      setFullScreenPref(fullScreenPref)
    }
  }

  function resetViewer() {
    TossCookie("displaywidth");
    //document.location.reload();
    setCurrentWidth(doAutoDetect());
  }
 
  function doAutoDetect() {
    for (i=0; i<widthChoice.length-1 
              && widthChoice[i+1][0]<screen.availWidth
              && widthChoice[i+1][1]<screen.availHeight; i++);
    //alert(i);
    return i;
  }

  var fullScreenPref = 0;
  function checkFullScreen() {
    alert("attempting to look at w.fS");
    foo = window.fullScreen;
    alert(foo);
    return;
    if (!window.fullScreen && fullScreenPref == 0) {
      change = confirm("You are not currently running in Full Screen mode.\n"
      +"Shall I try to change to Full Screen mode?");
      setFullScreenPref(change?1:-1);
    }
    if (change || fullScreenPref==1) {
      alert("trying to change");
      window.fullScreen=true;
    }
  }
  
  function setFullScreenPref(pref) {
    fullScreenPref = pref;
    BakeCookie("fullscreenpref", pref, expdate);
  }
            
  function stepForward(current) {
    if (current == pix.length - 1) 
    {
      displayallpix();
      return;
    }
    displaypix(parseInt(current)+1);
  }

  function stepBackward(current) {
    if (current == 0) 
    {
      displayallpix();
      return;
    }
    displaypix(parseInt(current)-1);
  }

  function displayallpix() {
    window.location = "imgindex";
  }

  function displaypix(number) {
    //alert(number);
    if (number >= 0 && number < pix.length)
    {
      current = parseInt(number);
      //checkFullScreen();
      with (document) {
        //need to generate the text first, otherwise Mozilla doesn't
        //know any other functions once open() is called
        var newtext=generateDisplayHTML(number);
        open();
        write(newtext);
        //write(generateDisplayHTML(number));
        close();
      }
    }
  }

  function displaypixbyname(filename) {
    for (i=0; i<pix.length && pix[i][0] != filename; i++);
    if (i != pix.length) {
      displaypix(i);
      return false;
    } else {
      //error condition
      //alert("how did that happen?\ni = " + i + "\npix.length = " + pix.length);
      return true;
    }
  }

  function generateDisplayHTML(num) {
    var WorH = getCurrentWidth()/getCurrentHeight() * pix[num][3] / pix[num][2];
    var output = new String(template);
    output = output.replace(/__NUMBER__/g,      num);
    output = output.replace(/__PIXFILE__/g,     pix[num][0]);
    output = output.replace(/__PIXTITLE__/g,    pix[num][1]);
    output = output.replace(/__PIXSIZE__/g,     
            (WorH < 1) ? "WIDTH=\"__WIDTH__\"" : "HEIGHT=\"__HEIGHT__\"");
    output = output.replace(/__WIDTH__/g,       getCurrentWidth());
    output = output.replace(/__HEIGHT__/g,      getCurrentHeight());
    output = output.replace(/__WIDTHSELECT__/g, getWidthSelect());
    return output;
  }

  function getCurrentHeight() {
    return widthChoice[currentWidth][1]
  }

  function getCurrentWidth() {
    return widthChoice[currentWidth][0]
  }

  function zoom(rel) {
    //alert(rel);
    var newWidth= currentWidth + rel;
    if((newWidth < widthChoice.length) && (newWidth >= 0)) {
      setCurrentWidth(currentWidth + rel);
      redisplay();
    }
  }

  function setCurrentWidth(width) {
    currentWidth = width;
    //alert("Baking cookie, dispwidth="+width);
    BakeCookie("displaywidth", width, expdate);
  }

  function getWidthSelect() {
    var output = "";
    for (i=0; i<widthChoice.length; i++)
    {
      output += "<"+"OPTION value='" + i + "' ";
      if (i == currentWidth) output += "selected";
      output += ">" + widthChoice[i][2];
    }
    return output;
  }

  function redisplay() {
    displaypix(current);
  }

  function BakeCookie(name,value) {
    var argv = arguments;
    var argc = arguments.length;
    var expires = (argc>2) ? argv[2] : null;
    var path    = (argc>3) ? argv[3] : null;
    var domain  = (argc>4) ? argv[4] : null;
    var secure  = (argc>5) ? argv[5] : false;
    document.cookie=name+"="+escape(value) +
      ((expires == null) ? "" : ("; expires="+expires.toGMTString())) +
      ((path == null) ? "" : ("; path="+path)) +
      ((domain == null) ? "" : ("; domain="+domain)) +
      ((secure == true) ? "; secure" : "");
  }

  function EatCookie(name) {
    var arg=name+"=";
    var alen=arg.length;
    var clen=document.cookie.length;
    var i=0;
    while (clen>i) {
      var j=i+alen;
      if (document.cookie.substring(i,j) == arg) {
        return EatCookieVal(j);
      }
      i=document.cookie.indexOf(" ",i) + 1;
      if (i == 0) {break};
    }
  }
  
  function EatCookieVal(offset) {
    var endstr=document.cookie.indexOf(";",offset);
    if (endstr == -1) {endstr=document.cookie.length};
    return unescape(document.cookie.substring(offset,endstr));
  }

  function TossCookie(name) {
    var exp=new Date();
    exp.setTime(exp.getTime()-1);
    //var cval=GetCookie(name);
    var cval = "";
    document.cookie=name+"="+cval+"; expires="+exp.toGMTString();
  }

  InitCookie();
