$(document).ready(function() {
	
	$(".gallery").gallery();
	$("#contactform").formvalidate();
	$(".search").searchbox();
	
	// Subnavigation
	if ($("#subnavigation .active").length) {
		$("#subnavigation ul:first > li").each(function() {
			if (!$(this).find(".active").length) {
				$(this).hide();
			}
		})
	}
});

/**
 * Gallery
 */
(function($) {
	$.fn.gallery = function() {
		
		return this.each(function() {
			
			var $this = $(this);
			
			var slidePanel = $('<div class="slide-panel"></div>').insertAfter($this.find("p")).hide();
			var galleryItem = $this.find(".gallery-item");
			var image = galleryItem.find(".small");
			var prev = $('<span class="prev">zurück</span>').appendTo(slidePanel);
			var next = $('<span class="next">weiter</span>').appendTo(slidePanel);
			var close = $('<span class="close">schliessen</span>').appendTo(slidePanel);
			
			image.click(function() {
				var bigImage = $(this).parent().find(".big");
				
				$(".gallery-item").removeClass("act");
				
				slidePanel.find("img").fadeOut("slow")
				slidePanel.find("img").remove();

				if (! $("#gallery-bg").length) {
					$("#content")
						.css("position", "absolute")
						.css("z-index", "99");
					var galleryBg = $('<div id="gallery-bg"></div>').insertBefore("#wrapper").hide();

					galleryBg.fadeIn(200);	
					
					if ($.browser.msie && (jQuery.browser.version < 8)) {	
						galleryBg.remove();
					}
				}
				
				var clonedBigImage = bigImage.clone().appendTo(slidePanel).hide();
				clonedBigImage.fadeIn("slow");
				
				slidePanel.slideDown("slow");
				
				$(this).parent().addClass("act");
			});

			slidePanel.mouseover(function() {
				prev.fadeIn(200);
				next.fadeIn(200);
				close.fadeIn(200);
				
				$(this).mouseleave(function() {
					prev.fadeOut(200);
					next.fadeOut(200);
					close.fadeOut(200);
				});
			});
			
			prev.click(function() {
				var act = $this.find(".act");
				
				slidePanel.find("img").fadeOut("slow")
				slidePanel.find("img").remove();
				
				if (act.prev(".gallery-item").length) {
					var prevGalleryItem = act.prev(".gallery-item");
				}
				else {
					var prevGalleryItem = $this.find(".gallery-item:last");
				}
				
				var prevImage = prevGalleryItem.find(".big").clone().appendTo(slidePanel).hide();
				prevImage.fadeIn("slow");
				
				act.removeClass("act");
				prevGalleryItem.addClass("act");
			});
			
			next.click(function() {
				var act = $this.find(".act");
				
				slidePanel.find("img").fadeOut("slow")
				slidePanel.find("img").remove();
				
				if (act.next(".gallery-item").length) {
					var nextGalleryItem = act.next(".gallery-item");
				}
				else {
					var nextGalleryItem = $this.find(".gallery-item:first");
				}
				
				var nextImage = nextGalleryItem.find(".big").clone().appendTo(slidePanel).hide();
				nextImage.fadeIn("slow");
				
				act.removeClass("act");
				nextGalleryItem.addClass("act");
			});
			
			close.click(function() {
				slidePanel.slideUp("slow", function() {
					slidePanel.find("img").remove();
					$(".gallery-item").removeClass("act");
					$("#gallery-bg").fadeOut(200, function() {
						$(this).remove();
					});
				});
			})

			$(window).scroll(function() {
				var docHeight = $(document).height();
				$("#gallery-bg").css("height", docHeight + "px");
			});
		});
	}
})(jQuery);

/**
 * Formvalidation
 */
(function($) {
	$.fn.formvalidate = function() {
		
		return this.each(function() {
			
			var $this = $(this);
			
			$this.submit(function() {
				
				var valid = true;
				
				$this.find('.notempty').each(function() {
					if ($.trim($(this).val()) == '') {
						$(this).addClass('error');
						valid = false;
					}
					else {
						$(this).removeClass('error');
					}
				});
				
				$this.find('.email').each(function() {
					if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test($(this).val())) {
						$(this).removeClass('error');
					}
					else {
						$(this).addClass('error');
						valid = false;
					}
				});
				
				if (!valid) {
					return false;
				}
			})
		});
	}
}) (jQuery);

