
$(document).ready(function() {
		// Tabs
		$('#tabs').tabs({});
		
		//po loadnuti vynulujeme miesto bydliska
		$("#okres, #mesto").empty().attr("disabled", "disabled");
		$("#kraj").val("");
		
		// ajax pre zadavanie bydliska
		/*$("#kraj, #okres, #mesto").live("change", function (e) {
			get_option($(this).attr("id"), $(this).val());	
		});*/
		
		$('tr input:text, tr select, tr textarea').focus(function(){
				$(this).parents('tr').addClass("sel_tr");
			}).blur(function(){
				$(this).parents('tr').removeClass("sel_tr");
			});
		
		//zistime si aktualny rok na vypocty
		var year = new Date().getFullYear();
		// datepicker
		$("input#birth_date").datepicker({
			//nastavime vyber datumu narodenia od 15 do 70r.
			yearRange: String(parseInt(year)-100) + ':' + String(parseInt(year)-0), 
			maxDate: '-0y',
			//minDate: '-70y',
			changeYear: true, 
			changeMonth: true,
			dateFormat: 'd. MM yy',
			altField: '#datum_narodenia',
			altFormat: 'dd.mm.yy',
			onSelect: function(dateText, inst) {
				
			},
			//synchronizujeme polia s datumom narodenia
			onClose: function(dateText, inst) {
				//ak je hodnota datumu zmazana, vynulujeme alternativny input
				if($(this).val() == "") $(this).datepicker( 'setDate' , '');
			}
 		
		});
		
		// nastup mozny od -  zvolime minimalny datum na dnesny den
		$("input#nastup_od").datepicker({minDate: 0});
		
		// pouzivame live aby mali klonovane inputy tiez kalendar
		$("input.datepicker").live("click", function(){
			$(this).datepicker({
				showOn:'focus', 
				yearRange: String(parseInt(year)-50) + ':' + String(parseInt(year)), 
				changeYear: true, 
				changeMonth: true})
				.focus();
		});
		
	// riesime klonovanie riadkov	
	$(".add").click(function(e) {
		e.preventDefault();
		typ = $(this).attr("id");
		idx = parseInt($("#" + typ + "_idx").val());
		idx = idx + 1;
		var x = $("." + typ + "_0").clone();
		
		$(x).attr("class", typ + "_" + idx)
			.find("label").each(function() {
				$(this).attr("for", $(this).attr("for").replace("_0", "_" + idx));
			})
			.end()
			.find("input, select").each(function() {
				/*if($(this).hasClass("datepicker")){
					$(this).removeClass($.datepicker.markerClassName); 
					$(this).removeClass('hasDatepicker').datepicker({
						showOn: 'focus',
						showButtonPanel: true
						}); 
					}*/
				$(this)
					.attr("id", $(this).attr("id").replace("_0", "_" + idx))
					.attr("name", $(this).attr("name").replace("[0]", "[" + idx + "]"))
					.val("");
				
			})
			.end()
			// treba odstranit classu 'hasDatepicker' aby sa znovuinicializoval datepicker
			.find('.' + $.datepicker.markerClassName).removeClass($.datepicker.markerClassName);
		
		
		$("#" + typ + "_idx").val(idx);
		idx = idx -1;
		$("." + typ + "_" + idx + ":last").after(x);
		jQuery("."+typ + "_" + parseInt(idx+1) + " label:first").before('<strong class="red counter">('+ parseInt(idx+2) +')</strong>&nbsp;&nbsp;&nbsp;&nbsp; ');
		
		$(".remove[id=" + typ + "]").show();
	});
	
	//mazanie riadkov
	$(".remove").click(function(e) {
		e.preventDefault();
		typ = $(this).attr("id");
		idx = parseInt($("#" + typ + "_idx").val());
		if(idx > 0){
			$("." + typ + "_" + idx).remove();
			if(idx == 1)
				$(this).hide();
		}
		$("#" + typ + "_idx").val(idx - 1);
	});
	
	
	
	// "odkial ste sa o nas dozvedeli" - v pripade ze je selectnute "ine" tak ukazeme input[text] a vymenime attr(name)
	
	jQuery("#zdroj").change(function(){
		if(jQuery("#zdroj").val() == 'iné'){
			jQuery("#zdroj").attr("name", "zdroj_ine");
			jQuery("#zdroj_ine").attr("name", "zdroj")
				.parent().slideDown(400, function(){
					jQuery("#zdroj_ine").focus()
				});
		}
		else{
			jQuery("#zdroj").attr("name", "zdroj");
			jQuery("#zdroj_ine").attr("name", "zdroj_ine").parent().slideUp();
			
		}
	})
	
		
});

/*
 * funkcia riesiaca dynamicke naplnanie selectov pre vyber bydliska
 */ 
function get_option(typ, select_val){
	// premenna druha bude drzat id toho selectu ktory budeme naplnat
	if(typ == "kraj") 
		druha = "okres";
	else if(typ == "okres")
		druha = "mesto";
	else
		return false;
	
	// ak je selectnuta hodnota nenulova zavolame ajax			
	if(select_val){
		// odovzdame typ - kraj, okres a k tomu konkretnu hodnotu
		$.getJSON(blade_root_path + "ajax/get_okresy_mesta.php", {typ: typ, val: select_val},
			//console.time('create list');
			function(json, status){
				if(status == "success"){
					// vypraznime select pred naplnanim
					$("#" + druha).empty();
					//vyhodnotime akym sposobom budeme nalnat, pretoze iny JSON nam vracaju okresy a iny mesta
					if(typ == "kraj"){
						var html = '<select id="okres" class="select" disabled="disabled" name="okres" onchange="get_option(this.id, this.value)"><option value="">&nbsp;&nbsp;&nbsp;&nbsp;</option>';
						$.each(json,function(i,item) {
							//alert(i + " : " + item);
							html += '<option value="' + i + '">' + item + '</option>';
						});
						html += '</select>';
						$("#" + druha).replaceWith(html);
					}
					else{
						var html = '<select id="mesto" class="select" disabled="disabled" name="mesto" onchange="get_option(this.id, this.value)"><option value="">&nbsp;&nbsp;&nbsp;&nbsp;</option>';
						$.each(json,function(i,item) {
							//alert(i + " : " + item);
							html += '<option value="' + item.psc + '|' + item.mesto + '">' + item.mesto + ' - ' + item.psc +'</option>';
						});
						html += '</select>';
						$("#" + druha).replaceWith(html);
					}
					// aktivujeme naplneny select	
					$("#" + druha + "-holder select").removeAttr("disabled");
				}
			});
			//console.timeEnd('create list');
	}else{
		// ak bola vybrana prazdna hodnota deaktivujeme prislusne selecty
		if(typ == "kraj"){
			$("#" + druha + "-holder select").empty().attr("disabled", "disabled");
			$("#mesto-holder select").empty().attr("disabled", "disabled");
			}
		else
			$("#" + druha + "-holder select").empty().attr("disabled", "disabled");
	}
}