$(document).ready(function(){

	var language = "DE";
	var currentProject = 0;
	var previousProject = 0;
	var countProjects = 0;
	var currentPage = "index";
	var offset = 9;
	
	init();
	
	$("#buttons a").click(function(event) { cycleProjekte(event.target.id); } );
	$("#mapNumbers a").hover(function(event) { hoverCarousel(event.target.id);	}, function(){ clearCarousel(); } );
	$("#mapNumbers a").click(function(event) { navProjekteByLocation(event.currentTarget.id); } );
	$(".mapEntry a").hover(function(event) { hoverCarouselById(event.target.id);	}, function(){ clearCarousel(); } );
	$(".quickLink").click(function(event) { navProjekte(event.currentTarget.id); } );



		function init() {
			hideStuff();
			}
		

		function hideStuff() {
			countProjects = $("div.projekt").length - 1 - offset;
			var locationProjekte = $("body").hasClass('projekte');
			if (locationProjekte) {
				$("div.projekt").hide();		
				showCurrentProject();
				currentPage = "projekte";
				} else {
				}			
			}

		
		function showCurrentProject() {
			var q = $.parseQuery();
			if (q.projekt) {
				$("div.p"+q.projekt).show();
				currentProject = q.projekt;
				} else if (q.archiv) {
				$("div.a"+q.archiv).show();
				currentProject = q.archiv;
				} else {
				$("div.p0").show();
				}
			resetButtons();
			tagCarousel();
			}
		
		
		function cycleProjekte(caller) {
			previousProject = currentProject;
			if (caller == "next") {
				if(currentProject<countProjects) {
					currentProject++;
					} else {
					currentProject = 0;					
					}
				showHideProjects(currentProject, previousProject);
				} else {
				if(currentProject>0) {
					currentProject--;
					} else {
					currentProject = countProjects;							
					}
				showHideProjects(currentProject, previousProject);
				}
			resetButtons();
			tagCarousel();
			}
		
		
function navProjekte(event) {
			previousProject = currentProject;
			currentProject = event.charAt(event.length-1);
			showHideProjects(currentProject, previousProject);
			resetButtons();
			}
		
		
function navProjekteByLocation(event) {
			previousProject = currentProject;
			currentProject = event.charAt(event.length-1);
			showHideProjectsByLocation(currentProject, previousProject);
			resetButtons();
			}
		
		
		function showHideProjects(appear, disappear) {
			/*alert("show: " + appear + " | hide: " + disappear);*/
			$('div.p'+disappear).fadeOut( 'fast', function() { $('div.p'+appear).fadeIn('fast',function() { tagCarousel(); }); });
			}

		function showHideProjectsByLocation(appear, disappear) {
			/*alert("show: " + appear + " | hide: " + disappear);*/
			$('div.p'+disappear).fadeOut( 'fast', function() { $('div.p'+appear).fadeIn('fast',function() { tagCarousel(); }); });
			}
		
		function resetButtons() {
			var q = $.parseQuery();
			if (q.archiv) {
				$("#buttons").hide();
				} else {$(
				"#buttons").show();
				if (currentProject>0 && currentProject<countProjects) {
				$("#previous").removeClass();
				$("#next").removeClass();
				$("#previous").addClass("active");
				$("#next").addClass("active");
				} else {
				if (currentProject>=countProjects) { $("#next").removeClass(); $("#previous").addClass("active"); }
				if (currentProject<=1) { $("#previous").removeClass(); $("#next").addClass("active"); }
				}
			}
		}
		
		
		function clearCarousel() {
			$("#carousel a").each( function() { $(this).removeClass(); });
			}
		
		
		function tagCarousel() {
			clearCarousel();
			var q = $.parseQuery();
			if (q.archiv) {			
				$("#ca"+currentProject).addClass("active");
				} else {				
				$("#c"+currentProject).addClass("active");
				}		
			}
		
		
		function hoverCarousel(event) {
			clearCarousel();
			var q = $.parseQuery();
			if (q.archiv) {
				} else {
				var currentHover = "l" + event.charAt(event.length-1);
				$("#carousel a").each( function() { 
					if($(this).attr("rel") == currentHover) {
						$(this).addClass("active");
						}
					});
				}
			}
		
		function hoverCarouselById(event) {
			clearCarousel();
			var q = $.parseQuery();			
			if (q.archiv) {
				} else {
				var currentHover = "c" + event.charAt(event.length-1);
				$("#carousel a").each( function() { 
					if($(this).attr("id") == currentHover) {
						$(this).addClass("active");
						}
					});
				}
			}
		
});
