var time = 500;

var newsboxHTML = null;
var anzahl_news = 7;

var terminboxJSON = null;
var anzahl_termine = 3;

var spielplanJSON = null;

var lockstatus_gmaps = "unlocked";	// unlocked, locked
$(document).ready(function(){	

$("#home").removeClass("navigation").addClass("navigationaktiv");

/*		TERMIN-BOX		*/
	$.ajax({
		type: "POST",
		url: "php/index/terminbox.php",
		data: "anzahl_termine="+anzahl_termine+"&team="+team,
		async: false,
		success: function(phpData) {
			terminboxJSON = eval("(" + phpData + ")");
			for(var i = 0; i < anzahl_termine; i++) {
				var html = terminboxJSON[i].html;
				$("#terminbox_datum").append(html);
			}
		},
		error: function() {
			alert("Kein Zugriff auf Inhalt der Termin-Box");	
		}
	});

/*		NEWS-BOX		*/
	$.ajax({
		type: "POST",
		url: "php/index/newsbox.php",
		data: "anzahl_news="+anzahl_news+"&team="+team+"&kategorie=newsbox",
		async: false,
		success: function(phpData) {
			$("#newsbox_datum").html(phpData);
			newsboxHTML = phpData;
		},
		error: function() {
			alert("Kein Zugriff auf Inhalt der News-Box");	
		}
	});

/*		SPIELPLAN		*/
	$.ajax({
		type: "POST",
		url: "php/index/spielplan.php",
		data: "team="+team+"&saison="+akt_saison,
		async: false,
		success: function(phpData) {
			var spielplan_daten = phpData.split(" - ");
			
			spielplanJSON = eval("(" + spielplan_daten[0] + ")");
			var runde = spielplan_daten[1];
			var anz_spiele = spielplanJSON.length;
			var akt_spiel;
			var html = "<div class='duell'>";
				html += "<div class='runde'>" + runde + ". Runde</div>";
			
			for(var i = 0; i < anz_spiele; i++) {
				akt_spiel = spielplanJSON[i];
				var heimTore, gastTore, heimstyle, gaststyle;
				akt_spiel.HeimTore != null ? heimTore = akt_spiel.HeimTore : heimTore = "-"; 
				akt_spiel.GastTore != null ? gastTore = akt_spiel.GastTore : gastTore = "-"; 
				akt_spiel.Heim == "FC Adrenalin" ? heimstyle=" style='color: yellow;'" : heimstyle="";
				akt_spiel.Gast == "FC Adrenalin" ? gaststyle=" style='color: yellow;'" : gaststyle=""; 
				var datetime = akt_spiel.Datum.split(" ");
				var date = datetime[0];
				var datum = date.split("-");
				var tag = datum[2]; var monat = datum[1]; var jahr = datum[0];
				var time = datetime[1];
				var zeit = time.split(":");
				var stunde = zeit[0]; var minute = zeit[1];
				var createDate = new Date(jahr, monat-1, tag, 0, 0);
				var dayOfWeek = createDate.getDay();
				var wochentag = new Array("So.", "Mo.", "Di.", "Mi.", "Do.", "Fr.", "Sa.");
			
				html += "<table style='margin-top: 3px;'>";
				html += "<tr><th class='left'" + heimstyle + ">" + akt_spiel.Heim + "</th><th class='center'>" + heimTore + ":" + gastTore + "</th><th class='right'" + gaststyle + ">" + akt_spiel.Gast + "</th></tr>";
				html += "<tr><td colspan='3' class='datum'>" + wochentag[dayOfWeek] + " " + tag + "." + monat + "." + jahr + ", " + stunde + ":" + minute + " Uhr</td></tr>";
				html += "</table>";
			}
			
			html += "</div>";
			
			$("#aktuelle_runde").append(html);
		},
		error: function() {
			alert("Kein Zugriff auf Inhalt der News-Box");	
		}
	});
	
	
	$(".news_hover").click(function() {
		if(lockstatus == "unlocked") {
			lockstatus = "locked";
			var news_id_split = $(this).attr('id').split("_");
			var news_art = news_id_split[0];
			var news_id = news_id_split[1];
			var spiel_id = news_id_split[2];
			
			if(news_art == "Match") {
				$.ajax({
					type: "POST",
					url: "php/index/newsbox.php",
					data: "news_id="+news_id+"&spiel_id="+spiel_id+"&kategorie=news_match",
					async: false,
					success: function(phpData) {
						changeInhalt("newsbox_inhalt", phpData, "change_newsbox", "newsbox_datum");
					},
					error: function() {
						alert("Kein Zugriff auf Inhalt der News-Box");	
					}
				});
			}	
		}
	});		
	
	$(".termin_hover").hover(
		function () {$(this).animate({opacity: "1.0"}, 0);}, 
		function () {$(this).animate({opacity: "0.5"}, 0);}
	);
	
	$(".news_hover").hover(
		function () {$(this).animate({opacity: "1.0" }, 0);}, 
		function () {$(this).animate({opacity: "0.5" }, 0);}
	);
	
	$("#terminbox_back").click(function () {
		var content_height = $("#terminbox_datum").height() + 20;
		$(".change_terminbox").animate({height: content_height}, time);
		$("#terminbox_inhalt").fadeOut(time, function() {
			$("#terminbox_datum").fadeIn(time, function(){
				$("#terminbox_ueberschrift").remove();
				$("#terminbox_inhalt_table").remove();
				$(".clickGMaps").remove();
				lockstatus = "unlocked";
			});
		});
	});
	
	$("#newsbox_back").click(function () {
		var content_height = $("#newsbox_datum").height() + 20;
		$("#change_newsbox").animate({height: content_height}, time);
		$("#newsbox_inhalt").fadeOut(time, function() {
			$("#newsbox_datum").fadeIn(time, function(){
				$("#newsbox_ueberschrift").remove();
				$("#newsbox_match").remove();
				$("#newsbox_content").remove();
				lockstatus = "unlocked";
			});
		});
	});	
});

