$(document).ready(function() {
	$.cookie('c', 'v');
	$('#ofx > li > a.z').click(function (ev) {
		var zz = $('#ofx > li > a.active').attr("href");
		if (zz){
			var zz = zz.replace('#','');
			$.cookie('c'+zz, 0);
		}
		$('#ofx > li > ul:visible').slideUp('fast');
		$('#ofx > li > a.z').removeClass('active');
	});
	$('#ofx > li > ul > li > a.z').click(function (ev) {
		var zz = $('#ofx > li > ul > li > a.active').attr("href");
		if (zz){
			var zz = zz.replace('#','');
			$.cookie('c'+zz, 0);
		}
		$('#ofx > li > ul > li > ul:visible').slideUp('fast');
		$('#ofx > li > ul > li > a.z').removeClass('active');

	});
	$('#ofx li a.z').click(function (ev) {
		ev.preventDefault();

		var anchor = $(this).attr("href");
		var anchor = anchor.replace('#','');
		if ( $('#ofx').find('.ch_'+anchor).is(":visible") ) {
			$.cookie('c'+anchor, 0);
			$(this).removeClass('active');
			$('#ofx').find('.ch_'+anchor).slideUp('fast');
		}
		else {
			$('#ofx').find('.ch_'+anchor).slideDown('fast');
			$(this).addClass('active');
			$.cookie('c'+anchor, 1);
		}


	});

	$("#rform").validate();
	rform_jezyki();
	$("#rform").change(function() {
		rform_jezyki();
	});
	$('#date').DatePicker({
			format:'Y-m-d',
			date: $('#date').val(),
			current: $('#date').val(),
			starts: 1,
			position: 'right',
			onBeforeShow: function(){
				if ($('#date').val()) $('#date').DatePickerSetDate($('#date').val(), true);
			},
			onChange: function(formated, dates){
				$('#date').val(formated);

					$('#date').DatePickerHide();
			}
		});
	open_acc();


	$('#osoby').change(
		function() {
			$val_sel = $('#osoby').val();
			if ($val_sel < 1) {
				hide_os('.os_1');
				hide_os('.os_2');
				hide_os('.os_3');
				hide_os('.os_4');
				hide_os('.os_5');
			}
			else if ($val_sel == 1) {
				show_os('.os_1');
				hide_os('.os_2');
				hide_os('.os_3');
				hide_os('.os_4');
				hide_os('.os_5');
			}
			else if ($val_sel == 2) {
				show_os('.os_1');
				show_os('.os_2');
				hide_os('.os_3');
				hide_os('.os_4');
				hide_os('.os_5');
			}
			else if ($val_sel == 3) {
				show_os('.os_1');
				show_os('.os_2');
				show_os('.os_3');
				hide_os('.os_4');
				hide_os('.os_5');
			}
			else if ($val_sel == 4) {
				show_os('.os_1');
				show_os('.os_2');
				show_os('.os_3');
				show_os('.os_4');
				hide_os('.os_5');
			}
			else if ($val_sel == 5) {
				show_os('.os_1');
				show_os('.os_2');
				show_os('.os_3');
				show_os('.os_4');
				show_os('.os_5');
			}
	});

	$("#rodzaj").change(function () {
		$("#firma").slideUp('fast'); //hide
		$("#osobyy").slideDown('fast'); //show
		$("#osobyy .requ").removeClass('requ').addClass('required');
		$("#firma .required").removeClass('required').addClass('req');
	});

	$("#rodzajb").change(function () {
		$("#firma").slideDown('fast'); //show
		$("#osobyy").slideUp('fast'); //hide
		$("#osobyy .required").addClass('requ').removeClass('required');
		$("#firma .req").addClass('required').removeClass('req');
	});
	
});

function rform_jezyki() {
	if ($('#ch_en').attr('checked')) {
		$('#sec_en').slideDown();
	}
	else {
		$('#sec_en').slideUp();
	}
	if ($('#ch_de').attr('checked')) {
		$('#sec_de').slideDown();
	}
	else {
		$('#sec_de').slideUp();
	}
	if ($('#ch_es').attr('checked')) {
		$('#sec_es').slideDown();
	}
	else {
		$('#sec_es').slideUp();
	}
}

function show_os($class) {
	if ($($class).not(":visible")) {
		$($class).slideDown('fast');
		$($class+' .req').addClass('required');
	}
}
function hide_os($class) {
	if ($($class).is(":visible")) {
		$($class).slideUp('fast');
		$($class+' .req').removeClass('required');
	}
}

function open_close(anchor) {
	$('#ofx').find('.ch_'+anchor).parent('li').parent('ul').parent('li').parent('ul').parent('li').parent('ul').slideDown('fast').prev().addClass('active');
	$('#ofx').find('.ch_'+anchor).parent('li').parent('ul').parent('li').parent('ul').slideDown('fast').prev().addClass('active');
	$('#ofx').find('.ch_'+anchor).parent('li').parent('ul').slideDown('fast').prev().addClass('active');
	$('#ofx').find('.ch_'+anchor).slideDown('fast').prev().addClass('active');

}

function open_acc() {
	var anchorValue;
	var url = document.location;
	var strippedUrl = url.toString().split("#");
	if(strippedUrl.length > 1)
	anchorValue = strippedUrl[1];
	if (anchorValue>0){
		open_close(anchorValue);
	}
}

function show_form() {
	$("#aplikuj_short").slideUp();
	$("#aplikuj_form").slideDown();
}
