function rthumbMouseOver(id, flag) {
  if(flag)
    jQuery('#roomthumbs div.active #thumboff' + id).show()
  else
    jQuery('#roomthumbs div.active #thumboff' + id).hide()
}

function gthumbMouseOver(id, flag) {
  if(flag)
    jQuery('#gallerythumbs div.active #thumboff' + id).show()
  else
    jQuery('#gallerythumbs div.active #thumboff' + id).hide()
}

/*** SLIDESHOW ***/

var start = 1;
var trans = 1000;
var delay = 4500;

function homeSlideShow(length) {
  imageInterval = window.setInterval("changeHomeImage(" + length + ");", delay);
}

function changeHomeImage(length) {
  var $active = jQuery("#homeshowcase div.active");
  var $inactive = jQuery("#homeshowcase div.inactive");
  //fade inactive (backup) in, while we load active
  $inactive.fadeIn(trans, function(){$inactive.removeClass("inactive");$inactive.addClass("active");});
  $active.fadeOut(trans,function(){
      $active.removeClass("active");
      $active.addClass("inactive");
      start = (start == length) ? 0 : start; //rewind if at end
      //load next slide into inactive ready for next fade in
      $active.load('/home/ChangeSlide/' + start);
      start += 1;
  });

  return true;
}

/* ROOM AJAX */

function ajaxRoomPhoto(url) {
  var $active = jQuery("#roomshowcase div.active");
  var $inactive = jQuery("#roomshowcase div.inactive");
  var $tactive = jQuery("#roomthumbs div.active");
  var $tinactive = jQuery("#roomthumbs div.inactive");

  $('#roomshowcase .active #caption').text('Loading...');
  //since we don't know what to load next we start fadein once ajax call complete, and fadeout after
  $inactive.load(url,null,function(){
    $inactive.fadeIn(trans/2, function(){$inactive.removeClass("inactive");$inactive.addClass("active");});
    $active.fadeOut(trans/2,function(){$active.removeClass("active");$active.addClass("inactive");});
   });
  $tactive.load(url + '?thumbs=1',null,function(){
/*
    $tinactive.fadeIn(0, function(){$tinactive.removeClass("inactive");$tinactive.addClass("active");});
    $tactive.fadeOut(0,function(){$tactive.removeClass("active");$tactive.addClass("inactive");});
*/
   });
  return false;
}

/* GALLERY AJAX */

var running = false; 
var pos = 1;
var trans = 500;
var delay = 5000;
var imageInterval = null;

function ajaxGallery(url, ipos, page) {
  var $active = jQuery("#galleryimgarea div.active");
  var $inactive = jQuery("#galleryimgarea div.inactive");

  var $tactive = jQuery("#gallerythumbs div.active");
  var $tinactive = jQuery("#gallerythumbs div.inactive");

  var $bg = jQuery('#galleryimgarea div.active .bg');
  var $loading = jQuery('#galleryimgarea div.active .loading');
  $bg.show();
  $loading.show();
  
  $inactive.text = "";
  var iurl = url + '/' + ipos + '?start=' + page;
  $inactive.load(iurl,null,function(){
    $inactive.fadeIn(trans, function(){$inactive.removeClass("inactive");$inactive.addClass("active");});
    $active.fadeOut(trans,function(){
      $active.removeClass("active");
      $active.addClass("inactive");
      $active.load(iurl + '&next=1');
      }
    );
    $bg.hide();
    $loading.hide();
   });
  var turl = url + '/' + ipos + '?start=' + page + '&thumbs=1';
  $tactive.load(turl,null,function(){
/*
    $tinactive.fadeIn(0, function(){$tinactive.removeClass("inactive");$tinactive.addClass("active");});
    $tactive.fadeOut(0,function(){$tactive.removeClass("active");$tactive.addClass("inactive");});
*/
   });
   
  var nurl = url + '/' + ipos + '?start=' + page + '&nav=1';
  $('#gallerynav').load(nurl);

  //turn off any running slideshow
  window.clearInterval(imageInterval);
  jQuery('#slideshow').removeClass("pause");
  running=false;
  
  return false;
}

function ajaxGalleryThumbs(url) {
  //remove thumb param from previous requests
  url = url.replace(/\?thumbs\=1/gi,'');
  url = url.replace(/\&thumbs\=1/gi,'')
  //make sure start has ?= not &=
  url = url.replace(/\&start\=/gi,'?start=')
  
  var $tactive = jQuery("#gallerythumbs div.active");
  var $tinactive = jQuery("#gallerythumbs div.inactive");

  $('#gallerythumbs #tbg').show();
  $('#gallerythumbs #tloading').show();

  $tactive.load(url + '&thumbs=1',null,function(){
/*
    $tinactive.fadeIn(0, function(){$tinactive.removeClass("inactive");$tinactive.addClass("active");});
    $tactive.fadeOut(0,function(){$tactive.removeClass("active");$tactive.addClass("inactive");});
*/
   });
  return false;
}

function slideShow(url, length, page) {
  if(!running) {
    imageInterval = window.setInterval("changeImage('" + url + "', " + length + "," + page + ");", delay);
    jQuery('#slideshow').addClass("pause");
    jQuery('#slideshow').attr("title","Pause Slideshow");
  } else {
    window.clearInterval(imageInterval);
    jQuery('#slideshow').removeClass("pause");
    jQuery('#slideshow').attr("title","Play Slideshow");
  }
  running = !running;
  return false;
}

function changeImage(url, length, page) {
  var $active = jQuery("#galleryimgarea div.active");
  var $inactive = jQuery("#galleryimgarea div.inactive");
  
  pos = (pos == length) ? 0 : pos; //rewind if at end
  url = url + '/' + pos + '?start=' + page;

  //load nav
  jQuery('#gallerynav').load(url + '&nav=1');
  //fade inactive (backup) in, while we load active
  $inactive.fadeIn(trans, function(){$inactive.removeClass("inactive");$inactive.addClass("active");});
  //fade out and pre-load next image into (changed to) inactive panel
  $active.fadeOut(trans,function(){
      $active.removeClass("active");
      $active.addClass("inactive");

      //we preload ShowNextImage
      $active.load(url.replace('\/ShowImage','\/LoadNext'));

      //update thumbs and nav
      //Thumb updates disabled until we get the reloading correct when user switches pages during slideshow
/*       jQuery("#gallerythumbs div.active").load(url + '&thumbs=1'); */

      pos += 1;
  });

  return true;
}
