/*
 *  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 = 'gallery';
		this.thumbnailColumns="3";
		this.thumbnailRows="3";
		this.lastThumbImageIndex=0;
		this.lastThumbsOnCurrentPage=0;
		this.thumbsPerPage = this.thumbnailRows * this.thumbnailColumns;
		this.title ='';	
		this.imgcat ='a';
	},
	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;
		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());
		}	
	},
	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());
		}	
	},	
	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());
		if (this.showShoppingCart == 'yes') {
			$('add2cart').setProperty('href', 'javascript:picturetransfer("' + "/" + gallery.albumName + '/' + this.images[0].getFileName() + '");');
		}
		var imCat = new Element("img")
		imCat.setProperty('src', this.getImageBaseURL() + this.imgcat + ".png");
		imCat.setProperty('id', 'imagoCategoria');
		imCat.setProperty('style','filter:progid:DXImageTransform.Microsoft.AlphaImageLoader (src=\'' + this.getImageBaseURL() + this.imgcat + ".png" + '\', sizingMethod="scale");');
		imCat.setProperty('alt', this.title);
		imCat.setProperty('title', this.title);
		$("imagoCategoria").appendChild(imCat);
		
		/*ElementHelper.setInnerHTML('imagoCategoria',"<span " + " title='" + this.title + "' alt='" + this.title + "'"
         + " style='width: 100 px; heigth: 100 px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + this.getImageBaseURL() + this.imgcat + ".png" + "\', sizingMethod=\'scale\');'></span>" );*/
		 
		 ElementHelper.setInnerHTML('imagoCategoria',"<img src='"+ this.getImageBaseURL() + this.imgcat + ".png' title='" + this.title + "' alt='" + this.title + "' />" );
		 
		ElementHelper.setInnerHTML('imagoGalleryTitle',this.title);
		ElementHelper.setInnerHTML('imagoGalleryTitle_b',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()) != null) {
			$(gallery.getCurrentSelection()).removeClass('imago_selectedThumb');
		}
		gallery.setCurrentSelection(this.getID());
		var myFx = new Fx.Style('imagoCurrentImg', 'opacity', {duration:1000, 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));
				ElementHelper.setInnerHTML('imagoCurrentImageTitle',title);
				if(oldSrc == $('imagoCurrentImg').getProperty('src')) {
					$('imagoCurrentImg').fireEvent('load', null, 0);
				}
		}}).custom(1,0);
	},
	fadeIn: function() {
		
		if (this.showShoppingCart == 'yes') {
			$('add2cart').setProperty('href', 'javascript:picturetransfer("' + "/" + gallery.albumName + '/' + fileName + '");');
		}
		ElementHelper.hide('imagoLoading');
		var myFx = new Fx.Style('imagoCurrentImg', 'opacity', {duration:1000}).custom(0,1);
	},	
	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="css/img/add2cart.gif" alt="Bild bestellen" title="Bild bestellen"/></a><br/><span class="imago_shoppinginfo">' +
			this.orderInfo + '</span></div>'
		} 
		var layout = '' + 
			'<div style="float:left;">' +
			'<div class="imago_thumbMenu_top"></div>' +
			'<div id="imagoThumbMenu" class="imago_thumbMenu" ><div align="center" id="imagoCategoria" class="imagoCategoria"></div><div id="imagoGalleryTitle" class="imago_galleryTitle" ></div></div>' +
			'<div class="imago_thumbMenu_bottom"></div>' +
			'</div>' +
			'<div style="float:left;">' +
			'<div class="imago_frame_cap"></div>' +
			'<div class="imago_frame">' +
			'<div class="catego" id="imagoGalleryTitle_b" ></div>' +
			'<div class="imago_cititle"><span id="imagoCurrentImageTitle" class="imago_cititle" ></span></div><br />' + 
			'<div id="imagoCurrentImage" class="imago_currentImage"><img src="css/img/loading.gif" id="imagoLoading" class="imago_loading" />' +
			'<a id="imagoError" class="imago_error" >Gallery definition or Gallery server not available.</a></div>' +
			
			
			cart + 
			'</div><div id="imagoDivLoading" class="imago_loading"><img src="css/img/loading.gif" /><br/>loading...</div>' +
			'<div class="imago_frame_bottom"></div>' +
			'</div>'+
			//'<div class="imago_downloadLink"><a href="http://codeboje.de" target="imagoDownload" style="color: #FFFFFF; font-size: xx-small;">Imago by codeboje.de</a></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) {
		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.imgcat = getAttributeValue(responseXML, 'simpleviewerGallery', 'imgcat');

		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);

