SPARCslideshowContent = {
	shows: {},
	init: function(id,options,json){
		this.shows[id] = {};
		this.shows[id].options = options.evalJSON();
		this.shows[id].timer = false;
		var show = $(id);
		this.shows[id].frames = show.select('.slideshow-content-frame-wrapper').length;
		this.setHandlers(id);
		if (this.shows[id].options.animate) {
			SPARCslideshowContent.shows[id].timer = setTimeout(function(){
				SPARCslideshowContent.autoswap(id);
			},(SPARCslideshowContent.shows[id].options.duration * 1000));
		};
	},
	// if we're autoanimating, this runs periodically
	autoswap: function(id){
		SPARCslideshowContent.clearHandlers(id);
		var li = $(id).select('li.active')[0],
			oldindex = li.id.split('-').pop() * 1,
			newindex = ((oldindex + 1) > (SPARCslideshowContent.shows[id].frames - 1)) ? 0 : oldindex + 1;
		this.effects(id,oldindex,newindex);
		if (SPARCslideshowContent.shows[id].options.animate) {
			clearTimeout(SPARCslideshowContent.shows[id].timer);
			SPARCslideshowContent.shows[id].timer = setTimeout(function(){
				SPARCslideshowContent.autoswap(id);
			},(SPARCslideshowContent.shows[id].options.duration * 1000));
		}
	},
	clearHandlers: function(id){
		$$('#' + id + '>.slideshow-content-nav li a', '#' + id + '>.slide-nav a').each(function(link){
			link.stopObserving();
		});
	},
	setHandlers: function(id){
		SPARCslideshowContent.clearHandlers(id);
		if (this.shows[id].options.show_indicators) {
			$$('#' + id + '>.slideshow-content-nav li a').each(function(link){
				link.observe('click',SPARCslideshowContent.jumpToSlide);
			});
		};
		if (this.shows[id].options.show_nav) {
			$$('#' + id + '>.slide-nav a').each(function(link){
				link.observe('click',SPARCslideshowContent.prevNext);
			});
		};
	},
	// effects handles animation routines
	effects: function(id,oldindex,newindex,which){
		var oldli = $$('#' + id + '>.slideshow-content-nav li')[oldindex],
			newli = $$('#' + id + '>.slideshow-content-nav li')[newindex],
			oldDiv = $(id).select('.slideshow-content-frame-wrapper')[oldindex],
			newDiv = $(id).select('.slideshow-content-frame-wrapper')[newindex];
		switch (SPARCslideshowContent.shows[id].options.transition) {
			case 'fadeout':
				Effect.Fade(oldDiv,{ duration: 0.5,
					afterFinish: function(){
						Effect.Appear(newDiv,{ duration: 0.5,
							beforeStart: function(){
								oldli.removeClassName('active');
								newli.addClassName('active');
								oldDiv.removeClassName('active');
							},
							afterFinish: function(){
								newDiv.addClassName('active');
								SPARCslideshowContent.setHandlers(id);
							}
						});
					}
				});
				break;
			case 'fadein':
				newDiv.setStyle({zIndex:100});
				oldDiv.setStyle({zIndex:50});
				Effect.Appear(newDiv,{ duration: 0.5,
					beforeStart: function(){
						oldli.removeClassName('active');
						newli.addClassName('active');
						oldDiv.removeClassName('active');
					},
					afterFinish: function(){
						newDiv.addClassName('active');
						SPARCslideshowContent.setHandlers(id);
					}
				});
				break;
			case 'slide-out':
				newDiv.makePositioned();
				switch (which) {
					case 'prev':
						newDiv.setStyle({'left': '-' + newDiv.getDimensions().width + 'px' });
						newDiv.show();
						new Effect.Parallel([
							new Effect.Morph(newDiv,{
								style:'left:' + oldDiv.getStyle('left'),
								transition: Effect.Transitions.sinoidal,
								beforeStart:function(){
									oldli.removeClassName('active');
									newli.addClassName('active');
									oldDiv.removeClassName('active');
								}
							}),
							new Effect.Morph(oldDiv,{
								style:'left:' + (oldDiv.getDimensions().width + (oldDiv.positionedOffset()[0] * 2)) + 'px',
								transition: Effect.Transitions.sinoidal,
								afterFinish: function(){
									oldDiv.hide();
									newDiv.addClassName('active');
									SPARCslideshowContent.setHandlers(id);
								}
							})
						]);
						break;
					case 'next':
						newDiv.setStyle({'left': (oldDiv.positionedOffset()[0] * 2) + oldDiv.getDimensions().width + 'px' });
						newDiv.show();
						new Effect.Parallel([
							new Effect.Morph(newDiv,{
								style:'left:' + oldDiv.positionedOffset()[0] + 'px',
								transition: Effect.Transitions.sinoidal,
								beforeStart:function(){
									oldli.removeClassName('active');
									newli.addClassName('active');
									oldDiv.removeClassName('active');
								}
							}),
							new Effect.Morph(oldDiv,{
								style:'left: -' + (oldDiv.getDimensions().width + oldDiv.positionedOffset()[0]) + 'px',
								transition: Effect.Transitions.sinoidal,
								afterFinish: function(){
									oldDiv.hide();
									newDiv.addClassName('active');
									SPARCslideshowContent.setHandlers(id);
								}
							})
						]);
						break;
				}
			}
	},
	// handle clicking on a specific tab
	jumpToSlide: function (event) {
		var li = Event.element(event).up('li'),
			id = li.up('.slideshow-content').id;
		SPARCslideshowContent.clearHandlers(id);
		if (Event.element(event).up('li').hasClassName('active')) {
			SPARCslideshowContent.setHandlers(id);
			return;
		}
		var active = $(id).down('div.slideshow-content-frame-wrapper.active');
		if (!active) {
			SPARCslideshowContent.setHandlers(id);
			return;
		}
		var oldindex = active.id.split('-').pop() * 1,
			newindex = li.id.split('-').pop();
		SPARCslideshowContent.effects(id,oldindex,newindex,'next');
		if (SPARCslideshowContent.shows[id].options.animate) {
			clearTimeout(SPARCslideshowContent.shows[id].timer);
			SPARCslideshowContent.shows[id].timer = setTimeout(function(){
				SPARCslideshowContent.autoswap(id);
			},(SPARCslideshowContent.shows[id].options.duration * 3000));
		}
	},
	// handle clicks on previous / next links
	prevNext: function(event){
		var div = Event.element(event).up('div'),
			id = div.up('.slideshow-content').id,
			active = $(id).down('div.slideshow-content-frame-wrapper.active');
		SPARCslideshowContent.clearHandlers(id);
		switch (div.hasClassName('prev')) {
			case true:
				var oldindex = active.id.split('-').pop() * 1,
					newindex = ((oldindex - 1) >= 0) ? oldindex - 1 : (SPARCslideshowContent.shows[id].frames - 1);
				SPARCslideshowContent.effects(id,oldindex,newindex,'prev');
				break;
			case false:
				var oldindex = active.id.split('-').pop() * 1,
					newindex = ((oldindex + 1) > (SPARCslideshowContent.shows[id].frames - 1)) ? 0 : oldindex + 1;
				SPARCslideshowContent.effects(id,oldindex,newindex,'next');
				break;
		}
		if (SPARCslideshowContent.shows[id].options.animate) {
			clearTimeout(SPARCslideshowContent.shows[id].timer);
			SPARCslideshowContent.shows[id].timer = setTimeout(function(){
				SPARCslideshowContent.autoswap(id);
			},(SPARCslideshowContent.shows[id].options.duration * 3000));
		}
	}
}
