$(document).ready(function(){

	$('ul.links li').ahover();

	$('input').focus(function () {
        $(this).parents('dl').addClass('dl_active');
        $(this).parents('dl').find('dt').addClass('dt_active');
    });

	$('input').blur(function () {
        $(this).parents('dl').removeClass('dl_active');
        $(this).parents('dl').find('dt').removeClass('dt_active');
    });

	$('textarea').focus(function () {
        $(this).parents('fieldset').addClass('dl_active_txtarea');
        $(this).parents('dl').find('dt').addClass('dt_active');
    });

	$('textarea').blur(function () {
        $(this).parents('fieldset').removeClass('dl_active_txtarea');
        $(this).parents('dl').find('dt').removeClass('dt_active');
    });


	// FORMS

	$('#kontakt_form').ajaxForm({
		target:'#kontakt_status',
		beforeSubmit: validate_kontakt,
		url:'kontakt.html',
		clearForm: true
	});

	$('#slider').easySlider({
		controlsShow: false,
		auto: true,
		continuous: true
	});

	// manage col heights

	if($('.col1').height() <= $('.col2').height())
	{
		$('.col1').height($('.col2').height());
	}
	else
	{
		$('.col2').height($('.col1').height());
	}

	if($('.col1_sub').height() <= $('.col2_sub').height())
	{
		$('.col1_sub').height($('.col2_sub').height());
	}
	else
	{
		$('.col2_sub').height($('.col1_sub').height());
	}

	// lightbox settings

	$('a.lightbox_link').lightBox({
		overlayBgColor: '#454545',
		overlayOpacity: 0.5,
		imageLoading: '/img/lightbox_images/loading.gif',
		imageBtnClose: '/img/lightbox_images/close.gif',
		imageBtnPrev: '/img/lightbox_images/prev.gif',
		imageBtnNext: '/img/lightbox_images/next.gif',
		imageBlank: '/img/lightbox_images/blank.gif',
		containerResizeSpeed: 350,
		txtImage: 'Image',
		txtOf: 'von'
	});

	// change body background

	$('.change_background_color').click(function() {
		$('body').animate({
			backgroundColor: $(this).find('span').html()
		}, 1500);
		$.cookie('GENSLER-MEDIENSERVICE_background_style', $(this).find('span').html()); // set cookie
	});

	if($.cookie('GENSLER-MEDIENSERVICE_background_style').length > 0)
	{
		$('body').css('background-color',$.cookie('GENSLER-MEDIENSERVICE_background_style'));

	}

});

var error_string = '';
var hasError = false;

function validate_input_regexpr(id,string,regexpr,regexpr_string) {

	id = "#" + id;

	if($(id).val() == '')
	{
		$(id).addClass('error_bg');
		error_string += "<li>" + string + "</li>";
		hasError = true;
	}
	else
	{
		if(regexpr_string.length > 0 && !regexpr.test($(id).val()))
		{
			$(id).addClass('error_bg');
			error_string += "<li>" + regexpr_string + "</li>";
			hasError = true;
		}
		else
		{
			$(id).removeClass('error_bg');
		}
	}

}

function validate_input(id,string) {

	id = "#" + id;

	if($(id).val() == '')
	{
		$(id).addClass('error_bg');
		error_string += "<li>" + string + "</li>";
		hasError = true;
	}
	else
	{
		$(id).removeClass('error_bg');
	}

}

function validate_radio(id,string) {

	if(!$("input[name=" + id + "]").is(":checked")) {
		error_string += "<li>" + string + "</li>";
		hasError = true;
	}

}

function validate_kontakt(){

		$(".error").hide();
		$(".success").hide();
		hasError = false;
		error_string = '';
		regexpr_mail = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		regexpr_plz = /^[0-9]{5}$/;
		regexpr_telefon = /^[0-9\(\)\-\+\/]{6,20}$/;

		validate_radio('anrede','Bitte Anrede wählen!');
		validate_input('nachname','Bitte Nachnamen angeben!');
		validate_input('vorname','Bitte Vornamen angeben!');
		validate_input('strasse','Bitte Straße angeben!');
		validate_input_regexpr('plz','Bitte Postleitzahl angeben!',regexpr_plz,'Bitte Postleitzahl überprüfen!');
		validate_input('ort','Bitte Ort angeben!');
		validate_input('telefon','Bitte Telefon angeben!');
		validate_input_regexpr('mail','Bitte E-Mail-Adresse angeben!',regexpr_mail,'Bitte E-Mail Adresse überprüfen!');
		validate_input('nachricht','Bitte Nachricht eingeben!');

		if(hasError == true) {
			$("#kontakt_error").html(error_string);
			$(".error").show();
			return false;
		} else {
			$("#kontakt_status").html('<li>Nachricht wird gesendet ...</li>');
			$(".success").show();
			return true;
		}

}
