window.kornspitzApp = new KornspitzApp();

function KornspitzApp() {
	var self = this;
	var events;

	(function KornspitzApp() {
		$(document).ready(init);
	})();

	function init() {
		sameColumnHeight($(".sameHeight>.column"));
		events = new KornspitzEvents();
	}

	function sameColumnHeight(_columns) {
		var _maxHeight = 0;
		for(var i=0; i < _columns.length; i++) {
			if(_columns.eq(i).height() > _maxHeight) {
				_maxHeight = _columns.eq(i).height();
			}
		}

		_columns.each(function() {
			$(this).height(_maxHeight);
		})
	}

	function KornspitzEvents() {
		var scController;
		var spController;
		var languageChanger;
		var overlayController;

		(function KornspitzEvents() {
			init();
		})();


		function init() {
			if($(".sportlich").length > 0) {
				scController = new SportlerCarouselController();

				$(".sportlich .arrow.right a").bind("click", scController.leftArrowClick);
				$(".sportlich .arrow.left a").bind("click", scController.rightArrowClick);
				$(".sportlich .arrow").show();

				$(".teaser.sport a").bind("click", scController.rightArrowClick);
			}

			if($(".startseite").length > 0) {
				spController = new StartPageCarouselController();
				$(".startseite .arrow.right a").bind("click", spController.leftArrowClick);
				$(".startseite .arrow.left a").bind("click", spController.rightArrowClick);
				$(window).bind("resize", spController.resizeWindow);
				$(".startseite .arrow").show();
				spController.resizeWindow();
			}

			languageChanger = new LanguageChanger();
			$(".langswitch li.active").bind("click",languageChanger.onSliderClick);
			oc = new OverlayController();
			$("a.overlayLink").bind("click", oc.click);
			
			if($(".tischdecke").length > 0) {
				$(window).bind("resize", tischdeckeResize);
				tischdeckeResize();
			}
		}

		function tischdeckeResize() {
			if($(window).width() < 1090) {
				$(".arrow").hide();
				$(".arrow-overview").hide();
			} else {
				$(".arrow").show();
				$(".arrow-overview").show();
			}
		}

	}

}

function LanguageChanger() {
	var self = this;
	var animate = false; 
	
	(function LanguageChanger() {
		$(".langswitch > ul > li.active").appendTo($(".langswitch > ul"));
		$(".langswitch").css({
			top: "-" + ($(".langswitch").height() - 26) + "px"
		});
	})();

	self.onSliderClick = onSliderClick;
	function onSliderClick(e) {
		$(".langswitch").animate({
			top: "0px"
		}, function() {
			
				
			$("body").one("click", function() {
				$(".langswitch").animate({
					top: "-" + ($(".langswitch").height() - 26) + "px"
				})
			});
		});
	}
}

function OverlayController() {
	var self = this;

	//TEMPLATE
	var overlay;
	var lightbox;
	var close;

	(function OverlayController() {
	})();

	self.close = close;
	function close() {
		overlay.fadeOut().remove();
	}

	function init() {
		overlay = $("<div>").addClass("overlay");
		overlay.bind("click", close);
		lightbox = $("<div>").addClass("lightbox");
		$("<iframe>").appendTo(lightbox);
		close = $("<a>").addClass("close").text("X").appendTo(lightbox);
		close.bind("click", close);
	}

	self.show = show;
	function show(_url) {
		init();
		overlay.height($(document).height());
		overlay.width($(window).width());

		iframe = lightbox.find("iframe");
		iframe.attr("src", _url);
		iframe.width("100%");
		iframe.height("100%");

		lightbox.prependTo(overlay);
		overlay.prependTo($("body")).fadeIn();
	}

	self.click = function click(e) {
		e.preventDefault();
		self.show($(this).attr("href"));
		//show(e.attr("href"));
		return false;
	}
}

function StartPageCarouselController() {
	var self = this;
	var animate = false;

	(function StartPageCarouselController() {
	})();

	self.rightArrowClick = rightArrowClick;
	function rightArrowClick(e) {
		e.preventDefault();
		//Animation
		if(!animate) {
			animate = true;
			$(".teaser-images").animate({
				left: $(window).width() + "px"
			}, function() {
				animate = false;
				$(".teaser-images .next").prependTo(".teaser-images");
				$(".teaser-images .teaser-img").eq(0).removeClass("first next").addClass("prev");
				$(".teaser-images .teaser-img").eq(1).removeClass("next prev").addClass("first");
				$(".teaser-images .teaser-img").eq(2).removeClass("first prev").addClass("next");

				$(".teaser-images").css("left","0");
			});
		}
		return false;
	}

	self.leftArrowClick = leftArrowClick;
	function leftArrowClick(e) {
		e.preventDefault();
		
		//Animation
		if(!animate) {
			animate = true;
			$(".teaser-images").animate({
				left: "-" + $(window).width() + "px"
			}, function() {
				animate = false;
				$(".teaser-images .prev").appendTo(".teaser-images");
				$(".teaser-images .teaser-img").eq(0).removeClass("first next").addClass("prev");
				$(".teaser-images .teaser-img").eq(1).removeClass("next prev").addClass("first");
				$(".teaser-images .teaser-img").eq(2).removeClass("first prev").addClass("next");

				$(".teaser-images").css("left","0");				
				if($(".first").hasClass("clickable")) {
					$(".clickdummy").show();
				} else {
					$(".clickdummy").hide();
				}
			});
		}

		return false;
	}

	self.resizeWindow = resizeWindow;
	function resizeWindow() {
		/*$(".teaser-img").width($(window).width());
		$(".startseite .teaser-images").height($(document).height() - $("#footer").height());
		$("#teaser.start-teaser").height($(window).height()- $("#footer").height());
		$(".startseite .teaser-images .teaser-img").height($(window).height())- $("#footer").height();
		$(".startseite .teaser-c").height($(window).height())- $("#footer").height();
		$("#teaser.start-teaser div.arrow").css("top", (parseInt($(".startseite .teaser-images").height()) / 2 - 20) + "px");*/
	}
}

