$(document).ready(function() {
	
	$(".text-image-item img").removeAttr("height");
	$(".project-big-img").removeAttr("height");
	$(".project-small-img").removeAttr("height");
	$("#project-home-image img").removeAttr("height");
	$(".related-image img").removeAttr("height");
	$(".news-img").removeAttr("height");
	$(".project-image img").removeAttr("height");
	
	$(".mainnav li:last").css("margin", "0");
	
	$(".project-detail-title").appendTo("#gallery-detail-heading");
	
	//call some functions only in public mode
	if (!$('.mgnlMainbar').length) {
		slideshow();
		project_slider();
	}
	
	//call some functions
	search_input();
	head_slider();
	related_products();

	//hide empty project preview elements
	$(".project-image").each(function() {
		var $this = $(this);

		var img = $this.children().find("img");
		if (img.size() == 0) {
			$this.hide();
		}	
	});
	
	//project preview images position
	$(".project-image img").each(function() {
		var $this = $(this);
		
		var width = $this.width();
		var height = $this.height();
		
		var offset_width_tmp = width-150;
		var offset_height_tmp = height-150;
		
		var offset_width = offset_width_tmp/2;
		var offset_height = offset_height_tmp/2;
		
		$this.css("margin-left", "-"+offset_width+"px");
		$this.css("margin-top", "-"+offset_height+"px");
	});
});

/**
 * Show and hide the value of the searchfield
 */
var search_input = function() {
	
	$("#search").click(function() {
		
		var $this = $(this);
		
		var src_val = $this.val();
		
		if (src_val == "SUCHE...") {
			$this.attr("value","");
		}
		
		$("#search").blur(function() {
			
			var $this = $(this);
			
			if ($this.val() == "") {
				$this.attr("value", "SUCHE...");
			}
		});
	});
}
	
/**
 * Homepage slideshow
 */
var slideshow = function() {
	
	var interval = setInterval("autoplay()", 5000);
		
	$('<div class="slideshow-next">Weiter</div>').appendTo("#slideshow-home");
	$('<div class="slideshow-prev">Zurück</div>').appendTo("#slideshow-home");
	
	
	var $imgs = $('#slideshow-home');
	var $imgs_children  = $imgs.children(".slideshow-item");
	var round = Math.floor(Math.random() * $imgs_children.length );
	var $add_active  = $( $imgs_children[ round ] );
	
	$add_active.addClass('active');
	$add_active.show();
	
	$(".slideshow-next").click(function() {
		
		clearInterval(interval);
		
		var act = $(".slideshow-item[class*=active]");
		
		act.removeClass("active");
		
		if (act.next().attr("class") != "slideshow-next" && act.next().attr("class") != "slideshow-prev") {
			act.next().addClass("active");
		}	
	
		if (act.next().size() == 0 || act.next().attr("class") == "slideshow-next" || act.next().attr("class") == "slideshow-prev") {
			$(".slideshow-item:last").removeClass("active");
			$(".slideshow-item:first").addClass("active");
		}
		
		$(".slideshow-item").fadeOut("slow");
		$(".slideshow-item.active").fadeIn("slow");

	});
	
	$(".slideshow-prev").click(function() {
		
		clearInterval(interval);
		
		var act = $(".slideshow-item[class*=active]");
		
		act.removeClass("active");
		
		if (act.prev().attr("class") != "slideshow-next" && act.prev().attr("class") != "slideshow-prev") {
			act.prev().addClass("active");
		}	
	
		if (act.prev().size() == 0 || act.prev().attr("class") == "slideshow-next" || act.prev().attr("class") == "slideshow-prev") {
			$(".slideshow-item:first").removeClass("active");
			$(".slideshow-item:last").addClass("active");
		}
		
		$(".slideshow-item").fadeOut("slow");
		$(".slideshow-item.active").fadeIn("slow");
	});
	
	autoplay = function() {
		
		var act = $(".slideshow-item[class*=active]");
		
		act.removeClass("active");
		
		if (act.next().attr("class") != "slideshow-next" && act.next().attr("class") != "slideshow-prev") {
			act.next().addClass("active");
		}	
	
		if (act.next().size() == 0 || act.next().attr("class") == "slideshow-next" || act.next().attr("class") == "slideshow-prev") {
			$(".slideshow-item:last").removeClass("active");
			$(".slideshow-item:first").addClass("active");
		}
		
		$(".slideshow-item").fadeOut("slow");
		$(".slideshow-item.active").fadeIn("slow");
		
	}
}

/**
 * Project slider
 */
