$(document).ready(function() {
	$('.line:first').hide();

	$('.line.sent').each(function() {
		var num = $('.geraeteart select', this).attr('name').match(/order\[(\d+)\]\[Geraet\]/)[1];
		var unit = sent[num];
		$(this).addClass('curcount_'+unit['Anzahl']);
		$('.geraeteart select option[value='+unit['Geraet']+']', this).attr('selected', 'selected');
	});	
	
	
	$('form').each(function() {
		var form = $(this);
		form.jqTransform({styleSelect: true});
		$('.delete-entries', form).click(function() {
			form.reset();
			return false;
		});
	});		
	
	$.fn.newLineHelper();
	
	$('.category_select ul a').click(function() {
		var value= "";
		var li = "";
		var options = "";
		value = $(this).attr('ref');
		var span = categoryArticles[value][0].name;
		for (var i=0;i<categoryArticles[value].length;i++) {
			li += "<li><a style=\"white-space: nowrap;\" href=\"#\" index=\""+i+"\">"+categoryArticles[value][i].name+"</a></li>";
			options += "<option value=\""+categoryArticles[value][i].id+"\">"+categoryArticles[value][i].name+"</option>";
		}
		$('.praeparat_select div span').text(span);
		$('.praeparat_select ul').html(li);
		$('.praeparat').html(options);
		
		$.fn.jqTransformHelper($('.praeparat'));				
	});			
	
	var row = $('.line.main').length;
	$('.link-more').click(function() {		
		var line = $('#extra-line .line').clone();
		$('.line:last').after(line);
		$('.line:last').each(function() {
			$('input, select', this).attr('disabled', false);
			$(this).show();
		});
		$('form.jqtransformdone').removeClass('jqtransformdone');
		$('form').jqTransform({styleSelect: true});
		$('[name]', line).each(function() {
			$(this).attr('name', 'order['+(row)+']['+$(this).attr('name')+']');
		});
		$.fn.newLineHelper();
		$('select.geraet', line).fillCountSelect().change(function() { $(this).fillCountSelect(); });
		row++;
		return false;
	});
	
	$('#content .kontaktFormular form').submit(function() {
		$('.line .medikament input[value=""]', $(this)).closest('.line').remove();
	});
	
	$('#search form input[type="text"]').each(function() {
		$(this).focus(function() {
			if ($(this).val() == 'Suche') $(this).val('');
		});
		
		$(this).blur(function() {
			if ($(this).val() == '') $(this).val('Suche');
		});
	});
	
	$('#loanunit > .line select.geraet').fillCountSelect().change(function() { $(this).fillCountSelect(); });
		
});

$.fn.jqTransformHelper = function(element) {
	var select = $(element);
	var wrapper = select.parent();
	var ul = $('ul', wrapper);
	var ulwrapper = ul.parent().parent(); 
	ul.find('a').click(function(){ 
		$('a.selected', wrapper).removeClass('selected');
		$(this).addClass('selected');	
		/* Fire the onchange event */
		if (select[0].selectedIndex != $(this).attr('index') ) { select[0].selectedIndex = $(this).attr('index'); select.change(); }
		select[0].selectedIndex = $(this).attr('index');
		$('span:eq(0)', wrapper).html($(this).html());
		ulwrapper.hide();
		$('a:first',ul).focus();
		return false;
	});
	ulwrapper.css({'height':$('li', ul).length*35});
};

$.fn.newLineHelper = function() {
	$('.bemerkung, .groesse').each(function() {
		$(this).focus(function() {
			if ($(this).val() == 'k.A.') $(this).val('');
		});
		
		$(this).blur(function() {
			if ($(this).val() == '') $(this).val('k.A.');
		});
	});
};

$.fn.fillCountSelect = function() {
	return $(this).each(function() {
		var sel = $(':selected', this);
		if (sel.length < 1) sel = $('option:first-child', this);
		if (sel.length >= 1) {
			
			// Anzahl der Einträge für das zweite Select extrahieren.
			var count = sel.attr('class').match(/count_(\d+)/)[1];
			
			// Markup für zweites Select zusammenbauen
			var html = '';
			for (var i=1; i<=count;i++) html+= '<option>'+i+'</option>';
			var target = $(this).closest('.geraeteart').next('.geraeteanzahl');
			target.html('<select name="'+$('select', target).attr('name')+'" class="anzahl">'+html+'</select>');
			// Beim ersten mal pro Zeile die Anzahl setzen, falls Formular das letzte mal abgeschickt wurde.
			var line = target.closest('.sent');
			if (line.length > 0) {
				line.removeClass('sent');
				var num = line.attr('class').match(/curcount_(\d+)/)[1];
				$('select option[value='+num+']', target).attr('selected', 'selected');
			}
			
			// jqTransform anwenden.
			target.removeClass('jqtransformdone').jqTransform({ styleSelect: true });
		}
	});
};


