/*
Guidelines and limitations for photos:
1. All photo and thumbnail images must be JPG
2. A maximum of 4 photos are allowed per page
3a. Landscape photos must be 494px x 329px
3b. Portrait photos must be 312px x 468px
4. Photos must be named 01, 02, 03, and 04
5. Thumbnails must be named 01_thumb, 02_thumb, 03_thumb, 04_thumb
6. Photos must be placed in one of the following folders:
   weddings--, winter--, destination--, engagements--, portraits--,
   bellies--, babies--, kids--, families--, pets--
   where -- is to be replaced with a two digit number that is one higher
   than the highest existing folder number. 
*/

/*
How to add more photos to the website
1. Prepare the images for the website in accordance with the guidelines and
   limitations above.
2. In this file, scroll down to where it says "function loadPageData()", then
   scroll down a few more lines to where it says "switch(currentPage)".
3. Locate the "case" beneath this line that coincides with the page that you
   are updating.  
   For example, if you are adding a picture to the 'Our Furry Friends' section,
   then scroll down to "case "pets":".
4. Immediately beneath that line, update the value of totalPages.
   For example, if there are currently 6 "pets" folders and you are adding
   two more pages, change totalPages to 8.
5. Following the pattern that already exists for the captions, add the new
   photo captions below the existing ones.  The syntax is very important, so
   be careful.  Ensure the line "break;" appears AFTER all of the captions.
   For example, add the following line underneath the existing list:
   captions[4] = "CAPTION";
6. That's all!
*/ 

var page = 1;
var captionIndex = 0;

// These will be defined in loadPageData below
var photoDirectory;
var totalPages;
var captions;

var photoPrefix;
var photoName;
var photoOrientation = 0; // 0 = landscape, 1 = portrait

// cross-browser onLoad event handler
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

// onLoad
addLoadEvent(pageInit);

// Initializes the page
function pageInit()
{
  document.getElementById("previous").style.visibility = "hidden";
  
  loadPageData();
  preloadImages();
  loadPageContent();  
}

// Preloads the images for all pages
function preloadImages()
{
  for(i = 1; i <= totalPages; i++) // totalPages set in loadPageData()
  {
    var preloadPrefix = "";
    
    if(i < 10)
      preloadPrefix = "images/photos/" + photoDirectory + "0" + i + "/";
    else
      preloadPrefix = "images/photos/" + photoDirectory + i + "/";

    var imgPhoto01 = new Image();
    imgPhoto01.src = preloadPrefix + "01.jpg";
    var imgPhoto02 = new Image();
    imgPhoto02.src = preloadPrefix + "02.jpg";
    var imgPhoto03 = new Image();
    imgPhoto03.src = preloadPrefix + "03.jpg";
    var imgPhoto04 = new Image();
    imgPhoto04.src = preloadPrefix + "04.jpg";
    
    var imgThumb01 = new Image();
    imgThumb01.src = preloadPrefix + "01_thumb.jpg";
    var imgThumb02 = new Image();
    imgThumb02.src = preloadPrefix + "02_thumb.jpg";
    var imgThumb03 = new Image();
    imgThumb03.src = preloadPrefix + "03_thumb.jpg";
    var imgThumb04 = new Image();
    imgThumb04.src = preloadPrefix + "04_thumb.jpg";
  }
}

