/*
 *		Copyright (c) 2010 Alexander Rottmaier. Alle Rechte Vorbehalten.
 *		Erstellt von Bernd Lutz, http://berndlutz.com
 */


var Diashow = {
	
	image: false,
	images: false,
	interval: false,
	zIndex: 100,
	mode: "closed",
	opened: false,
	
	init: function()
	{
		this.images = $("#diashow img").length;
		this.image = 0;
		
		if ( !document.getElementById("diashow") || this.images <= 1 )
			return false;
		
		$("#diashow").css({cursor: "pointer"});
		$("#diashow img").hide();
		$("#diashow img:first-child").show();
		$("#diashow").append("<span id=\"diashow-enlarge\">vergrößern</span>");
		$("#diashow-enlarge").hide();
		$("#diashow").mouseenter(function(){ $("#diashow-enlarge").fadeIn(500); });
		$("#diashow").mouseleave(function(){ if ( !$("#diashow-enlarge").hasClass("loading") ){$("#diashow-enlarge").fadeOut(500);} });
		$("#diashow").click(Diashow.enlarge);
		
		this.play();
		
		$(document).click(function(event)
		{
			if ( Diashow.opened && $(event.target).not("#diashow-large *").length )
				Diashow.min(event);
		});
	},
	
	
	fadeImage: function(n)
	{
		var fadeSpeed = 2000;
		
		if ( $("#diashow-large").length )
		{
			$("#diashow-large img:eq("+n+")").hide();
			$("#diashow-large img:eq("+n+"), #diashow img:eq("+n+")").css({ zIndex: ++Diashow.zIndex });
			$("#diashow-large img:eq("+n+")").fadeIn(fadeSpeed);
		}
		else
		{
			$("#diashow img:eq("+n+")").hide().css({ zIndex: ++Diashow.zIndex }).fadeIn(fadeSpeed);
		}
	},
	
	
	play: function()
	{
		if ( this.image != 0 )
			Diashow.next();

		this.interval = window.setInterval('Diashow.next()', 7000);
		$("#play").html("stopp").addClass("stopped");
	},
	
	
	stop: function()
	{
		if ( this.interval != false )
		{
			window.clearInterval(this.interval);
			this.interval = false;
		}
		
		$("#play").html("abspielen").removeClass("stopped");
	},
	
	
	next: function(event)
	{
		if ( event )
		{
			event.preventDefault();
			Diashow.stop();
		}
		
		Diashow.image = Diashow.image + 1 >= Diashow.images ? 0 : Diashow.image + 1;
		Diashow.fadeImage(Diashow.image);
	},
	
	
	previous: function(event)
	{
		if ( event )
		{
			event.preventDefault();
			Diashow.stop();
		}
		
		Diashow.image = Diashow.image - 1 < 0 ? Diashow.images - 1 : Diashow.image - 1;
		Diashow.fadeImage(Diashow.image);
	},
	
	
	enlarge: function(event)
	{
		Diashow.mousePosition = { "x": event.pageX, "y": event.pageY };
		Diashow.stop();
		Diashow.buildLargeView();
		Diashow.loading();
	},
	
	
	min: function(event)
	{
		event.preventDefault();
		
		Diashow.stop();
		
		$("#bg").fadeOut(2000, function()
		{
			$("#bg").remove();
			Diashow.play();
		});
		
		$("#diashow-large").fadeTo(1500, 0, function()
		{
			$("#diashow-large").remove();
			Diashow.opened = false;
		});
	},
	
	
	loading: function()
	{
		$("#diashow-enlarge").addClass("loading");
		$("#diashow-large img:eq("+Diashow.image+")").load(function()
		{
			Diashow.showLargeView();
			$("#diashow-enlarge").fadeOut(500, function(){ $("#diashow-enlarge").removeClass("loading"); });
		});
	},
	
	
	buildLargeView: function()
	{
		Diashow.html = "";
		$("#diashow img").each(function(i)
		{
			Diashow.html += '<img src="' + $(this).attr("src").replace(/diashow\//, "diashow/l/") + '" alt="" />';
		});

		$("body").append(
			'<div id="bg">&nbsp;</div>' +
			'<div id="diashow-large">' +
				Diashow.html +
				'<div id="diashow-nav"><a href="#" id="prev">zurück</a><a href="#" id="play">abspielen</a><a href="#" id="next">vor</a><a href="#" id="close">schließen</a></div>' +
			'</div>'
		);

		$("#bg").css({ width: $(window).width()+"px", height: $(window).height()+"px", opacity: 0 });

		$("#diashow-large").hide();


		$("#prev").click(this.previous);
		$("#next").click(this.next);
		$("#play").click(function(event)
		{
			event.preventDefault();
			
			if ( Diashow.interval == false )
				Diashow.play();
			else
				Diashow.stop();
		});
		$("#close").click(Diashow.min);
	},
	
	
	showLargeView: function()
	{
		$("#diashow-nav, #diashow-large img").hide();
		$("#diashow-large").show();
		
		$("#diashow-large img:eq("+Diashow.image+")").css({
			top: Diashow.mousePosition.y+"px",
			left: Diashow.mousePosition.x+"px",
			marginTop: 0,
			width: "30px",
			height: "20px",
			opacity: 0
		}).show();
		
		Diashow.imagePosition = {
			top: (Math.round(($(window).height()-540)/2)-40),
			left: Math.round(($(window).width()-810)/2)
		};
		
		$("#diashow-large img:eq("+Diashow.image+")").animate(
			{
				top: Diashow.imagePosition.top+"px",
				left: Diashow.imagePosition.left+"px",
				width: "810px",
				height: "540px",
				opacity: 1
			},
			2000,
			"linear",
			function()
			{
				$("#diashow-nav").css({ top: (Diashow.imagePosition.top+540)+"px", left: Diashow.imagePosition.left+"px" }).fadeIn(1000);
				$("#diashow-large img").css({ top: Diashow.imagePosition.top+"px", left: Diashow.imagePosition.left+"px" });
				
				Diashow.play();
				Diashow.opened = true;
			}
		);
		
		$("#bg").fadeTo(1500, 0.85);
	}
};




var Navigation = {

	zeroPosition: -54,
	mouseout: true,
	
	init: function()
	{
		$("#nav").append('<li id="moveable-bg">&nbsp;</li>');
		
		var items = ["home", "profil", "referenzen", "modelle", "kontakt"];
		for ( var i = 0; i < 5; i++ )
			if ( $("#"+items[i]+" #nav-"+items[i]).length )
				this.zeroPosition = (i * 54);
		
		$("#moveable-bg").css({top: this.zeroPosition+"px"});
		
		$("#nav").mouseenter(this.startMove);
		$("#nav").mouseleave(this.endMove);
		$("#nav a").mousemove(this.setTarget);
	},
	
	startMove: function(e)
	{
		Navigation.e = e;
		Navigation.targetY = 0;
		Navigation.currentY = 0;
		Navigation.interval = window.setInterval("Navigation.move();", 30);
		Navigation.mouseout = false;
	},
	
	endMove: function()
	{
		Navigation.targetY = Navigation.zeroPosition;
		Navigation.mouseout = true;
	},
	
	move: function()
	{
		this.getCurrentY();
		var to = this.currentY + Navigation.round((this.targetY - this.currentY) * 0.075);
		
		if ( to < -54 )
			to = -54;
		
		if ( to > 216 )
			to = 216;
		
		$("#moveable-bg").css({ top: to+"px" });
		
		if ( Navigation.currentY == Navigation.targetY && Navigation.mouseout == true )
			Navigation.clear();
	},
	
	setTarget: function(e)
	{
		Navigation.targetY = parseInt($(this).parent().css("top"));
	},
	
	getCurrentY: function()
	{
		Navigation.currentY = parseInt($("#moveable-bg").css("top"));
	},
	
	clear: function()
	{
		window.clearInterval(Navigation.interval);
		Navigation.mouseout = true;
	},
	
	round: function(n)
	{
		return n < 0 ? Math.floor(n) : Math.ceil(n);
	}
};




var Validation = {
	
	init: function()
	{
		if ( $("form").length == 0 )
			return false;

		this.fields = arguments;
		$("form").submit(Validation.form);
		$("label").each(function(){ $(this).attr("title", $(this).html()); });
	},
	
	empty: function(name)
	{
		var elt = $("label[for="+name+"]").get();
		console.log(elt);
		if ( jQuery.trim($("#"+name).val()).length == 0 )
		{
			console.log("^ empty");
			Validation.triggerError(elt, "Bitte ausfüllen.");
			return true;
		}
		
		return false;
	},
	
	form: function(event)
	{
		Validation.event = event;
		Validation.reset();
		
		for ( var i = 0; i < Validation.fields.length; i++ )
			Validation.empty(Validation.fields[i]);
	},
	
	triggerError: function(elt, text)
	{
		Validation.event.preventDefault();
		$(elt).css({ color: "#f00" });
		$(elt).append("<span> &mdash; " + text + "</span>");
	},
	
	reset: function()
	{
		$("label").css({ color: "#000" });
		$("label").each(function(){ $(this).html($(this).attr("title")); });
	}
};




$(document).ready(function()
{
	Navigation.init();
	Diashow.init();
	
	
	if ( $("#home").length == 0 && $("#wrapper").height() < $(window).height() )
	{
		$("#wrapper").css({
			position: "relative",
			height: ($(window).height()-92)+"px",
			padding: "0 0 92px 0"
		});
		
		$("#footer").css({
			position: "absolute",
			bottom: 0,
			left: 0
		});
	}
	
	
	if ( $("#keywords li").length )
	{
		$("#keywords li").each(function(i)
		{
			i = Number(i*3).toString(16);
			$(this).css({ color: "#"+i+i+i});
		});
	}
	
	
	Validation.init("name", "kemail", "text");
	
	
	if ( $("#project-nav").length )
	{
		$("#project-nav a").mouseenter(function()
		{
			$(document.body).append('<div id="project-nav-info">'+$(this).children("img").attr("alt")+'</div>');
			$("#project-nav-info").css({
				top: ($(this).offset().top + 30)+"px",
				left: ($(this).offset().left - $("#project-nav-info").width() - 24)+"px",
				opacity: 0.8
			});
		});
		
		$("#project-nav a").mouseleave(function()
		{
			$("#project-nav-info").remove();
		});
	}
});
