Object.extend(document, {
	loaded: function(callback) {

		if(!document._callbacks) document._callbacks = new Array();
		document._callbacks.push(callback);

		/* for Mozilla/Opera9 */
		if (document.addEventListener) {
		    document.addEventListener("DOMContentLoaded", this._contentLoadedInit, false);
		}

		/* for Internet Explorer */
		/*@cc_on @*/
		/*@if (@_win32)
		    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
		    var script = document.getElementById("__ie_onload");
		    script.onreadystatechange = function() {
			if (this.readyState == "complete") {
			    document._contentLoadedInit(); // call the onload handler
			}
		    };
		/*@end @*/

		/* for Safari */
		if (/WebKit/i.test(navigator.userAgent)) { // sniff
		    document._domTimer = setInterval(function() {
			if (/loaded|complete/.test(document.readyState)) {
			    document._contentLoadedInit(); // call the onload handler
			}
		    }, 10);
		}

		/* for other browsers */
		Event.observe(window, 'load', document._contentLoadedInit);
	},

	_contentLoadedInit: function() {

		if(document.callbackFired) return;
		document.callbackFired = true;

		// kill the timer
		if (document._domTimer) clearInterval(document._domTimer);

		document._callbacks.each(function(f) {

			f()
		});
	}
});




document.loaded( function(){
	$('scroll-gallery-viewport').setStyle({overflow:'hidden'});
	var gallerybuttons = $$('div.scroll-gallery-buttons');
	for (var i = 0; i < gallerybuttons.length; i++)
	{
		gallerybuttons[i].setStyle({display:'block'});
	}

	ScrollerGallery.initGalleries();
});


var ScrollerGallery = function() {

	var GALLERY_BUSY_ANIMATING = false;

	function initGalleries() {
		
		var gallerylist = $('gallery-list').getElementsByTagName('a');
	
		for(var i = 0; i < gallerylist.length; i++)
		{
			Event.observe(gallerylist[i], 'click', function(e){
				$('gallery-image').src = this.getElementsByTagName('img')[0].src;
				$('gallery-image').title = this.getElementsByTagName('img')[0].title;
				$('gallery-image').alt = this.getElementsByTagName('img')[0].alt;
				
				$('active-details').innerHTML = $('details-' + this.id).innerHTML;
				Event.stop(e);
				}.bind(gallerylist[i]));
		}
	
		var galleryupbuttons = $$('a.gallery-scroll-up');
		for(var i = 0; i < galleryupbuttons.length; i++) {
			Event.observe(galleryupbuttons[i], 'click', ScrollerGallery.scrollGalleryUp.bindAsEventListener(galleryupbuttons[i]));
		}
			
		
		var gallerydownbuttons = $$('a.gallery-scroll-down');
		for(var i = 0; i < gallerydownbuttons.length; i++) {
			Event.observe(gallerydownbuttons[i], 'click', ScrollerGallery.scrollGalleryDown.bindAsEventListener(gallerydownbuttons[i]));
		}
	}

	function scrollGalleryDown(e) {
		Event.stop(e);
		var parentcontainer = this.up('#scroll-gallery-container');
		var viewport = parentcontainer.down('#scroll-gallery-viewport');
		
		if (viewport) {
		
			var gallery = viewport.down("#gallery-list");
			
			if (gallery) {
				var totalheight = gallery.getHeight();

				var firstitem = gallery.down('li');
				
				var gallery_image_move_distance = firstitem.getHeight();
				
				var distance = Position.page(gallery)[1] - Position.page(viewport)[1];
				
				if (distance > -totalheight+(gallery_image_move_distance*2) ) {
					if (!ScrollerGallery.getAnimating()) {
						new Effect.Move(gallery, { duration:.75, y: -gallery_image_move_distance, mode: 'relative', queue: 'end', beforeStart: ScrollerGallery.setBusyAnimating, afterFinish: ScrollerGallery.doneAnimating });
					}
				}
			}
		}	
	}

	function scrollGalleryUp(e) {
		Event.stop(e);
		var parentcontainer = this.up('#scroll-gallery-container');
		var viewport = parentcontainer.down('#scroll-gallery-viewport');
		
		if (viewport) {
			var gallery = viewport.down("#gallery-list");
			
			if (gallery) {
				var totalheight = gallery.getHeight();

				var firstitem = gallery.down('li');
				var gallery_image_move_distance = firstitem.getHeight();
				
				var distance = Position.page(gallery)[1] - Position.page(viewport)[1];
				
				if (distance < 0 ) {
					if (!ScrollerGallery.getAnimating()) {
						new Effect.Move(gallery, { duration:.75, y: gallery_image_move_distance, mode: 'relative', queue: 'end', beforeStart: ScrollerGallery.setBusyAnimating, afterFinish: ScrollerGallery.doneAnimating });
					}
				}
			}
		}	
	}
	
	function setBusyAnimating() {
		ScrollerGallery.GALLERY_BUSY_ANIMATING = true;
	}
	
	function getAnimating() {
		return ScrollerGallery.GALLERY_BUSY_ANIMATING;
	}
	
	function doneAnimating() {
		ScrollerGallery.GALLERY_BUSY_ANIMATING = false;
	}
		
	return {
		initGalleries: initGalleries,
		setBusyAnimating: setBusyAnimating,
		getAnimating: getAnimating,
		doneAnimating: doneAnimating,
		scrollGalleryUp: scrollGalleryUp,
		scrollGalleryDown: scrollGalleryDown
	};
}();
