var timeout;
var galwidth = 0;
var timesscrolled = 0;
var scrollacceleration = 3; //the higher this number, the slower it will accelerate
var maxscrollspeed = 30; //in pixels per frame
var minscrollspeed = 10; //in pixels per frame
var scrollframedelay = 40; //the delay in milliseconds between frames

var browserName = navigator.appName;
if (browserName == 'Microsoft Internet Explorer') {
  var imagegap = 15; //the gap in pixels between gallery thumbnails
} else {
  var imagegap = 35; //the gap in pixels between gallery thumbnails
}

function ScrollGalleryPrev() {
  gal = document.getElementById('gallery');
  left = parseInt(gal.style.marginLeft) + Math.max(Math.min(Math.round(timesscrolled/scrollacceleration), maxscrollspeed), minscrollspeed);
  if(isNaN(left) || left > 0) {
    gal.style.marginLeft = '0px';
  } else {
    gal.style.marginLeft = left + 'px';
  }
  timeout = window.setTimeout(ScrollGalleryPrev, scrollframedelay);
  timesscrolled++;
}

function ScrollGalleryNext() {
  gal = document.getElementById('gallery');
  if(galwidth == 0) {
    for(i in gal.childNodes) {
      try {
        if(!isNaN(parseInt(gal.childNodes[i].childNodes[0].width))) {
          galwidth += parseInt(gal.childNodes[i].childNodes[0].width) + imagegap;
        }
      } catch(e) {}
    }
    gal.style.width = (galwidth+50) + 'px';
    galwidth -= 736;
    galwidth *= -1;
  }
  left = parseInt(gal.style.marginLeft) - Math.max(Math.min(Math.round(timesscrolled/scrollacceleration), maxscrollspeed), minscrollspeed);
  if(isNaN(left)) {
    left = 0;
  }
  if(left < galwidth) {
    gal.style.marginLeft = galwidth + 'px';
  } else {
    gal.style.marginLeft = left + 'px';
  }
  timeout = window.setTimeout(ScrollGalleryNext, scrollframedelay);
  timesscrolled++;
}

function StopScroll() {
  window.clearTimeout(timeout);
  timesscrolled = 0;
}


var timeout2;
var focuswidth = 0;
var timesfocusscrolled = 0;

function ScrollFocusPrev() {
  gal = document.getElementById('focus');
  left = parseInt(gal.style.marginLeft) + Math.max(Math.min(Math.round(timesfocusscrolled/scrollacceleration), maxscrollspeed), minscrollspeed);
  if(isNaN(left) || left > 0) {
    gal.style.marginLeft = '0px';
  } else {
    gal.style.marginLeft = left + 'px';
  }
  timeout2 = window.setTimeout(ScrollFocusPrev, scrollframedelay);
  timesfocusscrolled++;
}

function ScrollFocusNext() {
  gal = document.getElementById('focus');
  if(focuswidth == 0) {
    for(i in gal.childNodes) {
      try {
        if(!isNaN(parseInt(gal.childNodes[i].childNodes[0].width))) {
          focuswidth += parseInt(gal.childNodes[i].childNodes[0].width) + imagegap;
        }
      } catch(e) {}
    }
    gal.style.width = (focuswidth+50) + 'px';
    focuswidth -= 700;
    focuswidth *= -1;
  }
  left = parseInt(gal.style.marginLeft) - Math.max(Math.min(Math.round(timesfocusscrolled/scrollacceleration), maxscrollspeed), minscrollspeed);
  if(isNaN(left)) {
    left = 0;
  }
  if(left < focuswidth) {
    gal.style.marginLeft = focuswidth + 'px';
  } else {
    gal.style.marginLeft = left + 'px';
  }
  timeout2 = window.setTimeout(ScrollFocusNext, scrollframedelay);
  timesfocusscrolled++;
}

function StopFocusScroll() {
  window.clearTimeout(timeout2);
  timesfocusscrolled = 0;
}

function AddPage(subpage) {
  pageName = prompt('Please enter the name of the new page');
  if(pageName != null && pageName != '') {
    if(subpage) {
      document.forms['addpage'].elements['action'].value = 'addsubpage';
    }
    document.forms['addpage'].elements['name'].value = pageName;
    document.forms['addpage'].submit();
  }
}
