 $(document).ready(function(){
	 
	// PNG FIX
	$(document).pngFix();

	if ($.browser.msie && (jQuery.browser.version < 8)) {
	  var zIndexNumber = 1000;
	  $('div').each(function() {
	   $(this).css('zIndex', zIndexNumber);
	   zIndexNumber -= 10;
	  });
	}
	
	$(window).resize(function() { 		
		if ($('#nl-wrap').is(':visible')) {
			$('#nl-wrap').hide();
		}
		
		if ($('#total-form').is(':visible')) {
			$("#total-form").hide();
			$("#content").css("margin-bottom", "80px");
			$(".support-trigger1").removeClass("active");
			$(".support-trigger").removeClass("active");
		}
		
		if ($('.team-row').is(':visible')) {
			$('.team-row').each(function(index) {
				$(this).find(".hover-over-out").removeClass("active");
				$(this).find(".tooltip").hide();
			});
		}
	});

	if ($('.team-row')) {
		$('.team-row').each(function(index) {
			var ttr = $(this).find(".tooltip-trigger-right");
			ttr.find(".no-tool-tip").parent().removeClass("active").addClass("no-nothing");
			var ttl = $(this).find(".tooltip-trigger-left");
			ttl.find(".no-tool-tip").parent().removeClass("active").addClass("no-nothing");
		});
	}
	
	
	$('html').click(function() {
        // Search results
        if ($('#nl-wrap').is(':visible')) {
                $('#nl-wrap').hide();
        }
        
        if ($('#total-form').is(':visible')) {
        	$("#total-form").hide();
			$("#content").css("margin-bottom", "80px");
			$(".support-trigger1").removeClass("active");
			$(".support-trigger").removeClass("active");
        }
        
        if ($('.team-row').is(':visible')) {
			$('.team-row').each(function(index) {
				$(this).find(".hover-over-out").removeClass("active");
				$(this).find(".tooltip").hide();
			});
		}
	});
	
	/* calling the newsletter snippet and display it */	
	$('#newsletter').click(function(event) {	
		/* stop html clicking on newsletter button */
		event.stopPropagation();
		
		var nlh = $('#newsletter').outerHeight(true);
		var nlw = $('#newsletter').outerWidth();
		var pos  = $('#newsletter').position();  
		
		var top =  pos.top + nlh + 10;
		var left = (pos.left + (nlw/2)) - 125;
		
		$container = $('<div/>').attr('id','nl-owrap'); 
		
		/* if div w id nl-wrap does not exist, create it ... */
		if(! $('#nl-wrap').length ) {
			$container.css('position','absolute')
			.appendTo('body');	
	
			$($container).load('/docroot/s_am/snips/newsletter.jsp', $container.slideDown());
			
			$container.css({
				top:  top + 'px',
				/*we want the center of the newsletter div and we know it is 251px wide */
				left: left + 'px'
				});
			
			$('#nl-owrap').click(function(event) {
				event.stopPropagation();
			});
			
		} else if ($('#nl-wrap').length && $('#nl-wrap').is(':visible')) {  /* div exists but may be hidden so we want to show it ... */ 
			$('#nl-wrap').hide();	
		} else {
			$('#nl-wrap').show();	
			$('#nl-owrap').css({
				top:  top + 'px',
				/*we want the center of the newsletter div and we know it is 251px wide */
				left: left + 'px'
			}); 
		}
	});

	$('#slideshow').backslider({'indicator': true, 'delay': 5500});
	
	/* 
	 * Scrollable Start
	 */	
	$(".scrollable").scrollable();
	$(".single-item").parent("div").css("position", "relative");

	$(".items .single-item").click(function() {

		// see if same thumb is being clicked
		if ($(this).hasClass("active")) { return; }	
		
		var pos = $(this).position();
		var width = $(this).outerWidth();
		
		var margleft = ((width/2) + ( pos.left % 975 ))-33;
		
		if($("#item_wrap").find('.display'))
			$("#item_wrap").find('.display').remove();
		
		if($("#item_wrap").find('#triangle'))
			$("#item_wrap").find('#triangle').remove();
		
		var $tt = $(this).find('.tooltip');
				
		$container = $('<div/>').attr('class','display');
		$container.append($tt.html());
		
		$container1 = $('<div/>').attr('id','triangle').css('padding-left', margleft);
		$container1.append('<div class="triangle"></div>');
		
		$('#item_wrap').append($container1);		
		$('#item_wrap').append($container);		
		$("#item_wrap").show();	

		// activate item
		$(".items .single-item").removeClass("active");
		$(this).addClass("active");

	// when page loads simulate a "click" on the first image
	}).filter(":first").click();	
	
	
	$('#slider-wrapper .browse').click(function () {
		if($("#item_wrap").find('.display'))
			$("#item_wrap").find('.display').remove();
		if($("#item_wrap").find('#triangle'))
			$("#item_wrap").find('#triangle').remove();
		$(".items .single-item").removeClass("active");
	});

	$("#search_field").click(function() {
		if ($(this).val() == "Suchen") {
			$(this).val("");
		}
		$(this).blur(function() {
			if ($(this).val() == "") {
				$(this).val("Suchen");
			}
		});
	});

	$(".hover-over-out").click(function(event) {
		
		if ($.browser.msie && (jQuery.browser.version < 8)) {
			  var zIndexNumber = 1000;
			  $('div').each(function() {
			   $(this).css('zIndex', zIndexNumber);
			   zIndexNumber -= 10;
			  });
		}
		
		var $tooltip = $(this).find('.tooltip');
		
		$tooltip.click(function(event) {
			event.stopPropagation();
		});
		
		event.stopPropagation();
		
		$('.team-row').each(function(index) {
			$(this).find(".hover-over-out").removeClass("active");
			$(this).find(".tooltip").hide();
		});
		
		if($(this).find('.no-tool-tip').length == 0 ) {
			$(this).addClass("active");
			
			/* positioning */
			//var pos = $(this).position();
			var owidth = $(this).outerWidth();
			var oheight = $(this).outerHeight(true);
			
			var pos = $(this).position();
			
			//console.log(pos.top+' '+pos.left+' '+oheight+' '+owidth);
			
			
			var margleft = ((owidth/2) + ( pos.left % 645 ))-33;
			var posTop = (pos.top + (oheight +15));
			
			$tooltip.css("top", posTop + "px");
			//console.log(posTop);
			if($(this).find('.right').length==0 && $(this).find('.left').length!=0) {
				$tooltip.css("left", (pos.left+5)+"px");
				$tooltip.find(".triangle").css("margin-left", margleft + "px");
			}
			
			if($(this).find('.left').length==0 && $(this).find('.right').length!=0) {
				
				$tooltip.css("left", (pos.left -330)+"px");
				$tooltip.find(".triangle").css("margin-left", margleft+323 + "px");
				
			}
			
			$tooltip.show();
			
			$tooltip.mouseover(function() {
				hasEvent = true;
			}).mouseleave(function() {
				hasEvent = false;
			});
		}
		return false;
});
	
	
	
	

	$(".support-trigger").click(function(event) {
		
		if ($.browser.msie && (jQuery.browser.version < 8)) {
			  var zIndexNumber = 1000;
			  $('div').each(function() {
			   $(this).css('zIndex', zIndexNumber);
			   zIndexNumber -= 10;
			  });
		}
		
		event.stopPropagation();
		
		$('#total-form').click(function(event) {
			event.stopPropagation();
		});
		
		$('.support-row').each(function(index) {
			$(".support-trigger1").removeClass("active");
			$(".support-trigger").removeClass("active");
		});
			$(".form-desc").remove();

		$(this).addClass("active");

		var title = $(this).find(".h2-wrap h2").text();
		var formText = $(this).find(".form-text").clone();
		var formDesc = $("<div/>").addClass("form-desc").html(formText).insertBefore($("#form-wrapper").find("h2")).show();

		/* positioning */
		//var pos = $(this).position();
		var owidth = $(this).outerWidth();
		var oheight = $(this).outerHeight(true);
		
		var pos = $(this).parent().position();
		
		//console.log(pos.top+' '+pos.left+' '+oheight+' '+owidth);
		
		var margleft = ((owidth/2) + ( pos.left % 645 ))-33;
		var posTop = (pos.top + (oheight +20));
		//console.log(posTop);

		$("#total-form").find(".triangle").css("margin-left", margleft + "px");
		$("#total-form").css("top", posTop + "px");

		$("#content").css("margin-bottom", "750px");
		$("#total-form").css("z-index", "1000");
		$("#total-form").show();

		var hiddenInput = $("#category-field");
		hiddenInput.val(title);

		var hasEvent = false;

		$("#total-form").mouseover(function() {
			hasEvent = true;
		}).mouseleave(function() {
			hasEvent = false;
		});
		return false;
	});
	
	
	$(".support-trigger1").click(function(event) {
		
		if ($.browser.msie && (jQuery.browser.version < 8)) {
			  var zIndexNumber = 1000;
			  $('div').each(function() {
			   $(this).css('zIndex', zIndexNumber);
			   zIndexNumber -= 10;
			  });
		}
		
		event.stopPropagation();
		
		$('#total-form').click(function(event) {
    		event.stopPropagation();
    	});
		
		$('.support-row').each(function(index) {
			$(".support-trigger1").removeClass("active");
			$(".support-trigger").removeClass("active");
		});
		
		$(".form-desc").remove();

		
		$(this).addClass("active");

		var title = $(this).find(".h2-wrap h2").text();
		var formText = $(this).find(".form-text").clone();
		var formDesc = $("<div/>").addClass("form-desc").html(formText).insertBefore($("#form-wrapper").find("h2")).show();

		/* positioning */
		//var pos = $(this).position();
		var owidth = $(this).outerWidth();
		var oheight = $(this).outerHeight(true);
		
		var pos = $(this).parent().position();
		
		//console.log(pos.top+' '+pos.left+' '+oheight+' '+owidth);
		
		var margleft = ((owidth/2) + ( pos.left % 645 ) + 335)-33;
		var posTop = (pos.top + (oheight +20));
		//console.log(posTop);

		$("#total-form").find(".triangle").css("margin-left", margleft + "px");
		$("#total-form").css("top", posTop + "px");

		$("#content").css("margin-bottom", "750px");
		$("#total-form").css("z-index", "1000");

		$("#total-form").show();
		
		var hiddenInput = $("#category-field");
		hiddenInput.val(title);
		var hasEvent = false;
		
		$("#total-form").mouseover(function() {
			hasEvent = true;
		}).mouseleave(function() {
			hasEvent = false;
		});
		return false;
	});
	
	$("#fform").formvalidate();
});

/**
 * Formvalidation
 */
(function($) {
	$.fn.formvalidate = function() {
		
		return this.each(function() {
			
			var $this = $(this);
			
			$this.submit(function() {
				
				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,4})+$/.test($(this).val())) {
						$(this).removeClass('error');
					}
					else {
						$(this).addClass('error');
						valid = false;
					}
				});
				
				if (!valid) {
					return false;
				}
			})
		});
	}
}) (jQuery);

