var pathtotpl = '/fileadmin/styles/galeriak';				/* path to the templates directory */

$(document).ready(function(){
	/* Shows the second navigation level on hover of a parent item (li) */
	navHover();

	/* Loads the artist gallery */
	artistGallery();

	/* activates the lightbox */
	activateLightbox();

});

function navHover(){
	$("#nav_main ul li").hover(function(){
		$(this).children("ul").slideDown(100);
	}, function(){
		$(this).children("ul").slideUp(100);
	})
}

function artistGallery(){

	artistGalleryListPosition();
	
	$("div.scrollable").scrollable({
		size: 4,
		item: 'li',
	});
	$(".items img").click(function() { 
		var url = $(this).parent("a").attr("href");
		var caption = $(this).parent().parent().children("div.caption").html();
		
		$('#image_wrap').fadeTo('slow', 0.01, function() {
			var img = new Image();
			img.src = url;
			img.onload = function() { 
				$('#image_wrap').find("img").attr("src", url);
				$('#image_wrap .caption').html(caption);
				$('#image_wrap .img').css({'position' : 'relative'});
				/*
				if ($('#image_wrap .img').width() > $('#image_wrap img').width()) {
					var diff = $('#image_wrap .img').width() - $('#image_wrap img').width();
					if (!fTeiler(diff,2)) {diff++}
					var positionLeft = diff / 2;
					$('#image_wrap img').css({
						'position' : 'absolute',
						'left' : positionLeft + 'px'
					});
				}*/
				$('#image_wrap').fadeTo('fast', 1);
			}; 
		});

		return false;
	}).filter(":first").click();
}

/**
 * Sets the images to center.
 * 
 */
function artistGalleryListPosition(){
	var containerWidth = $('.artistGallery ul li').width();
	var containerHeight = $('.artistGallery ul li').height();

	$('.artistGallery ul li a img').each(function(){
		var width = $(this).width();
		var height = $(this).height();
		
		if (width > containerWidth){
			var diff = width - containerWidth;
			if (!fTeiler(diff,2)) {diff++}
			var positionLeft = diff * 0.5;
			$(this).css({
				'position' : 'absolute',
				'left'	: '-' + positionLeft + 'px'
			});
		} else if (width < containerWidth){
			var diff = containerWidth - width;
			if (!fTeiler(diff,2)) {diff++}
			var positionLeft = diff * 0.5;
			$(this).css({
				'position' : 'absolute',
				'left'	: positionLeft + 'px'
			});
		};

		if (height > containerHeight){
			var diff = height - containerHeight;
			if (!fTeiler(diff,2)) {diff++}
			var positionTop = diff * 0.5;
			$(this).css({
				'position' : 'absolute',
				'top'	: '-' + positionTop + 'px'
			});
		} else if (height < containerHeight){
			var diff = containerHeight - height;
			if (!fTeiler(diff,2)) {diff++}
			var positionTop = diff * 0.5;
			$(this).css({
				'position' : 'absolute',
				'top'	: positionTop + 'px'
			});
		};
	});
}

/**
 * Activates the jQuery lightbox
 * 
 */
function activateLightbox(){
	$(".recipe .gallery a[rel='lightbox'], .lightboxGallery .thumbnails a[rel='lightbox'], .csc-textpic a[rel='lightbox'], .news-single-item a[rel='lightbox']").lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.8,
		imageLoading: '/fileadmin/styles/galeriak/images/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: '/fileadmin/styles/galeriak/images/lightbox/lightbox-btn-close_ico.gif',
		imageBtnPrev: '/fileadmin/styles/galeriak/images/lightbox/lightbox-btn-prev_ico.gif',
		imageBtnNext: '/fileadmin/styles/galeriak/images/lightbox/lightbox-btn-next_ico.gif',
		containerResizeSpeed: 350,
		txtImage: 'Image',
		txtOf: 'from'
	});
}

function fTeiler(iZ,iT) {
	var iR =0;
	var bTeilbar=false;

	iR=iZ%iT;
	if(iR==0) {
		bTeilbar=true;
	}
	return bTeilbar;
}

