/*
 * Author: Levi Jackson
 * Date: 6/17/2011
 * Description: A very simple function to do a fade in/out slideshow
 */

(function($){

			jQuery.fn.bbamslideshow = function(options){

				var settings = {
					'time_interval' : 1000
				}

				 if ( options ) {
			        $.extend( settings, options );
			      }


                               var container = this.attr('id'); // main container id
                               
                               // Add links to paginate
                               $('#'+container).append('<div id="controls"></div>');
                               var controls_html = '';
                               $('#'+container+' img').each(function(index,Element){
                                   controls_html += '<a href="#">'+index+'</a>';
                               });
                               $('#controls').html(controls_html);

                               // set the first image as .selected if none are set
                               if( $('#'+container+' .selected').length == '0' ){
                                   $('#'+container+' img:eq(0)').addClass('selected')
                               }

                               // set the first item as selected
                               $('#controls a:eq(0)').addClass('selected');

								$('#'+container+' img').not('.selected').hide(); //hide all of the images except the .selected one
								
                               // function to swap the images
                               function changeImage(container, start){
                                  if(start == 'null'){ // starts from .selected if null
                                       var current_img = $('#'+container+' img.selected');
                                        $('#'+container+' img').not('.selected').hide(); //hide all of the images except the .selected one

                                  if(current_img.next('img').attr('src') != undefined)
                                       current_img.next('img').addClass('selected').hide().fadeIn('slow');
                                  else
                                       $('#'+container+' img:eq(0)').addClass('selected').hide().fadeIn('slow');

                                   current_img.removeClass('selected').fadeOut('slow'); // remove the selected class from the old one
                                   }
                                   else{ // starts from index if higher than 0
                                        $('#'+container+' img.selected').removeClass('selected').fadeOut('slow');
                                        $('#'+container+' img:eq('+start+')').addClass('selected').hide().fadeIn('slow');
                                    }


                                     //swap out active link
                                     $('#controls a.selected').removeClass('selected');
                                     $('#controls a:eq('+$('.selected').index()+')').addClass('selected').fadeIn('slow');
                               } // end changeImage function


                              

                                    var interval_slideshow = setInterval(
                                          (function(){
                                            changeImage( container,'null' );
                                           })
                                    ,settings.time_interval);

                                   

                               $('#controls a').live('click', function(event){
                                   event.preventDefault();
                                  changeImage(container,$(this).index());
                                  clearInterval(interval_slideshow); // reset slideshow timer

                                  interval_slideshow = setInterval(
                                          (function(){
                                            changeImage( container,'null' );
                                           })
                                    ,settings.time_interval);
                               });
			};
})(jQuery);;

