
var gallery = new Spry.Data.XMLDataSet("http://" + document.domain + "/shared/gallery_XML.cfm", "/root/content");

var gSlideShowInterval;
if (gSlideShowInterval == undefined)
  gSlideShowInterval = 7500; 

var gAutoStartSlideShow;
if (gAutoStartSlideShow == undefined)
  gAutoStartSlideShow = true;

var gSlideShowTimer = null;
var gSlideShowOn = false;


function fadeInContent(notificationType, notifier, data)
{
	if (notificationType != "onPostUpdate")
		return;
	Spry.Effect.AppearFade('spry_content', { to: 100, from: 0, duration: 2000 });
}

Spry.Data.Region.addObserver('spry_content', fadeInContent);

function fadeOutContentThenSetRow(rowID)
{
	if(rowID != gallery.getCurrentRowNumber()){
		Spry.Effect.AppearFade('spry_content', { to: 0, from: 100, duration: 2000, finish: function() {
			gallery.setCurrentRow(rowID);
			setSelectedControlColor();
			//displaySelectedRowNumber();
			 if (gSlideShowOn)
				setSlideShowTimer();
		}});
	}
}


function advanceToNextSlide(moveBackwards)
{
  var rows = gallery.getData();
  var curRow = gallery.getCurrentRow();
  
  if (rows.length < 1)
    return;

  for (var i = 0; i < rows.length; i++)
  {
    if (rows[i] == curRow)
    {
      if (moveBackwards)
        --i;
      else
        ++i;
      break;
    }
  }

  if (!moveBackwards && i >= rows.length)
    i = 0;
  else if (moveBackwards && i < 0)
    i = rows.length - 1;

  curRow = rows[i];
  //gallery.setCurrentRow(curRow["ds_RowID"]);
  fadeOutContentThenSetRow(curRow["ds_RowID"]);
}

function setSlideShowTimer()
{
  killSlideShowTimer();
  gSlideShowTimer = setTimeout(function(){ gSlideShowTimer = null; advanceToNextSlide(false); }, gSlideShowInterval);
}

function killSlideShowTimer()
{
  if (gSlideShowTimer)
    clearTimeout(gSlideShowTimer);
  gSlideShowTimer = null;
}

function startSlideShow(skipTimer){
	gSlideShowOn = true;
	if (!skipTimer)
		setSlideShowTimer();
	//document.getElementById('imgPlayPause').src = '../interface/pause_ctrl.gif';
	//document.getElementById('imgPlayPause').alt = 'Pause';
}

function stopSlideShow(){
	gSlideShowOn = false;
	killSlideShowTimer();
	//document.getElementById('imgPlayPause').src = '../interface/play_ctrl.gif';
	//document.getElementById('imgPlayPause').alt = 'Play';
}

function doPlayPause(){
	if(gSlideShowOn){
		stopSlideShow();
	} else {
		startSlideShow();
	}
}

function HandleRowClick(rowID)
{
  stopSlideShow();
  fadeOutContentThenSetRow(rowID);
}

function setSelectedControlColor(){
	for(var i = 0; i < gallery.getRowCount(); i++){
		if(i == gallery.getCurrentRowNumber()){
			document.getElementById('number_list_'+i).style.border = 'solid 2px #f5d409';
			//document.getElementById('number_list_'+i).style.position = 'relative';
			//document.getElementById('number_list_'+i).style.top = '0';
			//document.getElementById('number_list_'+i).style.left = '10';
			//document.getElementById('number_list_'+i).style.float = 'left';
			//document.getElementById('number_list_'+i).style.zIndex = '104';
			//document.getElementById('number_list_'+i).style.width = '103px';
			//document.getElementById('number_list_'+i).style.height = '57px';
			//document.getElementById('number_list_'+i).style.background = 'url(../interface/thumblight.gif) no-repeat';
			
			
			//document.getElementById('number_list_'+i).style.height = '30';
			//document.getElementById('number_list_'+i).style.width = '26';
		} else {
			document.getElementById('number_list_'+i).style.border = 'solid 2px #d9e5f0';
			//document.getElementById('number_list_'+i).style.position = '';
			//document.getElementById('number_list_'+i).style.top = '';
			//document.getElementById('number_list_'+i).style.left = '';
			//document.getElementById('number_list_'+i).style.float = '';
			//document.getElementById('number_list_'+i).style.zIndex = '104';
			//document.getElementById('number_list_'+i).style.width = '';
			//document.getElementById('number_list_'+i).style.height = '';
			//document.getElementById('number_list_'+i).style.background = '';
			//document.getElementById('number_list_'+i).style.height = '30';
			//document.getElementById('number_list_'+i).style.width = '26';
		}
	}
}

function displaySelectedRowNumber(){
	document.getElementById('currentrow').innerHTML = gallery.getCurrentRowNumber()+1;
}
