/**
 * fw Slide Gallery
 *
 * Animation, handlers, and invokers for sliding gallery
 *
 * @required Prototype     <prototypejs.org>
 * @required Scriptaculous <http://script.aculo.us/>
 * @required Behaviour     <http://www.bennolan.com/behaviour/>
 *
 * @author Andrew Murphy <andrew@fryewiles.com>
 *
 * @version 1.0.0a 20080111 AM
 */




/*
			description = a.select('.content');

			if ( description && description.length > 0 && description[0].innerHTML ) {
				description = description[0].innerHTML;
			} else {
				description = '';
			}
*/




var slide_gallery = {
	slides: [],

	index:       0,
	iSlideWidth: 0,

	elThumbnails:  null,
	elPhotos:       null,
	elTitle:       null,
	elDescription: null,




	init: function(slideWidth, idElThumbnails, idElPhotos, idElTitle, idElDescription) {
		slide_gallery.iSlideWidth   = slideWidth;
		slide_gallery.elThumbnails  = $(idElThumbnails);
		slide_gallery.elPhotos      = $(idElPhotos);
		slide_gallery.elTitle       = $(idElTitle);
		slide_gallery.elDescription = $(idElDescription);
		slide_gallery.load_slides($$('#'+idElThumbnails+' a'));

		slide_gallery.elPhotos.style.marginLeft = '0px';
		slide_gallery.goto_start();
	},


	behaviours: {
		'#viewer-container #prev-slide a': function(el) {
			el.onclick = function(event) {
				slide_gallery.previous();

				if ( el.blur ) {
					el.blur();
				}
				return false;
			}
		},
		'#viewer-container #next-slide a': function(el) {
			el.onclick = function(event) {
				slide_gallery.next();

				if ( el.blur ) {
					el.blur();
				}
				return false;
			}
		},
		'.slide_gallery_thumbnails a': function(el) {
			el.onclick = function(event) {
				var photoIndex = el.readAttribute('photoindex');
				if (photoIndex != null) {
					slide_gallery.goto_slide(parseInt(photoIndex));
				}

				if ( el.blur ) {
					el.blur();
				}
				return false;
			}
		}
	},



	load_slides:  function(anchors) {
		var i           = 0;
		var photo       = null;
		var index       = 0;

		for ( i in anchors ) {
			if ( anchors.hasOwnProperty(i) ) {
				photo = anchors[i].readAttribute('photo');
				index = slide_gallery.slides.length;

				if (
					photo == null || photo == ''
				) {
					return false;
				}

				anchors[i].writeAttribute('photoindex', index);

				slide_gallery.slides[slide_gallery.slides.length] = anchors[i];
				slide_gallery.add_slide(photo, slide_gallery.iSlideWidth);
			}
		}

		return true;
	},

	add_slide:  function(photo, width) {
		if ( !slide_gallery.elPhotos ) {
			return false;
		}

		var elPhoto = new Element(
			'div',
			{
				'class': 'photo',
				'style': 'background-image: url('+photo+');'
			}
		);

		var container = slide_gallery.elPhotos.select('.container');

		if ( container.length == 0 ) {
			return false;
		}

		container[0].appendChild(elPhoto);
		slide_gallery.elPhotos.style.width = (slide_gallery.iSlideWidth * slide_gallery.slides.length) + 'px';

		return true;
	},




	goto_slide:  function(index) {
		if ( index < 0 || slide_gallery.slides.length == 0 || index >= slide_gallery.slides.length ) {
			return false;
		}

		slide_gallery.index = index;

		var cMarginLeft = parseInt(slide_gallery.elPhotos.style.marginLeft),
			nMarginLeft = (index * slide_gallery.iSlideWidth) * -1,
			delta       = Math.abs(nMarginLeft - cMarginLeft),
			speed       = .33;

		new Effect.Transform(
			[{'slide-photos': "margin-left: " + nMarginLeft + 'px'}],
			{
				duration: speed,
				afterFinish: function() {
					slide_gallery.elTitle.innerHTML = slide_gallery.slides[index].title;

					var container = slide_gallery.slides[index].select('.content');

					if ( container.length == 0 ) {
						return false;
					}
					
					slide_gallery.elDescription.innerHTML = container[0].innerHTML;
				}
			}
		).play();
		
		return true;
	},

	next: function() {
		var newIndex = slide_gallery.index + 1;
		if ( newIndex >= slide_gallery.slides.length ) {
			newIndex = 0;
		}
		return slide_gallery.goto_slide(newIndex);
	},

	previous: function() {
		var newIndex = slide_gallery.index - 1;

		if ( newIndex < 0 ) {
			newIndex = slide_gallery.slides.length - 1;
		}


		return slide_gallery.goto_slide(newIndex);
	},

	goto_start: function() {
		return slide_gallery.goto_slide(0);
	},

	goto_end:   function() {
		return slide_gallery.goto_slide(slide_gallery.slides.length - 1);
	}
};


Event.observe(window, 'load', function() { slide_gallery.init(500, 'thumbs', 'slide-photos', 'slide-title', 'slide-description'); }, false);
Behaviour.register(slide_gallery.behaviours);

