// =============================================================================
//
// DIG CMS Template                                                             
//                                                                      
//  $Rev: 2192 $
//  $Author: herzog $
//  $Date: 2011-11-24 15:50:50 +0100 (Do, 24 Nov 2011) $
//
// =============================================================================

/*
Item Gallery

Generic gallery object. Can be used for displaying a  image gallery. The gallery loops in both directions.

Usage:

e.g.

<div id="xyz0" style="display:none;">any content</div>
<div id="xyz1" style="display:none;">any content</div>
...
<div id="xyz17" style="display:none;">any content</div>


try to create an instance of the ImageGalery object within a script tag:

	var obj = new ItemGalery({max:3,total:18,prefix:"xyz"});

	means:

		- the prefix of the item's id is "xyz"
		- show 3 Items at once
		- there are 18 items total
		
	    note: without a proper configuration like shown above, the script will not work as expected!

	obj.showItem();

	means:

		- display the current 3 items (in this case the first 3)
	
Use the nextItem() and the prevItem() method for navigating through your gallery.
*/
function ItemGallery(_config) {
	
	this.currentItem = 0;
	this.maxItem = _config && _config.max ? _config.max : 0;
	this.totalItem = _config && _config.total ? _config.total : 0;
	this.prefixItem = _config && _config.prefix ? _config.prefix : "";
	
	this.setItem = function(_pos) {
		
		if(typeof _pos != "undefined" && _pos >= 0 && _pos < this.totalItem) {
			
			this.currentItem = _pos;
		}
	};
	
	this.hideAllItems = function() {
		
		for(var i = 0; i < this.totalItem; i++) {
			
			try {

				document.getElementById(this.prefixItem + i).style.display = "none";
			} catch(e) { } // should not happen
		}
	};
	
	this.showAllItems = function() {
		
		for(var i = 0; i < this.totalItem; i++) {

			try {
	
				document.getElementById(this.prefixItem + i).style.display = "block";
			} catch(e) { } // should not happen
		}
	};
	
	this.showItem = function(_pos) {
		
		if(typeof _pos != "undefined") this.setItem(_pos);
		for(var i = this.currentItem; i < (this.currentItem + this.maxItem) && i < this.totalItem; i++) {

			try {
	
				document.getElementById(this.prefixItem + i).style.display = "block";
			} catch(e) { } // should not happen
		}
		try {
			// refresh trackers
			trackerRefresh();
		} catch(e) { }
	};
	
	this.hideItem = function() {
		
		for(var i = this.currentItem; i < (this.currentItem + this.maxItem) && i < this.totalItem; i++) {

			try {

				document.getElementById(this.prefixItem + i).style.display = "none";
			} catch(e) { } // should not happen
		}
	};
	
	this.nextItem = function() {
		
		this.hideItem();

		if(this.totalItem == 1) {
			
			this.currentItem = 0;
		} else {
			
			this.currentItem = this.currentItem + this.maxItem < this.totalItem ? this.currentItem + this.maxItem : 0;
		}
		this.showItem();
	};
	
	this.prevItem = function() {
		
		this.hideItem();
		if(this.totalItem == 1) {
			
			this.currentItem = 0;
		} else {
			
			this.currentItem = this.currentItem - this.maxItem >= 0 ? this.currentItem - this.maxItem : this.totalItem - (this.totalItem % this.maxItem == 0 ? this.maxItem : this.totalItem % this.maxItem);
			if(this.currentItem < 0) this.currentItem = 0;
		}
		this.showItem();
	};
}

