Array.prototype.max = function(){
	return Math.max.apply({},this)
}
Array.prototype.min = function(){
	return Math.min.apply({},this)
}

var counter = new Array();
counter['edukacja']  = 1;
counter['szkolenia'] = 1;
counter['praca']     = 1;

$(document).ready( function() {
	$('#edukacja_dodaj').click(function() {
		add_block('edukacja');
		return false;
	})
	usun_click_listener('edukacja');

	$('#szkolenia_dodaj').click(function() {
		add_block('szkolenia');
		return false;
	})
	usun_click_listener('szkolenia');

	$('#praca_dodaj').click(function() {
		add_block('praca');
		return false;
	})
	usun_click_listener('praca');
	
/*	$('#szkolenie_spawanie_dodaj').click(function() {
		var id = '#szkolenie_spawanie_'+$('#szkolenie_spawanie_select').val();
		$(id+' input').each(function() {
			this.disabled = null;
		})
		$(id).show();
	})*/
	$('#szkolenie_spawanie_select').change(function() {
		var id = '#szkolenie_spawanie_'+$(this).val();
		$(id+' input').each(function() {
			this.disabled = null;
		})
		$(id).show();
		$(this).val('');
	})
	
	$('a.szkolenie_spawanie_usun').each(function() {
		$(this).click(function() {
			var idFormat = 'szkolenie_spawanie_usun_';
			var id = this.id.substr(idFormat.length, this.id.length-idFormat.length)
			var container_id = 'szkolenie_spawanie_'+id;
			$('#'+container_id+' input').each(function() {
				this.disabled = 'disabled';
			});
			$('#'+container_id).hide();
			return false;
		})
	});
	
	$('#stanowisko').change(function() {
		if ($(this).val() == 'Spawacz') {
			$('#szkolenia_spawanie').show();
		} else {
			$('#szkolenia_spawanie').hide();
		}
	})
	
	$('input[name=ostatnie_stanowisko]').change(function() {
		if ($(this).val() == 'Brak doświadczenia zawodowego') {
			$('#praca').hide();
		}
		else {
			$('#praca').show();
		}
	})
	
	$('#stanowisko').change(function() {
		if ($(this).val() != '') {
			$('#p_dzial').hide();
			$('#dzial').attr('disabled', 'disabled');
		} else {
			$('#p_dzial').show();
			$('#dzial').attr('disabled', '');
		}
	})
	
	$('#dzial').change(function() {
		if ($(this).val() != '') {
			$('#p_stanowisko').hide();
			$('#stanowisko').attr('disabled', 'disabled');
		} else {
			$('#p_stanowisko').show();
			$('#stanowisko').attr('disabled', '');
		}
	})
	
	$('#cv_usun').click(function() {
		$('#cv_uploaded').hide();
		$('#cv_input_file').show();
		$('#cv_input_file').attr('disabled', '');
		return false;
	})
	
	$('#list_motywacyjny_usun').click(function() {
		$('#list_motywacyjny_uploaded').hide();
		$('#list_motywacyjny_input_file').show();
		$('#list_motywacyjny_input_file').attr('disabled', '');
		return false;
	})
})


var usun_click_listener = function(name) {
	$('.'+name+'_usun').click(function() {
		remove_block(this, name);
		return false;
	})
}

var add_block = function(name) {
	var children = $('#'+name).children();
	if (children.length > 1 && children[1].id.indexOf(name+'_hr_') !== 0)
	{
		$('#'+name).append('<hr id="'+name+'_hr_'+counter[name]+'" />');
	}
	var table = document.createElement('table');
	$(table).attr('class', 'laytable');
	$(table).attr('id', name+'_'+counter[name]);
//	var html = $('#'+name+'_table_template').html().replace(/disabled="disabled"/g, '');
	var html = $('#'+name+'_table_template').html();
	html = html.replace(/#index#/g, counter[name]);
	$(table).html(html);
	counter[name]++;
	$('#'+name).append($(table));
	
	$('#'+$(table).attr('id')+' input').each(function() {
		$(this).attr('disabled', '');
	})
	$('#'+$(table).attr('id')+' textarea').each(function() {
		$(this).attr('disabled', '');
	})
	//alert($(table).attr('id'));
	
	usun_click_listener(name);
}

var remove_block = function(item, name) {
	var idFormat = name+'_usun_';
	var id = Number(item.id.substr(idFormat.length, item.id.length-idFormat.length));
	$('#'+name+'_hr_'+id).remove();
	$('#'+name+'_'+id).remove();
	
	var children = $('#'+name).children();
	if (children.length > 1 && children[1].id.indexOf(name+'_hr_') === 0)
	{
		$(children[1]).remove();
	}
}

