	var firsttime = 0;
	var total_li;
	var current_li = 1;
	$(document).ready(function(){
		//Calculating the image wrapper width
		var total_li_div = $('#slider li').children().length;		
		li_div = parseInt(total_li_div) / 9;
		var li_mod	= parseInt(total_li_div) % 9;
		if(li_mod == 0)
			total_li = li_div;
		else
			total_li = parseInt(li_div + 1);
			
		var slider_width = parseInt(695 * total_li);		
		$('#slider').css({'position' : 'abosolute', 'width' : slider_width+'px', 'position' : 'relative', 'left' : '0px'});
		if(total_li > 1)
		{
			$("#slide-left").hide();
		}			
		else
		{
			$("#slide-left").hide();
			$("#slide-right").hide();
		}
		
		$('#slider li').click(function(){
			var hiddenval = $("#currentslide").val();
			var totalslide = $("#totalslide").val();
			var current_active = $("#currentActive").val();
			var clicked_ind = parseInt($("#slider li").index(this) + 1);
			if(clicked_ind <= (totalslide - 8))
				$("#currentslide").val(clicked_ind);
			else
				$("#currentslide").val((totalslide - 8));			
			$("#currentActive").val(clicked_ind);		
		});
		
		$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
		
		$('ul.gallery_demo').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : false, // helper for making the image clickable
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption - number in parens is speed in milliseconds
				image.css('display','none').fadeIn(750);
				caption.css('display','none').fadeIn(250);
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				// in paren - fade speed in milliseconds, brightness relative to 1
				// as delivered - fadeTo(500,0.3)
				_li.siblings().children('img.selected').fadeTo(250,0.7);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
//				image.attr('title','Next image >>');     *CHM - stop from pop-up 'Next image' when cursor over main image
			},
			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.7';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(250);  // original 1500
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.7); } // don't fade out if the parent is active

)
			}
		});		
	});
	function changeSlide(direction)
	{
		if(direction == 'right')
		{			
			if(current_li < total_li)
			{
				$('#slider').animate({left : -695 * current_li}, 1200);
				current_li++;
				if(current_li == total_li)
					$("#slide-right").hide();
				$("#slide-left").show();		
			}
		}
		if(direction == 'left')
		{
			if(current_li > 1)
			{
				$('#slider').animate({left : -(695 * (current_li-2))}, 1200);  // original 702
				current_li--;
				if(current_li == 1)
				 $("#slide-left").hide();
				$("#slide-right").show();	
			}
		}
	}	
	
