
var GalleryImage = new Class({
	initialize: function(fileName, title) {
		this.fileName = fileName;
		this.title = title;
	},
	getFileName: function() {
		return this.fileName;
	},
	getTitle: function() {
		return this.title;
	},
	getID: function() {
		return 'id_' + this.getFileName();
	}
});

var Gallery = new Class({
	initialize: function() {
		this.createLayout();
		this.currentImageDiv = $('imagoCurrentImage');
		this.menuDiv = $('imagoThumbMenu');
		this.images = new Array();
		this.baseURL = 'gallery';
		this.thumbnailColumns="6";
		this.thumbnailRows="1";
		this.lastThumbImageIndex=0;
		this.lastThumbsOnCurrentPage=0;
		this.thumbsPerPage = this.thumbnailRows * this.thumbnailColumns;
		this.title ='';				
	},
	addImage: function(img) {
		var counter=0;
		if (this.images.length !=0) {
			counter = this.images.length;
		}
		this.images[counter]=img;
	},
	getAlbumBaseURL: function() {
		if(this.albumName =='') {
			return '';
		}
		return this.baseURL + "/" + this.albumName + "/";
	},	
	getThumbBaseURL: function() {
		return this.getAlbumBaseURL() + 'thumbnails/';
	},
	getImageBaseURL: function() {
		return this.getAlbumBaseURL() + 'images/';
	},	
	getThumbImage: function(index) {
		if(this.images != null && this.images.length != 0 && index <= this.images.length) {
			if(this.images[index] != null) {
				var img = new Element("img");
				img.setProperty('src',  this.getThumbBaseURL() + this.images[index].getFileName());
				img.addClass('imago_thumbImg');
				img.setProperty('alt',  this.images[index].getTitle());
				img.setProperty('id', this.images[index].getID());
				img.onclick = this.switchImage.bind(this.images[index]);
				return img;			
			}
		}
		return null;
	},
	getCurrentThumbTable: function() {
		var thumbTable = new Element("table");
		var thumbTableBody = new Element("TBODY");		
		 
		thumbTable.setProperty('id', 'imagoCurrentThumbTable');
		thumbTable.setProperty('class', 'imago_currentThumbTable');
		var counter = this.lastThumbImageIndex;
		this.lastThumbsOnCurrentPage =0;
		for(i=0;i<this.thumbnailRows;i++) { 
		  var tr = new Element("tr");
		  		for(j=0;j<this.thumbnailColumns;j++) { 
				  var td = new Element("td");
				  if (this.getThumbImage(counter) != null) {
					  td.appendChild(this.getThumbImage(counter));
					  counter++;
					  this.lastThumbsOnCurrentPage++;
				  }
				  tr.appendChild(td);
			 	}
				
		  thumbTableBody.appendChild(tr);
	 	}
	 	thumbTable.appendChild(thumbTableBody);
	 	this.lastThumbImageIndex = counter;
	 	return thumbTable;
	},
	thumbMenuNext: function() {
		if (this.images.length>this.lastThumbImageIndex) {
			$('imagoCurrentThumbTable').remove();
			this.menuDiv.appendChild(this.getCurrentThumbTable());
		}	
	},
	thumbMenuPrev: function() {
		if(this.lastThumbImageIndex > this.thumbsPerPage) {
			this.lastThumbImageIndex -= (this.lastThumbsOnCurrentPage + this.thumbsPerPage);
			if(this.lastThumbImageIndex<0) {
				this.lastThumbImageIndex=0;
			}
			$('imagoCurrentThumbTable').remove();
			this.menuDiv.appendChild(this.getCurrentThumbTable());
		}	
	},	
	showImage: function() {
		this.menuDiv.appendChild(this.getCurrentThumbTable());
		if (this.images.length>this.lastThumbImageIndex) {
			var navPrevLink = new Element("a");
			navPrevLink.className = 'imago_navPrev';
			navPrevLink.onclick = this.thumbMenuPrev.bind(this);
			this.menuDiv.appendChild(navPrevLink);

			var navNextLink = new Element("a");
			navNextLink.className = 'imago_navNext';
			navNextLink.setProperty('id', 'imagoNavNextLink');
			navNextLink.onclick = this.thumbMenuNext.bind(this);
			this.menuDiv.appendChild(navNextLink);			
		}
		var img = new Element("img");
		img.setProperty('src', this.getImageBaseURL() + this.images[0].getFileName());
		img.setProperty('id', 'imagoCurrentImg');
		img.setProperty('alt', this.images[0].getTitle());
		img.setProperty('title', this.images[0].getTitle());

		if (this.showShoppingCart == 'yes') {
			$('add2cart').setProperty('href', 'javascript:picturetransfer("' + "/" + gallery.albumName + '/' + this.images[0].getFileName() + '");');
		}
		ElementHelper.setInnerHTML('imagoGalleryTitle',this.title);		
		ElementHelper.setInnerHTML('imagoCurrentImageTitle',this.images[0].getTitle());
		this.setCurrentSelection(this.images[0].getID());
		this.currentImageDiv.appendChild(img);
		ElementHelper.hide('imagoDivLoading');
	},
	switchImage: function() {
		ElementHelper.show('imagoLoading');
		fileName = this.getFileName();
		title = this.getTitle();
		if ($(gallery.getCurrentSelection()) != false) {
			$(gallery.getCurrentSelection()).removeClass('imago_selectedThumb');
		}
		gallery.setCurrentSelection(this.getID());
		var myFx = new Fx.Style('imagoCurrentImg', 'opacity', {duration:1000, onComplete: function() {
				$('imagoCurrentImg').setProperty('src', gallery.getImageBaseURL() + fileName);
				$('imagoCurrentImg').setProperty('alt', title);
				$('imagoCurrentImg').setProperty('title', title);
				if (this.showShoppingCart == 'yes') {
					$('add2cart').setProperty('href', 'javascript:picturetransfer("' + "/" + gallery.albumName + '/' + fileName + '");');
				}
				ElementHelper.setInnerHTML('imagoCurrentImageTitle',title);
				ElementHelper.hide('imagoLoading');
				var myFx = new Fx.Style('imagoCurrentImg', 'opacity', {duration:1000}).custom(0,1);
		}}).custom(1,0);
	},
	getCurrentSelection: function() {
		return this.selection;
	},
	setCurrentSelection: function(selection) {
		this.selection = selection;
		$(this.selection).addClass('imago_selectedThumb');
	},
	createLayout: function() {
		eval($('imagogallery').innerHTML);
		var cart = '';
		if (this.showShoppingCart == 'yes') {
			cart = '<div class="imago_cart"><a class="imago_cart" id="add2cart"><img src="styles/img/add2cart.gif" alt="Bild bestellen" title="Bild bestellen"/></a><br/><span class="imago_shoppinginfo">' +
			this.orderInfo + '</span></div>'
		} 
		var layout = '' + 
			
			'<div class="imago_frame">' +
			'<div id="imagoCurrentImage" class="imago_currentImage"><img src="styles/img/loading.gif" id="imagoLoading" class="imago_loading" />' +
			'<a id="imagoError" class="imago_error" >Gallery definition or Gallery server not available.</a></div>' +
		
			'</div><div id="imagoDivLoading" class="imago_loading"><img src="styles/img/loading.gif" /><br/>loading...</div>'+
		'<div id="imagoThumbMenu" class="imago_thumbMenu" ><div id="imagoGalleryTitle" class="imago_galleryTitle" ></div></div>' ;
		ElementHelper.setInnerHTML('imagogallery', layout);
	}
});

