$(function(){

	$('.accordionhead').click(function() {
		$(this).next().slideToggle('fast');
		$(this).toggleClass("expanded");return false;
	}).next().hide();
        /* Zahlen mit Tausenderpunkten versehen */
        $('.jqtsep').each(function(){
             $(this).parseNumber({format:"###,###,###,###,###", locale:"de"});
             $(this).formatNumber({format:"###,###,###,###,###", locale:"de"});
            }
        );
	if(location.hash == '#hilight_invite') {
	    $('.accordionhead_mailform').click();
	    setTimeout(function(){
	        $.scrollTo('.mailform_wrap',1000,{
	            easing:'swing',
	            offset:-40,
	            onAfter:function(){
	              $('.mailform_wrap').css('border','2px dotted red');
	              $('#email_text').trigger({
	                  type:"mouseover",
	                  pageX:$('#email_text').offset().left+10,
	                  pageY:$('#email_text').offset().top+$('#email_text').height()-10
	              });
	              $('.focus_after_scroll').focus().unbind('mouseover').removeClass('tooltip_btn');

	              setTimeout(function(){
	                  $('.mailform_wrap').css('border','0');
	              },1500);
	            }
	        });

	    },250);
	}
});

/**
 * Item html creation helper.
 */
jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({

    });
});

