function center_content(){

	var window_width 		= $(window).width();
	var content_div_width	= $('#content').width();	
	var margin_left			= (window_width - content_div_width) / 2;
	
	$('#content').css('left', margin_left + 'px');

}

function extended_ajax_load(url, selector) {

    $(selector).load(url, "",
        function(responseText, textStatus, XMLHttpRequest) {
            if(textStatus == 'error') {
                $(selector).html('<span style="color: red;">error while loading ajax content!</span>');
            }
        }
    );
   
}

function getFileExtension(filename){

	return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;

}

function gup( name, href ){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( href );
	if( results == null )
		return "";
	else
		return results[1];
}


function open_gallery_overlay(this_a){		
	
	// wich gallery?
	var this_a_rel  = $(this_a).attr('rel');
	// wich target?
	var this_a_href = $(this_a).attr('href');
	
	// leeren des bipics und der thumbleiste
	$('#gallery_overlay #thumbleiste').html('');
	$('#bigpic_div').html('');

	// mark all a that belong to this gallery / have the same rel-attribute
	var all_gallery_targets = $('a.show_as_thumb[rel=' + this_a_rel + ']');

	// blende overlay ein
	$('#gallery_overlay').fadeIn("fast", function(){
	
		// blende thumbleiste ein
		$('#thumbleiste').fadeIn("fast");

		// copy all a that belongs to the gallery in the "thumbleiste"		
		$('#gallery_overlay #thumbleiste').empty();		
		$(all_gallery_targets).each(function (){
		
			var this_html = $(this).html();
			var this_href = $(this).attr('href');

			$('#gallery_overlay #thumbleiste').append('<a href="' + this_href + '">' + this_html + '</a>');
		
		});
		// clone funktionierte nicht im ie8, komisch.
		// deswegen die dirty variante, siehe oben
		// $(all_gallery_targets).clone().appendTo('#gallery_overlay #thumbleiste').removeClass('show_as_thumb');		
				
		
		// blende bigpic container ein				
		$('#bigpic_container').fadeIn("fast");	
			
		// load the first target (could be image or text)
		load_big_content(this_a_href);			
	
	});

}

function close_gallery_overlay(){

	// versteckte thumbleiste	
	$('#thumbleiste').hide();
	
	// verstecke bigpic container
	$('#bigpic_container').hide();
	
	// blende overlay aus
	$('#gallery_overlay').fadeOut("fast");

}

function goto_next(){
	
	// get next href 
	
	if($('#thumbleiste a.active').next().length){
	
		var this_a_href = $('#thumbleiste a.active').next().attr('href');		
		load_big_content(this_a_href);		
	
	}else{
	
		// wenn es kein next gibt, wird wieder anfang angefangen
		
		var this_a_href = $('#thumbleiste a:first').attr('href');	
		load_big_content(this_a_href);			
		
		//alert('kein nächster content vorhanden');
		//close_gallery_overlay();
	
	}	

}

function goto_prev(){

	// get prev href 
	
	if($('#thumbleiste a.active').prev().length){
	
		var this_a_href = $('#thumbleiste a.active').prev().attr('href');			
		load_big_content(this_a_href);
	
	}else{
	
		// wenn es kein prev gibt, wird am ende weitergemacht
		
		var this_a_href = $('#thumbleiste a:last').attr('href');		
		load_big_content(this_a_href );			
	
		//alert('kein vorheriger content vorhanden');
		//close_gallery_overlay();
	
	}	

}

