/**
* Sliding gallery
* v. 1.0
*
* @author Aurimas Baubkus. Modified by Andrius Virbičianskas
*/

var Gallery = new Class({
  
  photos: null,
  uris: null,
  current: null,
  total: null,
  domain: null,
  
  initialize: function() {
    this.photos = new Array();
    this.uris = new Array();
    this.current = 0;
    this.total = -1;
    this.domain = '';
  },
  
  reset: function() {
    this.photos = new Array();
    this.current = 0;
    this.total = -1;
  },
  
  addPhoto: function(img) {
    this.photos.push(img);
    
    this.total++;
  },
  
  addUri: function(uri) {
    this.uris.push(uri);
  },
  
  setDomain: function(domain) {
    this.domain = domain;
  },
  
  leftMove: function() {
    this.current--;
    this.changePhotos();  
  },
  
  rightMove: function() {
    this.current++;
    this.changePhotos();
  },
  
  changePhotos: function() {
    if (this.current < 0) {
      this.current = this.total;
    } else if (this.current > (this.total)) {
      this.current = 0;
      this.resetPhotos();
      return;
    }

    $('photo1').src = this.domain + this.photos[this.current];
    $('uri1').href = this.uris[this.current];
    
    if (this.current + 1 > this.total) {
      $('photo2').src = this.domain + this.photos[this.total-this.current];
      $('uri2').href = this.uris[this.total-this.current];
    } else {
      $('photo2').src = this.domain + this.photos[this.current+1];
      $('uri2').href = this.uris[this.current+1];
    }
    
    if (this.current + 2 > this.total) {
      $('photo3').src = this.domain + this.photos[(this.current+1)-this.total];
      $('uri3').href = this.uris[(this.current+1)-this.total];
    } else {
      $('photo3').src = this.domain + this.photos[this.current+2];
      $('uri3').href = this.uris[this.current+2];
    }
    
    if (this.current + 3 > this.total) {
      $('photo4').src = this.domain + this.photos[(this.current+2)-this.total];
      $('uri4').href = this.uris[(this.current+2)-this.total];
    } else {
      $('photo4').src = this.domain + this.photos[this.current+3];
      $('uri4').href = this.uris[this.current+3];
    }
    
    if (this.current + 4 > this.total) {
      $('photo5').src = this.domain + this.photos[(this.current+3)-this.total];
      $('uri5').href = this.uris[(this.current+3)-this.total];
    } else {
      $('photo5').src = this.domain + this.photos[this.current+4];
      $('uri5').href = this.uris[this.current+4];
    }
    
    if (this.current + 5 > this.total) {
      $('photo6').src = this.domain + this.photos[(this.current+4)-this.total];
      $('uri6').href = this.uris[(this.current+4)-this.total];
    } else {
      $('photo6').src = this.domain + this.photos[this.current+5];
      $('uri6').href = this.uris[this.current+5];
    }
    
    if (this.current + 6 > this.total) {
      $('photo7').src = this.domain + this.photos[(this.current+5)-this.total];
      $('uri7').href = this.uris[(this.current+5)-this.total];
    } else {
      $('photo7').src = this.domain + this.photos[this.current+6];
      $('uri7').href = this.uris[this.current+6];
    }
    
    if (this.current + 7 > this.total) {
      $('photo8').src = this.domain + this.photos[(this.current+6)-this.total];
      $('uri8').href = this.uris[(this.current+6)-this.total];
    } else {
      $('photo8').src = this.domain + this.photos[this.current+7];
      $('uri8').href = this.uris[this.current+7];
    }
    
    if (this.current + 8 > this.total) {
      $('photo9').src = this.domain + this.photos[(this.current+7)-this.total];
      $('uri9').href = this.uris[(this.current+7)-this.total];
    } else {
      $('photo9').src = this.domain + this.photos[this.current+8];
      $('uri9').href = this.uris[this.current+8];
    }
    
    if (this.current + 9 > this.total) {
      $('photo10').src = this.domain + this.photos[(this.current+8)-this.total];
      $('uri10').href = this.uris[(this.current+8)-this.total];
    } else {
      $('photo10').src = this.domain + this.photos[this.current+9];
      $('uri10').href = this.uris[this.current+9];
    }
  },
  
  resetPhotos: function() {
    var number = 1;
    for (var i = 0; i < 10; i++) {
	    $('photo' + number).href = this.domain + this.photos[i];
	    number++;
    } 
  }
  
});

objGallery = new Gallery();