/* ============ START PhotoSplash Generator ============ */
function PhotoSplash(id) {
  if (id == null) id = "photo_splash_" + Math.floor(Math.random()*1000);
  this.id = id;
  this.image_id = id + "_image";
  this.copyright_id = id + "_copyright";
  this.teaser_id = id + "_teaser";
  this.className = "photo_splash";
  this.copyright = { 'visible': false }; 
  this.teaser = { 'visible': true }; 
  
	this.photos = new Array();
	this.photo_index = new Array();
	this.max_shown = 4;
	this.photo_width = 91;
	this.activePhotoIndex = 0;
	this.slideshow = false;
	this.slideshowTimer = 9000;
	
	this.ids = {'photoHolderID': 'photo_tiles' }
}

PhotoSplash.prototype.renderSplash = function (target_id) {
  target = document.getElementById(target_id);
  var blank_img = document.createElement("img");
  
  var splash = document.createElement("div");
  splash.className = this.className + "_image_container";
  var image_link = document.createElement("a");
  image_link.href = this.photos[this.activePhotoIndex].link;
  var image = document.createElement("img");
	image.id = this.image_id;
  image.src = this.photos[this.activePhotoIndex].photo_url;
  image.border = "0";
  image_link.appendChild(image);
  splash.appendChild(image_link);
  target.appendChild(splash);
  
  if (this.copyright.visible == true) {
	  var copyright = document.createElement("div");
	  copyright.className = this.className + "_copyright";
	  var copyright_bg = document.createElement("div");
	  copyright_bg.className = "bg";
	  copyright_bg.innerHTML = "&nbsp;";
	  copyright.appendChild(copyright_bg);
	  var copyright_text = document.createElement("div");
		copyright_text.id = this.copyright_id;
	  copyright_text.className = "text";
	  copyright_text.innerHTML = this.photos[this.activePhotoIndex].photo_info;
	  copyright.appendChild(copyright_text);
	  target.appendChild(copyright);
	}
	
	if (this.teaser.visible == false) {
	  var teaser = document.createElement("div");
	  teaser.className = this.className + "_teaser";
	  var teaser_bg = document.createElement("div");
	  teaser_bg.className = "bg";
	  teaser_bg.innerHTML = "&nbsp;";
	  teaser.appendChild(teaser_bg);
	  var teaser_text = document.createElement("div");
		teaser_text.id = this.teaser_id;
	  teaser_text.className = "text";
	  teaser_text.innerHTML = this.photos[this.activePhotoIndex].teaser;
	  teaser.appendChild(teaser_text);
	  target.appendChild(teaser);
	} else if (this.copyright.visible) {
	  copyright.style.top = "286px";
	}	

	var slider = document.createElement("div");
	slider.id = "photo_slider_bar";
	slider.className = "photo_slider_bar";
	target.appendChild(slider);
	this.renderSlider("photo_slider_bar");
	this.setActivePhotoByIndex(0);
	this.playAsSlideshow();
}

PhotoSplash.prototype.renderSlider = function(target, activePhotoIndex) {
  var self = this;
  if (activePhotoIndex != null) this.activePhotoIndex = activePhotoIndex;
  
  this.max_shown = this.photos.length;
  
	window_size = this.max_shown * this.photo_width;
	total_width = this.photos.length * this.photo_width;

  var slider = document.createElement("table");
  var galRow = slider.insertRow(-1);
  
  /*slider images*/
	var slider_images = galRow.insertCell(-1);
	slider_images.align = "right";
  var photosHolder = document.createElement("div");
  photosHolder.className = "photo_tiles_holder";
  photosHolder.style.width = window_size + "px";
  var photoTiles = document.createElement("div");
	photoTiles.id = this.ids.photoHolderID;
  photoTiles.className = "photo_tiles";
  photoTiles.style.width = total_width + "px";

	for (i=0; i<this.photos.length; i++) {
	  var img_path = this.photos[i].thumb_url;
	  var photoPreview = document.createElement("div");
		photoPreview.id = "preview_slot_" + i;
	  if (i == this.activePhotoIndex)	photoPreview.className = "selected";

	  var photoWrapper = document.createElement("div");
	  photoWrapper.className = "wrapper";
	  
	  var photoLink = document.createElement("a");
    photoLink.className = "fakeLink";
    photoLink.pid = i;
    photoLink.onclick = function() { self.setActivePhotoByIndex(this.pid); }
		
		var photoImage = document.createElement("img");
		photoImage.src = img_path;
		photoImage.border = 0;
		
		photoLink.appendChild(photoImage);
		photoWrapper.appendChild(photoLink);
		photoPreview.appendChild(photoWrapper);
		photoTiles.appendChild(photoPreview);
	}
  photosHolder.appendChild(photoTiles);
  slider_images.appendChild(photosHolder);
  
	try {
	  if (this.photos.length > 1) 
	  	this.enablePlaySlideshow();
	  document.getElementById(target).appendChild(slider);
	  this.repositionSlider();
  } catch(e) {}
}

