var curi = -1;
var waitpic = {
	name: 'wait.gif',
	path: 'http://sfg-singen.de/template_markus/wait.gif',
	width: 468,
	height: 260
}

function gallery_init() {
	
	
	$('#nextpic').click(function() {
		next_image();
	});
	$('#nextpic').hover(function() {
		$(this).animate({
			opacity: 1.0
		},300);
	},function() {
		$(this).animate({
			opacity: 0.0
		},300);
	});
	$('#prevpic').click(function() {
		prev_image();
	});
	$('#prevpic').hover(function() {
		$(this).animate({
			opacity: 1.0
		},300);
	},function() {
		$(this).animate({
			opacity: 0.0
		},300);
	});
}

function popup_open_single(src, width1, height1, name1) {
	var test = new Image();
	test.onload = function() {
		awidth = test.width;
		aheight = test.height;
		
		if(awidth >= aheight && awidth > 800) {
			var fac = aheight / awidth;
			width1 = 800;
			height1 = width1 * fac;
		}
		
		apics = new Array(1);
		apics[0] = {
			name: name1,
			path: src,
			width: width1,
			height: height1,
			title: '',
			text: ''
		}
		$('#nextpic,#prevpic').hide();
		$('#curpic').click(function() {
			popup_close();
		});
		popup_open(0);
	};
	test.src = src;
	

}

function popup_open(i) {
	if(popup_isAnimated()) {
		return false;
	}
	if(i == null) {
		i = 0;
	}
	$('#darkbg').click(function() {
		popup_close();
	});
	$('#pic_title').text('');
	$('#pic_text').text('');
	i = i % apics.length;
	curi = i;
	$('#curpic').attr({
		src: waitpic.path
	});
	$('#nextpicload').attr("src", apics[i].path);
	$('#curpic').css({
		width: waitpic.width+'px',
		height: waitpic.height+'px',
		display: "block"
	});
	$('#picbox').css({
		opacity: 1.0,
		width: waitpic.width+'px',
		height: waitpic.height+'px',
		marginLeft: '-'+Math.round(waitpic.width/2)+'px',
		marginTop: '-'+Math.round(waitpic.height/2)+'px',
		display: "block"
	});
	$('#darkbg').fadeTo(1000, 0.9, function() {
		swap_image(i);
	});
}

function popup_close() {
	if(popup_isAnimated()) {
		return false;
	}
	$('#pic_title').text('');
	$('#pic_text').text('');
	$('#curpic').fadeOut(300, function() {
		$('#picbox').animate({
			opacity: 0.0,
			height: '0px',
			marginTop: '0px'
		}, function() {
			$('#picbox').css({
				display: "none"
			})
		});
		$('#darkbg').fadeOut(1000);
	});
}

function swap_image(i) {
	i = (i+apics.length) % apics.length;
	curi = i;
	$('#pic_title').text('');
	$('#pic_text').text('');
	$('#curpic').fadeOut(500, function() {
		var wdur = ($('#picbox').width() == apics[i].width) ? 0 : 800;
		var hdur = ($('#picbox').height() == apics[i].height) ? 0 : 800;
		$('#picbox')
		.animate({ //resize width
			width: apics[i].width+'px',
			marginLeft: '-'+Math.round(apics[i].width/2)+'px'
		}, wdur, 'easeOutQuad')
		.animate({ //resize height
			height: (apics[i].height)+'px',
			marginTop: '-'+Math.round(apics[i].height/2)+'px'
		}, hdur, 'easeOutQuad', function() { //set prev/next image and fade in
			var prevI = (curi-1+apics.length) % apics.length;
			var nextI = (curi+1+apics.length) % apics.length;
			$('#prevpicload').attr({
				src: apics[prevI].path
			});
			$('#curpic').attr({
				src: apics[i].path
			});
			$('#nextpicload').attr({
				src: apics[nextI].path
			});
					
			$('#curpic').css({
				width: apics[i].width+'px',
				height: apics[i].height+'px',
			}).fadeIn(500, function() { //show title & text
				var plusheight = 0;
				if(apics[i].title.length > 0) {
					plusheight = 10;
				}
				if(apics[i].text.length > 0) {
					plusheight = 22;
				}
				if(apics[i].text.length > 120) {
					plusheight = 32;
				}
				$('#picbox').animate({
					height: $('#picbox').height()+plusheight
				}, function() {
					$('#pic_title').text(apics[i].title);
					$('#pic_text').text(apics[i].text);
				});
			});
		});
	});
}

function next_image() {
	if(popup_isAnimated()) {
		return false;
	}
	swap_image(curi+1);
}

function prev_image() {
	if(popup_isAnimated()) {
		return false;
	}
	swap_image(curi-1);
}

function popup_isAnimated() {
	return ($('#picbox:animated,#darkbg:animated,#curpic:animated').length > 0) ? true : false;
}

$(document).ready(function() {gallery_init();});
