$(document).ready(function() {
						   
	
		// Intro-Animation, wenn die Hauptseite geladen wird:
		if ($("div#intro").length > 0) {
			$("div#header").hide();
			$("div#content").hide();
			$("div#bottom").hide();
			$("div#main_content").hide();
			$("div#main_header").hide();
			
			setTimeout(function() {
				$("div#intro").fadeIn(250, function() {
					$("div#intro").animate({ height: "66px" }, 1500, "easeInOutExpo", function() {
								var time = 2000;
								$("div#intro").fadeOut(time);
								$("div#header").fadeIn(time);
								$("div#content").fadeIn(time);
								$("div#main_content").fadeIn(time);
								$("div#main_header").fadeIn(time);
								$("div#bottom").fadeIn(time);		
					});
				});
			},500);
		}
		
		// Email-Adressen chiffrieren
		$("a.mail").attr("href",'mailto:'+'__i_nf_o_@_3_d__f_act_or_y.d_e_'.replace(/_/g,''))
				 .html('__i_nf_o_@_3_d__f_act_or_y.d_e_'.replace(/_/g,''));
				 
		$("span.mail").html('__i_nf_o_@_3_d__f_act_or_y.d_e_'.replace(/_/g,''));	
		
		// Formular-Funktionen:	
		$("input#name").focus(function() { if ($(this).val() == "NAME") $(this).val(""); })
					   .blur( function() { if ($(this).val() == "") $(this).val("NAME"); });
					   
		$("input#email").focus(function() { if ($(this).val() == "EMAIL") $(this).val(""); })
					   .blur( function() { if ($(this).val() == "") $(this).val("EMAIL"); });

		$("textarea#comment").focus(function() { if ($(this).val() == "NACHRICHT") $(this).val(""); })
					   .blur( function() { if ($(this).val() == "") $(this).val("NACHRICHT"); });
		
		// "SEND"-Text sendet Kontakt-Email
		
		$("a.submit").click(function() {
			$("a.submit").after('<span id="loading">Bitte warten...</span>');

			var name = $('input#name').val();
			var email = $('input#email').val();
			var comment = $('textarea#comment').val();
	
			$.ajax({
				type: 'post',
				url: 'php_scripts/sendMail.php',
				data: 'name=' + name + '&email=' + email + '&comment=' + comment,
	
				success: function(results) { 
					$("span#loading").remove(); 
					$("div#errors").html(results); 
					if (results.indexOf("erfolgreich") >= 0) { // es gab keine Fehler bei der Email-Verarbeitung
						$("a.submit").remove(); // dann auch den Senden-Button entfernen
					}
				}
			}); // end ajax
		});
		
		// Referenzenliste mit aktuellen Eintraegen laden
		
		var currentReferencePage = 1;			// Aktuell angezeigte Seite, zu Anfang die erste
		var totalNumberOfReferencePages = 3;	// Anzahl der Seiten mit Referenzen insgesamt, 
												// entspricht der Anzahl der php-Dokumente im Order 'content/reference_sites' durch 2
		var loadingsComplete = 0;				// Wird fuer reshowIfComplete benoetigt
		var slideLeft = true;
		
		// Fuellt die beiden divs auf der Referenzseite mit den aktuell anzuzeigenden Referenzen:
		$.fn.loadReferenceEntries = function(skipShowing) {
			if ($(this).attr("id") == "list_left") {
				var url = "content/reference_sites/ref" + currentReferencePage + "_1.php";
			} else if ($(this).attr("id") == "list_right") {
				var url = "content/reference_sites/ref" + currentReferencePage + "_2.php";
			}
			if (skipShowing) {
				$(this).load(url);
			} else {
				$(this).load(url, function() { $("body").reshowIfComplete(); });
			}
		}
		
		// Zeigt die beiden Reference-divs wieder an, wenn beide geladen sind
		$.fn.reshowIfComplete = function() {
			loadingsComplete++;
			if (loadingsComplete == 2) {
				loadingsComplete = 0;
				var direc = slideLeft ? "left" : "right"
				$("div#content.reference div#list_left").show("drop", { direction: direc }, 400);
				$("div#content.reference div#list_right").show("drop", { direction: direc }, 400, function() {
					$("div#content.reference div#navigation").showNavigationArrows();
				});
			}
		}

		// Navigation in den Bilder eines Projekts:
		
		var currentProjectPic = 1;
		var totalNumberOfProjectPics = $("a.pic_page").length;
		if ($("a.pic_page").length != 0) {
			var projId = (($("a.pic_page")).attr("id").split("_"))[0];
		} else {
			var projId = 0;
		}
		
		$("a.proj_link[href$='id=" + projId + "']").addClass("active");
		
		$.fn.showCurrentProjectPic = function() {
			$("img.gal_pic").fadeOut(400);
			$("div#pictures div#pic td#pic_cell").html('<a href="content/projects/p' + projId + '/img' + currentProjectPic +'.jpg" class="thickbox"><img src="content/projects/p' + projId + '/img' + currentProjectPic +'_tn.jpg" class="gal_pic" /></a>');
			$("img.gal_pic").fadeIn(400);
			// Beschreibung des Bildes aus seiner Datei laden:
			$.ajax({
				type: 'post',
				url: 'php_scripts/get_pic_name.php',
				data: 'proj_id=' + projId + '&pic_id=' + currentProjectPic,
	
				success: function(results) { 
					$("span#pic_descr").html(results);
				}
			}); // end ajax
			$("body").showProjectPicNavigationButtons();
			// Binde den ImageViewer an das neue Bild:
			if (projId > 0) {
				tb_init('a.thickbox');
			}
		}
		
		$("a.pic_page").click(function() {
			$("div#pictures div#navigation").html("");
			currentProjectPic = ($(this).attr("id").split("_"))[1];
			$("a.pic_page").removeClass("active");
			$(this).addClass("active");
			$("body").showCurrentProjectPic();
		});
		
		$.fn.showProjectPicNavigationButtons = function() {
			var content_left = "";
			var content_right = "";
			if (currentProjectPic < totalNumberOfProjectPics) {
				content_right += '<img src="media/images/arrowr.png" id="proj_navigation_next" /><br />';
			}
			if (currentProjectPic > 1 && totalNumberOfProjectPics > 1) {
				content_left += '<img src="media/images/arrowl.png" id="proj_navigation_prev"/><br />';
			}
			$("div#pictures div#navigation_left").html(content_left);
			$("div#pictures div#navigation_right").html(content_right);
			$(this).addPicNavigationHandlers();
		}
		
		// Belegt die Navigationspfeile mit dem Klickereignis, die vorherige bzw. naechste Seite anzuzeigen:
		$.fn.addPicNavigationHandlers = function() {
			
			$("img#proj_navigation_prev").click(function() {
				currentProjectPic--;
				$("body").showCurrentProjectPic();
				$("a.pic_page").removeClass("active");
				$("a.pic_page#" + projId + "_" + currentProjectPic).addClass("active");
			});
			
			$("img#proj_navigation_next").click(function() {
				currentProjectPic++;
				$("body").showCurrentProjectPic();
				$("a.pic_page").removeClass("active");
				$("a.pic_page#" + projId + "_" + currentProjectPic).addClass("active");
			});
		}
		
		// Beim Laden der Seite das erste Bild laden:
		$("body").showCurrentProjectPic();
		$("a.pic_page#" + projId + "_" + currentProjectPic).addClass("active");
		$("div#pictures div#navigation").showProjectPicNavigationButtons();
		
});
