var values	=	new Array();

jQuery(function( $ ) {
	
	$('#relay').val('checked js');
		
			
	$('.form-title-switch').focus(function (e) {
		if (this.value && !values[this.id]) {
			values[this.id] = this.value;
			this.value = '';
		}		
	});
	
	$(".form-title-switch").blur(function (e) {
		if (!this.value) {
			this.value		=	values[this.id];
			values[this.id]	=	'';
		}
	});
	
	
	$("input.check-fields").click(function (e) {
		
		var returnValue	=	true;
		
		var fields	=	$(".required-field");
		
		$('li.li-input').removeClass('error');
		
		for (i = 0; i < fields.length; ++i) {
			field	=	fields[i];
			if (!values[field.id]) {
				if (returnValue) {
					$('#li-' + field.id).addClass('error');
					alert("Bitte alle benötigten Felder ausfüllen!");
					returnValue = false;
				}
			}
 		}
		
 
		return returnValue;
	});
	
	
	$(".form-select-field").change(function (e) {
		
		var option	=	this.options[this.selectedIndex];
		
		if (option.value == "freeoption") {
			id	=	'freeoption-' + option.id.split('-')[1];
			
			el	=	document.getElementById('freeoption-15');
			el.value	=	'';
			el.focus();
		}
		
	});
	
	$('.input-captcha').remove();
	
	
	
	/**
	 * "Gefällt mir" 
	 */
	var pleasingsval	=	null;
	
	$('input.pleasers-name').click(function (e) {
		
		if (!pleasingsval || pleasingsval == this.value) {
			pleasingsval = this.value;
			this.value = '';
		}
	});
	
		
	$('input.pleasers-name').blur(function (e) {
		if (this.value == '') {
			this.value = pleasingsval;
		}
	});
	
	
	$('a.pelasing-button').click(function (e) {
		
		var pleasersName	=	$('#pleasers-name').val();
		
		if (pleasersName == 'Dein Name ...') {
			pleasersName	=	'';
			$('#pleasers-name').val('');
		}
		
		var id	=	this.id.split('-')[1];
		var url	=	serverpath + '/' + alias + '?action[' + sockets_ID + ']=ajaxPleaseingSet&articles_ID=' + id;
		
		$.ajax({
			type:		"GET",
			dataType:	'json',
			url:	url,
			data: {			
				name:	pleasersName
			},
			success: function (msg) {
								
				if (msg.status == 'OK') {
					
					if ($('#please-others-count').length == 0 || $('#please-others-count').html().length == 0) {
						var pleasersCount	=	0;
					}
					else {
						var pleasersCount	=	parseInt($('#please-others-count').html());
					}
					
										
					$('#please-this').remove();
					
					$('#noPleaser').remove();
					
					if (pleasersName) {
						
						if (pleasersCount == 0 && $('#please-names').html().length == 0) {							
							$('#please-names').html(pleasersName);
						}
						else {
							if ($('#please-names').html().length > 0) {								
								$('#please-names').html($('#please-names').html() + ', ' + pleasersName);
							}
							else {								
								$('#please-names').html(pleasersName);								
							}
						}
					}	
					else {
						
						if (pleasersCount == 0) {
							
							if ($('#please-names').html().length > 0) {								
								$('#please-others').html('und <span id="please-others-count">1</span> weiteren');
							}
							else {
								$('#please-others').html('<span id="please-others-count">1</span> weiteren');
							}
						}
						else {
						
							$('#please-others-count').html(pleasersCount + 1);
						}
					}				
				}	
				else {
					alert(msg);
				}	
			}
		});
		
		return false;
	});
	
	
	
	
});
