/* ############ */
/* ### AJAX ### */
/* ############ */

var req = null;

// Abfrage von Daten requesten
function request(current_youth, next_youth, current_news, next_news) {
	
	// Nur wenn Hauptvariable (id Datensatz durch flyer; oder datestring durch monatsschalter) gegeben ist, weitermachen
	if (current_youth!=next_youth || current_news!=next_news) {
		
			
					// Hier wird das XMLHttpRequest Objekt angelegt
					// Für den IE6 sowie den IE7 wird der Request mit den Internet Explorer eigenen XMLHttpRequest überschrieben
					
					// Normale Browser
					try {
						req = new XMLHttpRequest();
					}
					catch (e) {
						// IE7
						try {
							req = new ActiveXObject("Msxml2.XMLHTTP");
						}
						
						catch (e) {
							// IE8
							try {
								req = new ActiveXObject("Microsoft.XMLHTTP");
							}
							
							// Weder noch => Stop
							catch (failed) {
								req = null;
							}
						}  
					}
					
					// Fehlerausgabe: Request nicht vorhanden
					if (req == null) {
						//alert("Error: Request Object could not be created!");
					}
					// Ansonsten weitermachen mit der Ausgabe
					else {
						
						
										
						
						
						// HTTP Get Request an eine Datei
						var params = "";
						if (current_youth!=next_youth) {
							if(current_youth) {
									params = params + "&current_youth=" + current_youth;	
							}
							if(next_youth) {
									params = params + "&next_youth=" + next_youth;	
							}
						}
						
						if (current_news!=next_news) {
							if(current_news) {
									params = params + "&current_news=" + current_news;	
							}
							if(next_news) {
									params = params + "&next_news=" + next_news;	
							}
						}
						

						if(params) {
						
							var url = "/?type=99";
							req.open("POST", url, true);
									
							// Wurde die Datei erfolgreich geladen => weitermachen
							req.onreadystatechange = handleRequest;
									
								
							
							
							
							// Header und Daten senden
							req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
							req.setRequestHeader("Content-length", params.length);
							req.setRequestHeader("Connection", "close");
							
							// Post Daten senden
							req.send(params);
						}
							
					}
			
	}
	else {
		return false;
	}
}


 
function handleRequest() {
	// Wird für den readyState der Fall 4 erreicht (= abgeschlossender Transfer; siehe http://www.w3.org/TR/XMLHttpRequest) => weitermachen
	if (req.readyState == 4) {
		// Hat der HTTP Statuscode den Wert 200 (= erfolgreiche Übertragung) => weitermachen
		if (req.status == 200) {
			// Antwort des Servers in JSON Form
			var output = eval('(' + req.responseText + ')');

			output_news = output.output_news;
			output_youth = output.output_youth;
			output_currentyouth = output.output_currentyouth; 
			output_nexttyouth = output.output_nextyouth;
			output_currentnews = output.output_currentnews;
			output_nextnews = output.output_nextnews;
			
			
		//TEMPORÄRE DIVS DAMIT MAN DIE ENDHÖHE AUSLESEN KANN, SONST IST DER FADEOUT EFFEKT NICHT WIRKLICH SCHÖN	
			
			$("#axquicklinks_quicklinks_tmpnews").css("height","auto");
			$("#axquicklinks_quicklinks_tmpyouth").css("height","auto");
						
			//TEMPORÄRE DIVS
			if(output_news) {
				$("#axquicklinks_quicklinks_tmpnews").html(output_news);
			}
			if(output_youth) {
				$("#axquicklinks_quicklinks_tmpyouth").html(output_youth);
			}
						
		
						
			//HÖHE NEWS UND JUNGEND AUS TEMPORÄREN DIVS
			if($("#axquicklinks_quicklinks_tmpyouth").height() >= $("#axquicklinks_quicklinks_tmpnews").height()) {
				$("#axquicklinks_quicklinks_tmpnews").css("height",$("#axquicklinks_quicklinks_tmpyouth").height() + "px");
				newscontainerHeight = $("#axquicklinks_quicklinks_tmpyouth").height();
			}
			if($("#axquicklinks_quicklinks_tmpnews").height() >= $("#axquicklinks_quicklinks_tmpyouth").height()) {
				$("#axquicklinks_quicklinks_tmpyouth").css("height",$("#axquicklinks_quicklinks_tmpnews").height() + "px");
				newscontainerHeight = $("#axquicklinks_quicklinks_tmpnews").height();
			}
			//MINDESTHÖHE 100
			if($("#axquicklinks_quicklinks_tmpnews").height()<100) {
				$("#axquicklinks_quicklinks_tmpnews").css("height","100px");
				$("#axquicklinks_quicklinks_tmpyouth").css("height","100px");
				newscontainerHeight = 100;
			}
						
			newscontainerHeight_bug = newscontainerHeight+2;//BORDER TOP/BOTTOM-BUG 2px
			
					
						
			
		//---			
					
			
						
			
			//NEWSTEMPLATE ÜBERBLENDEN
			if(output_news) {
				
				
				
				$("#templatenews").animate({"opacity":"0"}, { queue:true, duration:500, complete: function() {
					$("#templatenews").html(output_news);
							
							
					$("#templatenews").animate({"opacity":"1"}, { queue:true, duration:500, complete: function() {								
					}});
					//HÖHEN ANPASSEN
					$("#axquicklinks_quicklinks_newscontainer").animate({"height": newscontainerHeight_bug+ "px"}, { queue:true, duration:250, complete: function() {
					}});
					$("#axquicklinks_quicklinks_youth").animate({"height": newscontainerHeight+ "px"}, { queue:true, duration:250, complete: function() {
					}});
					$("#axquicklinks_quicklinks_news").animate({"height": newscontainerHeight+ "px"}, { queue:true, duration:250, complete: function() {
					}});
					
						
				}});
				
				
			}
			
			//YOUTHTEMPLATE ÜBERBLENDEN
			if(output_youth) {
				
				
				$("#templateyouth").animate({"opacity":"0"}, { queue:true, duration:500, complete: function() {
					$("#templateyouth").html(output_youth);
					
					
					$("#templateyouth").animate({"opacity":"1"}, { queue:true, duration:500, complete: function() {
					}});
					//HÖHEN ANPASSEN
					$("#axquicklinks_quicklinks_newscontainer").animate({"height": newscontainerHeight_bug+ "px"}, { queue:true, duration:250, complete: function() {
					}});
					$("#axquicklinks_quicklinks_youth").animate({"height": newscontainerHeight+ "px"}, { queue:true, duration:250, complete: function() {
					}});
					$("#axquicklinks_quicklinks_news").animate({"height": newscontainerHeight+ "px"}, { queue:true, duration:250, complete: function() {
					}});
					
						
				}});
				
			}
			
			
			
			
			//SET-TIMEOUT MIT NÄCHSTEN DATENSÄTZEN
			setTimeout(function(){ request(output_currentyouth, output_nexttyouth,output_currentnews, output_nextnews) }, 8000);
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		}
		// Fehlermeldung: Statuscode nicht gleich 200
		else {
				//alert("Error: The requested status is " + req.status);
		}
	}
	else {
		return false;
	}
}


