function checkStyle() {
  switch(style) {
  case 1:
    //column layout
    width= setWidth(pw+50);               //number of thumbnails wide
    height = setHeight();                 //number of thumbnails tall, SET AUTOMATICALLY
    var hth = th+30; var htw = tw+10; var phh = ph + 35;
    if (width == 0) {                    //not enough room for one thumbnail, so act like Row Style
        width=galleryarray.length; height=1;
        var hth = th+30; var htw = tw+10; var phh = ph + 35;
        document.write('<style type="text/css">#imageholder {margin: 0px auto;height:'+phh+'px;width:'+pw+'px;}.holder {width:'+htw+'px;height:'+hth+'px}.thumbName{width:'+htw+'px}</style>');
    } else {
        document.write('<style type="text/css">#imageholder {float:right;width:'+pw+'px;height:'+phh+'px;}.holder {width:'+htw+'px;height:'+hth+'px}.thumbName{width:'+htw+'px}</style>');
    }
    break
  case 2:
    //thumbs across the bottom
    width= galleryarray.length;           //number of thumbnails wide, SET AUTOMATICALLY
    height = 1;                           //number of thumbnails tall, DONT CHANGE THIS
    var phh = ph +35; var hth = th+30; var htw = tw+10;
    document.write('<style type="text/css">#imageholder {margin: 0px auto;height:'+phh+'px;width:'+pw+'px;}.holder {width:'+htw+'px;height:'+hth+'px}.thumbName{width:'+htw+'px}</style>');
    break
  case 3:
    //Full page of thumbs
    width= setWidth(10);                    //number of thumbnails wide, SET AUTOMATICALLY
    height = setHeight();                 //number of thumbnails tall, SET AUTOMATICALLY
    var hth = th+30; var htw = tw+10;
    document.write('<style type="text/css">.holder {width:'+htw+'px;height:'+hth+'px}.thumbName{width:'+htw+'px}</style>');
  }
  totalslots=width*height;
  href_target="_top"                    //Enter target attribute of links, _blank will load image in a new window
}



/***********************************************
* Photo Album script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for hundreds of DHTML scripts
* This notice must stay intact for legal use
***********************************************/
/***********
*Script heavily modified by Bibble Labs, Inc
***********/

function checkSize() {
if (style != "2") {
  if (style == "1" ) {
    newWidth = setWidth(pw+50);
  }
  if (style == "3" ) {
    newWidth = setWidth(10);
  }
  newHeight = setHeight();
  if ( (newHeight != height || newWidth != width)  ) {
    window.location.href=window.location.href
  }
}
}

function setHeight() {
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    myHeight = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) {
    myHeight = document.body.clientHeight;
  }
  var tempHeight = myHeight - 200;
  if (tempHeight < ph) {tempHeight = ph+35;}
  return Math.floor(tempHeight / (th+33));
}

function setWidth(gap) {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth ) {
    myWidth = document.body.clientHeight;
  }
  return Math.floor((myWidth - gap) / (tw+14));
}


function buildThumbs() {
document.getElementById('total').childNodes[0].nodeValue = galleryarray.length;
var curimage=0
if (height == 1)
  document.write('<div id="thumbnails"><div id="scroller">');
for (y=0; y<height; y++){
  for (x=0; x<width; x++){
    if (curimage<galleryarray.length)
      document.write('<div class="holder"><div id="slide'+curimage+'" class="slideshow">'+buildimage(curimage)+'</div></div>\n')
      curimage++
    }
    if (style < 3)
      document.write('<br style="clear: left" />\n')
  }
  if (height == 1) {
  document.write('</div></div>');
  document.getElementById('scroller').style.width = ((tw+14) * galleryarray.length ) +5 +'px';
  }
document.write('<br style="clear: both" />\n')
}

function buildImageHolder() {
if (style < 3) {
  document.write('<div id="imageholder"><a id="linkholder" href="#"><img id="placeholder" src="#" alt="" ></a><p id="desc">no image selected</p></div>\n\n');
  }
}

function buildimage(i){
  var tempcontainer=galleryarray[i][3]!=""? '<a onclick="return showPic('+i+')" href="'+galleryarray[i][4]+'" target="'+href_target+'">' : ""
  tempcontainer+='<img src="'+galleryarray[i][0]+'" title="'+galleryarray[i][1]+'">'
  tempcontainer=galleryarray[i][4]!=""? tempcontainer+'</a>' : tempcontainer
  tempcontainer=galleryarray[i][1]!=""? tempcontainer+'<div class=thumbName>'+galleryarray[i][1]+'</div>' : tempcontainer
  return tempcontainer
}