function loadGalleryXML() {
	try {
		ElementHelper.show('imagoDivLoading');
		ElementHelper.setOpacity('imagoDivLoading', 0.5);
		var myAjax = new Ajax(gallery.getAlbumBaseURL() + 'gallery.xml', {method: 'get', onComplete: parseGalleryXMLResponse}).request();
	} catch(e) {
		console.log(e);
		ElementHelper.show('imagoError');
	}
};

function parseGalleryXMLResponse(responseText, responseXML) {
	try {
	    var images = responseXML.getElementsByTagName('image');
	    countArticle = 0;
		for (var i=0;i<images.length;i++) {
			gallery.addImage(new GalleryImage(getNodeValue(images[i], 'filename'), getNodeValue(images[i], 'caption')));
		}
		gallery.thumbnailColumns=getAttributeValue(responseXML, 'simpleviewerGallery', 'thumbnailColumns');
		gallery.thumbnailRows=getAttributeValue(responseXML, 'simpleviewerGallery', 'thumbnailRows');
		gallery.thumbsPerPage = gallery.thumbnailRows * gallery.thumbnailColumns;
		gallery.title = getAttributeValue(responseXML, 'simpleviewerGallery', 'title');
		gallery.showImage();
	} catch(e) {
		console.log(e);
		ElementHelper.show('imagoError');
	}
};

//inspired by mooshow
var ImagoElement = new Class({
    initialize: function(){
    
    },
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	getHeight: function(element) {
	   	element = $(element);
	   	return element.offsetHeight; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	},	
	hide: function(element) {
      	element = $(element);
      	element.style.display = 'none';
  	},
  	show: function(element) {
      	element = $(element);
      	element.style.display = 'inline';
  	},
	setOpacity: function(element,opacity) {
    	element = $(element);
    	element.style.opacity = opacity; 
	}
});

var ElementHelper = new ImagoElement();

function getNodeValue(obj,tag) {
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}

function getAttributeValue(obj,tag, attr) {
	return obj.getElementsByTagName(tag)[0].getAttribute(attr);
}
//inspired by mooshow
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
var gallery;
function start() {
	gallery = new Gallery();
	loadGalleryXML();
}

addLoadEvent(start);