// Populates certain data structures based on which page is currently being viewed
function loadPageData()
{
  captions = new Array();
  
  var currentPage = document.body.id;
  photoDirectory = currentPage;
  
  switch(currentPage)
  {
    // WEDDINGS
    case "weddings":
      totalPages = 14;
      // Page 01
      captions[0] = "Lake Minnewanka, Banff, Alberta";
      captions[1] = 'Fish Creek Park, Calgary';
      captions[2] = "Autumn in Calgary";
      captions[3] = "Cadillac, Cochrane, Alberta";
      // Page 02
      captions[4] = "Fun with Keri and Nick!";
      captions[5] = "Kiss from Remy!";
      captions[6] = "Celebration";
      captions[7] = "Bridge at Fish Creek Park";      
      // Page 03
      captions[8] = "Sara laughing, Spruce Meadows";
      captions[9] = "Monica at Hotel Arts, Calgary";
      captions[10] = 'Candice at Kensington Riverside Inn, Calgary';
      captions[11] = "Gorgeous colours";
      // Page 04
      captions[12] = "Mirror at Buffalo Mtn. Lodge, Alberta";
      captions[13] = "Getting ready at Hotel Arts, Calgary";
      captions[14] = "Mirrors";
      captions[15] = "Bride at home";
      // Page 05
      captions[16] = "Girls at Spruce Meadows";
      captions[17] = "Hands";
      captions[18] = "Dash to the Church";
      captions[19] = "Evalina Schmidtke Couture gown";
      // Page 06
      captions[20] = "The littlest flower girls and ring bearer";
      captions[21] = "Christ Church, Calgary";
      captions[22] = "Piper at the Ranche Restaurant ceremony";
      captions[23] = "Wind at the Calgary Zoo";
      // Page 07
      captions[24] = "The beauty of black and white";
      captions[25] = "Calgary Tower in distance";
      captions[26] = 'Palliser Hotel, Calgary';
      captions[27] = "Fish Creek Park, Calgary, Alberta";
      // Page 08
      captions[28] = '"the Meadow" at Buffalo Mountain Lodge,<br />Banff, Alberta';
      captions[29] = "Cozy Hotel Arts, Calgary";
      captions[30] = "Cochrane, Alberta";
      captions[31] = "Edmonton, Alberta";
      // Page 09
      captions[32] = "Romano's gang";
      captions[33] = "Champagne in Canmore, Alberta";
      captions[34] = "All male bridal party!";
      captions[35] = "Edmonton, Alberta";
      // Page 10
      captions[36] = "Ready to go down the aisle!";
      captions[37] = "Flower girl";
      captions[38] = "Flower girl";
      captions[39] = "Girls at McDougall Centre, Calgary";
      // Page 11
      captions[40] = "The Ranche Restaurant, Calgary";
      captions[41] = "Stairs, Calgary";
      captions[42] = "Newell Lake, Brooks, Alberta";
      captions[43] = "Old City Hall, Calgary, Alberta";
      // Page 12
      captions[44] = "That's the Spirit!, Calgary";
      captions[45] = "Petal throwing, the Ranche<br />Restaurant, Calgary";
      captions[46] = "Walk to Stephen Ave, Calgary";
      captions[47] = "Rouge Restaurant wedding, Calgary";
      // Page 13
      captions[48] = "Bouquet at Fish Creek Park, Calgary";
      captions[49] = "Kiss at the Ranche Restaurant, Calgary";
      captions[50] = "Oval Room, Palliser Hotel, Calgary";
      captions[51] = "Evalina Schmidtke Couture gown";
      // Page 14
      captions[52] = "Grandmother's kiss";
      captions[53] = "Lovely bridesmaids";
      captions[54] = "Couple in forest";
      captions[55] = "Bride in a sea of white dress";
      break;
    
    // WINTER WEDDINGS
    case "winter":
      totalPages = 2;
      // Page 01
      captions[0] = '"Most Enthusiasm in the cold" EVER award';
      captions[1] = "Dec 31, 2005, Stephen Avenue, Calgary";
      captions[2] = "Champagne at the River Cafe, Calgary";
      captions[3] = '"Just Married"';
      // Page 02
      captions[4] = "Banff Springs Hotel, Banff at Christmas";
      captions[5] = "Elegant studio session";
      captions[6] = "Staircase, Palliser Hotel, Calgary";
      captions[7] = "Oval Room, Palliser Hotel, Calgary";
      break;
      
    // DESTINATION WEDDINGS
    case "destination":
      totalPages = 2;
      // Page 01
      captions[0] = "Cayo Blanco, Cuba";
      captions[1] = "Varadero, Cuba";
      captions[2] = "Varadero, Cuba"; 
      captions[3] = "Cayo Blanco, Cuba";
      // Page 02
      captions[4] = "Invermere, British Columbia";
      captions[5] = "U of S, Saskatoon, Saskatchewan";
      captions[6] = "Steinbach, Manitoba";
      captions[7] = "Toronto Hunt Club";
      break;
    
    // ENGAGEMENTS
    case "engagements":
      totalPages = 2;
      // Page 01
      captions[0] = "Kristin's ring";
      captions[1] = "Arm in arm, Calgary";
      captions[2] = "Stephen Avenue, Calgary"; 
      captions[3] = "Prince's Island Park, Calgary";
      // Page 02
      captions[4] = "Confederation Park, Calgary";
      captions[5] = "Prince's Island Park, Calgary";
      captions[6] = "Elbow Drive, Calgary";
      captions[7] = "By the River Cafe, Calgary";
      break; 
    
    // PORTRAITS
    case "portraits":
      totalPages = 2;
      // Page 01
      captions[0] = "Evalina Schmidtke, dress designer";
      captions[1] = "Savannah Wellman, singer";
      captions[2] = "Artist Lori Lukasewich"; 
      captions[3] = "Chocolateers at Coca West, Bowan Island, BC";
      // Page 02
      captions[4] = "High school graduation day photo";
      captions[5] = "Kiki";
      captions[6] = "Shae";
      captions[7] = "Shannon, actress";
      break; 
    
    // BELLIES
    case "bellies":
      totalPages = 2;
      // Page 01
      captions[0] = "Sunalta School, Calgary";
      captions[1] = "Okotoks, Alberta";
      captions[2] = "Fish Creek Park, Calgary"; 
      captions[3] = "Bowness Park, Calgary";
      // Page 02
      captions[4] = "First baby";
      captions[5] = "Listening";
      captions[6] = "Everyone's excited";
      captions[7] = "Studio session";
      break; 
    
    // BABIES
    case "babies":
      totalPages = 2;
      // Page 01
      captions[0] = "Twins at 6 months";
      captions[1] = "Fun in the bathtub";
      captions[2] = "Perfect poser"; 
      captions[3] = "Crazy haired girl!";
      // Page 02
      captions[4] = "Father and son";
      captions[5] = "Mother and daughter";
      captions[6] = "Tiny toes";
      captions[7] = "Mother and son";
      break; 
    
    // KIDS
    case "kids":
      totalPages = 2;
      // Page 01
      captions[0] = "The helicopter";
      captions[1] = "Joyful boy";
      captions[2] = "Boys at the Tuscany ravine"; 
      captions[3] = '"Best Tree Climbers" ever!';
      // Page 02
      captions[4] = "Fairy princess";
      captions[5] = "Let's catch the Photographer!";
      captions[6] = "Winter fun in Winnipeg, Manitoba";
      captions[7] = "6 cousins";
      break;
    
    // FAMILIES
    case "families":
      totalPages = 2;
      // Page 01
      captions[0] = "Sandy Beach, Calgary";
      captions[1] = "Riley Park, Calgary";
      captions[2] = "Sisters, Calgary"; 
      captions[3] = "Garrison Woods, Calgary";
      // Page 02
      captions[4] = "Family laughter";
      captions[5] = "Backyard slide";
      captions[6] = "Swinging boy";
      captions[7] = "Holding onto dad, Elbow River, Calgary";
      break; 
    
    // OUR FURRY FRIENDS
    case "pets":
      totalPages = 1;
      // Page 01
      captions[0] = "Jennifer Bartlett of Incredible Puppy";
      captions[1] = "Boston tags along";
      captions[2] = "Samson as a pup"; 
      captions[3] = "Dakota steals the show";
      break;
  }    
}

