$(document).ready(function(){

	$('.entry-content').hide();

	// Anchor Links

	var anchor = $.param.fragment();

	if (anchor !== '') {

		$('.'+anchor+' .entry-title a').addClass('open');	
		$('.'+anchor+' .entry-title').addClass('on');	
		$('.'+anchor+' .entry-title').removeClass('off');
		$('.'+anchor+' .entry-content').show();
		$.scrollTo('.'+anchor);

	} else {

		$('#content').find('.entry-title:eq(0) a').addClass('open');	
		$('#content').find('.entry-title:eq(0)').addClass('on');	
		$('#content').find('.entry-title:eq(0)').removeClass('off');	
		$('#content').find('.entry-content:eq(0)').show();
		
	}
	

// Action on links
$('.entry-title a').click(function(event) {

	var index = $('.entry-title').index($(this).parent());

	if ($(this).parent().hasClass('on')) {
			event.preventDefault();
			$(this).removeClass('open');
			$(this).parent().removeClass('on');
			$(this).parent().addClass('off');
			$('.entry-content:eq('+index+')').hide();
	} else {
			$(this).addClass('open');
			$(this).parent().removeClass('off');
			$(this).parent().addClass('on');
			$('.entry-content:eq('+index+')').show();
	}

});


// Gallery Slideshow
var slideWidth = $('.slide:eq(0)').width();

$('.slide:not(:only-child)').parent().each(function(index) {
	imageLength = $(this).find('.slide').length;

	$(this).wrap('<div class="gallerywrapper" />');
	$(this).css('width', slideWidth * imageLength);
	$(this).parent().append('<p class="slidecontrols"><a title="Previous" class="prev"><span>&larr;</span></a> <a title="Next" class="next"><span>&rarr;</span></a> <span class="counter" /></p>');

	$(this).parent().find('.slidecontrols').append('/' + imageLength);
	thisWrapper = $('.gallerywrapper').index($(this).closest('.gallerywrapper'));
	manageGallery(0, thisWrapper);
  	});


// The action on the controls
$('.slidecontrols a').click(function(event) {
	thisWrapper = $('.gallerywrapper').index($(this).closest('.gallerywrapper'));
	currentMargin = parseInt($(this).closest('.gallerywrapper').find('.gallery').css('marginLeft'), 10);
	currentPosition = currentMargin / -slideWidth;
	nextPosition = ($(this).attr('title')=='Next') ? currentPosition+1 : currentPosition-1;
	manageGallery(nextPosition, thisWrapper);				
	});

// The action on the image
$('.slide').click(function(event) {
	thisWrapper = $('.gallerywrapper').index($(this).closest('.gallerywrapper'));
	currentMargin = parseInt($(this).closest('.gallerywrapper').find('.gallery').css('marginLeft'), 10);
	currentPosition = currentMargin / -slideWidth;
	maxPosition = $(this).closest('.gallerywrapper').find('.slide').length -1;
	nextPosition = currentPosition < maxPosition ? currentPosition+1 : 0 ;
	manageGallery(nextPosition, thisWrapper);				
	});


function manageGallery(position, index){
	maxPosition = $('.gallerywrapper:eq('+index+')').find('.slide').length -1;
	next = $('.gallerywrapper:eq('+index+')').find('.next');
	prev = $('.gallerywrapper:eq('+index+')').find('.prev');
	gallery = $('.gallerywrapper:eq('+index+')').find('.gallery');
	counter = $('.gallerywrapper:eq('+index+') .slidecontrols .counter');
	displayposition = position + 1;

	if (position <= maxPosition && position >= 0) {
		$(gallery).animate({ 'marginLeft' : 700 * (-position) } );
		$(counter).html(displayposition);
		position == maxPosition ? $(next).removeClass('active') : $(next).addClass('active');	
		position == 0 ? $(prev).removeClass('active') : $(prev).addClass('active');
	}

}
// end Gallery slideshow


});
