// NaniMoose's Gallery Code

var ArtDB = new Array();
var totalwidth = 0;
var ArtDirectory = "";
var ZoomedIn = 1;
var ImgShown = -1;

var PrevOn   = new Image(); PrevOn.src   = "img/prev.png";
var PrevOff  = new Image(); PrevOff.src  = "img/prevoff.png";
var NextOn   = new Image(); NextOn.src   = "img/next.png";
var NextOff  = new Image(); NextOff.src  = "img/nextoff.png";
var ZoomFull = new Image(); ZoomFull.src = "img/full.png";
var ZoomFit  = new Image(); ZoomFit.src  = "img/fit.png";

function SetArtDirectory(n) {
  ArtDirectory = n;
}

function AddArt(t,d,f,e) {
  var a = ArtDB[ArtDB.length] = new Object();
  a.Title = t;
  a.Desc = d;
  a.File = f;
  a.Ext = e;
  a.Thumb = a.File.replace(/ /g, "-");
  if (a.Thumb.length > 27) {a.Thumb = a.Thumb.substring(0, 27);}
  a.num = ArtDB.length-1;
  a.img = 0;
  a.imgw = 0;
  a.imgh = 0;
}

function Init() {
  if ((!(document.getElementById))||(!(thumbs.document))) {
    alert("This gallery may not be compatible with your web browser.\nWe recommend IE 5.0+, NetScape 6.0+, and Mozilla 1.0+.");
    return;
  }
  var c = '<table border="0" cellspacing="2" cellpadding="0"><tr>';
  for (var n=0; n<ArtDB.length; n++) {
    var a = ArtDB[n];
    c += '<td><a href="javascript:parent.ShowImg('+n+')" title="'+a.Title+'"><img src="thumb/'+a.Thumb+'.jpg" class="Thumb" width="64" height="96"></a></td>';
    totalwidth += 70;
  }
  c += '</tr></table>';

  e = thumbs.document.getElementById("iconrow");
  e.innerHTML = c;

  window.setTimeout("ShowImg(0)", 100);
}

function ShowImg(n) {
  if (ImgShown > -1) {
    thumbs.document.images[ImgShown].className = "Thumb";
  }
  thumbs.document.images[n].className = "ThumbSel";

  var e = bl.document.images[0];
  if (e) {e.src = (n>0) ? PrevOn.src : PrevOff.src;}
  e = bl.document.images[1];
  if (e) {e.src = (top.ZoomedIn == 1) ? ZoomFull.src : ZoomFit.src;}
  e = br.document.images[0];
  if (e) {e.src = (n<ArtDB.length-1) ? NextOn.src : NextOff.src;}

  var a = ArtDB[n];
  var c = '<big>'+a.Title+'</big><br>';
  if (a.Ext == "swf") {
    // For shockwave flash animations
    sc = ScaleImage(4000, 3000);

    c += '<center><table border="0"><tr><td class="ThumbSel">';
    c += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+sc.w+'" height="'+sc.h+'" id="movie" align=""><param name="movie" value="'+ArtDirectory+a.File+'.swf"><embed src="'+ArtDirectory+a.File+'.swf" quality="high" width="'+sc.w+'" height="'+sc.h+'" name="movie" align="" type="application/x-shockwave-flash" plug inspage="http://www.macromedia.com/go/getflashplayer"></object>';
    c += '</td></tr></table></center>';
  } else if ((a.imgw > 0) && (top.ZoomedIn != 1)) {
    // for cached images
    sc = ScaleImage(a.imgw, a.imgh);
    c += '<img src="'+ArtDirectory+a.File+'.'+a.Ext+'" width="'+sc.w+'" height="'+sc.h+'" border="2">';
  } else {
    // For uncached images
    c += '<img src="'+ArtDirectory+a.File+'.'+a.Ext+'" onLoad="parent.ResizeImg('+n+')" border="2">';
  }
  c += '<br><br>'+a.Desc;
  var e = display.document.getElementById("disp");
  e.innerHTML = c;
  ImgShown = n;

  PrebufferImg(n+1);
  PrebufferImg(n-1);
}

function DoNext() {
  if (ImgShown+1 < ArtDB.length) {
    ShowImg(ImgShown+1);
    CenterThumbs();
  }
}

function DoPrev() {
  if (ImgShown-1 > -1) {
    ShowImg(ImgShown-1);
    CenterThumbs();
  }
}

function ChangeZoom() {
  top.ZoomedIn = (top.ZoomedIn == 1) ? 0 : 1;
  ShowImg(ImgShown);
}

function DoExit() {
  document.location = top.ThisArtist;
}

function ResizeImg(n) {
  i = display.document.images[0];
  if (!(i)) {return;}

  a = ArtDB[n];
  if ((n == ImgShown) && (a.imgw == 0)) {
    a.img = new Image();
    a.img.src = i.src;
    a.imgw = i.width;
    a.imgh = i.height;
  }

  if (top.ZoomedIn == 1) {return;}
  sc = ScaleImage(i.width, i.height);
  ShowImg(ImgShown);
}

function ScaleImage(iw,ih) {
  var w = GetW(display) - 8;
  var h = GetH(display) - 40;
  var r = w/h;
  var ir = iw/ih;
  if (r < ir) {
    nw = (w>iw)?iw:w;
    nh = Math.floor(nw/ir);
  } else {
    nh = (h>ih)?ih:h;
    nw = Math.floor(nh*ir);
  }
  r = new Object();
  r.w = nw;
  r.h = nh;
  return r;
}

function PrebufferImg(n) {
  if ((n<0) || (n>=ArtDB.length)) {return;}
  a = ArtDB[n];
  if (a.imgw > 0) {return;}
  a.img = new Image();
  a.img.src = ArtDirectory+a.File+'.'+a.Ext;
  a.img.onLoad = RescanImgs
}

function RescanImgs() {
  for (n=0; n<ArtDB.length; n++) {
    a = ArtDB[n];
    if (a.img) {
      if (a.imgw == 0) {
	if (a.img.width > 0) {
	  a.imgw = a.img.width;
	  a.imgh = a.img.height;
	}
      }
    }
  }
}


function CenterThumbs() {
  var w = GetW(thumbs);
  var x = Math.floor((totalwidth*ImgShown/ArtDB.length)-(w/2));
  if (x<0) {x=0;}
  thumbs.window.scrollTo(x,0);
}

function GetW(f) {
  var w = 700;
  if (f.window.innerWidth) {w = f.window.innerWidth;}
  else if (f.document.body) {
    if (f.document.body.offsetWidth) {
      w = f.document.body.offsetWidth;
    }
  }
  return w
}

function GetH(f) {
  var h = 400;
  if (f.window.innerHeight) {h = f.window.innerHeight;}
  else if (f.document.body) {
    if (f.document.body.offsetHeight) {
      h = f.document.body.offsetHeight;
    }
  }
  return h
}
