// JavaScript Document

/* --- galleryImagesFunctions --- */

function galleryImagesCallback(instance,method) {
  return function() {
    return method.apply(instance,arguments);
  }
}

function galleryImagesFunctions(objName) {
  this.objName = objName;
  this.slideshowTimerID = null;
  this.slideshowTimerRunning = false;
  this.slideshowTexts = null;
  this.slideshowRun = false;
  this.slideshowTime = 2000;
  this.blending = 500;
  this.fullArr = new Array;
  this.actualPos = 0;
  this.galleryLink = '';
  this.actualImage = new Image;
  this.elemImage = null;
  this.elemLoading = null;
  this.elemNext = null;
  this.elemPrevious = null;
  this.elemSlideshow = null;
  this.elemComments = null;
  this.commentTemplate = '';
  this.galleryType = 0; // 0=photo 1=video
  this.videoTemplate = '';

	this.resetPhoto = function() {
    this.actualImage.onload = galleryImagesCallback(this,this.replaceEnd);
    this.elemImage = document.getElementById('photo-img');
    this.elemLoading = document.getElementById('photo-loading');
    this.elemNext = document.getElementById('photo-next');
    this.elemPrevious = document.getElementById('photo-previous');
    this.elemSlideshow = document.getElementById('photo-slideshow');
    this.elemComments = document.getElementById('photo-comments');
    if (!this.elemImage || !this.elemLoading || !this.elemNext || !this.elemPrevious || !this.elemSlideshow || !this.elemComments || !this.fullArr.length>0) return false;
    this.elemLoading.src = this.loadingImgSrc;
    this.elemNext.onclick = galleryImagesCallback(this,this.clickNext);
    this.elemPrevious.onclick = galleryImagesCallback(this,this.clickPrevious);
    this.elemSlideshow.onclick = galleryImagesCallback(this,this.clickSlideshow);
    this.showButtonSlideshow();
	};

	this.resetVideo = function() {
    this.elemVideo = document.getElementById('photo-video');
    this.elemComments = document.getElementById('photo-comments');
    if (!this.elemVideo || !this.elemComments || !this.fullArr.length>0) return false;
	};
	
	this.init = function() {
    switch (this.galleryType) {
      case 0: this.resetPhoto(); break;
      case 1: this.resetVideo(); break;
    }
	};

	this.clickThis = function(pos,thisElem) {
    //alert('clickThis');
    thisElem.blur();
    thisElem.href = '#obr';
    this.actualPos = pos;
    switch (this.galleryType) {
      case 0: this.replaceBegin(); break;
      case 1: this.replaceVideo(); break;
    }
	};

	this.clickNext = function() {
    //alert('clickNext');
    this.elemNext.blur();
    if (this.fullArr.length>(this.actualPos+1)) {
      this.actualPos = this.actualPos+1;
    }
    if (this.fullArr.length==(this.actualPos+1) && this.slideshowRun) {
      this.elemSlideshow.onclick = galleryImagesCallback(this,this.clickSlideshowRewind);
      this.elemSlideshow.innerHTML = this.slideshowTexts[2];
      this.slideshowTimerStop();
      this.slideshowRun = false;
    }
    this.replaceBegin();
    return false;
	};

	this.clickPrevious = function() {
    //alert('clickPrevious');
    this.elemPrevious.blur();
    if (this.actualPos>=1) {
      this.actualPos = this.actualPos-1;
    }
    this.replaceBegin();
    return false;
	};

	this.clickSlideshow = function() {
    //alert('clickSlideshow');
    this.elemSlideshow.blur();
    if (this.slideshowRun) {
      this.elemSlideshow.innerHTML = this.slideshowTexts[0];
      this.slideshowTimerStop();
      this.slideshowRun = false;
    } else {
      this.elemSlideshow.innerHTML = this.slideshowTexts[1];
      this.slideshowTimerRun();
      this.slideshowRun = true;
    }
    return false;
	};
	
	this.clickSlideshowRewind = function() {
    //alert('clickSlideshowRewind');
    this.elemSlideshow.blur();
    this.elemSlideshow.onclick = galleryImagesCallback(this,this.clickSlideshow);
    this.elemSlideshow.innerHTML = this.slideshowTexts[0];
    this.actualPos = 0;
    this.replaceBegin();
    return false;
	};

  this.slideshowTimerStop = function() {
    //alert('slideshowTimerStop');
    if(this.slideshowTimerRunning) {
      clearInterval(this.slideshowTimerID);
    }
    this.slideshowTimerRunning = false;
  };

  this.slideshowTimerRun = function() {
    //alert('slideshowTimerRun');
    this.slideshowTimerStop();
    this.slideshowTimerID = setInterval(this.objName+".clickNext()",this.slideshowTime);
    this.slideshowTimerRunning = true;
  };

	this.replaceBegin = function() {
    //alert('replaceBegin');
    this.elemLoading.className = 'loading';
    this.actualImage.src = this.galleryLink+this.fullArr[this.actualPos][0];
    if (this.fullArr.length>(this.actualPos+1)) {
      this.elemNext.className = 'next';
    } else {
      this.elemNext.className = 'next invisible';
    }
    if (this.actualPos>=1) {
      this.elemPrevious.className = 'previous';
    } else {
      this.elemPrevious.className = 'previous invisible';
    }
	};

	this.replaceEnd = function() {
    //alert('replaceEnd');
    this.elemLoading.className = 'hidden';
    if (this.blending!=0) {
      this.opacityFade(this.elemImage.id, 100, 0, this.blending, true);
    } else {
      this.elemImage.src = this.actualImage.src;
    }
    this.replaceComments();
	};

	this.replaceVideo = function() {
    //alert('replaceVideo');
    this.elemVideo.parentNode.innerHTML = this.videoTemplate.replace('%0%',this.galleryLink+this.fullArr[this.actualPos][0]);
    this.resetVideo();
    this.replaceComments();
	};

	this.replaceComments = function() {
    var commentTemp = this.commentTemplate;
    for (var i=0;i<this.fullArr[this.actualPos].length;i++) {
      commentTemp = commentTemp.replace('%'+i+'%',this.fullArr[this.actualPos][i]);
    }
    this.elemComments.innerHTML = commentTemp;
	};

  this.showButtonSlideshow = function() {
    //alert('replaceSlideshow');
    if (this.fullArr.length<=1) return false;
    this.slideshowTexts = this.elemSlideshow.innerHTML.split('|');
    this.elemSlideshow.innerHTML = this.slideshowTexts[0];
    this.elemSlideshow.className = 'slideshow';
  };

  this.opacityChange = function(opacity, image1id)
  { // 100 visible 0 invisible
    var object = document.getElementById(image1id).style;
    object.opacity = (opacity / 101);
    object.MozOpacity = (opacity / 101);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
  };

  this.opacityFade = function(id, opacStart, opacEnd, millisec, ending) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;
    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
      for(i = opacStart; i >= opacEnd; i--) {
        setTimeout(this.objName+".opacityChange(" + i + ",'" + id + "')",(timer * speed));
        timer++;
      }
    } else if(opacStart < opacEnd) {
      for(i = opacStart; i <= opacEnd; i++) {
        setTimeout(this.objName+".opacityChange(" + i + ",'" + id + "')",(timer * speed));
        timer++;
      }
    }
    if (ending) {
      setTimeout(this.objName+".opacityEnd()",(timer * speed));
    }
  };

  this.opacityEnd = function() {
    this.elemImage.src = this.actualImage.src;
    this.opacityFade(this.elemImage.id, 0, 100, this.blending, false);
  };

  //this.init();
}

/* ---  --- */

