﻿var currentGalleryImageIndex = 0;
var currentGallerySlide = 0;
var galleryDetailsExpanded = false;
var currentlyFading = false;
var slideWidth = 0;
var isNIGUP = false;
var imagesPerGroup = 10;

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Gallery
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function InitGallery() {
  $(".lightbox").lightbox();

  if ($('#galley-wrapper').length < 1) {
    return;
  }

  $('#gallery-image-details').css('opacity', 0.8);

  $('div.gallery-image-button-next').click(function() { SlideRight(); });
  $('div.gallery-image-button-previous').click(function() { SlideLeft(); });

  $('#gallery-previous-image').each(function() {
    $(this).click(function() { LoadPreviousImage(); });
    $(this).css('display', 'none');
    $(this).css('cursor', 'pointer');
  });

  $('#gallery-next-image').each(function() {
    $(this).click(function() { LoadNextImage(); });
    $(this).css('cursor', 'pointer');

    if (imageArray.length > 1) {
      $(this).css('display', 'block');
    } else {
      $(this).css('display', 'none');
    }
  });
  
  $('div.gallery-image-button').each(function() {
    $(this).mouseover(function(){ShowBorder(this);});
    $(this).mouseout(function(){HideBorder(this);});
    //$(this).click(function(){LoadImage(this);});
  });
  
  $('div.gallery-image-button-next').each(function() {
    $(this).mouseover(function(){ShowBorder(this);});
    $(this).mouseout(function(){HideBorder(this);});
  });

  $('div.gallery-image-button-previous').each(function() {
    $(this).mouseover(function(){ShowBorder(this);});
    $(this).mouseout(function(){HideBorder(this);});
  });

  slideWidth = $('div.image-group').css('width');
  isNIGUP = $('#nigup-gallery').length > 0 || $('#nigup-gallery-narrow').length > 0;

  if ($('#nigup-gallery-narrow').length > 0) {
    imagesPerGroup = 7;
  }

  LoadImage(0);
}

function SlideRight() {
  Slide('right');
}

function SlideLeft() {
  Slide('left');
}

function Slide(direction) {
  var slideLength = '0';

  if (direction == "left") {
    currentGallerySlide--;
    slideLength = '+=' + slideWidth;
  } else {
    currentGallerySlide++;
    slideLength = '-=' + slideWidth;
  }

  if ((currentGallerySlide == 0 && imageArray.length > (imagesPerGroup + 1)) || (currentGallerySlide != GetSlideIndex(imageArray.length - 1))) {
    $('#current-image-text').html("Showing " + imagesPerGroup + " of " + imageArray.length + " images");
  } else if (currentGallerySlide == 0) {
    $('#current-image-text').html("Showing " + imageArray.length + " of " + imageArray.length + " images");
  } else {
    $('#current-image-text').html("Showing " + parseInt(imageArray.length - ((imagesPerGroup * currentGallerySlide) + 1)) + " of " + imageArray.length + " images");
  }

  $('#image-groups').animate({ 'left': slideLength }, 500);
}

function LoadNextImage() {
  LoadImage(currentGalleryImageIndex + 1);
  return false;
}

function LoadPreviousImage() {
  LoadImage(currentGalleryImageIndex - 1);
  return false;
}

function LoadImage(id) {
  if (currentlyFading) {
    return;
  }

  if (id < 0) {
    id = 0;
  } else if (id > imageArray.length - 1) {
    id = imageArray.length - 1;
  }

  var imageMetaData = imageArray[id];

  var image = $('<img>');
  $(image).attr('src', imageMetaData.url);
  $(image).attr('title', imageMetaData.title);
  $(image).attr('description', imageMetaData.description);
  $(image).attr('alt', imageMetaData.alttext);
  $(image).attr('link', imageMetaData.downloadurl);
  $(image).attr('index', id);
  $(image).attr('rel', imageMetaData.lightboxurl);

  if (imageMetaData.loaded) {
    ShowImage(image[0]);
  } else {
    $(image).load(function() {
      imageMetaData.loaded = true;
      ShowImage(this);
    });
  }

  return false;
}