var project_slider = function() {
	
	$(".project-big-img").hide();
	$('<div id="gallery-main"></div>').appendTo("#gallery-slider");
	$('<div id="gallery-slider-thumbs"></div>').appendTo("#gallery-slider");
	$(".project-detail-img").appendTo("#gallery-slider-thumbs");
	
	var prev_nav = $('<div id="gal-prev">Zur&uuml;ck</div>').appendTo("#gallery-main");
	var next_nav = $('<div id="gal-next">N&auml;chstes</div>').appendTo("#gallery-main");
	
	def_image_tmp = $(".project-detail-img:first .project-big-img");
	
	var $index = def_image_tmp;
	
	var def_image = def_image_tmp.clone().appendTo("#gallery-main");

	def_image.removeAttr("height");
	
	def_image.fadeIn('slow');
	
	//gallery overview
	$(".project-detail-img").click(function() {
		
		$('#gallery-main .project-big-img').fadeOut('slow');
		$('#gallery-main .project-big-img').remove();
		
		var big_img = $(this).children(".project-big-img");
		
		var gallery_image = big_img.clone().appendTo("#gallery-main");
	
		gallery_image.removeAttr("height");
	
		gallery_image.fadeIn('slow');
		
		$index = big_img;
		
		return $index;
	});
		
	//prev nav
	prev_nav.click(function() {
		
		$('#gallery-main .project-big-img').fadeOut('slow');
		$('#gallery-main .project-big-img').remove();
		
		var prev_image_tmp = $index.parent().prev().children(".project-big-img");
		
		if (prev_image_tmp.size() == 0 ) {
			prev_image_tmp = $(".project-detail-img:last .project-big-img");
		}		

		$index = prev_image_tmp;
		
		var prev_image = prev_image_tmp.clone().appendTo("#gallery-main");
		
		prev_image.removeAttr("height");
		
		prev_image.fadeIn('slow');
		
		return $index;
	});
	
	//next nav
	next_nav.click(function() {
		$('#gallery-main .project-big-img').fadeOut('slow');
		$('#gallery-main .project-big-img').remove();
		
		var next_image_tmp = $index.parent().next().children(".project-big-img");
		
		if (next_image_tmp.size() == 0 ) {
			next_image_tmp = $(".project-detail-img:first .project-big-img");
		}
		
		$index = next_image_tmp;
		
		var next_image = next_image_tmp.clone().appendTo("#gallery-main");
		
		next_image.removeAttr("height");
		
		next_image.fadeIn('slow');
		
		return $index;
	});	
}

/**
 * Head slider
 */
var head_slider = function() {
	
	var prev = $('<div id="head-slide-prev">Zur&uuml;ck</div>').appendTo("#head-slider");
	var next = $('<div id="head-slide-next">Weiter/div>').appendTo("#head-slider");
	var	ul = $("#head-slider ul");
	var li = $("#head-slider ul li");
	var img = $("#head-slider ul li img");
	
	//image position in the middle
	var img_height = img.height();
	var offset_height_tmp = img_height-80;
	var offset_height = offset_height_tmp/2;
	img.css("margin-top", "-"+offset_height+"px");
	
	var minus = "143";
	var which = "";
	
	ul.each(function() {
		
		li.each(function(i){
			li_items = i;
		});
		
		var width_tmp = li_items*143;
		var width = width_tmp - 858;
		
		
		
		//pev nav
		prev.click(function() {
			
			autopl.stop();
			
			sub = parseInt(minus);
			
			if (sub != 0) {
				minus = sub - 143;	
			}
			
			if (which == "next") {
				minus = sub - 286;
			}
			
			ul.animate({'marginLeft' : '-' + minus});
			
			which = "prev";
			
			return minus;
		});
		
		//next nav
		next.click(function() {

			autopl.stop();
			
			plus =  parseInt(minus);
			
			if (which == "prev") {
				plus = plus + 143;
			}
			 
			ul.animate({'marginLeft' : '-' + plus});
			
			if (plus < width) {
				minus = plus + 143;
			}
			
			which = "next";

			return minus;
		});

		//autoplay
		head_autoplay = function() {
			
			autopl = ul.stop().animate({'marginLeft' : '-' + width}, 150000, "linear");
			
		}
		
		head_autoplay();
		
		//mouseover
		li.mouseover(function() {
			var $this = $(this);
			
			var desc = $this.children(".head-slider-preview");
			
			desc.show();
			
		});
		
		//mouseleave
		li.mouseleave(function() {
			var $this = $(this);
			
			var desc = $this.children(".head-slider-preview");
			
			desc.hide();
			
		});
		
		
	});
}
/**
 * Related products
 */
var related_products = function() {
	$("#related-products").slideDown("slow");
	$(".detail-back-wrapper.toggle").addClass("open");
	
	$("#related-toggle").click(function() {
		
		if ($("#related-products").is(":hidden")) {
			
			$(".detail-back-wrapper.toggle").removeClass("closed");
			$(".detail-back-wrapper.toggle").addClass("open");
			
			$("#related-products").slideDown("slow");
			
		} 
		else {
			
			$(".detail-back-wrapper.toggle").removeClass("open");
			$(".detail-back-wrapper.toggle").addClass("closed");
			
			$("#related-products").slideUp("slow");
		}
	})
}

