var switching 		= false;
var hidden    		= true;
var isFirstSwitch	= true;
var oldContent    	= "";
var hidder;

function hideImage() {
/*	new Effect.Opacity('im', {from: 1, to: 0,
 		duration: 0.3,
		afterFinish: function() {
			hidder = new PeriodicalExecuter(function(pe) {
				$('images').update(oldContent);
  				hidder.stop();
			}, 2);
		}
		});
	hidden = true;*/
	if (hidder != undefined) hidder.stop();
			hidder = new PeriodicalExecuter(function(pe) {
				$('images').update(oldContent);
  				hidder.stop();
			}, 1);
}

function showImage(file, dir, ext, desc) {
	if (desc == undefined) desc = '';
	if (hidder != undefined) hidder.stop();
	
	if (isFirstSwitch) oldContent = $('images').innerHTML;
	if (!switching) {
	
		if (dir == undefined)  dir = 'images/gallery/titles/';
		if (ext == undefined)  ext = '.jpg';
		
		var img = '<div id="im"></div>';
		if (file != '') img = "<img id='im' style='display:none' src='/"+dir+file+ext+"' alt='' />"+"<div class='desc2'>"+desc+"</div>";
			
		$('images').update(img);
		$('im').setStyle({opacity: 1});
		$('im').show();
		/*new Effect.Opacity('im', {from: 0, to: 1,
			duration: 0.2,
			afterFinish: function() {
				switching			= false;
				hidden				= false;
			}});*/		

				switching			= false;
				hidden				= false;

	}
	
	//switching		= true;
	isFirstSwitch	= false;
}

function switchImage(image) {

	//if (!switching) {
		//$('image_'+image).show();
		//$('image_'+image).setStyle({opacity: 0});
	
/*		new Effect.Parallel([
			$('image_'+galleryCurrentImage).hide(); //new Effect.Opacity('image_'+galleryCurrentImage, { sync: true, from: 1, to: 0 }), 
			$('image_'+image).show(); //new Effect.Opacity('image_'+image, { sync: true, from: 0, to: 1 })
		], { 
			duration: 0.2,
			afterFinish: function() {
				galleryCurrentImage = image;
				switching			= false;
			}
		});	
*/
			$('image_'+galleryCurrentImage).hide(); //new Effect.Opacity('image_'+galleryCurrentImage, { sync: true, from: 1, to: 0 }), 
			$('image_'+image).show(); //new Effect.Opacity('image_'+image, { sync: true, from: 0, to: 1 })
			galleryCurrentImage = image;
	//}	
	//switching = true;
}

function scrollGalleryRight() {
	var dimensions 	= (galleryImageCount*59)  - $('imageThumbnails').getWidth();
	var positionLeft = $('thumbScroll').positionedOffset().left;

	if (positionLeft < -dimensions+60) {
		$('thumbRight').hide();
	} else {
		$('thumbRight').show();
		$('thumbLeft').show();
	}

	if (positionLeft > -dimensions)	{
		new Effect.Move('thumbScroll', {x:-59, y:0, duration:0.3, queue: 'end' });
	}
}

function scrollGalleryLeft() {
	var dimensions 	= (galleryImageCount*59)  - $('imageThumbnails').getWidth();
	var positionLeft = $('thumbScroll').positionedOffset().left;

	if (positionLeft > -60) {
		$('thumbLeft').hide();
	} else {
		$('thumbLeft').show();
		$('thumbRight').show();
	}

	if (positionLeft < 0) {
		new Effect.Move('thumbScroll', {x:59, y:0, duration:0.3});
	}
}