// JavaScript Document
//Data set for the images

var init = 0;
var gBehaviorsArray = [];
var gImageLoader = null;

function CancelBehavior(id){
  if (gBehaviorsArray[id])  {
    gBehaviorsArray[id].cancel();
    gBehaviorsArray[id] = null;
  }
}

function init_page(){
  var height;
  var width;
  var fullPath;
  var rows = dsGallery.getData();
  var id   = 0;

  // If we got here through the portfolio page
  // then we need to determine which gallery image to show first
  if((typeof(galleryStart) == "undefined") || (dsGallery.url == 'data/closeUp.xml')){
  // We do not have to set a particular painting
    id = 0;
  }else{
    id = galleryStart;
  }
  
  var curRow = rows[id];
  dsGallery.setCurrentRow(curRow["ds_RowID"]);
  
  height = rows[id]["@imgHeight"];
  width  = rows[id]["@imgWidth"];
  fullPath = "images/gallery/" + rows[id]["@mainFile"];

  SetMainImage(fullPath, width, height);
  return 0;
}


function SetMainImage(imgPath, width, height){
  var img = document.getElementById("mainImage");
  if (!img)
    return;

  CancelBehavior("mainImage");

  if (gImageLoader)
  {
    gImageLoader.onload = function() {};
    gImageLoader = null;
  }

  gBehaviorsArray["mainImage"] = new Spry.Effect.Opacity(img, parseInt(Spry.Effect.getOpacity(img)), 0, { duration: 1000,
    finish: function(){
      gBehaviorsArray["mainImage"] = new Spry.Effect.Size(img.parentNode, Spry.Effect.getDimensions(img.parentNode), { width: width, height: height, units:"px"}, {duration: 400,
            finish: function(){
          // Use an image loader to make sure we only fade in the new image after
          // it is completely loaded.
          gImageLoader = new Image();
          gImageLoader.onload = function(){
            img.src = gImageLoader.src;
            gImageLoader = null;
            gBehaviorsArray["mainImage"] = new Spry.Effect.Opacity(img, 0, 1, { duration: 1000,
              finish: function(){
                        gBehaviorsArray["mainImage"] = null;
			          }});
            gBehaviorsArray["mainImage"].start();
          };
          gImageLoader.src = imgPath;
        }
      });
      gBehaviorsArray["mainImage"].start();
    }
  });
  gBehaviorsArray["mainImage"].start();
} 

function laRestoreState(){
  dsGallery.setURL(laSaveState['laDSURL']);
  dsGallery.setXPath(laSaveState['laXPath']);
  dsGallery.loadData();
}

function laSaveCurrState(){
  laSaveState['laDSURL'] = dsGallery.getURL();
  laSaveState['laXPath'] = dsGallery.getXPath();
  laSaveState['laCurrRow'] = dsGallery.getCurrentRowID();
}

function laClearState(){
  laSaveState['laOnPostLoad'] = 0;
  laSaveState['laDSURL'] = "";
  laSaveState['laXPath'] = "";
  laSaveState['laCurrRow'] = 0;
}

function nextGalleryImage(){
  nextImage('gallery');
}

function nextDetailImage(){
  nextImage('detail');
}

function nextStageImage(){}

function nextImage(imgType){
  // iType is either 'gallery', 'detail' or 'stages'
  // We need to make sure that the right dataset is active
  // before going to the next record.
  // 
  // By design, dsGallery may be using any of gallery.xml, detail.xml or stages.xml
  // this function is called to advance any of the other galleries.
  // 
  // Prior to importing changing the current dataset, the developer should ensure
  // the laSaveState is filled out so that we can restore the current state here.
  var laCurrURL = dsGallery.getURL();
  
  switch(imgType){
    case 'gallery':
      if(laCurrURL != laGalleryURL){
		laSaveState['laOnPostLoad'] = 'next';
        laRestoreState();
		return 0;
      }
      break;
    case 'closeUp':
      if(laCurrURL != laCloseUpURL){
        laRestoreState();
      }
      break;
    case 'stages':
      if(laCurrURL != laStagesURL){
        laRestoreState();
      }
      break;
  }


  var rows = dsGallery.getData();
  var curRow = dsGallery.getCurrentRow();
  var i = 0;

  while(rows[i] != curRow){
    i++; 
  };
  if( i == (rows.length - 1) ){
    i = 0;
  }else{
    i++;
  }
  curRow = rows[i];
  dsGallery.setCurrentRow(curRow["ds_RowID"]);
  var height  = rows[i]["@imgHeight"];
  var width   = rows[i]["@imgWidth"];
  var imgPath = rows[i]["@mainFile"];
  imgPath = "images/gallery/" + imgPath;
  SetMainImage(imgPath, width, height);
}

function prevImage(imgType){
  // iType is either 'gallery', 'detail' or 'stages'
  // We need to make sure that the right dataset is active
  // before going to the next record.
  // 
  // By design, dsGallery may be using any of gallery.xml, detail.xml or stages.xml
  // this function is called to advance any of the other galleries.
  // 
  // Prior to importing changing the current dataset, the developer should ensure
  // the laSaveState is filled out so that we can restore the current state here.
  var laCurrURL = dsGallery.getURL();
  
  switch(imgType){
    case 'gallery':
      if(laCurrURL != laGalleryURL){
		laSaveState['laOnPostLoad'] = 'prev';
        laRestoreState();
		return 0;
      }
      break;
    case 'detail':
      if(laCurrURL != laCloseUpURL){
        laRestoreState();
      }
      break;
    case 'stages':
      if(laCurrURL != laStagesURL){
        laRestoreState();
      }
      break;
  }


  var rows = dsGallery.getData();
  var curRow = dsGallery.getCurrentRow();
  var i = 0;
  while(rows[i] != curRow){
    i++; 
  };
  if( i == 0 ){
    i = rows.length - 1;
  }else{
    i--;
  }
  curRow = rows[i];
  dsGallery.setCurrentRow(curRow["ds_RowID"]);
    var height  = rows[i]["@imgHeight"];
  var width   = rows[i]["@imgWidth"];
  var imgPath = rows[i]["@mainFile"];
  imgPath = "images/gallery/" + imgPath;
  SetMainImage(imgPath, width, height);
}


function showDetail(title){
  // If the user clicked this from the gallery page,
  // we'll need to save the current state and then
  // switch the dataset from data/gallery.xml to data/details.xml
  
  var curUrl = dsGallery.getURL();
  var xPath;
  if(curUrl == laGalleryURL){
    // We got here from the gallery page, save the gallery state
	// so when the user clicks the "next" button we can restore
	// the current state
	laSaveCurrState();
    dsGallery.setURL(laCloseUpURL);
	xPath = "gallery/detail[@title='" + title + "']";
    dsGallery.setXPath(xPath);
	dsGallery.loadData();
  }else{
    nextImage('closeUp');
  }
}