var Popup = {
	sel: "#popup",
	page: 0,
	directory: '',
	start: function(directory) {
		this.directory = directory;
		// load liste
		$.ajax({
			type: "GET",
			url: 'pics/'+directory+'/liste.js',
			dataType: "script",
			success: function(data, status) {
				Popup.show();
			},
			error: function(req, status) {
				alert(status + ": Die Daten konnten nicht geladen werden.");
			}
		});
		this.page = 0;
		$('#pop_back').css({background: 'url(pics/dummy.gif)'});
		$('#pop_forward').css({background: 'url(pics/pfeil_next.gif) no-repeat left center'});
	},
	show: function() {
		Popup.page = 0;
		$("#cover").css("display", "block");
		$("#cover").css({opacity: '0.0', backgroundColor: '#aaa', position: 'absolute', top: '0px', left: '0px',
			width: $(window).width() +'px', height: $(window).height()+'px' });
		$("#cover").fadeTo("slow", 0.7);
		$('#pop_title').html('');
		$('#pop_title').html(headline);	
		var width = "505px";
		var height = "580px";
		//$("#schliessen").css({float: "right", margin: "5px"});
		$('#pop_cont').css({height: '495px',
			background: '#373737 url(pics/'+Popup.directory+'/'+liste[0]+') center center no-repeat'});
		var top = ($(window).height()-parseInt(height))/2;
		if (top < 0) top = 0;
		$(this.sel).css({width: width, 
			position: "absolute", 
			left: ($(window).width()-parseInt(width))/2+"px",
			top: top+"px",
			backgroundColor: "#373737",
			zIndex: 100}).show('slow');
		$('#pop_back').unbind('click');
		$('#pop_back').click(function() {
			Popup.goPage("-");
			return false;
		});
		$('#pop_forward').unbind('click');
		$('#pop_forward').click(function() {
			Popup.goPage("+");
			return false;
		});
		this.setNumbers();
	},
	hide: function() {
		$("#cover").fadeTo("slow", 0.0, function() {
			$("#cover").css("display", "none");
		});
		$(this.sel).hide('slow');	
	},
	goPage: function(nr) {
		if (nr === '+') {
			if (this.page < (liste.length-1)) { this.page++; }
			else this.page = liste.length-1;
		}
		else if (nr === '-') {
			if (this.page > 0) { this.page--; }
			else this.page = 0;
		}
		else if (typeof nr === "number") {
			this.page = nr;
		}	
		if (this.page <= 0) {
			$('#pop_back').css({background: 'url(pics/dummy.gif)'});
		}
		else {
			$('#pop_back').css({background: 'url(pics/pfeil_back.gif) no-repeat left center'});
		}
		if (this.page >= (liste.length-1)) {
			$('#pop_forward').css({background: 'url(pics/dummy.gif)'});
		}
		else {
			$('#pop_forward').css({background: 'url(pics/pfeil_next.gif) no-repeat left center'});
		}
		$('#pop_cont').css({backgroundImage: 'url(pics/'+Popup.directory+'/'+liste[this.page]+')'});
		this.setNumbers();
	},
	setNumbers: function() {
		$('#pop_pages a').remove();
		$('#pop_pages span').remove();
		for (var i=0; i<liste.length; i++) {
			if (i == this.page) {
				$('#pop_pages').append(' <span>'+(i+1)+'</span> ');
			}
			else {
				$('#pop_pages').append(' <a href="" id="pop-pnr_'+i+'">'+(i+1)+'</a> ');
			}
		}
		$('#pop_pages a').click(function() {
			var pnr = $(this).attr('id');
			Popup.goPage(parseInt(pnr.substr(pnr.indexOf('_')+1)));
			return false;
		});
	}
}


$(document).ready(function(){
	$("p.f1 a").click(function() {
		if (typeof ($(this).attr('id')) != 'undefined') {
			Popup.start($(this).attr('id')); 
			return false;
		} 
	});
	$("#schliessen").click( function(event) { Popup.hide(); } );
});
