function makeEmail(user, site) {
	document.write('<a href=\"mailto:' + user + '@' + site + '\" title=\"'+user + '@' + site+'\">');
	document.write(user + '@' + site +'</a>');
}		


// When document is ready
jQuery(document).ready(function($) {
	
	/* Google Map */
	if ($("#map_canvas").length > 0) {
		var canvas = $('#map_canvas');
		var data = new Array();
		var markers = new Array();
		var i = 0;

		data['latitude'] = canvas.attr('data-latitude');
		data['longitude'] = canvas.attr('data-longitude');
		data['kml'] = canvas.attr('data-kml');
		data['zoom'] = canvas.attr('data-zoom');
		canvas.find('.marker').each(function() {
			markers[i] = new Array();
			markers[i][0] = $(this).find('.label').html();
			markers[i][1] = $(this).attr('data-latitude');
			markers[i][2] = $(this).attr('data-longitude');
			markers[i][3] = $(this).find('.text').html();
			markers[i][4] = $(this).attr('data-image');
			i++;			
		});
		data['markers'] = markers;
		
		loadMap(data);
	}


	/* Floating li / Carousels */
	$('.floating-li').each(function() {

		if ($(this).hasClass('box50-wrapper')) {
			var items_per_row = 2;
		}

		if ($(this).hasClass('box33-wrapper')) {
			var items_per_row = 3;
		}

		var pad_correction = parseInt($(this).find('.body .content').css('padding-top'))
							+parseInt($(this).find('.body .content').css('padding-bottom'));

		// Generate carousel if needed
		if ($(this).hasClass('carousel') && $(this).find('li').length > items_per_row) {
		
			// Get highest box content height
			var content_height = 0;
			var content = $(this).find('.body .content, .body .image');
			var highest_has_image = false;
			var highest_has_content = false;

			content.each(function() {
				if ($(this).height() > content_height) {
					if($(this).hasClass('image')) {
						highest_has_image = true;
						highest_has_content = false;
					} else {
						highest_has_image = false;
						highest_has_content = true;
					}
					content_height = $(this).height();
				}
			});

			// Make all box content same height
			content.each(function() {
				if($(this).hasClass('content') && highest_has_image == true) {
					pad_correction_for_content = pad_correction;
				} else {
					pad_correction_for_content = 0;
				}
				if($(this).hasClass('image') && highest_has_content == true) {
					pad_correction_for_image = pad_correction;
				} else {
					pad_correction_for_image = 0;
				}
				$(this).height(content_height-pad_correction_for_content+pad_correction_for_image);
			});
		
			// Get highest li height
			var li_height = 0;
			var li = $(this).find('li');
			li.each(function() {
				if ($(this).height() > li_height) {
					li_height = $(this).height();
				}
			});

			// Make all li same height
			li.each(function() {
				$(this).height(li_height);
			});

			// Make carousel
			$(this).carousel({
				loop : false,
				dispItems : 1
			}).find("input.carousel-control").each(function() {
				$(this).css({ 'top' : (li_height/2)-($(this).height()/2)-15 });
			});
		
		} else {

			var i = 0;
			var total_li = $(this).find('li').length;
			var total_rows = Math.floor((total_li/items_per_row)+0.5);

			for (i = 1; i < total_rows+1; i++) {
		
				var li_height = 0;
				var content_height = 0;
		
				// Get highest box content height in this row
				var content = $(this).find('.body .content.row_'+i+', .body .image.row_'+i);
				var highest_has_image = false;
				var highest_has_content = false;

				content.each(function() {
					if ($(this).height() > content_height) {
						if($(this).hasClass('image')) {
							highest_has_image = true;
							highest_has_content = false;
						} else {
							highest_has_image = false;
							highest_has_content = true;
						}
						content_height = $(this).height();
					}
				});
			
				// Make all box content in this row same height
				content.each(function() {
					if($(this).hasClass('content') && highest_has_image == true) {
						pad_correction_for_content = pad_correction;
					} else {
						pad_correction_for_content = 0;
					}
					if($(this).hasClass('image') && highest_has_content == true) {
						pad_correction_for_image = pad_correction;
					} else {
						pad_correction_for_image = 0;
					}
					$(this).height(content_height-pad_correction_for_content+pad_correction_for_image);
				});

				// Get highest li height in this row
				var li = $(this).find('li.row_'+i);
				li.each(function() {
					if ($(this).height() > li_height) {
						li_height = $(this).height();
					}
				});
				
				// Make all li in this row same height
				li.each(function() {
					$(this).height(li_height);
				});

			}
		
		}

	});
	
	
	/* Blend in Job-description */
	/* Start with first Job-Description open */
	$(".jobs ul li:first-child").addClass('active');
	title = $(this).find('.jobs li:first-child a span').html();
	$(".jobs ul li:first-child .job-description .content").prepend('<h3>'+title+'</h3>');
	height = $(".jobs ul li:first-child .job-description").height();
	$(".jobs").css('min-height', height);
	$(".jobs ul li:first-child .job-description").show();
	
	$(".jobs ul li a").hover(
		function() {
			var boxMarginTop = $('.jobs ul li.active .job-description').css('margin-top');
			$(this).parents('ul').find('.job-description').fadeOut('fast');
			$(this).parents('ul').find('li').removeClass('active');
			$(this).parents('li').addClass('active');
			$(this).parents('li').find('.job-description').find('h3').remove();


			title = $(this).find('span').html();
			box = $(this).parents('li').find('.job-description');
			box.find('.content').prepend('<h3>'+title+'</h3>');
			//$(this).parents('.jobs').css('height', box.height());
			box.css('margin-top', boxMarginTop);
			box.fadeIn('fast');
			moveJobDescriptionDetail();
		},		
		function() { 
			//$(this).find('.job-description').fadeOut('fast');
			//$(this).parents('.jobs').css('height', 'auto');
		}
	);

	/**
	 * Make the job description always visible.
	 */

	if ($('.jobs ul li.active .job-description').length) {
		var jobDescription = $('.jobs ul li.active .job-description');
		var minTop = jobDescription.offset().top;
		var maxBottom = $('#footer').offset().top;
		var screenMargin = 30;
		var lastDocumentTop = -1; 
	}
	function moveJobDescriptionDetail() {
		jobDescription = $('.jobs ul li.active .job-description');
		if (jobDescription.length) {
			var documentTop = $(window).scrollTop();
			if (lastDocumentTop != documentTop) {
				lastDocumentTop = documentTop;
				var jobDescriptionTop = jobDescription.offset().top;
				if (jobDescriptionTop <= (documentTop + screenMargin) || jobDescriptionTop > minTop) {
					var jobDescriptionMarginTop = documentTop - minTop + screenMargin;
					if ((jobDescriptionMarginTop + minTop + screenMargin + jobDescription.height()) > maxBottom) {
						jobDescriptionMarginTop = maxBottom - minTop - screenMargin - jobDescription.height();
					}
					if (jobDescriptionMarginTop >= 0) {
						jobDescription.animate({"margin-top": jobDescriptionMarginTop + "px"}, {duration: 500, queue: false});
					}
					else {
						jobDescription.animate({"margin-top": 0}, {duration: 500, queue: false});
					}
				}
			}
		}
	}

	window.setInterval(moveJobDescriptionDetail, 500);
	
	/* Remove unwanted html-tags */
	$("dd").each(function() {
		string = $(this).html();
		$(this).html(stripTag('h2', string));
	});
	
		
	function stripTag(tag, string) {
		return string.replace(/<\/?[^>]+>/gi, '');
 	}
 	
 	
	$('#apply-form').formvalidate();

	// Job animation on home.
	if ($('.teaser').length) {
		window.setInterval(function() {
			var jobItems = 3;
			var allJobs = $('.teaser').find('.job');
			var currentJobs = $('.teaser').find('.job:visible');
			var currentPosition = allJobs.index(currentJobs);
			var newPosition = 0;
			if (currentPosition + jobItems < allJobs.length) {
				newPosition = currentPosition + jobItems;
			}
			var newJobs = allJobs.slice(newPosition, newPosition + jobItems);
			currentJobs.stop(true,true).fadeOut('fast', function() {
				newJobs.fadeIn('fast');
			});
		}, 3000);
	}

	// Reset job filters.
	$('#filter-reset').click(function (){
		$('select[name=category]').val('Alle Berufsgruppen');
		$('select[name=region]').val('Alle Regionen');
		$('input[name=keyword]').val('');
		$('input[name=temp]').attr('checked', true);
		$('input[name=fix]').attr('checked', true);
		$('#job-filters').submit();
	});

	// Fix footer to bottom.
	if (($('#top').height() + $('#body').outerHeight() + $('#footer').height()) < $(window).height()) {
		var bodyPadding = $('#body').outerHeight() - $('#body').height();
		$('#body').css('min-height', $(window).height() - $('#top').height() - $('#footer').height() - bodyPadding - 2 + "px");
	}
	

});


