slideshowInterval = 0;
play = true;
slideshow = false;

function setSlideshow(turnon) {
	if(turnon) {
		if(play) {
			window.clearInterval(slideshowInterval);
			slideshowInterval = window.setInterval("loadOne()", 7000);
		}
	} else {
		window.clearInterval(slideshowInterval);
	}
}
setSlideshow(true);
imageArray = [];

$(document).ready(function() {

	$(".t").live("mouseover", function(event) {
		$(this).css("background", "url('../img/bgh.png') repeat scroll 0 0 transparent");
	});
	
	$(".t").live("mouseout", function(event) {
		$(this).css("background", "url('../img/bg.png') repeat scroll 0 0 transparent");
	});

	// Video-Duh.
	$(".video").live("click", function(event) {
		event.preventDefault();
		$(".video img").hide();
		var playing = $(this);
		var p_left = playing.css("left");
		var p_top = playing.css("top");
		playing.addClass("play");
		$(".video").not(".play").fadeOut(500, function(){});
		$(this).animate({
			left: "10px",
			top: "34px",
			width: "591px",
			height: "383px"
		}, 500, function(){
			jwplayer(playing.attr("id")).setup({
				autostart: true,
				file: $(this).attr("href"),
				width: "591px",
				height: "383px",
				stretching: "exactfit",
				flashplayer: "/vid/player.swf"
			});
			jwplayer().onPause(function() {
				closeVideo(p_left, p_top, "#"+$(this).attr("id"));
			});
			jwplayer().onComplete(function() {
				closeVideo(p_left, p_top, "#"+$(this).attr("id"));
			});
		});
	});
	
	currentnavitem = "Home";
	currentsubnavitem = "";
	currentsubsubnavitem = "";

	$.address.init(function(event){
		//do som3thing
	}).change(function(event) {
		urlHashes = getUrlHashes();
				
		if (slideshow){
			$("#content").animate({
				right: "0"
			}, globalslidetime, "swing", function() {
				slideshow = false;
				$("#slideshow a img").attr("src", "/img/slideshow-out.png");
			});
		}
		
		if(urlHashes.length != 0) {
			if(urlHashes.length == 3) {
				if(urlHashes[1] != currentnavitem.toLowerCase()) {				
					grabAndShowContent(urlHashes[1]);
				}
			} else if(urlHashes.length == 4) {
				if(urlHashes[1] == "updates"){
					current_month = "";
					grabAndShowContent(urlHashes[1], urlHashes[2], current_month);
				} else {
					grabAndShowContent(urlHashes[1], urlHashes[2]);
				}
			} else if(urlHashes.length == 5){
				if(urlHashes[1] == "updates"){
					current_month = urlHashes[3];
					grabAndShowContent(urlHashes[1], urlHashes[2], current_month);
				}
			}
		}
	});
		
	// Switch the language
	$("#language").live("click", function(event) {
		event.preventDefault();
		if(urlHashes[0] == "de") {
			current_language = "DE";
		} else {
			current_language = "EN";
		}
		$.ajax({
			type: "GET",
			url: "../inc/switch.php?current_language=" + current_language + "&n1=" + urlHashes[1] + "&n2=" + urlHashes[2] + "&n3=" + urlHashes[3],
			success: function(html){
				$("#language a").attr("href", $("#language a").attr("href") + html);
				window.location = $("#language a").attr("href");
			}
		});
	});
	
	// CONFIG
	globalslidetime = 700; // Slide time for everything that slides in from the right side oooof life, dub dubu dub.
	globalmenuopen = 200; // Time the mainmenu takes to fup open
	globalmenuclose = 1000; // Delay for main menu to close
	
	if(getUrlHashes().length == 2) {
		window.location.hash = "#!/" + "home";
	};

	var showpage = location.href.split("#")[1];

	// Thumbnail to background.
	$("#images img").live("click", function(event) {
		event.preventDefault();
		// Stop background slideshow.
		setSlideshow(false);
		// Load thumbnail to background.
		$.vegas({
			src: $(this).attr("src").replace(/_thumbnail/g, ""),
			fade: 1500,
			loading: true,
			complete: function() {
				// Hide #content.
				$("#content").animate({
					right: "-749px"
				}, globalslidetime, "swing", function() {
					view_mode = "thumbnail";
					
					// Bind content hovering it back.
					$("#content").hover(function(event) {
						if(view_mode == "thumbnail") {
							$("#content").animate({
								right: "0px"
							}, globalslidetime, "swing", function() {
								// Continue the background slideshow.
								setSlideshow(true);
							});
						}
					});
					
					// Bind navi clicking it back.
					$("#navi ul li").click(function(event) {
						if(view_mode == "thumbnail") {
							setSlideshow(true);
						}
					});
					
					// Bind navi clicking it back.
					$("#navi ul li ul li").click(function(event) {
						if(view_mode == "thumbnail") {
							setSlideshow(true);
						}
					});
					
					// Bind clicking it back.
					$(".vegas-background").click(function(event) {
						if(view_mode == "thumbnail") {
							$("#content").animate({
								right: "0px"
							}, globalslidetime, "swing", function() {
								// Continue the background slideshow.
								setSlideshow(true);
							});
						}
					});
					
				});
			}
		});
	});
	
	// The Slideshow Button
	$("#slideshow").bind("click", function(event) {
		event.preventDefault();
		if(slideshow) {
			$("#content").animate({
				right: "0"
			}, globalslidetime, "swing", function() {
				slideshow = false;
				$("#slideshow a img").attr("src", "/img/slideshow-out.png");
			});
		} else {
			$("#content").animate({
				right: "-749px"
			}, globalslidetime, "swing", function() {
				slideshow = true;
				$("#slideshow a img").attr("src", "/img/slideshow-in.png");
			});
		}
	});

	// The Play/Pause Button.
	$("#playpause").bind("click", function(event) {
		if(play) {
			$("#playpause a img").attr("src", "/img/play.png");
			setSlideshow(false);
			play = false;
		} else {
			$("#playpause a img").attr("src", "/img/pause.png");
			play = true;
			setSlideshow(true);
		}
		event.preventDefault();
	});
	
	function clearStrings(string) {
		string = string.toLowerCase();
		string = string.replace(/ä/, "ae").replace(/ö/, "oe").replace(/ü/, "ue").replace(/ß/, "ss");
		string = string.replace(/<br>/g, "");
		
		//string = string.replace(/[^a-z_]/g, "");
		
		return string;
	}
	

	// Bind clicking the first level navigation.
	$("#navi > ul > li > a").click(function(event) {
		event.preventDefault();
		navitem = $(this).attr("id");
		// URL
		window.location.hash = "#!/" + $(this).html().toLowerCase();
	});

	// Ajax the first level content.
	function grabAndShowContent(navitem, subnavitem, current_month) {
		if(navitem != currentnavitem) {
			currentnavitem = navitem;
			$("#content").animate({
				right: "-754px"
			}, globalslidetime, "swing", function() {
				$.ajax({
					type: "GET",
					url: "../inc/content.php?content=" + navitem,
					success: function(html){
						$("#content").replaceWith(html);
						
						// Get the Subnavi. Oh I'm that ugly.
						$.ajax({
							type: "GET",
							url: "../inc/navi.php?&subid=" + navitem + "&sub=true",
							success: function(html){
								if(html != ""){
									$("#content").append(html);
									$("#content").append('<div class="subcontent"></div>');
									
								}
							}
						});
						
						$("#content").css("right", "-754px");
						$(".maincontent").show();
						
						$("#content").animate({
							right: "0"
						}, globalslidetime, "swing", function() {
							// subnavi-fetch-ism
							$("#content").find("#subnavi").show();
							$("#content").find("#subnavi").animate({
								right: "0"
							}, globalslidetime, "swing", function(){
								// Open subnavi
								if(subnavitem != "") {
									if(urlHashes[1] == "updates"){
										grabAndShowSubContent(navitem, subnavitem, "", current_month);
									} else {
										grabAndShowSubContent(navitem, subnavitem);
									}
								}
								
								// Bind the subnavi.
								$("#subnavi ul li a").click(function(event) {
									event.preventDefault();
									window.location.hash = "#!/" + $(this).parent().parent().parent().parent().find("div:eq(0)").attr("id") + "/" + $(this).html().toLowerCase();
								});
															
							});
						});
					}
				});
			});
		} else if(subnavitem != undefined) {
			if(current_month != undefined){
				grabAndShowSubContent(navitem, subnavitem, "subnavi", current_month);
			} else {
				grabAndShowSubContent(navitem, subnavitem, "subnavi");
			}
		}
	}
	
	// Bind clicking the second level navigation.
	$("#navi > ul > li > ul > li > a").click(function(event) {
		event.preventDefault();
		window.location.hash = "#!/" + clearStrings($("a:eq(0)", $(this).parent().parent().parent()).html()) + "/" + $(this).html().toLowerCase();
	});
	
	// Ajax the second level content.
	function grabAndShowSubContent(navitem, subnavitem, source, current_month) {
		if(subnavitem != currentsubnavitem) {
			currentsubnavitem = subnavitem;
			
			// Remove delay if we are in the same submenu
			var slidetime = globalslidetime;
			
			// This did shit to the SlideOutToTheRight Animation!!!!!!!!!!11111111111
			if(source != "subnavi") {
				if(navitem == currentnavitem) {
					slidetime = 1;
				}
			}
			
			if(subnavitem != undefined) {
				$(".subcontent").css("right", "0").animate({
					right: "-754px"
				}, slidetime, "swing", function() {
					if(current_month != undefined){
						$.ajax({
							type: "GET",
							url: "../inc/subupdate.php?content=" + subnavitem + "&current_month=" + current_month,
							success: function(html) {
								$(".subcontent").replaceWith(html);
								$(".subcontent").css("right", "-754px");
								$(".subcontent").show();
								$(".subcontent").animate({
									right: "0"
								}, globalslidetime, "swing", function() {
									// Bind the subsubnavi.
									$("#subsubnavi li a").live("click", function(event) {
										event.preventDefault();
										window.location.hash = "#!/" + $(this).parent().parent().parent().parent().find("div:eq(0)").attr("id") + "/" + $(this).parent().parent().parent().attr("id") + "/" + $(this).attr("id");
									});
								});
							}
						});
					} else {
						$.ajax({
							type: "GET",
							url: "../inc/subcontent.php?content=" + subnavitem,
							success: function(html) {
								$(".subcontent").replaceWith(html);
								$(".subcontent").css("right", "-754px");
								$(".subcontent").show();
								$(".subcontent").animate({
									right: "0"
								}, globalslidetime, "swing", function() {
									// Done
								});
							}
						});					
					}
				});
			}
		} else {
			// Remove delay if we are in the same submenu
			var slidetime = globalslidetime;
		
			$(".subcontent").css("right", "0").animate({
				right: "-754px"
			}, slidetime, "swing", function() {
				if(current_month != undefined){
					$.ajax({
						type: "GET",
						url: "../inc/subupdate.php?content=" + subnavitem + "&current_month=" + current_month,
						success: function(html) {
							$(".subcontent").replaceWith(html);
							$(".subcontent").css("right", "-754px");
							$(".subcontent").show();
							$(".subcontent").animate({
								right: "0"
							}, globalslidetime, "swing", function() {
								// Done
							});
						}
					});
				}
			});
		}
	}

	// Holy-Hover-Animation (magic)
	closing = new Object();
	closeobj = new Object();
	openobj = new Object();
	$("#navi > ul > li > ul").parent().find("a:eq(0)").each(function(h) {
		$(this).bind("mouseover", function() {
			if(!$("ul", $(this).parent()).is(":visible")) {
				for(var x in openobj) {
					window.clearTimeout(closeobj[$(this).html().toLowerCase().replace(/[^a-z]/g, "")]);
					closeMenu(openobj[x]);
				}
				
				$("ul", $(this).parent()).show();
				$("ul li", $(this).parent()).each(function(i) {
					if($(this).attr("orgheight") == undefined) {
						$(this).attr("orgheight", $(this).css("height"));
					}
				}).css("height", "1px").each(function(j) {
					$(this).stop().animate({
						height: $(this).attr("orgheight")
					}, globalmenuopen);
				});
				
				openobj[$(this).html().toLowerCase().replace(/[^a-z]/g, "")] = $(this);
			} else if(closing[$(this).html().toLowerCase().replace(/[^a-z]/g, "")]) {
				closing[$(this).html().toLowerCase().replace(/[^a-z]/g, "")] = false;
				window.clearTimeout(closeobj[$(this).html().toLowerCase().replace(/[^a-z]/g, "")]);
			}
		}).bind("mouseout", function() {
			closing[$(this).html().toLowerCase().replace(/[^a-z]/g, "")] = true;
			var $tmpthis = $(this);
			closeobj[$(this).html().toLowerCase().replace(/[^a-z]/g, "")] = window.setTimeout(function() { closeMenu($tmpthis); }, globalmenuclose);
		});
		$("ul", $(this).parent()).bind("mouseout", function() {
			var $tmpthis = $(this).parent().find("a:eq(0)");
			closing[$tmpthis.html().toLowerCase().replace(/[^a-z]/g, "")] = true;
			closeobj[$tmpthis.html().toLowerCase().replace(/[^a-z]/g, "")] = window.setTimeout(function() { closeMenu($tmpthis); }, globalmenuclose);
		}).bind("mouseover", function() {
			closing[$(this).parent().find("a:eq(0)").html().toLowerCase().replace(/[^a-z]/g, "")] = false;
			window.clearTimeout(closeobj[$(this).parent().find("a:eq(0)").html().toLowerCase().replace(/[^a-z]/g, "")]);
		});
	});
	
	// Adoptable close-menu-function
	function closeMenu(object) {
		if(object != null) {
			$("ul li", object.parent()).each(function(k) {
				$(this).stop().animate({
					height: "1px"
				}, globalmenuopen, function() {
					$(this).parent().hide();
					closing[$(this).html().toLowerCase().replace(/[^a-z]/g, "")] = false;
					openobj[$(this).html().toLowerCase().replace(/[^a-z]/g, "")] = null;
				});
			});
		}
	}
});