function SportlerCarouselController() {
	var self = this;
	var animate = false;
	
	(function SportlerCarouselController() {
		
	})();

	self.rightArrowClick = rightArrowClick;
	function rightArrowClick(e) {
		e.preventDefault();
		if(!animate) {
			$.ajax({
				url: $(this).attr("href"),
				success: function(data) {
					var _obj = $(data);
					moveSportlerOut("right", function() {
						
						$(".sportler").attr("src", _obj.find("#sportler_image").attr("src"));
						$(".sportler_info > span").html(_obj.find("#sportler_quote").html());
						/*$(".sportler_info > .sign > img").attr("src", _obj.find("#sportler_sign").attr("src"));
						$(".sportler_info > .sign > div").html(_obj.find("#sportler_name").html());*/
						$(".arrow.right > a").attr("href", _obj.find("#next_link a").attr("href"));
						$(".arrow.left > a").attr("href", _obj.find("#prev_link a").attr("href"));
						$(".sportler").css( {
							left: "-" + ($(".sportler").width() + $(".sportlich").offset().left ) + "px",
							right: "auto"
						});
						moveSportlerIn("left", function() {
							animate = false;	
						});
					})
				}
			})
		}
		document.location.hash = "#";
		return false;
	}

	self.leftArrowClick = leftArrowClick;
	function leftArrowClick(e) {
		e.preventDefault();
		if(!animate) {
			$.ajax({
				url: $(this).attr("href"),
				success: function(data) {
					var _obj = $(data);
					moveSportlerOut("left", function() {
						
						$(".sportler").attr("src", _obj.find("#sportler_image").attr("src"));
						$(".sportler_info > span").html(_obj.find("#sportler_quote").html());
						$(".sportler_info > .sign > img").attr("src", _obj.find("#sportler_sign").attr("src"));
						$(".sportler_info > .sign > div").html(_obj.find("#sportler_name").html());
						$(".arrow.right > a").attr("href", _obj.find("#next_link a").attr("href"));
						$(".arrow.left > a").attr("href", _obj.find("#prev_link a").attr("href"));
						$(".sportler").css( {
							right: "-" + ($(".sportler").width() + $(".sportlich").offset().left ) + "px",
							left: "auto"
						});
						moveSportlerIn("right", function() {
							animate = false;	
						});
					})
				}
			})
		}
		document.location.hash = "#";
		return false;
	}

	function moveSportlerOut(direction, callbackFunc) {
		if(direction == "left") {
			animate = true;
			$(".sportler_info").fadeOut();
			$(".sportler").css("left", "485px").animate({
				left: "-" + ($(".sportler").width() + $(".sportlich").offset().left ) + "px"
			}, 1000, function() {
				if(callbackFunc) {
					callbackFunc();
				}
			});
		}
		if(direction == "right") {
			animate = true;
			$(".sportler_info").fadeOut();
			$(".sportler").animate({
				right: "-" + ($(".sportler").width() + $(".sportlich").offset().left ) + "px"
			}, 1000, function() {
				if(callbackFunc) {
					callbackFunc();
				}
			});
		}
	}

	function moveSportlerIn(direction, callbackFunc) {
		if(direction == "right") {
			$(".sportler").animate({
				right: "-80px"
			}, 1000, function() {
				$(".sportler_info").fadeIn();
				if(callbackFunc) {
					callbackFunc();
				}
			});
		}
		if(direction == "left") {
			$(".sportler").animate({
				left: "485px"
			}, 1000, function() {
				$($(this)).css({
					right: "-80px",
					left: "auto"
				})
				$(".sportler_info").fadeIn();
				if(callbackFunc) {
					callbackFunc();
				}
			});
		}
	}

}


(function customSelect(w,d,$,undefined) {
	$(d).ready(function() {
		$("html").addClass("js");
		$("select").each(function() {
			var script = $("#tmpl-custom-"+$(this).attr("id"));
			if(script.length) {
				var html = script.html();
				script.remove()
				var custom = $(html);
				$(this).after(custom);
			}
		});
		$(".custom-select > a").live("click",function(){
			$(this).parent().hasClass("open") ? $(this).parent().removeClass("open") : $(this).parent().addClass("open")
			return false;
		});

		$(".custom-select ul li a").live("click",function(){
			$(".custom-select").siblings("select").find("option:eq("+$(this).parent().index()+")").attr('selected',true);
			$(".custom-select > a").html($(this).text()+"<span></span>");
			$(".custom-select").removeClass("open");
			return false;
		});
	}); 
}(window,document,jQuery));

(function validateForm(w,d,$,undefined){
	$(d).ready(function() {
		$("form").each(function() {
			var form = $(this);
			$(this).delegate('input[type="submit"]','click',function(e) {
				var error = false;
				form.find(".require").each(function(){
					if($(this).val().length <= 0) {
						error = true;
						$(this).parent().addClass("error");
					}
				});
				if(error) {
					e.preventDefault();
				}
			});
		});
	});
}(window,document,jQuery));
