/*
 * Functions for automatic slider
 */
var currentGallerySlide = 0;
var gallerySlideCount = 0;
var galleryinterval;

function gall_resetInterval() {
	galleryinterval = setInterval(function(){
				currentGallerySlide++;
				
				if (currentGallerySlide==gallerySlideCount) {
					currentGallerySlide=0;
				}
				
				$('ul.galleria li:eq('+currentGallerySlide+') img').trigger('click');
	},6000);
	
}


$(document).ready(function(){
	/*
	 * galleria
	 */
	 $('ul.gallery').css('display','none');
	 
	var galleria_currentimage;
	jQuery(function($) { $('ul.gallery').galleria({
			history   : false, // activates the history object for bookmarking, back-button etc.
			clickNext : false, // helper for making the image clickable
			insert    : 'p.gallery', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
											
				curimage = galleria_currentimage;
				if (curimage == undefined) {
					curimage = $(image).attr('src');
				}
				else {
					$('.galleria_wrapper').append('<img class="curimage" src="' + curimage + '" />');
					$('.galleria_wrapper .curimage').fadeOut(1000);
				}
				
				
				// fade in the image & caption
				if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
					//image.css('display','none').fadeIn(1500);
				}
				caption.css('display','none').fadeIn(1500);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
									
				// add a title for the clickable image
				image.attr('title','');
				
				galleria_currentimage=$(image).attr('src');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'inline'});
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',0.5); },
					function() { _li.children('img').fadeTo('fast',1); } // don't fade out if the parent is active
				)
				
				
				
			}
		}); }); 
		$('ul.galleria li:eq(0) img').css('border','1px solid red');
		
		/*
		 * Clear the interval on proper click
		 */		
		
		var x = $('ul.galleria li').length;
		gallerySlideCount = x;
		if (x > 0) {
			
			/*
			 * Start slider for automatic clicks
			 */
			gall_resetInterval();
		}
		
		
});

