/* 
		Flowers by Fiona 
		Home Gallery
*/


(function( $ ){
  $.fn.Flowers = function() {
  	
  //	setInterval(galle);
  	start();
  	
  
		var fimg = $('.pagination li a:first-child').attr('href');
		
		$('.full').css("background-image", 'url(' +fimg +' )');

		$('.pagination a').click(function(e) {
			e.preventDefault();
			
			// Remove class from all li elements
			$('.pagination li').removeClass("here");

			// Get href of clicked thumbnail and make it background image of .full
			var imgg = $(this).attr('href');
			$('.full').css("background-image", 'url(' + imgg + ' )');	

			// Add class here to clicked thumbnail 			
			$(this).parent().addClass("here");	
	
			// Stop the gallery
			action = 'stop';
			start(action);
			
		});	
			
		// Play Through Images 
		$('#play').click(function(e) {
			e.preventDefault();
				
				nextimg();
				start();						
		
		});
		
		// Pause Button
		
		$('#pause').click(function(e) {
			e.preventDefault();
			
			action = 'stop';
			start(action);
		
		});	
		
		
		// The Function!
		
		
		function start(action) {
		
			// Add class Active to play button
			$('#play').addClass('active');
			
			// Remove class Active from pause button
			$('#pause').removeClass('active');			

			if (action == 'stop') { 
			
				clearInterval(galle);
				$('#pause').addClass('active');
				$('#play').removeClass('active');
				
			} else {

			galle = setInterval(function() {

				nextimg();
		
				},10000);
		
			}

		}

		function nextimg() {
		
				current = $('.pagination .here a').attr('href');
		
		
				if($('.pagination .here').next().length == 0) {
		
					$('.pagination .here').removeClass('here');
		
					$('.pagination li').first().addClass('here');	
					next = $('.pagination .here a').attr('href');
					
				} else {
					
					//alert('Other');
				
					$('.pagination .here').next().addClass('next');	
					
					//Get href from a and assign to next var
					next = $('.pagination .next a').attr('href');
					
					//Remove next class
					$('.pagination .next').removeClass('next');
					$('.pagination .here').next().addClass('here');
					$('.pagination .here').prev().removeClass('here');
					
				}
				
				// Add image to background
				//$('.full').css("background-image", 'url(' +next +' )');		

				$('.full').fadeTo('slow', 0.1, function() {
				    $(this).css('background-image', 'url(' + next + ')');
				}).fadeTo('slow', 1);
				
		}		

  };
})( jQuery );
