var relImgs=new Array();
var relCurrent=0;
var ThmbWidth=85;
var ThmbHeight=85;
var LrgWidth=388;
var LrgHeight=340;
var GalLstImg=-1;
function RelAddImage(Link,thmbLink,Title,Desc, Copy, ht, wdth, fullsize) {
  var img=new Object();
  var buf, pos;
  pos=thmbLink.indexOf('&');
  if (pos!=-1) 
    buf=thmbLink.substr(0,pos);
  else
    buf=thmbLink;
  if (buf.indexOf('?')!=-1)  
    img.Thumb=buf + GalResize(ht, wdth, ThmbHeight, ThmbWidth);
  else
    img.Thumb=buf;
  pos=Link.indexOf('&');
  if (pos!=-1) 
    buf=Link.substr(0,pos);
  else
    buf=Link;  
  if (buf.indexOf('?')!=-1)  
    img.Large=buf + GalResize(ht, wdth, LrgHeight, LrgWidth);
  else
    img.Large=buf;
  img.Title=Title;
  img.Desc=Desc;
  img.Copy=Copy;
  img.Fullsize=fullsize;
  var i=relImgs.length;
  relImgs[i]=img
}
function GalResize(ht, wdth, tHt, tWdth) {
  var nHt, nWdth;
  if (ht/wdth>tHt/tWdth) {
    nHt=Math.round(tHt * ht/wdth);
    nWdth=tWdth;
  } else {
    nHt=tHt;
    nWdth=Math.round(tWdth * wdth/ht);
  }    
  return '&Height=' + nHt + '&Width=' +nWdth;
}
var GalStart=0;

function GalleryInit() {
  GalleryShowImage(0);
  GalleryLoadThumbs();
  var pgCnt=Math.floor((relImgs.length-1)/12)+1;
  var id=document.getElementById('GalPrev');
  if (GalStart==0) 
    id.style.visibility='hidden';
  else
    id.style.visibility='visible';
  var id=document.getElementById('GalNext');
  if (Math.floor(GalStart/12)==pgCnt-1) 
    id.style.visibility='hidden';
  else
    id.style.visibility='visible';
  var buf="";
  for(i=1;i<=pgCnt;i++) {
    if (GalStart==(i-1)*12) 
      buf+="|<i>" + i + "</i>";
    else
      buf+="|<b style='cursor:pointer;' onclick='GalShowPage(0," + (i-1)*12 + ");'>" + i + "</b>";
  }
  buf +="|"
  id=document.getElementById('GalPage');
  id.innerHTML=buf;
}
function GalShowPage(dr,idx) {
  if (dr==0) 
    GalStart=idx;
  else
    GalStart+=dr*12;
  GalleryInit();
}
function GalleryLoadThumbs() {
  var i, id, idx;
  for(i=0;i<12;i++) {
    idx=GalStart+i;
    id=document.getElementById('GalThumb' +i);
    if (idx<relImgs.length) {
      id.innerHTML="<img src='" + relImgs[idx].Thumb + "' onLoad='GalleryImageLoaded(this,85,85," + (idx % 3) + ");'/>"
      id.style.cursor='pointer';
    } else {
      id.innerHTML="";
      id.style.cursor='default';
    }
  }
}
function GalleryShowImage(i) {
  var id, buf, styl;;
  if (i+GalStart>=relImgs.length) return;
  if (GalLstImg!=-1) {
    id=document.getElementById('GalThumb'+GalLstImg);
    id.style.border='3px solid #EBEBED';
  }
  id=document.getElementById('GalThumb'+i);
  GalLstImg=i;
  id.style.border='3px inset #A0A0A0';
  id=document.getElementById('GalImage');
  var idx=GalStart+i;
  id.innerHTML="<img src='" + relImgs[idx].Large + "' onLoad='GalleryImageLoaded(this,360,380,0);'/ >"
  //id=document.getElementById('GalTitle');
  //id.innerHTML=relImgs[idx].Title;
  id=document.getElementById('GalDesc');
  id.innerHTML=relImgs[idx].Desc;
  id=document.getElementById('GalCopy');
  id.innerHTML=relImgs[idx].Copy;
  id=document.getElementById('GalDownload');
  if (id!=null) {
   id.href=relImgs[idx].Fullsize;
  }
  
}

function GalleryImageLoaded(id, tHt, tWdth) {
  var ht,wdth, ratio;
  ht=id.height;
  wdth=id.width;
  if (ht>tHt && wdth>tWdth) {
    ratio=ht/wdth;
    if (id.height/id.width >tHt/tWdth) {
      ht=Math.round(tHt * ratio);
      wdth=tWdth;
      id.style.marginLeft=0;
    } else {
      ht=tHt;
      wdth=Math.round(tWdth / ratio);
      id.style.marginLeft=(tWdth-wdth)/2;
    }
    id.height=ht;
    id.width=wdth;    
  } else {
    if (id.height/id.width >tHt/tWdth) {
      id.style.marginLeft=0;
    } else {
      id.style.marginLeft=(tWdth-wdth)/2;  
    }
  }
}

function GalleryGetOffset(wdth, tWdth) {
  if (wdth<tWdth) return 0;
  return Math.floor((tWdth-wdth)/2);
}

function isIE(){  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);}

function PAL_MenuHover(id, on) {
  if (on) {
  id.style.backgroundColor='#F0F0F0';
 } else
  id.style.backgroundColor='#FFFFFF';
}