function naviOut() {
	$(".projects:visible").slideUp();
	$(".info:visible").slideUp();
	$("#navigation").css({ backgroundColor: "transparent" });
	$(".toggleProjects").children("span").show();
};

function fadeNews() {
	var $this = $(".homeNews:visible");
	if($(".homeNews").size() != $this.prevAll().size()) {
		$this.next().css("z-index", "-1").show();
	} else {
		$(".homeNews:first").css("z-index", "-1").show();
	}
	$this.fadeOut("slow", function() {
		$(".homeNews:visible").css("z-index", "0");
	});
};

$(document).ready(function(){

/*INITIALS*/

	$(".projects").hide();
	$(".info").hide();

	var curl = (document.URL);
	if (curl.match("#zcr")) {
		$("div.newsletter_infos").hide();
	}


/*NAVIGATION*/

	var agent = navigator.userAgent.toLowerCase();

	if (agent.indexOf('khtml') != -1 || agent.indexOf('webkit') != -1 ) {
		$("p.search_input").css({"margin-top" : -1});
	};

	if (agent.indexOf('iphone') != -1 || agent.indexOf('ipod') != -1 || agent.indexOf('ipad') != -1 || agent.indexOf('android') != -1) {

		var vsbl = 0;
		
		$(".projectsList1, .toggleProjects").toggle(function() {
			$("#navigation").css({ backgroundColor: "white"});
			$(".projects:not(:visible)").slideDown();
			$(".toggleProjects").children("span").hide();
			vsbl = 0;
		}, function() {
			naviOut();
			vsbl = 1;
		});
		
		$(".toggleProjects").click(function(event) {
			if (vsbl == 1) {
				var curl = (window.location.pathname);
				window.location = curl.substr(0,4) + "projects/";
			}
		});
		
		$(".projectsList1").click(function(event) {
			if (vsbl == 1) {
				var curl = (window.location.pathname);
				window.location = curl.substr(0,4);
			}
		});
		
	} else {
		$(".toggleProjects, .projectsList1").hover(function() {
			$("#navigation").css({ backgroundColor: "white"});
			$(".projects:not(:visible)").slideDown();
			$(".toggleProjects").children("span").hide();
		});

		$("#navigation").mouseleave(function() {
			timerId = setTimeout("naviOut()", 100);
		}).mouseenter( function() {
			clearTimeout( timerId );
		});
	}


/*SLIDESHOW*/

	var newsCount = $(".homeNews").length;
	if (newsCount > 1) {
		newsId = setInterval("fadeNews()", 7000);
	}
	
	function centerImg() {
		var xPos = ($(window).width() - 1200) / 2,
		yPos = ($(window).height() - 900) / 2;
		if (xPos < 0) {
			$(".homeNews .background").css({
				left : xPos
			});
		} else {
			$(".homeNews .background").css({
				left : 0
			});
		}
		
		if (yPos < 0) {
			$(".homeNews .background").css({
				top : yPos
			});
		} else {
			$(".homeNews .background").css({
				top : 0
			});
		}
	};
	//centerImg();
	
	$(window).resize(function() {
		//centerImg();
	});
	
/*NEWSLETTER*/ 

	$("label.zemRequired").each(function() {
		var txt = $(this).text();
		txt += ' *';
		$(this).text(txt);
	});

	var $submit = $("input.zemSubmit");

	$("input.zemRequired").each(function() {
		if ($(this).val().length == 0) {
			$submit.attr("disabled","true");
		}
	});

	$("input.zemRequired").blur(function() {
		$("input.zemText.zemRequired").each(function(i) {
			if ($(this).attr("value").length > 0) {
				$submit.removeAttr("disabled");
			} else {
				$submit.attr("disabled","true");
				return false;
			}
		});
	});

/*SCROLL TO TOP*/

	$("a.top").click(function() {
		$("html:not(:animated), body:not(:animated)").animate({scrollTop: 0}, 500);
	});
});
