
Pennypacker = {} 
Pennypacker.Slideshow = function(elementId, options) {
  this.init(elementId, options );
  var autoNext = function(){this.next_image()}
}

$.extend(Pennypacker.Slideshow.prototype, {
	fadeSpeed: 500,
	changeFrequency: 2000,
	
	
	init: function(elementId, options) {
     //initialization
		this.container = $(elementId);
		if (!options) {   
			var options = []   
		}	

		//set the image file extension if you want, default .png		
	 	if (options['ext']) {	this.ext = options['ext']; 	}

		//speed at which an image fades in or out.
		if (options['fadeSpeed']) {	this.fadeSpeed = options['fadeSpeed']; }

		//set the frequency that the image changes
		if (options['changeFrequency']) { this.changeFrequency = options['changeFrequency']; }
				
		this.imageCount = this.container.find('img').size();
	 	this.show_image();	
   	},
		
	show_image:function(nthChild) {
		var firstTime = false
		if (!nthChild) {
			var nthChild = 0
			this.selectedIndex = nthChild;
			firstTime = true
		}	
		
		this.container.find('img.visibleSlideshowElement')
			.fadeOut(this.fadeSpeed, function(){$(this).removeClass('visibleSlideshowElement')} ) ;
		if (firstTime) {
			this.container.find('a:nth-child(' + nthChild + ') img').addClass('visibleSlideshowElement');
		} else {
			this.container.find('a:nth-child(' + nthChild + ') img')
				.fadeIn(this.fadeSpeed, function(){$(this).addClass('visibleSlideshowElement')} ) ;	
		}		
	},
	
	next_image:function(){
		
		if ( (this.selectedIndex + 1) > this.imageCount) {
			this.selectedIndex = 1;
			this.stop_auto_play();
		} else {
			this.selectedIndex = this.selectedIndex + 1
		}	
			
		this.show_image(this.selectedIndex);
	},
	
	previous_image:function(){
		
		if ( (this.selectedIndex -1 ) <= 0) {
			this.selectedIndex = this.imageCount;
		} else {
			this.selectedIndex = this.selectedIndex - 1
		}	
			
		this.show_image(this.selectedIndex);
	},
	
	start_auto_play:function(changeFrequency) {		
		 if (changeFrequency) {
			this.changeFrequency = changeFrequency;
		 }	
		
		 var _self = this
		 this.autoPlayer = setInterval(function(){ _self.next_image(); }, this.changeFrequency );		
		

	},	
	
	stop_auto_play:function() {	
		clearInterval(this.autoPlayer);
	}
});