$(document).ready(function() {
	addPrintLink();
	if ($('body.fotos').length) {
		initGallery();
		lightbox();
	};
});

function addPrintLink() {
	var $printLink = $('<a href="#print" class="print">ausdrucken</a>');
	$printLink.click(function() {
		window.print();
		return false;
	});
	$('#content h2:first').append($printLink);
};

function lightbox() {
	var $galleries = $('ul.images');
	$galleries.each(function(index) {
		$('a',this).lightBox({
			imageLoading: '/workspace/img/lightbox/lightbox-ico-loading.gif',
			imageBtnPrev: '/workspace/img/lightbox/lightbox-btn-prev.gif',
			imageBtnNext: '/workspace/img/lightbox/lightbox-btn-next.gif',
			imageBtnClose: '/workspace/img/lightbox/lightbox-btn-close.gif',
			imageBlank: '/workspace/img/lightbox/lightbox-blank.gif',
			txtImage: 'Bild',
			txtOf: 'von'
		});
	});
}


function initGallery() {
	var $activeGallery = $('#galleries .images:first');
	var $image_links =$('.images a');
	
	showGallery($activeGallery);

	$('#gallery-navigation a').click(function() {
		$activeGallery = $($(this).attr('href'));
		showGallery($activeGallery);
		$('a:first',$activeGallery).trigger('click');
		// startGallery($activeGallery);
		return false;
	}).hover(function() {
		$activeGallery = $($(this).attr('href'));
		showGallery($activeGallery);
	}, function() {
		$activeGallery = $($(this).attr('href'));
		showGallery($activeGallery);
	});
	
	// $('#galleries .images').click(function() {
	// 	startGallery(this);
	// 	return false;
	// });
	// 
	// $image_links.each(function(index) {
	// 	var $img = $('<img />');
	// 	$img.attr('src', $(this).attr('href'));
	// });
	
};

function showGallery($el){
	$el.siblings('.images').addClass('inactive');
	$el.removeClass('inactive');
};

function startGallery(el) {
	var $galleryContainer = $('<div class="gallery"></div>');
	var $gallery = $('<ul></ul>');
	$('a',el).each(function(index) {
		var $img = $('<img />');
		var $list = $('<li></li>');
		$img.attr('src', $(this).attr('href'));
		$list.append($img);
		$gallery.append($list);
	});
	$('img:first', $gallery).load(function() {
		$galleryContainer.append($gallery);
		$galleryContainer.modal({
			onShow: function(dialog){
				$(dialog.container).append('<a class="gallery_control" id="gallery_prev">&lt;</a> <a class="gallery_control" id="gallery_next">&gt;</a>');
				$('ul',$galleryContainer).cycle({
					prev: '#gallery_prev',
					next: '#gallery_next',
					before: function(){
						var width = $(this).width();
						$('#simplemodal-container').animate({
							width: width
						}, {
							duration: 300,
							step: function(){
								$(window).trigger('resize.simplemodal');
							}
						});
					},
					speed: 300,
					timeout: 0 
				});
			}
		});
	});
};