// DO DA PRELOAD BRAAAAAAAAAAAAAAAAAAAAAAH
function startPreload(firster) {
	if($.cookie("SpiesUndSchwarz_IntroShown")) {
		firster = false;
	} else {
		$.cookie("SpiesUndSchwarz_IntroShown", "yes", { path: "/" }); // DORKNESS PREVAILS!
	}

	if(firster) {
		$("#home").hide();
		$("#navi").hide();
		$("#sm").hide();
		$("#IMMAPRELOADINMALAZOR").append('<img src="' + "../img/bg/intro.jpg" + '" />');
	}
	
	$.ajax({
		type: "GET",
		url: "../inc/bg.php",
		success: function(html) {
			var bgArray = html.split("\n");
			$.each(bgArray, function(key, value) {
				// Why the fork do I need this. There should be no empty value!?
				if(value != "") {
					imageArray.push(value);
				}
			});
			
			var ii = 0;
			while(ii < 5 && imageArray.length > 0) {
				$("#IMMAPRELOADINMALAZOR").append('<img src="' + imageArray[0] + '" />');
				imageArray.shift();
				ii++;
			}
			
			loadOne(firster);
		}
	});
}

function loadOne(firster) {
	if(imageArray.length > 0) {
		$("#IMMAPRELOADINMALAZOR").append('<img src="' + imageArray[0] + '" />');
		imageArray.shift();
	}
	
	var next_bg_image = $("#IMMAPRELOADINMALAZOR").find("img:first");
	$("#debug").html(next_bg_image.attr("src"));
	if(next_bg_image.length == 0){
		startPreload();
		return;
	}
	
	// CRAP STARTS HERE
	$.vegas({
		src: next_bg_image.attr("src"),
		fade: 1500,
		loading: false
	});
	// CRAP ENDS HERE
	
	if(firster){
		$(this).oneTime(2500, function() {
			loadOne();
			$(this).oneTime(400, function() {
				$("#home").fadeIn(1500);
				$("#navi").fadeIn(1500);
				$("#sm").fadeIn(1500);
			});
		});
		next_bg_image.remove();
	}
	next_bg_image.remove();
}

function getUrlHashes(){
    var hashes = [];
	var count = (window.location.href.split('/').length) - 1;
    var slashes = window.location.href.slice(window.location.href.indexOf('/', 7)).split('/');
    for(var i = 0; i < count; i++) {
		if (slashes[i] != "") {
			if (slashes[i] != "#!") {
				hashes.push(slashes[i]);
			}
		}
    }
    return hashes;
}
	
function closeVideo(p_left, p_top, playing_item){
	jwplayer().remove();
	$(".play").animate({
		left: p_left,
		top: p_top,
		width: "290px",
		height: "187px"
	}, 500, function() {
		$(".video img").show();
		$(".video").fadeIn(500);
		$(playing_item).removeClass("play");
	});
}

