jQuery.noConflict();
jQuery(document).ready(function($) {

	 

	$(".gallery").each(function() {
	
		var gallery = $(this);
		var pics = gallery.find('.pics');
		var show = gallery.find('.show');
		 
		pics.find('a').click(function(e) {
			e.preventDefault();
			var link = $(this);
			var rel = link.attr('rel');
			var duration = 0;
			if(show.is(':visible')) duration = 'slow';
			show.stop().slideUp(duration,function() {
				show.empty().append('<img src="'+rel+'" />');
				show.hide().find("img").load(function() {
					show.slideDown('slow');
				});
			});
			
			//show.css('position','relative');
			/*show.find('img').css({
				'position':'absolute',
				'top':'10px',
				'left':'0px',
				'zIndex':'1000',
				'border':'3px solid black'
			});*/
		});
		
		/*pics.find('a').mouseout(function() {
			show.empty();
		});*/
	});

	/*** Navigation ***/
	$("#navigation>li").not(".open").mouseover(function() {
		var navi = $(this).parents("ul:first");
		var navi_element = $(this);
		var submenu = navi_element.children("ul");
		var actualHeight = submenu.height();
		submenu.css("height","auto").show();
		var height = submenu.height();
		submenu.css("height",actualHeight+"px").hide();
		
		submenu.show().stop().animate({
			"height" : height
		},"fast");
	});
	
	$("#navigation>li").not(".open").mouseout(function() {
		var navi = $(this).parents("ul:first");
		var navi_element = $(this);
		var submenu = navi_element.children("ul");
		var actualHeight = submenu.height();
		submenu.css("height","auto").show();
		var height = submenu.height();
		submenu.css("height",actualHeight+"px").hide();
		
		submenu.show().stop().animate({
			"height" : 0
		},"fast");
	});

	/**** Tabbed ****/
	$(".content.tabbed").each(function() {
		var content = $(this);
		var tabs = content.find(".tabs");
		var active = tabs.find(".active").find("a");
		
		content.find(".element").hide();
		content.find(".element.type-"+active.attr("rel")).show();
		
		tabs.find("li").click(function(e) {
			e.preventDefault();
			
			var link = $(this).find("a");
			
			self.location.href = self.location.href.replace(/#.*/,"")+"#tab="+link.attr("rel");
			
			tabs.find("a").parents("li").removeClass("active");
			link.parents("li").addClass("active");
			
			content.find(".element").hide();
			content.find(".element.type-"+link.attr("rel")).show();
		});
		
		if(self.location.href.indexOf("#tab=")!=-1) {
			var index = self.location.href.replace(/.+?#tab=/,"");
			tabs.find("li a[rel="+index+"]").click();
		}
	});
	
	$("#dimmer").css("opacity","0.2");


	$("#footer form").submit(function(){
		
	});
	
	
	$("#trailer").each(function() {
		var trailer = $(this);
		var navi = trailer.find('.navi');
		var elements = trailer.find('ul').children('li');
		
		navi.attr("rel",0);
		var rel = 0;
		elements.each(function() {
			navi.append('<a href="#" rel="'+rel+'">dot</a>');
		rel++; });
		
		var dots = navi.find('a');
		dots.eq(0).addClass('active');
		navi.find("a").click(function() {
			var dot = $(this);
			var rel = parseInt(dot.attr("rel"));
			
			dots.removeClass("active");
			dot.addClass("active");
			elements.filter(':visible').fadeOut('slow');
			elements.eq(rel).stop().fadeIn('slow');
			return false;
		});
		
		window.setInterval("trailer_next()",11000);
	});
	
	
	$("input[name=Email2]").replaceWith('<input type="text" name="Email2" class="email labelit check-email" id="i-email" />');
	$("input[name=Email]").val('0')
	labelit();
	window.setTimeout('jQuery("input[name=Email]").val("123");',5000);
	$("#footer form").submit(function(e) {
		e.preventDefault();
		var form = $(this);
		var name = form.find('input[name=Name]').val();
		var email = form.find('input[name=Email]').val();
		var email2 = form.find('input[name=Email2]').val();
		var nachricht = form.find('textarea[name=Nachricht]').val();
		var query = 	'type=50'+
						'&send=1'+
						'&L=1'+
						'&emailid=1'+
						'&Name='+name+
						'&Email='+email+
						'&Email2='+email2+
						'&Nachricht='+nachricht;
		$.get('http://p137044.mittwaldserver.info/?'+query,function(data) {
			form.html('<fieldset class="answer">'+data+'</fieldset>');
		});
	});
});

function trailer_next() {
	$ = jQuery;
	
	var trailer = $("#trailer");
	var navi = trailer.find('.navi');
	var dots = navi.find('a');
	var activeIndex = 0;
	var nextIndex = 0;
	
	var i = 0; dots.each(function() {
		if($(this).hasClass('active')) activeIndex=i;
	i++; });
	
	nextIndex = activeIndex+1;
	nextIndex = nextIndex<dots.length ? nextIndex : 0;
	
	dots.eq(nextIndex).click();
	
}

/*
 * -- Labels in Inputs anzeigen
 */
function labelit() {
	$ = jQuery;
	
	$("input.labelit, textarea.labelit").each(function(){
		
		/*
		 * --	Label Inhalt in Inputs schreiben
		 */
		$(this).parents("form").find("label[for="+$(this).attr("id")+"]").hide();
		if( $(this).val()=="" ) $(this).val($(this).parents("form").find("label[for="+$(this).attr("id")+"]").text());
		
		/*
		 * --	Focus Event
		 */
		$(this).focus(function(e){
			$(this).css("color","");
			if( $(this).parents("form").find("label[for="+$(this).attr("id")+"]").text() == $(this).val() ) {
				$(this).val("");
			}
		});
		$(this).blur(function(e) {
			if( $(this).val()=="" ) {
				$(this).val($(this).parents("form").find("label[for="+$(this).attr("id")+"]").text());
			}
		});
	});
}

/*
 * -- Check Formular
 */
function checkFormular(form) {
	var $ = jQuery;
	var error = false;
	$(form).find(".error").removeClass("error");
	$(form).find(".check-filled").each(function() {
		if($(this).val()=="") {
			error = true;
			$(this).addClass("error");
		}
		if($(this).hasClass("labelit")) {
			if($(this).val() == $(this).parents("form").find("label[for="+$(this).attr("id")+"]").text()) {
				error = true;
				$(this).addClass("error");
			}
		}
	});
	$(form).find(".check-email").each(function() {
		if(!validateEmail($(this).val())) {
			error = true;
			$(this).addClass("error");
		}
		if($(this).hasClass("labelit")) {
			if($(this).val() == $(this).parents("form").find("label[for="+$(this).attr("id")+"]").text()) {
				error = true;
				$(this).addClass("error");
			}
		}
	});
	if(error) return false;
	else return true;
}

function validateEmail(id)
{
	var emailPattern = /^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(id);
} 