function load_big_content(this_a_href){
	
	var big_content_file	= this_a_href;
	var margin_bottom		= 1;
	var navi_line_height	= $('#navi_line').height() + margin_bottom;
	
	// blende navi-leiste aus um ruckler zu vermeiden
	$('#bigpic_container #navi_line').hide();
	
	// markiere das thumb zum big_content-file
	$('#thumbleiste a').removeClass('active');	
	var thumb_wrapper = $("#thumbleiste a[href*='" + big_content_file + "']").addClass('active');		
	
	// wenn es nur eine big_content gibt werden die navi-pfeile ausgeblendet
	if($("#thumbleiste a").length < 2){	
		$('#bigpic_container #navi_line .buttons a').hide();	
	}else{	
		$('#bigpic_container #navi_line .buttons a').show();	
	}
	
	// wenn big_content-file ein jpg	
	if(getFileExtension(this_a_href) == 'jpg'){		
		
		// entferne mögliche text-class
		$('#bigpic_div').removeClass('txt_content');		
		
		// lese und schreibe bildunterschrift
		$('#navi_line div.desc p').html('');
		var desc = $(thumb_wrapper).find('img').attr('alt'); //  bei img-thumbs wird der wert des alt-tags genommen
		$('#navi_line div.desc p').html(desc);
		
		// leere container
		$('#bigpic_container #bigpic_wrapper #bigpic_div').html('');	
		
		// füge bild ein	
		$('#bigpic_container #bigpic_wrapper #bigpic_div').html('<img src="" />');	
		var img = $('#bigpic_container #bigpic_wrapper #bigpic_div img').hide();
		
		// füge lade animation hinzu
		$('#bigpic_container').addClass('loading');

		$(img).load(function (){
			
			// berechne größen
			var bigpic_container_height = $('#bigpic_container').height();
			var bigpic_container_width  = $('#bigpic_container').width();
			var thumb_height			= $(thumb_wrapper).height();
			var thumb_width				= $(thumb_wrapper).width();		
			var thumb_ratio				= thumb_height / thumb_width;	
			var bigpic_height			= bigpic_container_height - navi_line_height;		
			var bigpic_width			= bigpic_height / thumb_ratio;		

			// wenn extremes querformat richten sich die
			// maße nach der breite des bigpic_containers
			if(bigpic_width > bigpic_container_width){				
				bigpic_width  = bigpic_container_width;
				bigpic_height = bigpic_width * thumb_ratio;			
			}
			
			// setze bigpic und container größe
			$('#bigpic_container #bigpic_wrapper #bigpic_div img').css('height', bigpic_height + 'px');
			$('#bigpic_container #bigpic_wrapper #bigpic_div img').css('width',  bigpic_width + 'px');
			$('#bigpic_container #bigpic_wrapper #bigpic_div').css('height', bigpic_height + 'px');
			$('#bigpic_container #bigpic_wrapper #bigpic_div').css('width',  bigpic_width + 'px');			
			
			// centriere wrapper_container horizontal
			var margin_left = (bigpic_container_width - bigpic_width) / 2;
			//alert('margin-left: ' + margin_left);
			$('#bigpic_container #bigpic_wrapper').css('margin-left', margin_left + 'px');		
			
			
			// centriere wrapper_container vertical
			var spacer_for_navi = 4;
			var margin_top = (bigpic_container_height - bigpic_height - navi_line_height) / 2;
			//alert(bigpic_container_height + ' / ' + bigpic_height);
			$('#bigpic_wrapper').css('margin-top', margin_top + 'px');

			
			// zeige bild
			$(this).fadeIn('fast');			
			
			// scroll im thumbstreifen zu dem bild
			var active_thumb_height 	   = $('#thumbleiste a.active').height();
			var active_thumb_margin_bottom = parseInt($('#thumbleiste a.active').css('margin-bottom'));			
			var thumbleiste_scroll_down    = active_thumb_height; // + active_thumb_margin_bottom;
			
			// todo, scroll down the exact thumb height!
			
			setTimeout("$('#thumbleiste').scrollTo('a.active', 500, {offset: {top:-100}})", 200);
			
			// entferne lade animation
			$('#bigpic_container').removeClass('loading');
			
			//blende navi-leiste wieder ein
			$('#bigpic_container #navi_line').fadeIn('fast');
			
			//sepcial treatment for ie7
			//--------------------------
			var close_button_width    = $('.close_button').width();
			var buttons_width		  = $('.buttons').width();
			var bigpic_div_width      = $('#bigpic_div').width();			
			var close_button_new_left = bigpic_div_width - close_button_width;			
			$('.close_button').css('left', close_button_new_left + 'px');			
			var desc_width			  = bigpic_div_width - close_button_width - buttons_width - 6;
			$('.desc').css('width', desc_width + 'px');
			
		}).error(function () {
		
			alert('content file not found! \n ' + big_content_file);
			
		}).attr('src', big_content_file);	
		
	}

	// wenn big_content-file ein txt 
	if(getFileExtension(this_a_href) == 'txt'){	

		// füge text classe hinzu
		$('#bigpic_div').addClass('txt_content');
		
		// lese und schreibe bildunterschrift
		$('#navi_line div.desc p').html('');
		var desc = $(thumb_wrapper).find('div p').html(); // bei text-thumbs wird der inhalt des divs genommen
		$('#navi_line div.desc p').html(desc);		
		
		// leere container
		$('#bigpic_container #bigpic_wrapper #bigpic_div').html('');		
		
		// füge txt wrapper ein
		$('#bigpic_container #bigpic_wrapper #bigpic_div').html('<div id="txt_wrapper"></div>');					
		
		//blende navi-leiste wieder ein
		//$('#bigpic_container #navi_line').fadeIn('fast');	
		$('#bigpic_container #navi_line').show();	
		
		// lade content
		extended_ajax_load(big_content_file, '#txt_wrapper');
		
		// scroll im thumbstreifen zu dem bild
		setTimeout("$('#thumbleiste').scrollTo('a.active', 500, {offset: {top:-100}})", 200);		
		
		// get dimensions
		var bigpic_container_width	= $('#bigpic_container').width();
		var bigpic_container_height = $('#bigpic_container').height();				
		
		// passe höhe an
		var bigpic_height			= bigpic_container_height - navi_line_height;
		$('#bigpic_div').css('height', bigpic_height + 'px'); 
		
		// reset width
		$('#bigpic_div').css('width', '');
		
		// zentriere container
		var bigpic_div_width		= $('#bigpic_div').width();
		var margin_left 			= (bigpic_container_width - bigpic_div_width) / 2;	
		$('#bigpic_container #bigpic_wrapper').css('margin-left', margin_left + 'px');	

		//sepcial treatment for ie7
		//--------------------------
		var close_button_width    = $('.close_button').width();
		var buttons_width		  = $('.buttons').width();
		var bigpic_div_width      = $('#bigpic_div').width();			
		var close_button_new_left = bigpic_div_width - close_button_width;			
		$('.close_button').css('left', close_button_new_left + 'px');			
		var desc_width			  = bigpic_div_width - close_button_width - buttons_width - 6;
		$('.desc').css('width', desc_width + 'px');		
		
	}	
	
	// special treatment for ie7, damit die buttons und desc richtig angezeigt werden
	// muss mit direkten css zuweisungen gearbeitet werden
	
	var close_button_width    = parseInt($('.close_button').css('width'));
	var bigpic_div_width      = $('#bigpic_div img').width();
	var close_button_new_left = parseInt(bigpic_div_width);
	
	//$('.close_button').css('left', '100px');
	//alert(close_button_new_left);
	
}
