var currentSerie = [],
		curIDX = 0;
function debug(str){
	/*if(console && (typeof console.log == 'function')){
		console.log(str);
	}*/
}
function showImage(a){
	curIDX += a;
	if(curIDX < 0){
		curIDX = currentSerie.length - 1;
	}else if(curIDX >= currentSerie.length){
		curIDX = 0;
	}
	var thumb = $(currentSerie[curIDX]),
			size = thumb.width() > thumb.height() ? 'w' : 'h',
			para = $('<p>'+thumb.attr('alt')+'</p>');
	$('#bildlayer img, #bildlayer p').remove();
	$('#bildlayer').append('<img id="bigImage" src="'+ thumb.attr('src').replace(/140/,'560') + '" title="'+thumb.attr('alt')+'"/>');
	setTimeout(function(){
					var bigImg = $('#bigImage'),
						pos = bigImg.offset();
					para.css({width: bigImg.width() + 'px', top: pos.top + bigImg.height() + 10, left: pos.left, lineHeight:'21px' });
					$('#bildlayer').append(para);
				}, 500);
}

$(document).ready(
	function(){
		$('div.serie>div').click(
			function(ev){
				var _self = this,
						thumb = $('img', this),
						idx = 0,
						headline = $('h4',$(this).parents('.block')).html();

				ev.preventDefault();
				if(currentSerie.length > 0){
					currentSerie = [];
				}
				debug(currentSerie = $('img',$(this).parents('.serie')));
				for(var i = 0; i < currentSerie.length; i++){
					if(thumb[0] == currentSerie[i]){
						curIDX = i;
						break;
					}
				}
				$('#bildlayer').remove();
				$('#overlay').remove();
				$('object').css({visibility:'hidden'});
				$('body').append($('<div id="overlay"><p class="title">'+headline+'</p></div>').click(function(e){
					$(this).remove();
					$('#bildlayer').remove();
					$('object').css({visibility:'visible'});
				})).append($('<div id="bildlayer"/>').click(function(e){
					$(this).remove();
					$('#overlay').remove();
					$('object').css({visibility:'visible'});
				}));
				$('#bildlayer').css('line-height',$('#bildlayer').height() + 'px');
				$('#bildlayer').append($('<div id="nextImage" title="n&auml;chstes Bild"/>').click(function(ev){
					ev.preventDefault();
					ev.stopPropagation();
					showImage(1);
				})).append($('<div id="prevImage" title="vorheriges Bild"/>').click(function(ev){
					ev.preventDefault();
					ev.stopPropagation();
					showImage(-1);
				}));
				showImage(0);
			}
		);
});
