/*
---
script: gallery.js
decription: Gallery - MooTools-based gallery
license: MIT-style license.
authors:
- Gerrit Boettcher, fresh Frames GmbH & Co. KG (http://www.freshframes.com/)
requires: [ ReMooz ]
core:1.2.3:
- Class.Extras
- Element.Event
- Element.Style
- Element.Dimensions
- String
- Array
provides: [Gallery]
...
*/
var Gallery = new Class({
	
	Implements: [ Events, Options ],
	
	options: {
		framework: 'remooz', // remooz // slimbox
		galleryDiv: 'gallery',
		tagName:	'a',
		className:	'galaLink',
		imagesRow: 4,
		spinnerDiv: 'spinner',
		spinner:	'spinner.gif' // path to the spinner 
	},
	
	initialize: function( url, options ) {
		this.setOptions( options );
		
		this.url = url;
		
		this.galleryDiv = $( this.options.galleryDiv );
		
		this.imageCount = 0;
		this.currentPage = 1;
		this.pageCount = 1;
		
		this.currentPageImagesTo = this.currentPage * 20;
		this.currentPageImagesFrom = this.currentPageImagesTo - 20;
				
		this.chain = null;
		this.nextPage = false;
		this.prevPage = false;
		
		this.build();
	},
	
	build: function() {
		
		var linkClass = this.options.className;
		var tagName = this.options.tagName;
		
		$each( $$( tagName + '.' + linkClass ), function( item, index ) { this.buildAlbum( item ); }.bind( this ) );
		
	},
	
	buildAlbum: function( item ) {
		item.addEvent( 'click', function( event ) { this.albumOnClick( event, item ); }.bind( this ) );
	},
	
	albumOnClick: function( e, item ) {
		e.preventDefault();
		
		// enable spinner
		if( $( this.options.spinnerDiv ) ) {
			$( this.options.spinnerDiv ).expandAbbr( 'img#tmp_spinner^src=' + this.options.spinner + ',border=0,styles=width:16px;height:16px;margin:0;' );
		}

		var request = new Request.JSON( {
			url: this.url + '?album=' + item.title,
			method: 'get',
			onSuccess: function( response ) {
				if( response.result ) {
					this.galleryDiv.empty();
					$each( $$( '.deleteOnAjax' ), function( delItem ) {
						delItem.destroy();
					});
					new Element( 'h1' ).set( 'html', response.album.title ).inject( this.galleryDiv );
					new Element( 'p' ).set( 'html', response.album.desc ).inject( this.galleryDiv );
					
					var images = response.album.images;
					if( images.length > -1 ) {
						$each( images, function( image, index ) {
							this.addImage( image, index );
						}.bind( this ));
						
						if( this.options.framework == 'remooz' ) {
							ReMooz.assign('div.galerie_bild a', {
								centered: true,
								origin: 'img'
							});
						} else {
							$$( 'div.galerie_bild a' ).slimbox();
						}
					}
				}
			}.bind( this ),
			onComplete: function() {
				if( $('tmp_spinner') ) $('tmp_spinner').destroy();
			}
		}).get();
	},
	
	addImage: function( image, index ) {
		var fullpath = image.fullpath;
		var thumbnail = image.thumb;
		var title = image.title;
		
		var wrap 		= new Element( 'div', { 'class': 'galerie_block' } );
		var imgdiv   	= new Element( 'div', { 'class': 'galerie_bild' } ).inject( wrap );
		var aimgbild 	= new Element( 'a', { 
			'href': fullpath,
			'title': title,
			'alt': title
		}).set( 'html', '<img src="' + thumbnail + '" alt="' + title + '" title="' + title + '" border="0" />' ).inject( imgdiv );
		var titlediv 	= new Element( 'div', { 'class': 'galerie_titel' } ).inject( imgdiv );
		var titlep   	= new Element( 'p' ).set( 'html', title ).inject( titlediv );
		
		if( index > 1 && index % this.options.imagesRow == 0 ) {
			wrap.setStyle( 'clear', 'left' );
		}
		
		wrap.inject( this.galleryDiv );
	}
		
});
/*
var imgCount = 0, currentPage = 1, myChain = null;
var nextPage = false, prevPage = false, pageCount = 1;
var toImg = currentPage * 20, fromImg = toImg - 20;

var addImage = function(image) {
	
	var rahmen   = new Element('div', {'class':'galerie_block'});
	var imgdiv   = new Element('div', {'class':'galerie_bild'}).injectInside(rahmen);
	var aimgbild = new Element('a', {'href':image.replace(/-s/i,''),'rel':'lightbox[album]'}).setHTML('<img src="' + image + '" border="0">').injectInside(imgdiv);

	var titlediv = new Element('div', {'class':'galerie_titel'}).injectAfter(imgdiv);
	var titlep   = new Element('p').setHTML('&nbsp;').injectInside(titlediv);
	
	rahmen.injectInside($('galerie'));
}

$each ( $$('a.galaLink'), function(item, index) {
	$(item).addEvent('click', function(e) {
		ahref = $(item).target;
		galName = $(item).title;
		//alert($(item).title);
		$('galerie').setStyle('display', 'none');
		_this = e;
		var jsonreq = new Json.Remote(ahref, {
			method: 'get',
			autoCancel: true,
			onFailure: function(){
				$('goBack').innerHTML = '<p>Die Daten konnten nicht geladen werden!</p>';
			},
			onComplete: function(object) {
				if (object.desc == 'default') object.desc = '';
				
				$('galerie').innerHTML = '';
				$('galDesc').innerHTML = object.desc;
				
				var images = object.images;
				
				nextPage = false;
				prevPage = false;
				pageCount = 1;
				imgCount = 0;
				toImg = currentPage * 20;
				fromImg = toImg - 20; 
				$each ( images, function(image) {
					if ( images.length > 25 ) {
						if (imgCount >= fromImg && imgCount <= toImg) {
							addImage(image);
						} else {
							if (imgCount < fromImg) prevPage = true;
							if (imgCount > toImg) nextPage = true; 
							pageCount = images.length / 20;
							pageCount = pageCount.toFixed(0);
						}
						imgCount = imgCount + 1;
					} else {
						addImage(image);
						imgCount = imgCount + 1;
					}

				});
				
				$('galerie').setStyle('display', 'block');
				//$('goBack').innerHTML = '<p>' + galName + ' | <a href="galerie.php">Back to Gallery</a></p>';
				naviStr = '<p>Seite <strong>' + currentPage + '</strong> von ' + pageCount;
				if (prevPage || nextPage) naviStr += ' | ';
				if (prevPage) {
					naviStr += '<a href="#" id="viewPrev">previous page</a> ';
				}
				if (nextPage) {
					naviStr += '<a href="#" id="viewNext">next page</a> ';
				}
				$('pageNavi').innerHTML = naviStr + '</p>';
				
				// damits unten auch wiedergegeben wird + funktionsfähig ist alles nochma
				naviStr = '<p>Page <strong>' + currentPage + '</strong> of ' + pageCount;
				if (prevPage || nextPage) naviStr += ' | ';
				if (prevPage) {
					naviStr += '<a href="#" id="viewPrev2">previous page</a> ';
				}
				if (nextPage) {
					naviStr += '<a href="#" id="viewNext2">next page</a> ';
				}
				$('pageNavi2').innerHTML = naviStr + '</p>';
				
				
				// und nochma lustig abfragen + events definieren
				if (prevPage) {
					$('viewPrev').addEvent('click', function(e) {
						currentPage = currentPage - 1;
						$(item).fireEvent('click', _this);
						new Event(e).stop();
					});
					$('viewPrev2').addEvent('click', function(e) {
						currentPage = currentPage - 1;
						$(item).fireEvent('click', _this);
						new Event(e).stop();
					});
				}
				if (nextPage) {
					$('viewNext').addEvent('click', function(e) {
						currentPage = currentPage + 1;
						$(item).fireEvent('click', _this);
						new Event(e).stop();
					});
					$('viewNext2').addEvent('click', function(e) {
						currentPage = currentPage + 1;
						$(item).fireEvent('click', _this);
						new Event(e).stop();
					});
				}
				setTimeout('Lightbox.init();', 500);
				$('goBack').innerHTML = '<p><strong>' + galName + '</strong> | <a href="galerie.php">Back to Gallery</a></p>';
			
			}
		}).send();
		
		new Event(e).stop();
	});
});
*/
