/*
 *  Copyright 2006, 2007 by codeboje.de and author
 *  Name    : Imago 
 *  Version : 0.3b
 *  Author  : Azarai (azarai@codeboje.de)
              http://codeboje.de/blog/pages/impressum.html  
	Terms of Use:
			Imago may be used in any kinds of personal and/or commercial projects. 
			Redistributed or reselling to other companies or third parties is prohibited.
			Please ensure that the Imago download link is clearly visible.
*/

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 = 'images';
		this.thumbnailColumns="1";
		this.thumbnailRows="7";
		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.onmouseover = this.ImgMouseOver.bind(this.images[index]);
				img.onmouseout = this.ImgMouseOut.bind(this.images[index]);
				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;
		if (this.lastThumbImageIndex == 0) {
			//ElementHelper.hide('imagoNavPrevLink');
		} else {
			ElementHelper.show('imagoNavPrevLink');
		}
		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++;
					  if(this.images.length>this.thumbsPerPage) {
						  ElementHelper.show('imagoNavNextLink');
					  }
				  } else {
					  //ElementHelper.hide('imagoNavNextLink');
				  }
				  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());
			$('imagoCurrentThumbTable').effect('opacity', {duration: 4000, wait: true}).start(0,1);
		}
	},
	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());
			$('imagoCurrentThumbTable').effect('opacity', {duration: 4000, wait: true}).start(0,1);
		}
	},
	showGallery: function() {
		var navPrevLink = new Element("a");
		navPrevLink.className = 'imago_navPrev';
		navPrevLink.setProperty('id', 'imagoNavPrevLink');
		navPrevLink.onclick = this.thumbMenuPrev.bind(this);
		this.menuDiv.appendChild(navPrevLink);
		//ElementHelper.hide('imagoNavPrevLink');

		var navNextLink = new Element("a");
		navNextLink.className = 'imago_navNext';
		navNextLink.setProperty('id', 'imagoNavNextLink');
		navNextLink.onclick = this.thumbMenuNext.bind(this);
		this.menuDiv.appendChild(navNextLink);	
		//ElementHelper.hide('imagoNavNextLink');
		
		if (this.images.length>this.thumbsPerPage) {
			ElementHelper.show('imagoNavNextLink');
		}
		this.menuDiv.appendChild(this.getCurrentThumbTable());

		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());
		this.setCurrentSelection(this.images[0].getID());
		this.currentImageDiv.appendChild(img);
	},
	switchImage: function() {
		fileName = this.getFileName();
		title = this.getTitle();
		if ($(gallery.getCurrentSelection()) != null) {
			$(gallery.getCurrentSelection()).removeClass('imago_selectedThumb');
		}
		gallery.setCurrentSelection(this.getID());
		var myFx = new Fx.Styles('imagoCurrentImg', {duration:1000,transition: Fx.Transitions.linear, onComplete: function() {
				oldSrc = $('imagoCurrentImg').getProperty('src');
				$('imagoCurrentImg').setProperty('src', gallery.getImageBaseURL() + fileName);
				$('imagoCurrentImg').setProperty('alt', title);
				$('imagoCurrentImg').setProperty('title', title);
				$('imagoCurrentImg').addEvent('load', gallery.fadeIn.bind(this));
				if(oldSrc == $('imagoCurrentImg').getProperty('src')) {
					$('imagoCurrentImg').fireEvent('load', null, 0);
				}
		}}).start({
				'margin-left': [0,-500]
		}).chain(function(){ ElementHelper.show('imagoLoading'); });
	},
	ImgMouseOver: function() {
		var thumb_img_id = this.getID();
		new Fx.Styles($(thumb_img_id), {wait: false, duration: 600}).start({
			'border-top-color': '#000000',
			'border-right-color': '#000000',
			'border-bottom-color': '#000000',
			'border-left-color': '#000000'
		});
	},
	ImgMouseOut: function() {
		var thumb_img_id = this.getID();
		new Fx.Styles($(thumb_img_id), {wait: false, duration: 600}).start({
			'border-top-color': '#FFFFFF',
			'border-right-color': '#FFFFFF',
			'border-bottom-color': '#FFFFFF',
			'border-left-color': '#FFFFFF'
		});
	},
	fadeIn: function() {
		ElementHelper.hide('imagoLoading');
		new Fx.Styles('imagoCurrentImg', {duration:1000,transition: Fx.Transitions.linear}).start({
				'margin-left': [-500,0]
		});
	},	
	getCurrentSelection: function() {
		return this.selection;
	},
	setCurrentSelection: function(selection) {
		this.selection = selection;
		$(this.selection).addClass('imago_selwidthectedThumb');
	},
	createLayout: function() {
		eval($('imagogallery').innerHTML);
		var layout = '' + 
			'<div class="imago_frame">' +
			'<div id="imagoCurrentImage" class="imago_currentImage"><img src="images/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="imagoThumbMenu" class="imago_thumbMenu" ></div>';
		
		ElementHelper.setInnerHTML('imagogallery', layout);
	}
});

function loadGalleryXML() {
	try {
		var myAjax = new Ajax(gallery.getAlbumBaseURL() + 'gallery.xml', {method: 'get', onComplete: parseGalleryXMLResponse}).request();
	} catch(e) {
		ElementHelper.show('imagoError');
	}
};

function parseGalleryXMLResponse(responseText, responseXML) {
	try {
	    var images = responseXML.getElementsByTagName('image');
	    countArticle = 0;
		for (var i=0;i<images.length;i++) {
			caption = getNodeValue(images[i], 'caption');
			if (caption == null) {
				caption = "&nbsp;";
			}
			gallery.addImage(new GalleryImage(getNodeValue(images[i], 'filename'),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.showGallery();
	} catch(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) {
	child = obj.getElementsByTagName(tag)[0].firstChild;
	if(child != null) {
		return child.nodeValue;
	} 
	return null;
}

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);
