/*
 * 	Diffuse Slideshow by Diffuse Interactive
 *
 */

(function($) {

	$.fn.slideShow = function(options){
	  
		// default configuration properties
		var defaults = {			
			speed: 			800,
            autoslide:      true,
            controls:       false
		}; 
		
		var options = $.extend(defaults, options);

        var timer = setInterval(timerFunction, 5000);

        var active = 0;
				
		this.each(function() {  

            if(options.controls == true){
                $("#photocontrols h2").html($('#photos li.active').find("img").attr("alt"));

                $("#prev").click(function(){
                    if(active == 0){
                        showPhoto("prev");
                    }
                });

                $("#next").click(function(){
                    if(active == 0){
                        showPhoto("next");
                    }
                });

                $("#indicators li a").click(function(){
                    showPhoto($(this).html());
                });
            }

			
		});

        function timerFunction(){
            showPhoto("next");
        }

        function showPhoto(direction) {

                clearInterval(timer);
                timer = setInterval(timerFunction, 5000);

                active = 1;

                var $active = $('#photos li.active');
                
                if ($active.length == 0)
                    $active = $('#photos li:last');


                if (direction == parseFloat(direction)){

                    var num = direction - 1; 

                    var $next =  $('#photos li:eq(' + num + ')');

                }
                else if(direction == "prev"){

                    var $next =  $active.prev().length ? $active.prev() : $('#photos li:last');
                    
                }
                else
                {
                    var $next =  $active.next().length ? $active.next() : $('#photos li:first');
                }

                $active.addClass('last-active');

                if(options.controls == true){
                    $("#photocontrols h2").html($next.find("img").attr("alt"));

                    $("#indicators li").removeClass("active");
                    $("#indicators li:eq(" + $next.index() + ")").addClass("active");
                }

                $next.css({opacity: 0.0})
                    .addClass('active')
                    .animate({opacity: 1.0}, options.speed, function() {
                        $active.removeClass('active last-active');
                        active = 0;
                    });
          }
	  
	};

})(jQuery);




