//Polluting Global namespace - to fix.

var images = [];
var links = [];
var videos = [];
var files = [];
var currentlyDisplayedImage = 0;
var resourceInc = 0;
var imageInc = 0;
var videoInc = 0;

function clickImageId(imageId) {
  var imageIndex = getImageIndex(imageId);
  showImage(imageIndex);
  }

//The true passed indicates its an internal navigation call and no need to re-display the modal box.
function showNextImage() {
  var nextIndex = parseInt(currentlyDisplayedImage) + 1;
  showImage(nextIndex, true);  
  }

function showPreviousImage() {
  var previousIndex = parseInt(currentlyDisplayedImage) - 1;
  showImage(previousIndex, true);
  }

function imageGalleryClose() {
  $('#imageGallery').jqmHide();  
  }
//nav defaults to false - display modal box - set to true only if calling function from within jqModal box.
function showImage(index, nav) {
  if (!nav) {
    nav = false;
    }
  $('#previousImage').show();
  $('#imageNavSeparator').show();
  $('#nextImage').show();
  if (index == 0) {
    $('#previousImage').hide();
    $('#imageNavSeparator').hide();
    }
  if (index == images.length - 1) {
    $('#nextImage').hide();
    $('#imageNavSeparator').hide();
    }
  if (currentlyDisplayedImage == 0) {
    images[index].preload();
    }
  var documentHeight = $(window).height();
  var documentWidth = $(window).width();
  var imageHeight = parseInt(images[index].height);
  var imageWidth = parseInt(images[index].width);
  var posTop = Math.floor((documentHeight - imageHeight) / 2);
  var posLeft = Math.floor((documentWidth - imageWidth) / 2);
  var heightValue = imageHeight + 80;
  heightValue = heightValue + "px";
  var widthValue = imageWidth + 20;
  widthValue = widthValue + "px";
  if (images[index].imageObject && images[index].imageObject.loaded == true) {
//    $('#imageLoading').hide();
    $('#imageGallery').animate({width: widthValue}, "fast").animate({height: heightValue}, "fast", function() {
      $('#largeImage').attr("src", images[index].imageObject);
      $('#imageDescription').html(images[index].description);
      }); 
    } else {
    $('#imageLoading').show();
    $('#imageDescription').html('');
    $('#imageGallery').animate({width: widthValue}, "fast").animate({height: heightValue}, "fast", function() {
      $('#largeImage').attr("src", images[index].href);
      $('#largeImage').load(function() {
        $('#imageLoading').hide();
        $('#imageDescription').html(images[index].description);
        });
      }); 
    }

  if (nav == false) {
    $('#imageGallery').jqmShow();
    }
  currentlyDisplayedImage = index;
  //Preload previous and next images  
  if (index != 0) {
    images[index - 1].preload();
    }
  if (index < images.length - 1) {
    images[index + 1].preload();
    }  
  }

function getImageIndex(imageId) {
  var matchedIndex = false;
  for (var i=0; i < images.length; i++) {
    var currId = images[i].id;
    //alert(currId);
    if (currId == imageId) {
      matchedIndex = images[i].index;
      }
    }
  return matchedIndex;  
  }

var Event = function(data, type) {
  this.id = data.id;
  this.title = data.title;
  this.type = type;
  }

Event.prototype.getHtml = function() {
  var html = '';
  html += "<div class='otherEventsLinks'>";
  if (this.id != contentId) {
    html += "<a href='event.php?id=" + this.id + "'>";
    html += this.title;
    html += "</a>";
    } else {
    html += "<span class='currentEvent'>";
    html += this.title;
    html += "</span>";
    html += "<br />";
    }
  html += "</div>";
  return html;
  }

var Link = function(index) {
  this.id = 0;
  this.href = '';
  this.description = '';
  this.index = index;
  }

Link.prototype.populate = function(linkData) {
  this.id = linkData.id;
  this.href = linkData.href;
  this.description = linkData.description;
  }

Link.prototype.getHtml = function() {
  var html = '';
  html += "<div id='link" + this.index + "' class='links'>";
  html += "<div class='linksHref'>";
  html += "<a href='" + this.href + "' title='" + this.description + "'>";
  html += this.href.substring(0,32);
  html += "</a>";
  html += "</div>";
  html += "<div class='linksDescription'>";
  html += this.description;
  html += "</div>";
  html += "</div>";
  return html;
  }

var MyImage = function(index) {
  this.id = 0;
  this.thumbHref = '';
  this.href = '';
  this.description = '';
  this.height = 480;
  this.width = 640;
  this.index = index;
  this.imageObject = new Image();
  }

MyImage.prototype.populate = function(imageData) {
  this.id = imageData.id;
  this.href = imageData.href;
  this.description = imageData.description;
  if (imageData.width) { this.width = imageData.width; }
  if (imageData.height) { this.height = imageData.height; }
  this.thumbHref = imageData.href.replace("images/", "images/thumbs/");
  }