PhotoSplash.prototype.queuePhoto = function(photo_url, thumb_url, title, description, photographer, copyright, link) {
  if (title == null || title.length == 0) title = " ";
  if (description == null || description.length == 0) description = " ";
  else if (description.length > 130) description = String.substr(description, 0, 125) + "...";
  if (photographer == null || photographer.length == 0) photographer = "Unknown";
  if (copyright == null || copyright.length == 0) copyright = "Unknown";
  if (link != null && link.length > 0) description += " <a href="+link+">En savoir +</a>";

  var photo_id = this.photos.length;
  var teaser = "<h1><a href='" + link + "'>" + title + "</a><h1><p>" + description + "</p>";
  var photo_info = "Rubrique: " + photographer + " / " + copyright;

  var photo = { 'photo_id': photo_id,
  							'photo_url': photo_url,
  							'thumb_url': thumb_url,
  							'teaser': teaser,
								'photo_info': photo_info,
								'link': link};
  this.photo_index[photo_id] = this.photos.length;
  this.photos.push(photo);
}

PhotoSplash.prototype.enablePlaySlideshow = function() {
  try {
    document.getElementById(this.playAsSlideshowLinkID).style.display = "";
  } catch(e) {}
}

PhotoSplash.prototype.stopSlideshow = function() {
  if (this.slideshow != false) {
	 	var self = this;
	 	document.getElementById(this.playAsSlideshowLinkID).innerHTML = "Play as Slideshow";
	 	document.getElementById(this.playAsSlideshowLinkID).onclick = function() {self.playAsSlideshow();}
		clearInterval(this.slideshow);
	}
}

PhotoSplash.prototype.playAsSlideshow = function() {
  if (this.slideshow == false) {
	 	var self = this;
	  this.slideshow = setInterval(function(){self.showNextPhoto();}, this.slideshowTimer);
	}
}

PhotoSplash.prototype.showNextPhoto = function() {
  nextPhoto = this.activePhotoIndex + 1;
  if (nextPhoto >= this.photos.length || this.photos[nextPhoto].photo_id < 0)
  	nextPhoto = 0;
  this.setActivePhotoByIndex(nextPhoto);
}

PhotoSplash.prototype.toggleHighlight = function(id) {
  try {
    target = document.getElementById(id);
    if (target.className != "selected")
    	target.className = "selected";
    else
    	target.className = "";
  } catch(e) {}
}

PhotoSplash.prototype.setActivePhotoByIndex = function(index, override) {
  try {
    if (index == this.activePhotoIndex && override == null)
    	return true;
	  obj = "preview_slot_" + this.activePhotoIndex;
	  this.toggleHighlight(obj);
	  this.activePhotoIndex = index;
	  obj = "preview_slot_" + this.activePhotoIndex;
	  this.toggleHighlight(obj);
	  
		var image = document.getElementById(this.image_id);
		image.parentNode.href = this.photos[index].link;
		image.src = this.photos[index].photo_url;
		this.fadeIn(image, .1, 1);
		image.parentNode.parentNode.parentNode.style.margin="0px";

		cpdiv = document.getElementById(this.copyright_id).parentNode;
		this.slide(cpdiv, 700, 25);
		tdiv = document.getElementById(this.teaser_id).parentNode;
		this.fadeIn(tdiv.firstChild, .035, 0.75);

		this.setPhotoInfoByIndex(index);
	} catch(e) {}  
}

PhotoSplash.prototype.setActivePhotoById = function(id) {
  index = this.photo_index[id];
  this.setActivePhotoByIndex(index);
}

PhotoSplash.prototype.setPhotoInfoByIndex = function(index) {
  try {
    copyright_text = document.getElementById(this.copyright_id);
	  copyright_text.innerHTML = this.photos[index].photo_info;
    teaser_text = document.getElementById(this.teaser_id);
	  teaser_text.innerHTML = this.photos[index].teaser;
	} catch(e) {}  
}

PhotoSplash.prototype.slide = function (target, amount, speed) {
  target.style.left = amount;
  amount = amount - speed;
  var self = this;
  if (speed > 0) {
	  if (amount <= 0) {
	  	amount = 0;
	  	speed = 0;
	  }
	  setTimeout(function(){self.slide(target, amount, speed);}, 10);
	}
}

PhotoSplash.prototype.fadeIn = function (target, speed, end, opacity) {
  if (opacity == null) opacity = 0;
	if (end == null) end = 1;
  target.style.opacity = opacity;
  ie_opacity = 100 * opacity;
  target.style.filter = "alpha(opacity=" + ie_opacity + ");";
  opacity = opacity + speed;
  var self = this;
  if (speed > 0) {
	  if (opacity >= end) {
	  	opacity = end;
	  	speed = 0;
	  }
	  setTimeout(function(){self.fadeIn(target, speed, end, opacity);}, 20);
	}
}

/* ============ END Photo Splash Generator ============ */