
// ----- WARNING ----- \\
// COPYRIGHTED CONTENT \\
//  COPYRIGHT © NERVA  \\
//   nerva@nerva.ee    \\
// All rights reserved \\
// ------------------- \\

var galleryshow1 = null; // default galleryshow object
 // Remark: Do not alloc more than 1 galleryshow object.

function galleryshow_init() {
	
	// Initializing variables:
	this.root = "";
	this.path = "";
	this.title = "";
	this.files = null;
	this.fileIndex = -1;
	this.newFilename = "";
	this.unhover = "";
	this.nextFileIndex = -1; // -1 means not initialized
	this.buttonsHeight = 0;
	this.containerw = 1;
	this.containerh = 1;
	this.containerdw = 1
	this.containerdh = 1;
	this.imagex = 1;
	this.imagey = 1;
	this.imagew = 1;
	this.imageh = 1;
	this.imagedx = 1;
	this.imagedy = 1;
	this.imagedw = 1;
	this.imagedh = 1;
	this.container = document.getElementById("galleryshow1_container");
	this.loaderNext = document.getElementById("galleryshow1_loader_next");
	this.image = document.getElementById("galleryshow1_image");
	this.imageContainer = document.getElementById("galleryshow1_imagecontainer");
	this.filename = document.getElementById("galleryshow1_filename");
	
	// Initializing methods:
	this.initFiles = galleryshow_initFiles;
	this.hide = galleryshow_hide;
	this.unhide = galleryshow_unhide;
	this.isHidden = galleryshow_isHidden; // returns true if gallery is closed or closing
	this.expand = galleryshow_expand;
	this.collapse = galleryshow_collapse;

  this.containerResizeCloser = galleryshow_containerResizeCloser;
	this.containerResizeLoop = galleryshow_containerResizeLoop;
	this.containerResizing = false;
	this.calculateStep = galleryshow_calculateStep;
	this.prepareNext = galleryshow_prepareNext;
	this.next = galleryshow_next;
	this.previous = galleryshow_previous;
	this.imageResizeCloser = galleryshow_imageResizeCloser;
	this.imageResizeLoop = galleryshow_imageResizeLoop;
	this.imageResizing = false;
	this.applyIndex = galleryshow_applyIndex;
	
	this.playing = false;
	this.playLoop = galleryshow_playLoop;
	this.play = galleryshow_play;
	this.pause = galleryshow_pause;
	this.togglePlay = galleryshow_togglePlay;
  }

function galleryshow_initFiles() {
	this.files = new Array();
	this.fileIndex = -1;
	this.nextFileIndex = -1;
  }

function galleryshow_hideobject(obj) {
	if (obj == null) return false;
  obj.className = obj.className + " galleryshow_hidden";
  }

function galleryshow_unhideobject(obj) {
	if (obj == null) return false;
	var i = obj.className.indexOf("galleryshow_hidden");
	if (i >= 0) obj.className = obj.className.substr(0, i) + obj.className.substr(i + 18);
  }

function galleryshow_hide() {
	galleryshow_hideobject(this.container);
  }

function galleryshow_unhide() {
	galleryshow_unhideobject(this.container);
	galleryshow_unhideobject(document.getElementById("galleryshow1_buttonscontainer"));
  }

function galleryshow_isHidden() {
	return (this.containerdw < 2);
  }

function galleryshow_calculateStep(z, destz, speed) {
	if (speed == null) speed = 6;
	if (z < destz) return Math.max(Math.ceil(((destz - z) * speed) / 16), 1);
	if (z > destz) return -Math.max(Math.ceil(((z - destz) * speed) / 16), 1);
	return 0;
  }

function galleryshow_containerResizeCloser() { // returns true if object was resized
  var m = false;
	var z = this.calculateStep(this.containerh, this.containerdh, 4);
	if (z != 0) {
		this.containerh += z;
		this.container.style.height = "" + this.containerh + "px";
		m = true;
	  }
	z = this.calculateStep(this.containerw, this.containerdw, 4);
	if (z != 0) {
		this.containerw += z;
		this.container.style.width = "" + this.containerw + "px";
		m = true;
	  }
  return m;
  }

