$(document).ready(function() {
	
	$(".portfolio").portfolio();
	$("#searchform").clearinput();
	$("#slideshow-home").slideshow();
	
});
/**
 * Slideshow
 */
(function($) {
	
	$.fn.slideshow = function() {
		
		$this = $(this);
		
		$this.children().find("li:first").show();
		$this.children().find("li:first").addClass("active");
		
		setInterval("$.fn.slideshow.slide()", 4000);
	}
	
	$.fn.slideshow.slide = function() {
		
		var act = $this.children().find("li[class*=active]");
		
		act.removeClass("active");
		
		act.next("li").addClass("active");	

		if (act.next().size() == 0) {
			$this.children().find("li:last").removeClass("active");
			$this.children().find("li:first").addClass("active");
		}
		
		$this.children().find("li").fadeOut("slow");
		$this.children().find("li.active").fadeIn("slow");
	}
	
}) (jQuery);

/**
 * Portfolio
 */
(function($) {
	
	$.fn.portfolio = function() {
		
		$this = $(this);
	
		$this.find(".portfolio-item:first .portfolio-inner").slideDown();
		$this.find(".portfolio-item:first .portfolio-title").addClass("active");
		
		title = $this.find(".portfolio-item .portfolio-title");
		inner = $this.find(".portfolio-item .portfolio-inner");
		
		title.click(function() {
			
			var ct = $(this);
			var slide_el = ct.parent().find(".portfolio-inner");
			
			if (slide_el.is(":hidden")) {
				
				inner.slideUp();
				title.removeClass("active");
				
				slide_el.slideDown();
				ct.addClass("active");
			}
		});
		
		title.mouseover(function() {
			
			var mt = $(this);
			
			var heading = mt.parent().find("h3");
			
			heading.css("color", "#d86b7f");
			
			mt.mouseleave(function() {
				heading.css("color", "#333");
			});
		});
	}
}) (jQuery);

/**
 * Show and hide the value of the searchfield
 */
(function($) {
	
	$.fn.clearinput = function() {
		
		$this = $(this);
		
		input = $this.find("#s");
	
		input.click(function() {
		
			var el = $(this);
			
			var input_val = el.val();
			
			if (input_val == "Suchbegriff") {
				el.attr("value","");
			}
			
			input.blur(function() {
				
				var blur_el = $(this);
				
				if (blur_el.val() == "") {
					blur_el.attr("value", "Suchbegriff");
				}
			});
		});
	}
}) (jQuery);

/**
 * Formvalidation
 */
var formvalidate = function(obj) {
	
	var $this = $(obj);
	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,3})+$/.test($(this).val())) {
			$(this).removeClass('error');
		}
		else {
			$(this).addClass('error');
			valid = false;
		}
	});
	
	return valid;
}
