jQuery(document).ready(function() {
	bindBlockToggle();
	bindSearchToggle();
	bindDatepicker();
	bindTooltip();
	bindValidator();
	bindPrintbutton();
	
	if(jQuery('.trainingright .block:eq(0) h3')) {
		jQuery('.trainingright .block:eq(0) h3').click();
	}
	
	var wiki = jQuery('.glossary .wikiTitle').html();
	jQuery('.glossary .wikiTitle').each(function() {
		var elm = jQuery(this);
		jQuery.get('/typo3conf/ext/apprenticeships/Resources/Public/php/proxy.php',{artikel:elm.text()}, function(data) {
			var data = jQuery('#bodyContent', data).find('p:first');
			data.find('a').each(function() { 
			jQuery(this).replaceWith(jQuery(this).text());
			});
			data.find('.reference').each(function() {
				jQuery(this).remove();
			});	
			
			data.html(data.text().substring(0,200) + "...")
	
			elm.after(data);
		});	
	});

	
	jQuery('.glossary a').each(function () {
		jQuery(this).attr('href', function() {
			return 'http://de.wikipedia.org/' + jQuery(this).attr('href');
		});
	});
	
});


function bindBlockToggle() {
	jQuery('.trainingright .block h3.closed').click(function() {
		jQuery(this).toggleClass('closed').next('.description').slideToggle();
	});
}

function bindSearchToggle() {
	jQuery('.toggleTrainingSearch').click(function() {
		jQuery('#trainingSearchForm').slideToggle();
	});
}

function bindDatepicker() {
	$("#trainingSearchForm .datePicker").datepicker({dateFormat: 'dd.mm.yy'});
}

function bindTooltip(){
	$(".apprenticeShipsShow .tooltip").tooltip({showBody: " - "});
}

function bindValidator(){
	$("#apprenticeShipsRegistrationForm").validate({
		rules: {
		    "apprenticeShipsRegistration[nr]":{required:true, houseNr:true},
			"apprenticeShipsRegistration[billingaddress][nr]":{houseNr:true},
			"apprenticeShipsRegistration[phone]":{required:true, phone:true},
			"apprenticeShipsRegistration[billingaddress][phone]":{phone:true},
			"apprenticeShipsRegistration[billingaddress][fax]":{phone:true},
			"apprenticeShipsRegistration[plz]":{required:true, plz:true},
			"apprenticeShipsRegistration[billingaddress][plz]":{plz:true}
  		},
		errorLabelContainer: '#errormsg',
   		wrapper: 'p',
		ignore:'.ignore'
		
		
	});
}
function bindPrintbutton(){
	$('#printreg').click(function(){
		window.print();
		return false;
	});
}

/*
 * additional validator methods*/
(function() {
 	jQuery.validator.addMethod("houseNr", function(value, element) { 
	    return this.optional(element) || value.match(/^[0-9a-zA-Z \-]{1,5}$/g)?true:false; 
	}); 
        
	jQuery.validator.addMethod("nr", function(value, element) { 
	    return this.optional(element) || value.match(/^\d+\s*\w{0,3}$/g)?true:false; 
	}); 
	 
	jQuery.validator.addMethod("phone", function(value, element) { 
	    return this.optional(element) || value.match(/^[0-9\-\/\+\(\) ]+$/g)?true:false; 
	}); 
	 
	
	jQuery.validator.addMethod("plz", function(value, element) { 
	    return this.optional(element) || value.match(/^\d{5}$/g); 
	});//, jQuery.validator.format("Please enter between {0} and {1} words."));
	

})();

