(function ($) {

	$(document).ready(function(){

		initializeImages();
		
		$("#foto_gallery_arrow_left").click(previousImage);
		
		$("#foto_gallery_arrow_right").click(nextImage);
		
		$(".foto").click(nextImage);
		
		$(".show_comment_form").click(function() {
			$comment_form = $(this).next();
			if($comment_form.css("display") != "block") {
				$(".show_comment_form").next().css("display", "none");
				$comment_form.css("display", "block");
				$(".comment-form-to-remove").removeClass("comment-form-to-remove");
				$(this).addClass("comment-form-to-remove");
				$comment_form.addClass("comment-form-to-remove");
			} else {
				$comment_form.css("display", "none");
			}
		});
	});
	
	/** Initialize image links */
	function initializeImages() {
		$("#foto_gallery img:first").addClass("display_image");
		hidePrevLink();
		
		$images = $(".foto img");
		anzahl_fotos = $images.length;
		
		if(anzahl_fotos < 2) {
			hideNextLink();
		} else {
			showNextLink();
		}
	}

	/** Previous image */
	function previousImage() {
		$current = $("#foto_gallery .display_image");
		$next = $current.prev();
		if($next.size() > 0) {
			$next.addClass("display_image");
			$current.removeClass("display_image");
			$nextnext = $next.prev();
			showNextLink();
			if($nextnext.size() == 0) {
				hidePrevLink();
			}
		}
	}
			
	/** Next image */
	function nextImage() {
		$current = $("#foto_gallery .display_image");
		$next = $current.next();
		if($next.size() > 0) {
			$next.addClass("display_image");
			$current.removeClass("display_image");
			$nextnext = $next.next();
			showPrevLink();
			if($nextnext.size() == 0) {
				hideNextLink();
			}
		} else {
			window.location.href = "http://www.crescendo-music.ch/pictures";
		}
	}
	
	/** Show and Hide links */

	function showNextLink() {
		$("#foto_gallery_arrow_right").addClass("foto_more");
		$(".foto").addClass("foto_more");
	}

	function hideNextLink() {
		$("#foto_gallery_arrow_right").removeClass("foto_more");
		$(".foto").removeClass("foto_more");
	}

	function showPrevLink() {
		$("#foto_gallery_arrow_left").addClass("foto_more");
	}

	function hidePrevLink() {
		$("#foto_gallery_arrow_left").removeClass("foto_more");
	}

})(jQuery);;

