/**
*
*
*
*/
function showDetails(e){
	var myObj = null;
	
	if (arguments.length == 1) {
		e.preventDefault();
		this.blur();
		myObj = this;
	}
	else {
		myObj = arguments[1];
	}
	
	if(!$(myObj).is(':animated') && !$('div#details').is(':animated')){
	
		if($('.thumb.active').length > 0){
			closeDetails(null, myObj);
		}
		else if($('.thumb.active').length == 0){
			setDetailBox(myObj);
		}
		
		var bio = $(myObj).next().next().html();
		
		$(myObj).delay(600).animate({opacity: 0}, 800, function(e){
			$(myObj).hide();
			$(myObj).parents('.thumb').addClass('active');
		});


		var close = $(myObj).parent().children('a.close');
		close.css('opacity', 0).show().click(closeDetails);
		close.delay(600).animate({opacity: 1}, 600, function(e){
			
			$('div#details').css({opacity: 0, height: 'auto'});
			
			var x = $(myObj).parents('.thumb').position().left + 130;
			$('div#details .arrow').css({left: x});
			
			$('div#details .detailcontent').html(bio);
			var targetHeight = $('div#details').height();

			$('div#details').css('height', 0).show().animate({opacity: 1, height: targetHeight}, 600, function(e){
				$('html, body').animate({scrollTop: $('.thumb.active').offset().top}, 'slow');
			});

			$('div#details a.close').click(closeDetails);
		});
	}
}


function closeDetails(e, test){

	if(e != null && isNaN(e)){
		e.preventDefault();
		$(this).blur();
	}
	
	
	if(!$('div.person').is(':animated') && !$('div#details').is(':animated')){
		
		
		if($('div#details').is(':visible')){
		$('div#details').animate({opacity: 0, height: 0}, 600, function(e){
				
				if(test != null)
				setDetailBox(test);
			
				var close = $('div.person.active a.show');

				$('div.person.active a.close').animate({opacity: 0}, 800, function(e){
			        $(this).hide();
					$(this).parents('.thumb').removeClass('active');
				});



				close.css('opacity', 0).show();
				close.animate({opacity: 1}, 800, function(e){
					
				});
		});
		
		
		}
	}
}


function setDetailBox(test){

	var target = null;
	$('.landingbox').each(function(index){
		if(target == null)
			target = $(this);
			
		var t = $(this).position().top;
		
		if(t == $(test).parents('.thumb').position().top){
			target = $(this);
			return false;
		}
		
	});
	
	
	var where = target;
	var clone = $('div#details').html();
	$('div#details').remove();
	$('<div>').attr('id', 'details').html(clone).appendTo(where);

}


$(document).ready(function() {
	
	
	setMaps();
	function setMaps(){
		
		if($('.map').length > 0){
		
			$.each($('.map'), function(index, value) { 
				setGoogleMap($(this)[0], $.trim($(this).text()));
		
			});
		
		}
	}
	
	
	

	$('.person a.show').click(showDetails);
	$('.person a.close').click(closeDetails);
	
	
	function setGoogleMap(container, address) {
		
		var map;
		var geocoder	    = new google.maps.Geocoder();
		var latlng	    	= new google.maps.LatLng(-34.397, 150.644);

		var myOptions = {
		    zoom:							15,
		    center:							latlng,
		    mapTypeId:						google.maps.MapTypeId.ROADMAP,
		    navigationControlOptions:		{style: google.maps.NavigationControlStyle.SMALL},
		    mapTypeControl:					false,
		    navigationControl:				true,
		    scaleControl:					true
		};
		
		

		map = new google.maps.Map(container, myOptions);


		if (geocoder) {
		    geocoder.geocode( { 'address': address}, function(results, status) {
		    if (status == google.maps.GeocoderStatus.OK) {
			map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker({
			    map: map,
			    position: results[0].geometry.location
			});
		    } else {
				alert("Geocode was not successful for the following reason: " + status);
		    }
		    });
		}
	}
	
	
	$('.DEMO').click(demo);
	function demo(e){
		e.preventDefault();
		$(this).blur();
		
		jAlert('Wir freuen uns Ihnen diesen Bereich demnächst vorzustellen.', 'In Bearbeitung');
	}
	
	
	
	$('a.top').click(function(e){
		e.preventDefault();
		$(this).blur();
		$('html, body').animate({scrollTop:0}, 'slow');
	});
	
});
