/*
	Das Letzte Schweigen
	HTML 5 / jQuery Website
	Sebastian Bayer, 2010 for Novagraphix
*/

var ie = 0;
var gbStart = 1;
var gbMax = 1;
var zitat = 1;
var num_zitate = 4;

function apple() {
	return 	( (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPod/i)) );
}

function getFlashMovieObject(movieName) {
	if (window.document[movieName]) {
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName];
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		return document.getElementById(movieName);
	}
}


$.fn.animate2 = function(css, speed, fn) {
	if(speed === 0) { // differentiate 0 from null
		this.css(css)
		window.setTimeout(fn, 0)
	} else {
		if(apple()) {
			var s = []
			for(var i in css) 
				s.push(i)
			this.css({webkitTransitionProperty: s.join(", "), webkitTransitionDuration: speed+ "ms" });
			window.setTimeout(function(x,y) {x.css(y)},0, this, css);
			window.setTimeout(fn, speed);
		} else {
			this.animate(css, speed, fn)
		}
	}
}

function nextZitat() {
	if(apple()) return false;
	zitat++;
	if (zitat>num_zitate) zitat = 1;
	$("#zitat").attr("src", "images/zitat"+zitat+".png");
	setTimeout(nextZitat, 5000);
}

function closeIntroTrailer() {
	if(!apple()) {
		var p1=getFlashMovieObject("player1");
		if (typeof p1.TotalFrames != "undefined") p1.GotoFrame(0);
	}
	$("#trailer-start").css('display', 'none');
}

function resetTrailers() {
	if(!apple()) {
		var p1=getFlashMovieObject("player1");
		var p2=getFlashMovieObject("player2");
		var p3=getFlashMovieObject("player3");
			if (typeof p1.TotalFrames != "undefined") p1.GotoFrame(0);
		if (typeof p2.TotalFrames != "undefined") p2.GotoFrame(0);
		if (typeof p3.TotalFrames != "undefined") p3.GotoFrame(0);
	}
}

function getgbPage() {
	if (gbStart<=1) {
		gbStart = 1;
	}
	if (gbStart>gbMax) {
		gbStart = gbMax;
	}
	$("#gbuch").html('<img src="images/ajax-loader.gif" width="32" height="32">');
	$.get('php/gbuch.php?s='+gbStart+'&a='+String(Math.random()), function(data) {
		var d = $.parseJSON(data);
		var gbhtml = "";
		gbMax = Math.ceil(d.inhalt.gesamt);
		$("#pagenum").html("Seite "+String(gbStart)+" von "+String(gbMax));
		for (i=0;i<5;i++) {
			if (d.inhalt.gb[i]!=undefined) {
				gbhtml += '<div class="datum">'+d.inhalt.gb[String(i)+"_attr"].name+' | '+d.inhalt.gb["0_attr"].datum+'</div><p>'+d.inhalt.gb[i]+'</p>';
			}
		}
		$("#gbuch").html(gbhtml);
		if (!ie) s2 = new iScroll('gbuch',{checkDOMChanges: true});
	});
}


