$(document).ready(function () {
	
	$('#country-picker').fancybox({
		'overlayOpacity'	: 0.7,
		'width'				: 897,
		'height'			: 432,
		'autoScale'			: false,
		'autoDimensions'	: false,
		'titleShow'			: false,
		'href'				: '#country-overlay',
		'onComplete'		: changeMapBg()
	});
	
	
	if ($("#banner .item").length > 1)	{
		$('#banner').cycle({
			timeout: 8000,
			pause: 1
		});
	}
	
	$('.news-tab-header').click(function(){
		$('.news-tabs .active .news-tab-content').fadeOut('100');
		$('.news-tab').removeClass('active');		
		$(this).parent().addClass('active');
		setTimeout(function () {			
			$('.news-tabs .active .news-tab-content').fadeIn('500');
		}, 200);		
	});	

	// click event on whole element
	$('.box-item').click(function(){
		window.location.href = $(this).find('a').attr('href');
	});
	
	
	if ($('input[name=SubmitForm]').length > 0) {
		$('input[name=SubmitForm]').parent().submit(function () {
			// find the fields to validate and validate them
			formValidationArray = [];
			$('li.required').each(function (index) {
				if ($(this).find('input:not([type=checkbox])').length > 0) {
					if ($(this).find('input').val() != "") {
						if($(this).find('input').attr('name').indexOf('Email') != '-1')	{
							if($(this).find('input').val().indexOf('@') !='-1')	{
								formValidationArray.push('true');		
							} else	{
								formValidationArray.push('false');
							}
						}	else	{
							formValidationArray.push('true');
						}						
					} else {
						formValidationArray.push('false');
					}
				}
				if ($(this).find('textarea').length > 0) {
					if ($(this).find('textarea').val() != "") {
						formValidationArray.push('true');
					} else {
						formValidationArray.push('false');
					}
				}
				if ($(this).find('select').length > 0) {
					if ($(this).find('select option:selected').index() != 0) {
						formValidationArray.push('true');
					} else {
						formValidationArray.push('false');
					}
				}
			});
			// loop through the fields and mark them if failed to validate and form the alert
			var i = 0;
			 if($("html").attr("lang") == "de-DE")
        var validationMessage = 'Sie müssen diese Felder ausfüllen:\n';
			 else
				var validationMessage = 'You must fill out these fields:\n';
			var fieldName = '';
			for (i = 0; i < formValidationArray.length; i++) {
				jq = 'li.required:eq(' + i + ')';
				if (formValidationArray[i] == 'true') {
				$(jq).removeClass('failed');
			} else {
				fieldName = $(jq).find('label').html();
				validationMessage += '- ' + fieldName + '\n';
				$(jq).addClass('failed');
				}
			}
			// determin if the required form fields are valid
			if ($("li.failed").length > 0) {
				alert(validationMessage);
				return false;
			} else {
				return true;
			}
		});
	} 
	
	
});

function changeMapBg()	{
	// set the first map element
	$('#country-content li a:eq(0)').addClass('active');
	mapId = 'url(\''+$('#country-content li a:eq(0)').attr('rel')+'\')';
	$('#country-overlay').css('background-image',mapId);
	
	// activate the hover function on fancybox load
	$('#country-content li a').hover(
		function () {
   			mapId = 'url(\''+$(this).attr('rel')+'\')';
			$('#country-overlay').css('background-image',mapId);
			$('#country-content li a').removeClass('active');
			$(this).addClass('active');	
  		}
	);
}

	

