/* For JS enabled browsers only */
document.write('<style type="text/css"> div.picturepresentationbox { display:none; } </style>');

jQuery(document).ready(function(){

	var current_image_index = 1;
	var hash = window.location.hash;

	
	function updateImageIndex()
	{
		jQuery("p.imagecounter").html('Image ' + current_image_index + ' of ' + jQuery("div.picturepresentationbox").length );
	}
	
	function computeImageIngex( hash )
	{
		items = jQuery("div.picturepresentationbox");
		for(var i=0; i<items.length; i++)
		{

			if( items.eq(i).attr('id') == hash)
			{
				return i+1;
			}
		}

		return 1;
	}
	
	function action_showAllImages()
	{
		jQuery("#showall").hide();
		jQuery("#showone").show();

		jQuery("a.prev").hide();
		jQuery("a.next").hide();

		jQuery("p.imagecounter").hide();
		
		jQuery("div.picturepresentationbox").show();
		jQuery("#content").removeClass("contentstaticheight");
	}
	
	function action_showOneImage()
	{
		jQuery("div.picturepresentationbox").hide()
		jQuery("#showone").hide();
		jQuery("#showall").show();

		jQuery("a.prev").show();
		jQuery("a.next").show();

		current_image_index = 1;
		updateImageIndex();
		jQuery("p.imagecounter").show();

		jQuery("div.picturepresentationbox:first").show();
		jQuery("#content").addClass("contentstaticheight");
	}
	
	
	/* Removing name parameter, avoiding going to anchor */
	jQuery("div.picturepresentation a").removeAttr('name');

	/* Making the content having static height */
	jQuery("#content").addClass("contentstaticheight");
	


	/* Show all / show one */
	jQuery("p.navbar").append(' &raquo; <a id="showall" href="'+ window.location.pathname + '#album">Show all images</a> <a id="showone" href="'+ window.location.pathname + '#first">Show one image</a>');
	
	jQuery("#showall").click( function() {
		action_showAllImages();
	});
	
	jQuery("#showone").click( function() {
		action_showOneImage();
	});



	

	jQuery("div.picturepresentationbox").each(
		function( i ) {
			var prev =	jQuery(this).prev().attr('id') ? '<a class="prev" href="' + window.location.pathname + '#' + jQuery(this).prev().attr('id').substr(3) + '">&laquo;</a>' : '';
			var next = jQuery(this).next().attr('id') ? '<a class="next" href="' + window.location.pathname + '#' + jQuery(this).next().attr('id').substr(3) + '">&raquo;</a>' : '';
			jQuery( this ).children('div').children('div.prevlinklocation').html(prev);
			jQuery( this ).children('div').children('div.nextlinklocation').html(next);
		}
	);		

	jQuery("a.prev").click( function() {
		jQuery("div.picturepresentationbox").hide();
		var href = jQuery(this).attr('href');
		jQuery('#ds_' + href.substr(href.indexOf('#')+1)).show();

		current_image_index--;
		updateImageIndex();
	});

	jQuery("a.next").click( function() {
		jQuery("div.picturepresentationbox").hide();
		var href = jQuery(this).attr('href');
		jQuery('#ds_' + href.substr(href.indexOf('#')+1)).show();

		current_image_index++;
		updateImageIndex();
	});
	
	
	
	
	if( hash )
	{
		hash = '#ds_' + hash.substr(1);
		
		if( hash == '#ds_album' )
		{
			action_showAllImages();
		}
		else if( jQuery(hash).length == 0 )
		{
			hash = "div.picturepresentationbox:first";
		}
		else
		{
			current_image_index = computeImageIngex( hash.substr(1) );
		}
		
		jQuery("#showone").hide();
		jQuery(hash).show();
	}
	else
	{
		jQuery("#showone").hide();
		jQuery("div.picturepresentationbox:first").show();
	}
	
	updateImageIndex();
	
	

});
