$(document).ready(function() {
	
	$(".data-media").each(function() {
		$(this).find(".media-title h3:last").css("border", "none");
	})
	
	$(".work-wrapper").each(function() {
		if ($(this).children().length == 0) {
			$(this).hide();
		}
	});
	
	$(".calendar-dates").each(function() {
		if ($(this).children().length == 0) {
			$(this).hide();
		}
		else {
			var first_date = $(this).find("li:first");
			$('<li><b>Weitere Auff&uuml;hrungen: </b></li>').insertBefore(first_date);
		}
	});
	
	$(".concert-calendar").tsort(".achrainer-title h2",{attr:"data-datetime"});

	if (!$('.mgnlMainbar').length) {
		$(".homepage-slideshow").slideshow();
	}
	
	$(".video-container").video();
	$(".audio-container").audio();
	
	$(".gallery-wrapper").gallery({
		big_gallery: true
	});
	
	$(".picture-wrapper").gallery();
	
	
	//content-wrapper height
	var doc_height = $(document).height();
	var win_height = $(window).height();
	doc_height = doc_height- $(".homepage-slideshow").height();
	
	if (doc_height <= win_height) {
	
		var con_height = win_height-124;
		
		$("#content-wrapper").css("height", con_height+"px");
	
		$(window).resize(function() {
			var win_height = $(window).height();
			var con_height = win_height-124;
			
			$("#content-wrapper").css("height", con_height+"px");
		})
	}
});

/**
 * Media
 */
(function($) {
	
	$.fn.media = function() {
		
		return this.each(function() {
		
			var $this = $(this);
			
			var at = $this.find(".media-audio-title");
			var vt = $this.find(".media-video-title");
			var pt = $this.find(".media-picture-title");
			var ct = $this.find(".media-critics-title");
			
			var aw = $this.find(".audio-wrapper");
			var vw = $this.find(".video-wrapper");
			var pw = $this.find(".picture-wrapper");
			var cw = $this.find(".critics-wrapper");
			
			var sl = $this.find(".slide-panel");
			
			sl.hide();
			
			at.click(function() {
				
				$(".slide-panel").slideUp("slow");
				
				if (aw.is(":visible")) {
					var visible = true;
				}
				
				var wait = setInterval(function() {
					if (!$(".slide-panel").is(":animated")) {
						clearInterval(wait);
						
						if($.browser.msie) {
							ie_reset();
						}
						
						if (!visible) {
							aw.slideDown("slow");
						}
					}
				}, 100);
				
			});
			
			vt.click(function() {
				
				$(".slide-panel").slideUp("slow");
				
				if (vw.is(":visible")) {
					var visible = true;
				}
				
				var wait = setInterval(function() {
					if (!$(".slide-panel").is(":animated")) {
						clearInterval(wait);
						
						if($.browser.msie) {
							ie_reset();
						}
						
						if (!visible) {
							vw.slideDown("slow");
						}
					}
				}, 100);
				
			});
			
			pt.click(function() {
				
				$(".slide-panel").slideUp("slow");
				
				if (pw.is(":visible")) {
					var visible = true;
				}
				
				var wait = setInterval(function() {
					if (!$(".slide-panel").is(":animated")) {
						clearInterval(wait);
						
						if($.browser.msie) {
							ie_reset();
						}
						
						if (!visible) {
							pw.slideDown("slow");
						}
					}
				}, 100);
				
			});
			
			ct.click(function() {
				
				if($.browser.msie){
					ie_reset();
				}
				
				$(".slide-panel").slideUp("slow");
				
				if (cw.is(":visible")) {
					var visible = true;
				}
				
				var wait = setInterval(function() {
					if (!$(".slide-panel").is(":animated")) {
						clearInterval(wait);
						
						if($.browser.msie) {
							ie_reset();
						}
						
						if (!visible) {
							cw.slideDown("slow");
						}
					}
				}, 100);
			});
			
			//stop players for ie
			var ie_reset = function() {
				$f("*").each(function() {
					this.stop();
				});
				
				var youtube = vw.children("object");
				
				if (youtube.size() > 0) {
					var new_youtube = youtube.clone();
					vw.append(new_youtube);
					youtube.remove();
				}
			}
			
		});
	}
}) (jQuery);

