var imageGalleryButtons = document.querySelectorAll('article .button');
if (imageGalleryButtons) {
	
	var counter = 1,
	t,
	pieceSwap,
	pieceSwapLength,
	oppositeButton, 
	currentButton, 
	limitIndex,
	textIndictor = document.querySelectorAll('article .selfclear h1'),
	imageHolder = document.getElementById('display'),
	pieceInfo = document.querySelectorAll('article hgroup h5'),
	jsonCategories = ["Title: ", "Date: ", "Medium: ", "Info: "],
	imageNumberDisplay = document.getElementById("imageNumber"),
	whichHide;
	
	function setUpGallery(assetData) {
		pieceSwap = assetData.pieces.piece;
		pieceSwapLength = assetData.pieces.piece.length;
		this.init();
	}

	setUpGallery.prototype = {
		init: function(){
			var i, j;
			for(i=0; i<imageGalleryButtons.length; i+=1) {
				this.bind(imageGalleryButtons[i]);
			}
			for(j=0; j < pieceInfo.length; j+=1){
				pieceInfo[j].innerHTML = jsonCategories[j] + pieceSwap[counter - 1].information[j];
			}
			//imageGalleryButtons[0].style.cursor = "auto";
			//imageGalleryButtons[0].style.visibility = "hidden";
			imageNumberDisplay.innerHTML = "Image " + (counter) + " of " + (pieceSwapLength);
			
		},
		
		bind: function(a){
			a.addEventListener('click', function (e) {
				trackCount(this.id);
			}, true);
			a.addEventListener('mouseover', function (e) {
				this.style.opacity = ".7";
			}, true);
			a.addEventListener('mouseout', function (e) {
				this.style.opacity = ".3";
			}, true);
		},
	}
	
	function trackCount(direction){
		if(direction === "prev"){
			counter--;
			if(counter === 0){ counter = 14; }
			calculate(counter, 1);
		}else{
			counter++;
			if(counter === pieceSwap.length + 1){ counter = 1; }
			calculate(counter, pieceSwap.length);
		}
	}
	
	function calculate(counter, limit){
		
		var i, j;
		imageHolder.src = pieceSwap[counter - 1].path;
		for(i=0; i < pieceInfo.length; i+=1){
			pieceInfo[i].innerHTML = jsonCategories[i] + pieceSwap[counter - 1].information[i];
		}
		imageNumberDisplay.innerHTML = "Image " + (counter) + " of " + (pieceSwapLength);
		
		// Some script to check if we reached the limit and hide the button from adding or subtracting to the counter
		// uncomment to use this feature and comment out 55 and 59 above.
		
		// if(counter === limit){
		// 	whichHide = (limit === 1) ? 0 : 1;
		// 	imageGalleryButtons[whichHide].style.cursor = "auto";
		// 	imageGalleryButtons[whichHide].style.visibility = "hidden";
		// 	return;
		// }
		// for(j=0; j<imageGalleryButtons.length; j+=1) {
		// 	imageGalleryButtons[j].style.cursor = "pointer";
		// 	imageGalleryButtons[j].style.visibility = "visible";
		// }
		
	}
}