/**
 * Formvalidation
 */
(function($) {
	$.fn.formvalidate = function() {
		
		return this.each(function() {
			
			var $this = $(this);

			$this.submit(function() {
				
				var valid = true;

				$this.find('.notempty input').each(function() {
					if ($.trim($(this).val()) == '') {
						$(this).parents('.notempty').addClass('error');
						valid = false;
					}
					else {
						$(this).parents('.notempty').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;
					}
				});

				$this.find("select").each(function() {
					if ($(this).val() == "empty") {
						$(this).addClass('error');
						valid = false;
					}
					else {
						$(this).removeClass("error");
					}
				});
				
				var hasPhone = false;
				$this.find(".validate-phone input").each(function() {
					if ($.trim($(this).val()) == '') {
						hasPhone = false;
					}
					else {
						hasPhone = true;
						return false;	
					}
				});

				if (!hasPhone) {
					$this.find(".validate-phone").addClass("error");
					valid = false;
				}
				else {
					$this.find(".validate-phone").removeClass("error");
				}
			/*	
				var hasChecked = false;
				$this.find(".checkbox-field input").each(function() {
					if ($(this).is(":checked")) {
						hasChecked = true;
					}
				});

				if (!hasChecked) {
					$this.find(".checkbox-label").addClass("checkbox-error");
					valid = false;	
				}
				else {
					$this.find(".checkbox-label").removeClass("checkbox-error");
				}
			*/	
				if (!valid) {
					return false;
				}
				return true;
			})
		});
	}
}) (jQuery);




