jQuery(document).ready(function(){

	jQuery('.entry-content').hide();
	jQuery('.entry-title a').click(function(event) {
		event.preventDefault();

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

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

	});


	jQuery('#content').find('.entry-title:eq(0)').addClass('on');	
	jQuery('#content').find('.entry-title:eq(0)').removeClass('off');	
	jQuery('#content').find('.entry-content:eq(0)').show();	
	


	// Gallery Slideshow
	var slideWidth = jQuery('.image:eq(0)').width();

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

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

		//jQuery(this).parent().find('.slidecontrols').append('/' + imageLength);
		jQuery(this).parent().find('.nextcontrols').append('/' + imageLength);
 		thisWrapper = jQuery('.gallerywrapper').index(jQuery(this).closest('.gallerywrapper'));
		manageGallery(0, thisWrapper);
		//var urlm = window.location.hash;
		//var hash = urlm.substring(urlm.indexOf('#')+1);
		//manageGallery(hash, thisWrapper);		
		//var imgname = jQuery('.photo-slide-'+hash).children().attr('src');
		//jQuery('.slide-image').	empty();
		//jQuery('.slide-image').append('<img src='+imgname+'>');
		
	  	});


	// The action on the slidecontrols
	jQuery('.slidecontrols a').click(function(event) {
		thisWrapper = jQuery('.gallerywrapper').index(jQuery(this).closest('.gallerywrapper'));
		currentMargin = parseInt(jQuery(this).closest('.gallerywrapper').find('.gallery').css('marginLeft'), 10);
		currentPosition = currentMargin / -slideWidth;
		nextPosition = (jQuery(this).attr('title')=='Next') ? currentPosition+1 : currentPosition-1;
		manageGallery(nextPosition, thisWrapper);				
		});
		
		// The action on the nextcontrols
	jQuery('.nextcontrols a').click(function(event) {
		/*thisWrapper = jQuery('.gallerywrapper').index(jQuery(this).closest('.gallerywrapper'));
		currentMargin = parseInt(jQuery(this).closest('.gallerywrapper').find('.gallery').css('marginLeft'), 10);
		currentPosition = currentMargin / -slideWidth;
		nextPosition = (jQuery(this).attr('title')=='Next') ? currentPosition+1 : currentPosition-1;
		manageGallery(nextPosition, thisWrapper);*/
		var urlm = window.location.hash;
		var hash = urlm.substring(urlm.indexOf('#')+1);
		if(hash === undefined || hash == null || hash.length <= 0)
		 hash = 0;
		else hash = parseInt(hash) - 1;
		/*var imgname = jQuery('.photo-slide-'+hash).children().attr('src');
		jQuery('.slide-image').	empty();
		
		var mainimgna = imgname.substring(imgname.lastIndexOf('-')+1);
		var mainimg = imgname.substring(0, imgname.length-mainimgna.length-1);
		var mainimgex = imgname.substring(imgname.lastIndexOf('.'));
		
		jQuery('.slide-image').append('<img src='+mainimg+mainimgex+'>');
		thisWrapper = jQuery('.gallerywrapper').index(jQuery('.photo-slide-'+hash).children().closest('.gallerywrapper'));
		manageGallery(hash-1 , thisWrapper);
				
		var p = location.pathname;
		p = p.substring(p.length-1) == '/' ? p.substring(0, p.length-1) : p;
		location.hash = hash;
		jQuery(this).animate({opacity: 1},300); 
		jQuery(this).siblings().stop().animate({opacity: 0.4}, 300);   
                    //e.stopPropagation();*/
		jQuery('.photo-slide-'+hash).children().click();			
					
		});

	// The action on the image
	/*jQuery('.image').click(function(event) {
 						
		});*/
		jQuery('.image').click(function(event) {
 		var imgname = jQuery(this).children().children().attr('src');
		var pname = jQuery(this).children().attr('class');
		var svalue = pname.substring(pname.lastIndexOf('-')+1);
		//var ptitle = jQuery(this).children().children().attr('title');
		//var palt = jQuery(this).children().children().attr('alt');
		jQuery('.slide-image').	empty();
		
		var bigimgna = imgname.substring(imgname.lastIndexOf('-')+1);
		var bigimg = imgname.substring(0, imgname.length-bigimgna.length-1);
		var bigimgex = imgname.substring(imgname.lastIndexOf('.'));
		jQuery('.slide-image').append('<img src='+bigimg+bigimgex+'>');
		
		var p = location.pathname;
		p = p.substring(p.length-1) == '/' ? p.substring(0, p.length-1) : p;
		location.hash = svalue;
		jQuery(this).animate({opacity: 1},300); 
		jQuery(this).siblings().stop().animate({opacity: 0.4}, 300);   
                    //e.stopPropagation();
					
		thisWrapper = jQuery('.gallerywrapper').index(jQuery(this).children().children().closest('.gallerywrapper'));
		currentMargin = parseInt(jQuery(this).children().children().closest('.gallerywrapper').find('.gallery').css('marginLeft'), 10);
		currentPosition = currentMargin / -slideWidth;
		maxPosition = jQuery(this).children().children().closest('.gallerywrapper').find('.image').length -1;
		nextPosition = currentPosition < maxPosition ? currentPosition+1 : 0 ;
		manageGallery(nextPosition, thisWrapper);				
		});
		
		
		
	function manageGallery(position, index){
		maxPosition = jQuery('.gallerywrapper:eq('+index+')').find('.image').length -1;
		next = jQuery('.gallerywrapper:eq('+index+')').find('.next');
		prev = jQuery('.gallerywrapper:eq('+index+')').find('.prev');
		gallery = jQuery('.gallerywrapper:eq('+index+')').find('.gallery');
		counter = jQuery('.gallerywrapper:eq('+index+') .slidecontrols .counter');
		displayposition = position + 1;

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

	}
	// end Gallery slideshow

});
