jQuery(document).ready(function(){
	
	/* TELL A FRIEND */
	
	jQuery("#tellafriend_name").focus(function () {	if (jQuery(this).attr('value')=='Dein Name') { 	jQuery(this).attr('value',''); } });
	jQuery("#tellafriend_name").blur(function () {	if (jQuery(this).attr('value')=='') { 	jQuery(this).attr('value','Dein Name'); } });
	jQuery("#tellafriend_name_empf_1").focus(function () { if (jQuery(this).attr('value')=='Empfaenger-Name') { 	jQuery(this).attr('value',''); } });
	jQuery("#tellafriend_name_empf_1").blur(function () {	if (jQuery(this).attr('value')=='') { 	jQuery(this).attr('value','Empfaenger-Name'); } });
	jQuery("#tellafriend_email_empf_1").focus(function () { if (jQuery(this).attr('value')=='Empfaenger-Email') { 	jQuery(this).attr('value',''); } });
	jQuery("#tellafriend_email_empf_1").blur(function () {	if (jQuery(this).attr('value')=='') { 	jQuery(this).attr('value','Empfaenger-Email'); } });	
	jQuery("#tellafriend_nachricht").focus(function () { if (jQuery(this).text()=='Nachricht') { 	jQuery(this).text(''); } });	
	jQuery("#tellafriend_nachricht").blur(function () { if (jQuery(this).text()=='') { 	jQuery(this).text('Nachricht'); } });	
	
	var options_taf = { 
        target:        '#tellafriendOutput',   
		url:       	'assets/snippets/ezhTellafriend/taf.php'
    }; 

    jQuery('#tellafriendForm').ajaxForm(options_taf); 
	
	var tellafriend=2;
	
	jQuery("#tellafriend_add").click(function () { 
		jQuery('#tellafriend_extra').append('<div class="tellafriend_divider"></div><span class="small">Empf&auml;nger-Email '+tellafriend+'</span><br/><input class="tellafriend_input" type="text" id="tellafriend_email_'+tellafriend+'" name="tellafriend_email_'+tellafriend+'" />');
		tellafriend++;	
   });
	
	jQuery('#name').keyup(function(event){ajax_check(this,jQuery('#validateName'));});
	jQuery('#vorname').keyup(function(event){ajax_check(this,jQuery('#validateVorname'));});
	jQuery('#email').keyup(function(event){ajax_check(this,jQuery('#validateEmail'));});
	jQuery('#ort').keyup(function(event){ajax_check(this,jQuery('#validateOrt'));});
	
	jQuery("#name").triggerHandler("keyup");
	jQuery("#vorname").triggerHandler("keyup");
	jQuery("#email").triggerHandler("keyup");
	jQuery("#ort").triggerHandler("keyup");	
	
	 var options = { 
        target:        '#petitionFormDivOutput',   
		beforeSubmit:  beforeSubmit, 
        success:       afterSubmit,
		url:       	'assets/snippets/ezhPetition/save.php'
    }; 
    jQuery('#petitionForm').ajaxForm(options); 
	

	
	var show=0;
	
	if (maxshow<100) {
			jQuery("#nav_next").hide();
	}

	jQuery(".nav_prev").hide();

jQuery(".nav_prev").click(function () { 
		if (show!=0) {
			jQuery("#petitionOutputDiv").load("assets/snippets/ezhPetition/show.php",{ 'show': show-100 });
			show-=100;
			if ((show+100)<maxshow) {
				jQuery(".nav_next").show();	
			}
			if (show==0) {
				jQuery(".nav_prev").hide();	
			}
		}
    });

	jQuery(".nav_next").click(function () { 
		jQuery("#petitionOutputDiv").load("assets/snippets/ezhPetition/show.php",{ 'show': show+100 });
		show+=100;
		if ((show+100)>maxshow) {
			jQuery(".nav_next").hide();	
		}
		;
		jQuery(".nav_prev").show();
    });
	
	jQuery("#info_tellafriend").mouseover(function(){jQuery("#info_tellafriend_div").fadeIn(500);   });
	jQuery("#info_tellafriend").mouseout(function(){ jQuery("#info_tellafriend_div").fadeOut(500);   });	
	
	jQuery("#info_sms").mouseover(function(){jQuery("#info_sms_div").fadeIn(500);   });
	jQuery("#info_sms").mouseout(function(){ jQuery("#info_sms_div").fadeOut(500);   });		
	
});


function beforeSubmit(formData, jqForm, options) { 
	jQuery("#register").attr("disabled", true);
	jQuery('#petitionFormDivOutput').fadeOut(500);
} 

function afterSubmit(responseText, statusText)  {
	if(responseText=='true') {
		jQuery('#petitionFormDivOutput').html('<div class="success"><div class="info_icon"><img width="16" height="16" class="icon" src="assets/snippets/ezhPetition/img/form_ok.png" alt="ok" /></div>Vielen Dank - du erh&auml;ltst in K&uuml;rze eine Best&auml;tigungsemail zugestellt. Deine Unterschrift wird nur ber&uuml;cksichtigt, wenn du dieses Best&auml;tigungsemail quittierst.<br/><br/>Alle Infos und News rund um die Petition "Energy4ever" findest du <a href="http://www.energy4ever.ch/news">hier</a>.</div>');
		jQuery('#petitionFormDiv').fadeOut(500);
		jQuery("#petitionOutputDiv").load("assets/snippets/ezhPetition/show.php");
	} else {
		jQuery("#register").attr("disabled", false);			
		jQuery('#petitionFormDivOutput').html('<div class="fail"><div class="info_icon"><img width="16" height="16" class="icon" src="assets/snippets/ezhPetition/img/form_fail.png" alt="ok" /></div>'+responseText+'</div>');		
	}
	jQuery('#petitionFormDivOutput').fadeIn(500);
} 

function ajax_check(t, validateResponse) {
	if (t.value != t.lastValue) {
 		if (t.timer) clearTimeout(t.timer);
		validateResponse.html('<img class="icon" src="assets/snippets/ezhPetition/img/ajax-loader.gif" height="16" width="16" />');
		  
		t.timer = setTimeout(function () {
		jQuery.ajax({
			  url: 'assets/snippets/ezhPetition/ajax-validation.php',
			  data: 'item=' + t.name + '&value=' + t.value,
			  dataType: 'json',
			  type: 'post',
			  success: function (j) {
				validateResponse.html(j.msg);
			  }
			});
		  }, 200);
		  
		t.lastValue = t.value;
	}
}