$(window).load(function() {

	var activePage = 0;
	var MENU_ITEMS = 5;
	for (i=1;i<MENU_ITEMS+1;i++)  {
		$("#menu"+i).attr('alt', i);
		$("#menu"+i).focus(function() {
	 		$(this).blur();
		});
		
		$("#menu"+i).click(function() {
			if (activePage == $(this).attr('alt')) return false;
			$("#zitat").animate2({top:'270px'}, 1000, function(){});
			var a=Number(activePage)+1;
			$("#content-tabs-"+String(a)).slideUp(500);
			if (activePage==1) {
				$("#trailer-start").hide();
			}
			if (activePage==2) {
				$("#trailerdl").slideUp(500);
			}
			activePage = $(this).attr('alt');
			if ($("#tab-1").width()>234) {
				closeIntroTrailer();
				if(apple()) {
					$("#tab-1-small").css('display', 'block');
				} else {
					$("#tab-1-small").fadeIn(1000, function() {});
				}
			}
			for (r=1;r<MENU_ITEMS+1;r++)  {
				if (r<=$(this).attr('alt')) {
					if (r==1) {
						$("#tab-1").animate2({width:'200px'}, 1000, function(){
							var a=Number(activePage)+1;
							$("#content-tabs-"+String(a)).slideDown(500, function(){
								if (!ie) s1 = new iScroll('scroller1',{checkDOMChanges: true});
								if (!ie) s2 = new iScroll('gbuch',{checkDOMChanges: true});
							});
							if (activePage==2) {
								$("#trailerdl").slideDown(500);
							}
						}); 
					} else {
						$("#tab-"+r).animate2({width:'254px'}, 1000, function(){});
					}
				} else {
					$("#tab-"+r).animate2({width:'780px'}, 1000, function(){});
				}
			}
		});

	}
	$("#tab-1-small").click(function() {
		resetTrailers();
		$("#zitat").animate2({top:'400px'}, 1000, function(){});
		document.location = "#";
		var a=Number(activePage)+1;
		$("#content-tabs-"+String(a)).slideUp(500,function(){
			if (activePage==2) {
				$("#trailerdl").slideUp(500);
			}
			activePage = 0;
			$("#trailer-start").show();
			$("#tab-1-small").fadeOut(1000, function() {});
			$("#tab-1").animate2({width:'775px'}, 1000, function(){});
			for (r=2;r<MENU_ITEMS+1;r++) {
				$("#tab-"+r).animate2({width:'780px'}, 1000, function(){
				});
			}
		});
	});
	$("#trailerdl").hide();
	for (r=1;r<=MENU_ITEMS+1;r++)  {
		$("#content-tabs-"+r).hide();
	}
	$(".content-tabs").corner("10px");
	$("#trailer-close").corner("5px top");
	$(".tab_link").corner("5px top");	
	$(".tab_text").corner("10px bottom");
	$("#trailer-start").corner("10px");
	
	$("a.tab_link").click(function() {		   
		resetTrailers();
		var idx = $(this).index()+1;
		$(this).siblings().removeClass('tab_link_selected');
		$(this).addClass("tab_link_selected");
		$(this).parent().parent().find(".tab_contents > .tab_text").removeClass("active_tab");
		$(this).parent().parent().find(".tab_contents > .tab_text").css("display", "none");
		$(this).parent().parent().find(".tab_contents > .tab_text:nth-child("+idx+")").addClass("active_tab");
		$(this).parent().parent().find(".tab_contents > .tab_text:nth-child("+idx+")").css("display", "block");
		if (!ie) s1 = new iScroll('scroller1',{checkDOMChanges: true});
		if (!ie) s2 = new iScroll('gbuch',{checkDOMChanges: true});
	});
	var foto = 1;
	$("#pfeil-l").click(function() {
		foto--;
		if (foto<=0) foto = 16;
		$("#cfoto").fadeOut("fast", function() {
			$("#cfoto").attr("src", "fotos/" + String(foto) + ".jpg");
		});
	});
	$("#pfeil-r").click(function() {
		foto++;
		if (foto>16) foto = 1;
		$("#cfoto").fadeOut("fast", function() {
			$("#cfoto").attr("src", "fotos/" + String(foto) + ".jpg");
		});
	});
	$("#cfoto").load(function() {
		$("#cfoto").fadeIn("fast");
	});

	$("#gbsubmit").click(function() {
		if ($("#gbposter").val() && $("#gbtext").val()) {
			$.post("php/tunnel.php", { name: $("#gbposter").val(), msg: $("#gbtext").val(), action: "add" }, function(data) {
				$("#gbform").html("Vielen Dank, Ihr Eintrag wird nun &uuml;berpr&uuml;ft und ist dann in K&uuml;rze hier zu sehen.");
			});
		} else {
			alert("Bitte alle Felder ausfuellen.");
		}
	});
	
	if(apple()) {
		$(".trailer").html('<a href="media/dls_trailer_iphone.mp4"><img src="media/dls_trailer_play.jpg" width="464" height="192" alt=""></a>');
		$(".teaser").html('<a href="media/dls_teaser_iphone.mp4"><img src="media/dls_trailer_play.jpg" width="464" height="192" alt=""></a>');
	}

	$("#page").css('display', 'block');
	
	$("#gbback").click(function() {
		if (gbStart>=0) {
			gbStart--;
			getgbPage();
		}
	});
	$("#gbnext").click(function() {
		if (gbStart<=gbMax) {
			gbStart++;
			getgbPage();
		}
	});
	getgbPage();

	/*Deeplinks*/
	var myFile = document.location.toString();
		if (myFile.match('#')) {
		var myAnchor = myFile.split('#')[1];
		switch(myAnchor) {
			case "infos": $("#menu1").trigger('click'); break;
			case "media": $("#menu2").trigger('click'); break;
			case "gaestebuch": $("#menu3").trigger('click'); break;
			case "downloads": $("#menu4").trigger('click'); break;
			case "partner": $("#menu5").trigger('click'); break;
		}
	}
	
	setTimeout(nextZitat, 5000);
	
	if (!ie) s1 = new iScroll('scroller1',{checkDOMChanges: true});
	if (!ie) s2 = new iScroll('gbuch',{checkDOMChanges: true});
	
});