function galleryshow_containerResizeLoop() {
	if (this.containerResizeCloser()) {
		var thisObj = this;
		setTimeout(function() { thisObj.containerResizeLoop.call(thisObj); }, 10);
	  }
	else {
		this.containerResizing = false;
		if (this.isHidden()) { // at the end of collapse
		  this.hide();
		  }
		else { // at the end of expand
	 	  var b = document.getElementById("galleryshow1_buttonscontainer");
		  if (b != null) {
				var y = this.containerdh - 20 - this.buttonsHeight;
				b.style.top = "" + y + "px";
			  }
			galleryshow_unhideobject(this.imageContainer);
			b = document.getElementById("galleryshow1_title");
			if (b != null) {
			  galleryshow_unhideobject(b);
				b.innerHTML = this.title;
			  }
			
			galleryshow_unhideobject(document.getElementById("galleryshow1_copyright"));
			if (this.fileIndex < 0) this.next();
			this.play(5000);
		  galleryshow_unhover_button_soon("play");
		  }
	  }
  }

function alertSize() {
  var myWidth = 0, myHeight = 0;
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

function galleryshow_expand() {
  if (typeof( window.innerWidth) == 'number') {
    this.containerdw = window.innerWidth;
    this.containerdh = window.innerHeight;
    }
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    this.containerdw = document.documentElement.clientWidth;
    this.containerdh = document.documentElement.clientHeight;
    }
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
    this.containerdw = document.body.clientWidth;
    this.containerdh = document.body.clientHeight;
    }
	if (this.container == null) return false;
	this.unhide();
	if (!this.containerResizing) {
		this.containerResizing = true;
		this.containerResizeLoop();
		this.prepareNext();
	  }
  }

function galleryshow_collapse() {
	this.pause();
	this.containerdw = 1;
	this.containerdh = 1;
	if (this.container == null) return false;
	if (!this.containerResizing) {
		this.containerResizing = true;
		this.containerResizeLoop();
	  }
	galleryshow_hideobject(this.imageContainer);
	galleryshow_hideobject(document.getElementById("galleryshow1_buttonscontainer"));
	galleryshow_hideobject(document.getElementById("galleryshow1_copyright"));
	galleryshow_hideobject(document.getElementById("galleryshow1_title"));
  }

function galleryshow_toggle() {
	if (this.containerdw > 1) {
		this.collapse();
	  }
	else {
		this.expand();
	  }
  }

function galleryshow_prepareNext() {
	if (this.loaderNext == null) return false;
	this.nextFileIndex = (this.fileIndex + 1) % this.files.length;
	var file = this.files[this.nextFileIndex].split("|");
	this.loaderNext.src = "" + this.path + file[0];
	}

function galleryshow_next() {
	if ((this.image == null) || (this.nextFileIndex < 0)) return false;
	this.fileIndex = this.nextFileIndex;
	this.applyIndex();
	this.prepareNext();
	
  }

function galleryshow_previous() {
	if ((this.image == null) || (this.fileIndex < 0)) return false;
	this.fileIndex = (this.files.length + this.fileIndex - 1) % this.files.length;
	this.applyIndex();
	this.prepareNext();
	}

function galleryshow_imageResizeCloser() { // returns true if object was resized
  var m = false;
	var z;
	z = this.calculateStep(this.imagey, this.imagedy);
	if (z != 0) {
		this.imagey += z;
		this.imageContainer.style.top = "" + this.imagey + "px";
		m = true;
	  }
	z = this.calculateStep(this.imageh, this.imagedh);
	if (z != 0) {
		this.imageh += z;
		this.image.height = "" + this.imageh;
		m = true;
	  }
	z = this.calculateStep(this.imagex, this.imagedx);
	if (z != 0) {
		this.imagex += z;
		this.imageContainer.style.left = "" + this.imagex + "px";
		m = true;
	  }
	z = this.calculateStep(this.imagew, this.imagedw);
	if (z != 0) {
		this.imagew += z;
		this.image.width = "" + this.imagew;
		m = true;
	  }
  return m;
  }