// Shows/hides previous/next links, loads four new images onto the page, rotates photo if necessary 
function loadPageContent()
{
  // Determine whether/not to show "previous" link
  if(page == 1)
    document.getElementById("previous").style.visibility = "hidden";
  else
    document.getElementById("previous").style.visibility = "visible";

  // Determine whether/not to show the "next" link    
  if(page == totalPages)
    document.getElementById("next").style.visibility = "hidden";
  else
    document.getElementById("next").style.visibility = "visible";  
  
  // Determine photo filename prefix
  if(page < 10)
    photoPrefix = "images/photos/" + photoDirectory + "0" + page + "/";
  else
    photoPrefix = "images/photos/" + photoDirectory + page + "/";
  
  photoName = photoPrefix + "01.jpg";

  checkPhotoOrientationInit();

  document.getElementById("thumb01").src = photoPrefix + "02_thumb.jpg";
  document.getElementById("thumb02").src = photoPrefix + "03_thumb.jpg";
  document.getElementById("thumb03").src = photoPrefix + "04_thumb.jpg";
}

// Swaps the photo and a thumbnail
function loadPhoto(thumbIndex)
{
  var photoFile;
  var photoObject;
  
  // Retrieve currently displayed photo
  if(photoOrientation == 0) // landscape
  {
    photoFile = document.getElementById("photo-landscape").src;
    photoObject = document.getElementById("photo-landscape");
  }
  else
  {
    // portrait
    photoFile = document.getElementById("photo-portrait").src;
    photoObject = document.getElementById("photo-portrait");
  }
  
  var thumbFile01 = document.getElementById("thumb01").src;
  var thumbFile02 = document.getElementById("thumb02").src;
  var thumbFile03 = document.getElementById("thumb03").src;
  
  switch(thumbIndex)
  {
    case 1:
      photoObject.src = thumbFile01.replace("_thumb.jpg", ".jpg");
      document.getElementById("thumb01").src = photoFile.replace(".jpg", "_thumb.jpg");
      break;
    case 2:
      photoObject.src = thumbFile02.replace("_thumb.jpg", ".jpg");
      document.getElementById("thumb02").src = photoFile.replace(".jpg", "_thumb.jpg");
      break;
    case 3:
      photoObject.src = thumbFile03.replace("_thumb.jpg", ".jpg");
      document.getElementById("thumb03").src = photoFile.replace(".jpg", "_thumb.jpg");
      break;
  }
  
  photoName = photoObject.src; 
  checkPhotoOrientationInit();
}