function google_maps(ort) {
	if(lockstatus_gmaps == "unlocked") {
		lockstatus_gmaps = "locked";
		$.ajax({
			type: "POST",
			url: "php/index/google_maps.php",
			data: "ort="+ort,
			async: false,
			success: function(phpData) {
				var adresse = phpData;
				var html = 	"<div class='main_box' id='gmaps'>" +
						"<div class='ueberschrift' style='position: relative;'>" + ort + "<div  id='close_gmaps' onclick='close_gmaps()' /></div>" +
						"<iframe src='http://www.map-generator.net/extmap.php?name=" + ort + "&address=" + adresse + "&width=498&height=300&maptype=hybrid&amp&zoom=16' width='498' height='300' marginwidth='0' marginheight='0' frameborder='0' scrolling='no'></iframe>" +
						"<div class='mainbox_close'></div>" +
					"</div>";
				$("#main_content").prepend(html);
				$("#gmaps").show("slow");
			},
			error: function() {
				alert("Kein Zugriff auf Inhalt der Google-Maps");	
			}
		});	
	}
}

function close_gmaps(){
	$("#gmaps").hide("slow", function() {
		$("#gmaps").remove();
	});
	lockstatus_gmaps = "unlocked";
}

function changeInhalt(inhalt, html, animation, fadingOut) {
	if(html != "back") {
		$("#"+inhalt).prepend(html);
	}
	var content_height = $("#"+inhalt).actual("outerHeight");
	$("#"+animation).animate({height: content_height}, time);

	$("#"+fadingOut).fadeOut(time, function() {
		$("#"+inhalt).fadeIn(time);
		if(html == "back") {
			$("#" + fadingOut).html("");
			$("#" + fadingOut).hide();
		}
		lockstatus = "unlocked";
	});
}

function show_termin(art, id) {
	if(lockstatus == "unlocked") {
		lockstatus = "locked";	
		
		$.ajax({
			type: "POST",
			url: "php/index/terminbox.php",
			data: "termin_id="+id+"&art="+art,
			async: false,
			success: function(phpData) {
				var html = phpData;
				changeInhalt("terminbox_inhalt", html, "change_terminbox", "terminbox_datum");
			},
			error: function() {
				alert("Kein Zugriff auf Inhalt der Termin-Box");	
			}
		});
	}
}
