var canNavigate = false;



Event.observe(window,'load',function()
{
	canNavigate = true;
	
	for ( key in gallery )
	{
		if ( !( key in Object.prototype ) || gallery[key] !== Object.prototype[key] )
		{
			//temp = new Image();
			//temp.src = gallery[key]['img'];
		}
	}
});



function galleryShow(dir)
{
	var nextKey = ( dir == 'next' ) ? currentKey + 1 : currentKey - 1;
	
	
	
	if ( galleryKeys[nextKey] != undefined && canNavigate )
	{
		var nextData = gallery[galleryKeys[nextKey]];
		
		//alert(nextData['img']);
					
		
		
		window.location = '#' + galleryKeys[nextKey];		
		
		
		
		currentKey = nextKey;
		
		
		
		doShow(nextData,false);
		
		
		
		// preload images around current one
		
		var nextKeyPl = currentKey + 1;
		var prevKeyPl = currentKey - 1;
		
		if ( galleryKeys[nextKeyPl] != undefined )
		{
			temp = new Image();
			temp.src = gallery[galleryKeys[nextKeyPl]]['img'];
		}
		
		if ( galleryKeys[prevKeyPl] != undefined )
		{
			temp = new Image();
			temp.src = gallery[galleryKeys[prevKeyPl]]['img'];
		}
	}
}

function doShow(obj,instant)
{
	if ( instant )
	{
		//$('galleryName').innerHTML = obj['name'];
		$('galleryImg').src = obj['img'];
		$('galleryCaption').innerHTML = obj['caption'];
	}
	else
	{
		canNavigate = false;
		
		new Effect.Opacity('galleryImg', { from: 1, to: 0, duration: .5, queue: 'end', afterFinish:function(){ $('galleryImg').src = obj['img']; } });
		new Effect.Opacity('galleryImg', { from: 0, to: 1, duration: .5, queue: 'end', afterFinish:function(){ canNavigate = true; } });
		
		//$('galleryName').innerHTML = obj['name'];
		$('galleryCaption').innerHTML = obj['caption'];
		
		if ( obj['caption'] != '' )
		{
			document.title = obj['caption'] + ' // Pip | Commercial portrait photographer'
		}
		else
		{
			document.title = 'Pip | Commercial portrait photographer'
		}
	}
	
	setPrevNextButtons();
}

function setPrevNextButtons()
{
	if ( currentKey == 0 )
	{
		$('prevLinkActive').className = 'hidden';
		$('prevLinkInactive').className = '';
	}
	else
	{
		$('prevLinkActive').className = '';
		$('prevLinkInactive').className = 'hidden';
	}
	
	if ( currentKey == ( galleryKeys.length - 1 ) )
	{
		$('nextLinkActive').className = 'hidden';
		$('nextLinkInactive').className = '';
	}
	else
	{
		$('nextLinkActive').className = '';
		$('nextLinkInactive').className = 'hidden';
	}
}





var currentKey = 0;

var galleryKeys = [];

var i = 0;

for ( key in gallery )
{
	if ( !( key in Object.prototype ) || gallery[key] !== Object.prototype[key] )
	{
		galleryKeys.push(key);
		
		if ( key == current )
		{
			currentKey = i;
		}
		
		i++;
	}
}

if ( gallery[current] == undefined )
{
	current = galleryKeys[0];
}

var hash = window.location.hash.replace('#','');

if ( hash != current && gallery[hash] != undefined )
{
	current = hash;
	
	i = 0;
	
	for ( key in gallery )
	{
		if ( !( key in Object.prototype ) || gallery[key] !== Object.prototype[key] )
		{
			if ( key == current )
			{
				currentKey = i;
			}
			
			i++;
		}
	}
	
	Event.observe(window,'load',function()
	{
		doShow(gallery[current],true);
	});
}
