/*
 * jQuery Gallery 0.1
 *
 * Copyright (c) 2009 
 * Author: Gleidson @ SEO Works
*	GPL (GPL-LICENSE.txt) licenses.
 *
 *
 */
 (function($){      	
 	var slideshow_gallery = number_of_items = null;
 	var current_item = 0;
 	var caption_el = $('.gallerise_caption');
 	$.slide_photo = function (obj, direction ){
 			var $active = obj.find('img:eq('+ current_item + ')');
 			 //check if there is a caption div.
		  	obj.find('.gallerise_caption:eq('+ current_item + ')').hide("slide", { direction: "right" }, 800); 		
			current_item += direction; 
			if(current_item >= number_of_items)//Reset it if it's out of boundaries.
				current_item = 0;				
			if(current_item<0)
				current_item = number_of_items-1;
			$next =  obj.find('img:eq('+ current_item + ')');
			// uncomment the 3 lines below to pull the images in random order    
		    // var $sibs  = $active.siblings();
		    // var rndNum = Math.floor(Math.random() * $sibs.length );
		    // var $next  = $( $sibs[ rndNum ] );
		    
		   
		    $active.addClass('last-active');
		    $next.css({opacity: 0.0})
		        .addClass('active')
		        .animate({opacity: 1.0}, 1000, function() {		        	 	
		            $active.removeClass('active last-active');
		            obj.find('.gallerise_caption:eq('+ current_item + ')').show("slide", { direction: "right" }, 500);		        	
		        });  
}; 
	 $.fn.gallerise = function(options) {  
		var defaults = {  
			transition_time: 8000,  
			ctrl_buttons: false 
		};  
		var options = $.extend(defaults, options);  
    	return this.each(function() {
    	//functionality starts here 
    	obj = $(this); 
    	obj.find('.gallerise_caption:eq('+ current_item + ')').show("slide", { direction: "right" }, 500);
    	number_of_items = obj.find('img').size();
    	if(number_of_items < 2 )//If there's less than one image, we can't slideshow. Return
    		return false;
    	//if($('.'+ obj.attr('className') + '_caption .caption').size() > 1 )
    		//options.captions = $('.'+ obj.attr('className')  + '_caption');
    	obj.find('img:eq('+ current_item + ')').addClass('active');    	
    	
    	slideshow_gallery = setInterval( "new jQuery.slide_photo( obj, 1 );", options.transition_time ); 
    	
    	
    	/***********************Add the next and previous buttons************************************/
    	if(options.ctrl_buttons) {
	    		obj.after('<div class="btn_rw"></div><div class="btn_fw"></div>');
	    	$(".btn_fw").click(function(){
				$.slide_photo(obj, 1);
				clearInterval(slideshow_gallery);
				slideshow_gallery = setInterval( "new jQuery.slide_photo( obj, 1 );", options.transition_time ); 
			});	
			$(".btn_rw").click(function(){
				$.slide_photo(obj, -1);
				clearInterval(slideshow_gallery);
				slideshow_gallery = setInterval( "new jQuery.slide_photo( obj, 1 );", options.transition_time ); 
			});
		}//ends adding ctrl_buttons
			
		//ends functionality
		});  
	};  
})(jQuery);