function checkPhotoOrientationInit()
{
  oPhoto = new Image();
   
  oPhoto.onload = checkPhotoOrientation;
  oPhoto.onerror = photoError;
  oPhoto.onabort = photoError;
  
  oPhoto.src = photoName;
}

function photoError()
{
  alert("An error has occurred loading the photos for this page.  Please press the Refresh/Reload button in your web browser to resolve this problem.  If the problem persists, please contact marilyn@marilyngillespie.com");
}

function checkPhotoOrientation()
{
  if(oPhoto.width > oPhoto.height) // landscape photo
  {
    photoOrientation = 0;
    document.getElementById("photo-landscape").src = photoName;
    
    document.getElementById("photo-landscape").style.visibility = "visible";
    document.getElementById("photo-portrait").style.visibility = "hidden";
    
    document.getElementById("caption-landscape-area").style.visibility = "visible";
    document.getElementById("caption-portrait").style.visibility = "hidden";
    
    document.getElementById("hrule-top").src = "images/hrule_short.gif";
  }
  else                             // portrait photo
  {
    photoOrientation = 1;
    document.getElementById("photo-portrait").src = photoName;
    
    document.getElementById("photo-landscape").style.visibility = "hidden";
    document.getElementById("photo-portrait").style.visibility = "visible";
    
    document.getElementById("caption-landscape-area").style.visibility = "hidden";
    document.getElementById("caption-portrait").style.visibility = "visible";
    
    document.getElementById("hrule-top").src = "images/hrule_full.gif";
  }
  
  loadCaption();
}

function loadCaption()
{
  // Determine which caption to dispay
  captionIndex = -1;
  for(i = 0; i < 4; i++) // max of 4 photos/folder
  {
    var regex = /01.jpg$/;
    
    switch(i)
    {
      case 1:
        regex = /02.jpg$/;
        break;
      case 2:
        regex = /03.jpg$/;
        break;
      case 3:
        regex = /04.jpg$/;
        break;
    }
    
    if(photoOrientation == 0) // landscape
    {
      if(regex.test(document.getElementById("photo-landscape").src))
      {
        captionIndex = i;
        break;
      }
    }
    else
    {
      // portrait
      if(regex.test(document.getElementById("photo-portrait").src))
      {
        captionIndex = i;
        break;
      }
    }
  }
  
  // Display the caption
  if(captionIndex == -1)
  {
    //alert("no caption");
  }
  else
  {
    captionIndex = (4 * (page - 1)) + captionIndex;
    if(photoOrientation == 0) // landscape
    {   
      document.getElementById("caption-landscape").innerHTML = captions[captionIndex];
    }
    else
    {
      // portrait
      document.getElementById("caption-portrait").innerHTML = captions[captionIndex];
    }
  }
}

function nextPage()
{
  page++;
  
  loadPageContent();
}

function prevPage()
{
  page--;
  
  loadPageContent();
}