function galleryshow_imageResizeLoop() {
	if (!this.image.complete) {
		var thisObj = this;
		setTimeout(function() { thisObj.imageResizeLoop.call(thisObj); }, 100);
		return false;
	  }
	if (this.newFilename != "") {
	  if (this.filename != null) this.filename.innerHTML = '<a href="' + this.path + this.newFilename + '" target="outside">' + this.newFilename + '</a>';
		this.newFilename = "";
	  }
	if (this.unhover != "") {
		galleryshow_unhover_button_soon(this.unhover);
		this.unhover = "";
	  }
  if (this.imageResizeCloser()) {
		var thisObj = this;
		setTimeout(function() { thisObj.imageResizeLoop.call(thisObj); }, 10);
		}
	else {
		this.imageResizing = false;
		}
	}

function galleryshow_applyIndex() {
	if (this.image == null) return false;
	var file = this.files[this.fileIndex].split("|");
	this.imagedw = Number(file[1]);
	this.imagedh = Number(file[2]);
	if (this.imagedw > (this.containerdw - 100)) {
		this.imagedh = Math.round(this.imagedh * ((this.containerdw - 100) / this.imagedw));
		this.imagedw = this.containerdw - 100;
	  }
	if (this.imagedh > (this.containerdh - 120 - this.buttonsHeight)) {
		this.imagedw = Math.round(this.imagedw * ((this.containerdh - 120 - this.buttonsHeight) / this.imagedh));
		this.imagedh = this.containerdh - 120 - this.buttonsHeight;
	  }
	this.imagedx = Math.round((this.containerdw - this.imagedw) / 2);
	this.imagedy = Math.round((this.containerdh - 20 - this.buttonsHeight - this.imagedh) / 2);
	this.image.src = "" + this.path + file[0];
	if (!this.imageResizing) {
		this.imageResizing = true;
		this.imageResizeLoop();
	  }
	this.newFilename = file[0];
	}


function galleryshow_playLoop() {
	if (this.playing) {
		if (this.image.complete) {
			this.next();
			var thisObj = this;
			setTimeout(function() { thisObj.playLoop.call(thisObj); }, 5000);
		  }
		else {
			setTimeout(function() { thisObj.playLoop.call(thisObj); }, 100);
		  }
	  }
  }

function galleryshow_play(timeout) {
	if (!this.playing) {
		if (timeout == null) timeout = 1000;
		this.playing = true;
		var thisObj = this;
		setTimeout(function() { thisObj.playLoop.call(thisObj); }, timeout);
		var b = document.getElementById("galleryshow1_button_play");
		if (b != null) b.src = this.root + "pause.png";
	  }
  }

function galleryshow_pause() {
	this.playing = false;
	var b = document.getElementById("galleryshow1_button_play");
	if (b != null) b.src = this.root + "play.png";
  }

function galleryshow_togglePlay() {
	if (this.playing) this.pause();
	  else this.play();
  }


// Global functions:

function galleryshow_hover_button(name) {
	var b = document.getElementById("galleryshow1_button_" + name);
	if (b != null) b.className = "hover";
  }

function galleryshow_unhover_button(name) {
	var b = document.getElementById("galleryshow1_button_" + name);
	if (b != null) b.className = "";
  }

function galleryshow_unhover_button_soon(name, timeout) {
	if (timeout == null) timeout = 400;
	setTimeout('galleryshow_unhover_button("' + name + '")', timeout);
  }


// Using default galleryshow object:

function galleryshow_keydown(e) {
	if ((galleryshow1 == null) || galleryshow1.isHidden()) return true;
	var keynum = 0;
	if (window.event) { // IE
		keynum = e.keyCode;
		}
	else if (e.which) { // Others
		keynum = e.which;
		}
	switch (keynum) {
		case 37:
			galleryshow_hover_button("previous");
			galleryshow1.unhover = "previous";
		  galleryshow1.previous();
		  return false;
		case 32:
			galleryshow_hover_button("play");
		  galleryshow_unhover_button_soon("play");
			galleryshow1.togglePlay();
			return false;
		case 39:
			galleryshow_hover_button("next");
			galleryshow1.unhover = "next";
			galleryshow1.next();
		  return false;
		case 27:
			galleryshow1.collapse();
		  return false;
	  }
	return true;
  }

function galleryshow_create() {
	if (galleryshow1 == null) galleryshow1 = new galleryshow_init();
  }

function galleryshow_running() {
	return ((galleryshow1 != null) && (!galleryshow1.isHidden()));
  }