$(function() {

	var error_cls = "dena20_error";

	function mark_error (selector) {
		$(selector).addClass(error_cls);
	}

	function clear_form() {

		var is_invitation = $('#mail_form').hasClass( 'invite' );

		if (is_invitation)
		{
			$('#salutation').val("");
			$('#email_addr').val("");
			$('#prename').val("");
			$('#surname').val("");
		}

	    $('#email_text').val("");
	}

	function validate(formData, jqForm, options) {

		var is_invitation = $('#mail_form').hasClass( 'invite' );

		blockPageWait();

		var hasError = false;

		if (is_invitation) {
			$('#email_row').removeClass(error_cls);
			$('#email_row .wwdngpd-validator_msg').html("");
			$('#prename_row').removeClass(error_cls);
			$('#prename_row .wwdngpd-validator_msg').html("");
			$('#surname_row').removeClass(error_cls);
			$('#surname_row .wwdngpd-validator_msg').html("");
		}
		else {
			$('#agb_row').removeClass(error_cls);
			$('#agb_row .wwdngpd-validator_msg').html("");
		}


		$('#text_row').removeClass(error_cls);
		$('#text_row .wwdngpd-validator_msg').html("");

		var EMailValue = $('#email_addr').fieldValue();
		var PrenameValue = $('#prename').fieldValue();
		var SurnameValue = $('#surname').fieldValue();
	    var MsgValue   = $('#email_text').fieldValue();
	    var AgbValue   = $('#agb').fieldValue();

	    if (is_invitation && $.trim(EMailValue[0]) == "") {
	    	mark_error ('#email_row');
	    	$('#email_row .wwdngpd-validator_msg').html("Bitte geben Sie die Email-Adresse des Beteiligten an.");
	    	hasError = true;
	    }

	    if (is_invitation && $.trim(PrenameValue[0]) == "") {
	    	mark_error ('#prename_row');
	    	$('#prename_row .wwdngpd-validator_msg').html("Bitte geben Sie den Vornamen des Beteiligten an.");
	    	hasError = true;
	    }

	    if (is_invitation && $.trim(SurnameValue[0]) == "") {
	    	mark_error ('#surname_row');
	    	$('#surname_row .wwdngpd-validator_msg').html("Bitte geben Sie den Nachnamen des Beteiligten an.");
	    	hasError = true;
	    }

	    if (is_invitation && $.trim(MsgValue[0]) == "") {
	    	mark_error ('#text_row');
	    	$('#text_row .wwdngpd-validator_msg').html("Bitte geben Sie einen Nachrichtentext an den Beteiligten an.");
	    	hasError = true;
	    }

	    if (!is_invitation && $.trim(MsgValue[0]) == "") {
	    	mark_error ('#text_row');
	    	$('#text_row .wwdngpd-validator_msg').html("Bitte geben Sie Ihre Leistungen an dem Projekt ein.");
	    	hasError = true;
	    }

	    if (!is_invitation && $.trim(AgbValue[0]) != "yes") {
	    	mark_error ('#agb_row');
	    	$('#agb_row .wwdngpd-validator_msg').html("Bitte akzeptieren die Nutzungsbedingungen.");
	    	hasError = true;
	    }

	    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	    if (is_invitation && !filter.test(EMailValue[0])) {
	    	mark_error ('#email_row');
	    	$('#email_row .wwdngpd-validator_msg').html("Bitte geben Sie eine g&uuml;ltige Email-Adresse an.");
	    	hasError = true;
	    }

	    if (!hasError) {
	    	return true;
	    }

	    blockPageEvalError("Bitte korrigieren Sie Ihre Angaben!");

    	return false;
	}

	var blockPageSuccess = function (headline, text) {
		$.blockUI({
			message: '<div onClick="$.unblockUI();"><h1 style="margin-top: 20px;">'+headline+'</h1><span style="font-size: 13px; display: block; height: 100px; padding: 10px; margin-bottom: 10px; text-align: left;">' + text + '</span><span style="font-weight: bold; font-size: 12px;"><input type="image" src="fileadmin/templates/media/btn_schliessen.gif" onMouseOver="this.src=\'fileadmin/templates/media/btn_schliessen_hover.gif\'" onMouseOut="this.src=\'fileadmin/templates/media/btn_schliessen.gif\'" class="img_hover" value="senden"/></span></div>',
			css : { height: '200px', top: '100px', width: '400px', left: '288px' },
			timeout: 0,
			focusInput : false
		});
	}

	var blockPageWait = function () {
		$.blockUI({
			message: '<div onClick="$.unblockUI();"><h1 style="margin-top: 20px;">Anfrage wird gesendet</h1><img style="margin-top: 15px;" src="/fileadmin/templates/media/ajax-loader.gif" width="31" height="31" alt="please wait" /><br /><br/><span style="font-weight: bold; font-size: 12px;">Bitte warten</span></div>',
			css : { height: '150px', top: '200px', width: '300px', left: '338px' },
			timeout: 0,
			focusInput : false
		});
	}

	var blockPageEvalError = function (info) {
		$.blockUI({
			message: '<div onClick="$.unblockUI();"><h1 style="margin-top: 20px;">Unzureichende Angaben</h1><img style="margin-top: 15px;" src="/fileadmin/templates/media/ErrorCircle-32x32.png" width="32" height="32" alt="failure" /><br /><br/><span style="font-weight: bold; font-size: 12px;">'+info+'</span><span style="display: block; margin-top: 15px;"><input type="image" src="fileadmin/templates/media/btn_korrigieren.gif" onMouseOver="this.src=\'fileadmin/templates/media/btn_korrigieren_hover.gif\'" onMouseOut="this.src=\'fileadmin/templates/media/btn_korrigieren.gif\'" class="img_hover" value="korrigieren"/></span></div>',
			css : { height: '175px', top: '150px', width: '300px', left: '338px'},
			timeout: 0,
			focusInput : false
		});
	}

	var blockPageHttpError = function (XMLHttpRequest, textStatus, errorThrown) {

		var errinfo = "HTTP " + textStatus;
		try {
		    if( XMLHttpRequest.status == 0 )
		        return;
			errinfo += ' ' + XMLHttpRequest.status + " : " + XMLHttpRequest.statusText;
		} catch (err) {
		}
		var info = errinfo;

		$.blockUI({
			message: '<div onClick="$.unblockUI();"><h1 style="margin-top: 20px;">Fehler</h1><img style="margin-top: 15px;" src="/fileadmin/templates/media/ErrorCircle-32x32.png" width="32" height="32" alt="failure" /><br /><br/><span style="font-weight: bold; font-size: 12px;">'+info+'</span><span style="display: block; margin-top: 15px;"><input type="image" src="fileadmin/templates/media/btn_abbrechen.gif" onMouseOver="this.src=\'fileadmin/templates/media/btn_abbrechen_hover.gif\'" onMouseOut="this.src=\'fileadmin/templates/media/btn_abbrechen.gif\'" class="img_hover" value="korrigieren"/></span></div>',
			css : { height: '175px', top: '150px', width: '300px', left: '338px'},
			timeout: 0,
			focusInput : false
		});
	}


	function showResponse(responseText, statusText) {
		if (statusText == "success") {
			clear_form();
			blockPageSuccess(responseText.headline, responseText.message);
		}
	}

	var options = {
        beforeSubmit:  validate,
        success:       showResponse,
        error: 		   blockPageHttpError,
        dataType: 	   'json'
	}

    $('#mail_form').submit(
    	function () {
    		$(this).ajaxSubmit(options);
    		return false;
    	}
    );
});

