/**
* Sliding gallery
* v. 1.12
*
* @todo:
*  - add transition effect
*  - add video compatibility
*  - prototype.js -> mootools.js
*
* @author Aurimas Baubkus
*/
   
var Gallery = Class.create();
Gallery.prototype = {
  photos: null,
  photos_big: null,
  thumbs: null,
  text: null,
  current: null,
  total: null,
  domain: null,
  show: null,   // how many photos to show
  
  initialize: function() {
    this.photos = new Array();
    this.photos_big = new Array();
    this.thumbs = new Array();
    this.text = new Array();
    this.current = 0;
    this.total = -1;
    this.domain = '';
    this.show = 6;
  },
  
  reset: function() {
    this.photos = new Array();
    this.thumbs = new Array();
    this.current = 0;
    this.total = -1;
  },
  
  addPhoto: function(img, thumb, text, img_big) {
    this.photos.push(img);
    this.photos_big.push(img_big);
    this.thumbs.push(thumb);
    this.text.push(text);
    
    this.total++;
  },
  
  setDomain: function(domain) {
    this.domain = domain;
  },
  
  leftMove: function() {
    this.current--;
    if (this.current < 0) {
    	this.current = this.total;
    }
    this.changePhotos();  
  },
  
  rightMove: function() {   
    this.current++;
    if (this.current > this.total) {
    	this.current = 0;
    }
    this.changePhotos();
  },
  
  changePhotos: function() {
    if (this.current < 0 || this.current > this.total) {
      this.current = 0;
    }
    
    var el = $("gallery_image");
    el.src = this.domain + this.photos[this.current];
    el.setAttribute("src_big", this.domain + this.photos_big[this.current]);
    $("gallery_photo_title").innerHTML = this.text[this.current];
    this.renderPaging();
  },
  
  renderPaging : function() {	
	var el_left = $("gallery_paging_left");
	var el_right = $("gallery_paging_right");
	var el_parent = el_left.parentNode;
	var el_paging2 = $("gallery_paging_2");
	var nodes = el_parent.childNodes;
	for (var i = 0; i < nodes.length; i++) {
		var node = nodes[i];
		if (node && node.nodeName && node.id !== el_left.id && node.id !== el_right.id) {
			i--;
			el_parent.removeChild(node);
		}
	}
	var pages_size = 20;
	var total_pages = Math.ceil(this.photos.length / pages_size);
	var page_of_pages = Math.floor(this.current / pages_size);
	var first_item = page_of_pages * pages_size;
	var last_item = Math.min(first_item + pages_size, this.photos.length);	
	for (var i = first_item; i < last_item; i++) {
		var node_td = el_parent.insertBefore(document.createElement("TD"), el_right);
		var node_div = node_td.appendChild(new Element("DIV"));
		node_div.className = "gallery_nr" + (i === this.current ? "_sel" : "");
		node_div.__id = i;
		node_div.observe('click', function() {objGallery.changePhoto(this.__id);return false;}); 
		var node_page = node_div.appendChild(document.createTextNode(i + 1));
	}
		
	el_paging2.style.display = total_pages < 2 ? "none" : "";		
	
	if (total_pages < 2) {
		return true;
	}
	var el_paging2_links = $("gallery_paging_2_links");
	var parts = new Array();
	for (var i = 0; i < total_pages; i++) {
		parts.push("<a href='#' class='gallery_link' onclick='objGallery.changePhoto(" + (i * pages_size) + ");return false;'>" + ((i * pages_size) + 1) + "-" + (((i + 1) * pages_size))  + "</a>");
	}
	el_paging2_links.innerHTML = parts.join("&nbsp;&nbsp;&nbsp;");
	
	return true;
  },
  
  resetPhotos: function() {
    var number = 1;
    for (var i = 0; i < this.show; i++) {
	    $('thumb' + number).src = this.domain + this.thumbs[i];
	    $('photo' + number).href = this.domain + this.photos[i];
	    number++;
    }
  },
  
  addValues: function(id, photo_id) {
	myThumb = '';
	myThumb = $('thumb' + id);
	myThumb.stopObserving('click');
	myThumb.observe('click', function() {
	  objGallery.changePhoto(photo_id);
	  return false;
	});
  },  
  
  changePhoto: function(id) {
	this.current = id;
    this.changePhotos();
  },
  
  setPagingArrowsVisible : function(visible) {
	  $("gallery_back").parentNode.style.display = $("gallery_next").parentNode.style.display = visible ? "" : "none";
  }
  
};

objGallery = new Gallery();