/**
 * fw Slide Gallery
 *
 * This version is specific for GoldenVoice.
 *
 * @required Prototype     <prototypejs.org>
 * @required Scriptaculous <http://script.aculo.us/>
 * @required Behaviour     <http://www.bennolan.com/behaviour/>
 *
 * @author Justin Johnson <justin@fryewiles.com>, justin@booleangate.org
 * @author Andrew Murphy <andrew@fryewiles.com>
 *
 * @version 2.0.8 20080326 JJ
 * @version 2.0.7 20080319 JJ
 * @version 2.0.1 20080305 JJ
 * @version 1.0.0 20080111 AM
 */

var slide_gallery = {
	slides:       [],
	index:        0,
	iSlideWidth:  0,
	duration:     0.33,
	pause:        3000,
	timeoutID:    null,
	fields: {
		gallery:      null,
		slide:        null,
		container:    null,
		image:        null,
		title:        null,
		venue:        null,
		date:         null,
		linkElements: []
	},


	init: function(slideWidth, container, fields, dataURL, duration, pause) {
		slide_gallery.iSlideWidth = slideWidth;
		slide_gallery.duration    = duration;
		slide_gallery.pause       = pause;

		slide_gallery.fields = {
			gallery:      $(fields.gallery),
			slide:        $(fields.slide),
			container:    $(fields.container),
			image:        $(fields.image),
			title:        $(fields.title),
			venue:        $(fields.venue),
			date:         $(fields.date),
			linkElements: $(container).getElementsByClassName(fields.groupLink)
		}
		
		slide_gallery.loadSlides();
		slide_gallery.goto_slide(1);
	},
	
	
	loadSlides: function() {
		var count = featuredJSON.length;
		
		slide_gallery.slides = new Array(count+2);
			
		slide_gallery.fields.container.innerHTML = '';
		
		for ( var i=0; i<count; ++i ) {
			slide_gallery.slides[i+1] = {
				id:    featuredJSON[i].id,
				title: featuredJSON[i].title,
				venue: featuredJSON[i].venue,
				image: featuredJSON[i].image,
				date:  featuredJSON[i].date
			};
		}
		
		// Copy the first after the last and the last before the first
		slide_gallery.slides[count+1] = slide_gallery.slides[1];
		slide_gallery.slides[0]     = slide_gallery.slides[count];
		
		// Add all of the photos to the page
		count += 2;
		
		for ( var i=0; i<count; ++i ) {
			photo = slide_gallery.slides[i];
			
			slide_gallery.fields.container.appendChild(
				new Element(
					'a',
					{
						'class': 'photo',
						'style': 'background-image: url('+photo.image+');',
						'href':  slide_gallery.detailsLink(photo.id),
						'title': "Click for event information for " + photo.title.replace(/\"/g, '')
					}
				)
			);
		}
		
		slide_gallery.fields.slide.style.width = (slide_gallery.iSlideWidth * count) + 'px';
		slide_gallery.fields.gallery.style.display = 'block';
	},

	behaviours: {
		'.slide-gallery .previous': function(el) {
			el.onclick = function(event) {
				slide_gallery.previous();

				if ( el.blur ) {
					el.blur();
				}
				return false;
			}
		},
		
		'.slide-gallery .next': 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;
			}
		}
	},

	goto_slide:  function(index) {
		clearTimeout(slide_gallery.timeoutID);
		
		if ( slide_gallery.slides.length == 0 ) return false;
		
		
		slide_gallery.index = index;

		var cMarginLeft = slide_gallery.fields.image.style.marginLeft;
		cMarginLeft = cMarginLeft.substr(0, cMarginLeft.length-2);
		cMarginLeft = parseInt(cMarginLeft);

		var nMarginLeft = (index * slide_gallery.iSlideWidth) * -1,
			delta = Math.abs(nMarginLeft - cMarginLeft);

		var transition_complete = function() {
			slide_gallery.fields.title.innerHTML = slide_gallery.slides[index].title;
			slide_gallery.fields.venue.innerHTML = slide_gallery.slides[index].venue;
			slide_gallery.fields.date.innerHTML  = slide_gallery.slides[index].date;
			slide_gallery.setLinks();
			
			slide_gallery.timeoutID = setTimeout(slide_gallery.next, slide_gallery.pause);
			
			// Determine if we need to wrap to the nautral beginning/ending
			var wrap = false;
			
			// Is at the beginning
			if ( index == 0 ) {
				wrap = true;
				slide_gallery.index  = slide_gallery.slides.length - 2;
			}

			// Is at the end	
			else if ( index == slide_gallery.slides.length - 1 ) {
				wrap = true;
				slide_gallery.index  = 1;
			}
			
			if ( wrap ) {
				slide_gallery.fields.slide.style.marginLeft = ((slide_gallery.index  * slide_gallery.iSlideWidth) * -1) + 'px';
			}
		}

		new Effect.Transform(
			[{'slide-photos': "margin-left: " + nMarginLeft + 'px'}],
			{
				duration: slide_gallery.duration,
				afterFinish: transition_complete
			}
		).play();

		return true;

	},

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

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

		if ( newIndex < 0 ) {
			return slide_gallery.goto_end();
		}


		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);
	},
	
	setLinks: function() {
		var c = slide_gallery.fields.linkElements.length;
		for ( var i=0; i<c; ++i ) {
			slide_gallery.fields.linkElements[i].href = slide_gallery.detailsLink(slide_gallery.slides[slide_gallery.index].id);
		}
	},
	
	detailsLink: function(id) {
		return "/shows/details/?id=" + id;
	}
};


Event.observe(
	window, 
	'load', 
	function() {
		slide_gallery.init(
			322, 
			'featured-event',
			{
				gallery:   'featured-event',
				slide:     'slide-photos',
				container: 'slide-container',
				image:     'featured-image',
				title:     'featured-name',
				venue:     'featured-venue',
				date:      'featured-date',
				groupLink: 'featured-link'
			},
			{
				service:     'http://server.local:81/data/homeFeaturedEvents.php'
			},
			0.25,
			4500
		);
	}, 
	false
);

Behaviour.register(slide_gallery.behaviours);