MyImage.prototype.getHtml = function() {
  var html = '';
  html += "<span id='imageThumb" + this.index + "' class='imageThumb'>";
  html += "<a href='Javascript:clickImageId(" + this.id + ")'>";
  html += "<img src='" + this.href.replace("images/", "images/thumbs/") + "' alt='" + this.description.replace("'", "") + "' />";
  html += "</a>";
  html += "</span>";
  return html;
  }

MyImage.prototype.preload = function() {
  //this.imageObject = new Image();
  this.imageObject.src = this.href;
  //alert("Preloaded " + this.href);
  }

var File = function(index) {
  this.id = 0;
  this.href = '';
  this.description = '';
  this.index = index;
  }

File.prototype.populate = function(fileData) {
  this.id = fileData.id;
  this.href = fileData.href;
  this.description = fileData.description;
  }

File.prototype.getHtml = function() {
  var html = '';
  html += "<div id='file" + this.index + "' class='files'>";
  html += "<div class='filesLink'>";
  html += "<a href='" + this.href + "' title='" + this.description + "'>";
  html += this.href.substring(10, this.href.length);
  html += "</a>";
  html += "</div>";
  html += "<div class='filesDescription'>";
  html += this.description;
  html += "</div>";
  html += "</div>";
  return html;
  }


var ChildEvent = function() {
  this.id = 0;
  this.title = '';
  this.techNotes = '';
  this.resources = [];    
  }

ChildEvent.prototype.populate = function(data) {
  this.id = data.id;
  this.title = data.title;
  this.techNotes = data.techNotes;
  if (data.resources) { this.resources = data.resources; } else { this.resources = '' }
  }

ChildEvent.prototype.getTechHtml = function() {
  var html = '';
  if (this.techNotes) {
    html += "<div id='childTech" + this.id + "' class='childTech'>";
    html += "<div class='childTechTitle'>";
    html += this.title + ":";
    html += "</div>";
    html += "<div class='childTechNotes'>";
    html += this.techNotes
    html += "</div>";
    html += "</div>";
    }
  return html;
  }

ChildEvent.prototype.getRightHtml = function() {
  var html = '';
  html += "<a href='event.php?id=" + this.id + "'>";
  html += this.title;
  html += "</a>";
  html += "<br />";
  return html;    
  }    

ChildEvent.prototype.getImagesHtml = function() {
  var childImagesHtml = '';
  for (var i=0; i < this.resources.length; i++) {
    var childResource = this.resources[i];
    if (childResource.type == 2) {
      images[imageInc] = new MyImage(imageInc);
      images[imageInc].populate(childResource);
      //images[imageInc].preload();
      childImagesHtml += images[imageInc].getHtml();
      imageInc++;
      }
    }
  var html = '';
  if (i != 0) {
    html += "<div id='childImages" + this.id + "' class='childImages'>";
    html += "<div class='childImageTitle'>";
    html += this.title + ":";
    html += "</div>";
    html += "<div class='childThumbs'>";
    html += childImagesHtml;
    html += "</div>";
    html += "</div>";
    html += "<br />";
    }
  return html;      
  }

var Video = function(index) {
  this.id = 0;
  this.sha1 = '';
  this.href = '';
  this.flvlow = '';
  this.flvhi = '';
  this.ogg = '';
  this.mp4 = '';
  this.thumbNo = 0;
  this.thumbLoc = '';
  this.duration = 0;
  this.index = index;
  this.width = 0;
  this.height = 0;
  //this.flashvars = {};
  }

Video.prototype.populate = function(videoData) {
  this.id = videoData.id;
  //this.sha1 = videoData.sha1;
  this.href= videoData.href;
  this.width = videoData.width;
  this.height = videoData.height;
  this.duration = videoData.duration;
  this.title = videoData.title;
  this.thumbNo = videoData.thumbNo;
  this.description = videoData.description;
  //this.imageLoc = "videos/images/" + this.sha1.substring(0,8) + "/" + this.thumbNo + ".jpg";
  this.imageLoc = "videos/images/" + videoData.image;  
  this.flvlow = "videos/flvlow/" + this.href + ".flv";
  this.mp4 = "videos/mp4/" + this.href + ".mp4";
  this.flashvars = {file: "../camputer/" + this.mp4, image: "../camputer" + this.imageLoc };
  }

Video.prototype.getHtml = function() {
  var html = '';
  html += "<div class='videoWrapper'>";
  html += "<div class='videoTitle'>";
  html += this.title;
  html += "</div>";
  html += "<div class='videoPlayer' id='videoPlayer" + this.index + "'>";
  html += "Sorry you need Flash version 9+ to view this video ... <a href='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=shockwaveFlash' target='_blank'>download here</a>";
  html += "</div>";
  html += "<br /><br />";
  html += "<div class='videoDescription'>";
  html += this.description;
  html += "</div>";
  html += "</div>";
  html += "<br /><br />";
  return html;
  }
  