function ShowImage(loadedImage) {
  var image = $('#gallery-image-main').find('img')[0];
  var linkNode = $('#gallery-image-main').find('a')[0];
  var clone = $(image).clone();
  $(clone).css('display', 'none');
  $(image).after(clone);
  $(clone).attr('id', 'gallery-image-main-clone');

  if (isNIGUP) {
    $(clone).css('width', $(image).css('width'));
    $(clone).css('height', $(image).css('height'));
  }

  if ((image.height != loadedImage.height || image.width != loadedImage.width) && !isNIGUP) {
    var borderWidthTopBottom = parseInt((334 - loadedImage.height) / 2);

    if (borderWidthTopBottom < 0 || borderWidthTopBottom > 165) {
      borderWidthTopBottom = 0;
    }

    var borderWidthLeftRight = parseInt((501 - loadedImage.width) / 2);

    if (borderWidthLeftRight < 0 || borderWidthLeftRight > 249) {
      borderWidthLeftRight = 0;
    }

    $(image).css({
      'border-style': 'solid',
      'border-color': '#cccccc',
      'border-top-width': borderWidthTopBottom,
      'border-bottom-width': borderWidthTopBottom,
      'border-left-width': borderWidthLeftRight,
      'border-right-width': borderWidthLeftRight
    });
  }

  var index = $(loadedImage).attr('index');
  var newGalleryImageIndex;

  if (!index) {
    newGalleryImageIndex = 0;
  } else {
    newGalleryImageIndex = parseInt(index);
  }

  var newGallerySlide = GetSlideIndex(newGalleryImageIndex);

  if (newGallerySlide - currentGallerySlide > 0) {
    SlideRight();
  } else if (newGallerySlide - currentGallerySlide < 0) {
    SlideLeft();
  }

  currentGalleryImageIndex = newGalleryImageIndex;
  currentGallerySlide = newGallerySlide;

  var title = $(loadedImage).attr('title');
  var description = $(loadedImage).attr('description');
  var link = $(loadedImage).attr('link');
  var lightboxlink = $(loadedImage).attr('rel');

  var titleNode = $('#gallery-image-title');
  var descriptionNode = $('#gallery-image-description');
  var downloadLinkNode = $('#gallery-image-download-link')[0];

  $(titleNode).css('color', '#000000'); //Stupid IE bug requires the text to change color to update.
  $(descriptionNode).css('color', '#000000'); //Stupid IE bug requires the text to change color to update.
  $(titleNode).html(title);
  $(descriptionNode).html(description);
  downloadLinkNode.href = link;
  $(titleNode).css('color', '#ffffff'); //Stupid IE bug requires the text to change color to update.
  $(descriptionNode).css('color', '#ffffff'); //Stupid IE bug requires the text to change color to update.

  $(linkNode).attr('title', title);
  $(linkNode).attr('href', lightboxlink);

  if (galleryDetailsExpanded) {
    SlideDescription(GetDescriptionHeight() + 25);
  }

  if (currentGalleryImageIndex > 0) {
    $('#gallery-previous-image').css('display', 'block');
  } else {
    $('#gallery-previous-image').css('display', 'none');
  }

  if (currentGalleryImageIndex < (imageArray.length - 1)) {
    $('#gallery-next-image').css('display', 'block');
  } else {
    $('#gallery-next-image').css('display', 'none');
  }

  if (true) {//$.browser.mozilla
    image.src = loadedImage.src;
    image.title = loadedImage.title;
    image.alt = loadedImage.alt;
    $('#gallery-image-main-clone').remove();
    currentlyFading = false;
  } else {
    currentlyFading = true;
    $(clone).css('opacity', 0);
    $(clone).css('display', 'block');
    $(clone).css('opacity', 1);
    $(image).css('left', -4000);
    $(image).css('opacity', 0);
    $(image).css('display', 'none');

    image.src = loadedImage.src;
    image.title = loadedImage.title;
    image.alt = loadedImage.alt;

    $(image).css('opacity', 1);
    $(image).css('left', 0);

    $(image).fadeIn(500, function() {
      $('#gallery-image-main-clone').remove();
      currentlyFading = false;
    });
  }
}

function GetSlideIndex(galleryImageIndex) {
  var newGallerySlide = parseInt((galleryImageIndex - 1) / imagesPerGroup)

  if (newGallerySlide < 0) {
    newGallerySlide = 0;
  }

  return newGallerySlide;
}

function ToggleDescription() {
  $('#gallery-image-details').each(function() {
    var endValue = 0;
    var oldUrl = $('#gallery-info')[0].src;
    var newUrl = '';

    if (galleryDetailsExpanded) {
      endValue = 20;
      galleryDetailsExpanded = false;
      newUrl = oldUrl.replace('gallery-close-info.gif', 'gallery-open-info.gif');
    } else {
      endValue = GetDescriptionHeight() + 25;
      galleryDetailsExpanded = true;
      newUrl = oldUrl.replace('gallery-open-info.gif', 'gallery-close-info.gif');
    }

    $('#gallery-info')[0].src = newUrl;

    SlideDescription(endValue);
  });
}

function GetDescriptionHeight() {
  return $('#gallery-image-description').height();
}

function SlideDescription(endValue) {
  $('#gallery-image-details').animate({ height: endValue }, 500);
}

function HideBorder(node) {
  ChangeBorder(node, false);
}

function ShowBorder(node) {
  ChangeBorder(node, true);
}

function ChangeBorder(node, displayBorder) {
  $(node).children().each(function() {
    if (this.className == 'gallery-image-border') {
      if (displayBorder) {
        $(this).css('display', 'block');
      } else {
        $(this).css('display', 'none');
      }
    }
  });
}