function buildNavLinks() {
if ( height != 1) {
  //build nav links
  document.write('<div id="navlinks">');
  for (i=1; i<Math.ceil(galleryarray.length/totalslots)+1; i++) {
    document.write('<a id="navlink'+i+'" href="javascript:jumptopage('+i+')\">Page '+i+'</a> ')
  }
    document.getElementById("navlink1").className="current"
  document.write('</div>');
}
}

function jumptopage(p){
  if (p==0) p=1;
  var startpoint=(p-1)*totalslots
  var y=1;
  for (i=0; i<totalslots; i++){
    document.getElementById("slide"+i).innerHTML=(typeof galleryarray[startpoint+i]!="undefined")? buildimage(startpoint+i) : ""
  }
  while(document.getElementById("navlink"+y)!=null){
    document.getElementById("navlink"+y).className=""
    y++
  }
  document.getElementById("navlink"+p).className="current"
}

function showPic (i) {
  cur = i + 1;
  y = 0;
  while(document.getElementById("slide"+y)!=null){
    document.getElementById("slide"+y).className="slideshow"
    y++
  }
  curpage = Math.ceil(cur/totalslots) -1;
  newI = i - curpage * totalslots;
  document.getElementById("slide"+newI).className="slideshowon"
  if (height == 1)
    document.getElementById("slide"+i).parentNode.scrollIntoView();

  if (document.getElementById && style < 3) {
    document.getElementById('placeholder').src = galleryarray[i][3];
    document.getElementById('desc').childNodes[0].nodeValue = galleryarray[i][2];
    document.getElementById('linkholder').href = galleryarray[i][4];
    document.getElementById('linkholder').target = href_target;
    document.getElementById('cur').childNodes[0].nodeValue = cur; //display the ordinal
    return false;
  } else {
    document.getElementById('cur').childNodes[0].nodeValue = cur; //display the ordinal
    return true;
  }
}

function listenKey () { document.onkeydown = getKey; }

function getKey(e){
  if(!e) {
    //ie
    e = window.event;
  }
  switch(e.keyCode) {
  case 33: //page up
    if (height == 1) {
      nextImg(-10);
    } else {
      nextImg(-totalslots);
    }
    return false;
    break;
  case 34: //page down
    if (height == 1) {
      nextImg(10);
    } else {
      nextImg(totalslots);
    }
    return false;
    break;
  case 35: //end
    nextImg('last');
    return false;
    break;
  case 36: //home
    nextImg('first');
    return false;
    break;
  case 37: //left
    nextImg(-1);
    return false;
    break;
  case 38: //up
    nextImg(-width);
    return false;
    break;
  case 39: //right
    nextImg(1);
    return false;
    break;
  case 40: //down
    nextImg(width);
    return false;
    break;
  case 13: //enter
    window.location.href = galleryarray[cur - 1][4];
    return false;
    break;
  }
}

function nextImg(next) {
  if (next == 'last') {
    next = galleryarray.length - 1;
  } else if (next == 'first') {
    next = 0;
  } else {
    next = cur + next - 1;
  }
  if (next < 0)
    next = galleryarray.length -1
  if (next >=  galleryarray.length)
    next = 0
  if (height > 1) {
    curpage = Math.ceil(cur/totalslots) ;
    var topage = Math.ceil((next+1)/totalslots);
    if (curpage != topage) jumptopage(topage);
  }
  showPic(next);
}

function getValue(varname)
{
  var url = window.location.href;
  var qparts = url.split("?");
  if (qparts.length < 2) {
    return ""; 
  } else {
  var query = qparts[1];
  var vars = query.split("&");
  var value = "";
  for (i=0;i<vars.length;i++)
  {
    var parts = vars[i].split("="); 
    if (parts[0] == varname)
      {
       value = parts[1];
       break;
      } else {
       value = "";
      }
   }
  value = unescape(value);
  value.replace(/\+/g," ");
  return value;
  }
}

function initiate() {
  buildImageHolder();
  checkStyle();
  buildThumbs();
  buildNavLinks();
  listenKey();
  
  cur = getValue("image");
  if (cur == "") {
    p = getValue("page");
    if (p == "" ) {
        cur = 1;
      } else {
	if (p > (galleryarray.length / totalslots ) ) {
	  p = 1;
	  cur = 1;
	} else {
        cur = 1 + ((p-1) * totalslots);
        jumptopage(p);
      }}
  } else {
    if (cur > galleryarray.length) cur = 1;
    p = Math.ceil(cur/totalslots) ;
  }
  if (p > 1 ) jumptopage(p);
  showPic(cur-1);
}