/**
 * Audio player
 */
(function($) {
	
	$.fn.audio = function() {
		
		return this.each(function() {
		
			var $this = $(this);
			
			var ap = $this.children().find(".ap-wrapper").hide();
			
			var at = $this.children().find("h3");
			
			at.click(function() {
				
				var $curr = $(this);
				
				var curr_ap = $curr.parents(".audio-item").find(".ap-wrapper");
				
				ap.slideUp("slow");
				
				if($.browser.msie) {
					$f("*").each(function() {
						this.stop();
					});
				}
				
				if (curr_ap.is(":hidden")) {
					curr_ap.slideDown("slow");
				}
			});
		});
	}
}) (jQuery);

/**
 * Video player
 */
(function($) {
	
	$.fn.video = function() {
		
		return this.each(function() {
		
			var $this = $(this);
			
			$this.children().find(".video-wrapper").hide();
			
			var vt = $this.children().find("h3");
			
			vt.click(function() {
				
				var $act = $(this);
				
				var back = $('<div id="video-back">Zurück</div>').insertAfter("#content");
				var vw = $act.parents(".video-item").find(".video-wrapper").clone();
				var player = vw.find(".video-player");
				
				var id = player.attr("id");
				var clonedId = id+"cloned";
				
				player.attr("id", clonedId);
				
				vw.insertAfter("#video-back");
				$("#content").hide();
				$(".subnav").hide();
				
				var pl = $f(clonedId, ctx+"/docroot/achrainer/js/flowplayer/flowplayer-3.2.4.swf", {
					plugins: {
						controls: conf.video		
					},
					clip: {
						autoPlay: true
					}
				});

				vw.slideDown("slow");
				
				back.click(function() {
					
					vw.slideUp("slow", function() {
						pl.stop();
						back.remove();
						$("#content").show();
						$(".subnav").show();
						
						if(! $.browser.msie) {
							$(this).remove();
						}
					});
				});
			});
		});
	}
}) (jQuery);

/**
 * Gallery
 */
(function($) {
	
	$.fn.gallery = function(options) {
		
		return this.each(function() {
			
			var opts = $.extend({}, $.fn.gallery.defaults, options);
		
			var $this = $(this);
			
			var item = $this.find(".gallery-item");
			
			item.each(function() {
				
				$curr = $(this);
				
				var href = $curr.find(".gal-big-img").attr("src");
				
				$curr.find("a").attr("href", href);
				
			});
			
			var big_gallery = opts.big_gallery;
			
			if (big_gallery) {
				$(".subnav").hide();
				$('<div id="gallery-back"><a href="' + ctx + '/main/media/fotos">Zurück</a></div>').insertBefore(".gallery-title");
			}
			
		});
	}
	
	$.fn.gallery.defaults = {
			big_gallery: false
	};
	
}) (jQuery);

/**
 * Homepage Images
 */
(function($) {
	
	$.fn.slideshow = function() {
		
		return this.each(function() {
			
			var $this = $(this);
			
			$("h1").hide();
			
			$("#content").addClass("homepage");
			var title = $('<h2 class="slideshow-title">Martin Achrainer Bass Bariton</h2>').insertBefore(".homepage-slideshow");
			
			//random image
			var $imgs = $this.children();
			var round = Math.floor(Math.random() * $imgs.length );
			var $add_active  = $( $imgs[ round ] );
			
			var src = $add_active.find("img").attr("src");
			
			$("#content-wrapper").css("background", "url("+ src +") no-repeat");
			
		});
	}	
}) (jQuery);

/**
 * Formvalidation
 */
var formvalidate = function (f) {
	f = $(f);
	var valid = true;
	f.find('.notempty').each(function() {
		if ($.trim($(this).val()) == '') {
			$(this).addClass('error');
			valid = false;
		}
		else {
			$(this).removeClass('error');
		}
	});
	f.find('.email').each(function() {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($(this).val())) {
			$(this).removeClass('error');
		}
		else {
			$(this).addClass('error');
			valid = false;
		}
	});
	
	return valid;
}