/**
 * Media
 */
(function($) {
	$.fn.media = function(options) {
		
		return this.each(function() {
			
			var $this = $(this);
			var opts = options;
			
			var title = $this.find(".media-title");
			
			title.click(function() {
				
				$(".media-title").removeClass("active");
				
				var deviceAgent = navigator.userAgent.toLowerCase();
				var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
				
				// ipad, ipod or iphone, show html5 player
				if (agentID) {
					$(".html5-player-wrapper").slideUp("slow");
					
					var video = $this.parent().find("#video-" + opts.uuid).get(0);
					var audio = $this.parent().find("#audio-" + opts.uuid).get(0);
					
					var playerWrapper = $(this).parent().find(".html5-player-wrapper");
					
					if (playerWrapper.is(":hidden")) {
						playerWrapper.slideDown("slow");
						
						$(this).addClass("active");
						
						if (video) {
							video.play();
						}
						else {
							audio.play();
						}
					}
					else {
						playerWrapper.slideUp("slow");
						
						if (video) {
							video.pause();
						}
						else {
							audio.pause();
						}
					}
				}
				
				// other agents, show flowplayer
				else {
					// video
					if (opts.type == "video") {
						var pl = $f("video-flowplayer-" + opts.uuid, opts.ctx + "/docroot/augusta/js/flowplayer/flowplayer-3.2.4.swf", {
							plugins: {
								controls: conf.video		
							},
							clip: {
								autoPlay: true
							}
						});
					}
					
					// audio
					else {
						var pl = $f("audio-flowplayer-" + opts.uuid, opts.ctx + "/docroot/augusta/js/flowplayer/flowplayer-3.2.4.swf", {
							plugins: {
								controls: conf.audio		
							},
							clip: {
								autoPlay: true
							}
						});
					}
					
					$(".player-wrapper").slideUp("slow");
					var playerWrapper = $(this).parent().find(".player-wrapper");
					
					if (playerWrapper.is(":hidden")) {
						
						if($.browser.msie) {
							ie_reset();
						}
						
						var wait = setInterval(function() {
							if (!$(".player-wrapper").is(":animated")) {
								clearInterval(wait);
								
								$(this).addClass("active");
								
								playerWrapper.find("a").show();
								playerWrapper.slideDown("slow");
								
							}
						}, 100);
						
						
					}
					else {
						playerWrapper.slideUp("slow");
						playerWrapper.find("a").hide();
					}
				}
			});
			
			//stop flowplayers for ie
			var ie_reset = function() {
				$f("*").each(function() {
					this.stop();
				});
			}
		});
	}
}) (jQuery);

/**
 * Searchbox
 */
(function($) {
	$.fn.searchbox = function() {
		
		return this.each(function() {
			
			var $this = $(this);
			
			var linkElem = $this.find("a");
			
			linkElem.click(function() {
				var link = $(this);
				
				var input = $this.find("input");
				var form = $this.find("form");
				
				var actPos = 0;
				var searchWidth = 0;
				$(".mainnav li").each(function() {
					
					if (! $(this).hasClass("search")) {
						var width = $(this).width();
						
						actPos = actPos + width + 5;
					}
					else {
						searchWidth = $(this).width();
					}
				})
				
				var freeWidth = 720 - actPos;
				
				var searchActWidth = searchWidth + freeWidth
				var inputWidth = freeWidth - 6;
				
				input.css("width", inputWidth + "px");
				
				if (! link.hasClass("open")) {
					var wait = setInterval(function() {
						
						if (! $this.is(":animated")) {
							
							clearInterval(wait);
							
							$this.css("left", actPos + "px");
							
							$this.addClass("active search-active");
							
							input.show();
							input.focus();
							
							$this.animate({
								width: searchActWidth + "px"
							},300);
							
							link.addClass("open");
						}
						
					}, 100);
				}
				
				input.blur(function() {
					
					if ($.trim($(this).val()) == '') {
						$this.animate({
							width: "74px"
						}, 300, function() {
							$this.removeClass("active search-active");
							link.removeClass("open");
						});
						
						input.hide();
					}
				});
				
				$(".open").live("click", function() {
					if ($.trim(input.val()) != '') {
						form.submit();
					}
				});
			})
		});
	}
})(jQuery);

