$(document).ready(function(){

	/*  WIDGET article  */

	$('#selectAll').click(function(){
		if( $('#selectAll:checked').val() !== undefined ){
			$('.ac_option').attr('selected','selected');
		}else{
			$('.ac_option').removeAttr('selected');
		}
	});

	$('.ac_option').click(function(){
		$('#selectAll').removeAttr('checked');
	});



	/* BEGIN - UŻYTKOWNIK - wybór branży/formularz rejestracji/profil użytkownika */

	$('#fake_select, #select-branch-content div.accept').click( function(){
		$('#select-branch-content').toggle();
		if( $('#select-branch-content').css('display') == 'none' ){
			addWorkQuestion();
		}
	});


	$('#select-branch-content input').click( function(){
		var i = 0;
		branch = $(this).attr('id');
		$("#select-branch-content input:checked").each( function(){
			if( i > 2 ){$('#select-branch-content input#'+branch ).removeAttr("checked");}
			else{i++;}
		});

		if( $('#select-branch-content input#'+branch ).attr('checked') == true  ){
			$('#select-branch-content input#'+branch ).parent().children('label').addClass('selected');
		}else{
			$('#select-branch-content input#'+branch ).parent().children('label').removeClass('selected');
		}

		if( i == 0 ){$('#fake_select').html('wybierz branże');}
		else{$('#fake_select').html('wybranych: '+i);}

	});


	$('#u_licence_zarz_check').change( function(){
		if( !$(this).is(':checked') ){$('input#u_licence_zarz').attr('disabled', 'disabled');}
		else{$('input#u_licence_zarz').removeAttr('disabled');}
	});

	$('#u_licence_pos_check').change( function(){
		if( !$(this).is(':checked') ){$('input#u_licence_pos').attr('disabled', 'disabled');}
		else{$('input#u_licence_pos').removeAttr('disabled');}
	});

	$('#u_licence_rzecz_check').change( function(){
		if( !$(this).is(':checked') ){$('input#u_licence_rzecz').attr('disabled', 'disabled');}
		else{$('input#u_licence_rzecz').removeAttr('disabled');}
	});

	// rodzaj zarządzanej nieruchomosci - inne
	$('#cover_total_check').change( function(){
		if( !$(this).is(':checked') ){ 
			$('input#cover_total').attr('disabled', 'disabled');
			$('input#cover_company_name').attr('disabled', 'disabled');
			$('input#cover_end').attr('disabled', 'disabled');
		}else{
			$('input#cover_total').removeAttr('disabled');
			$('input#cover_company_name').removeAttr('disabled');
			$('input#cover_end').removeAttr('disabled');
		}
	});

	// użytkownik posiada ubezpieczenie OC
	$('#property-other-check').change( function(){
		if( !$(this).is(':checked') ){$('input#property_other').attr('disabled', 'disabled');}
		else{$('input#property_other').removeAttr('disabled');}
	});

	// użytkownik wybrał branże, możliwośc wprowadzaenia powierzchni
	$('.checkbox_userEditProperty').change( function(){
		id_userHasProperty = $( this ).attr('id_userHasProperty');
		if( !$(this).is(':checked') ){$( 'input#area_userEditProperty_'+id_userHasProperty ).attr('disabled', 'disabled');}
		else{$('input#area_userEditProperty_'+id_userHasProperty ).removeAttr('disabled');}
	});

	function addWorkQuestion(){
		var display = 'none';
		var work_place = '';

		$("#select-branch-content input:checked").each( function( i, k){

			branch = $(this).attr('id_user_branch');
			title = $(this).parent().children('label').html();

			if( $('input').is('#branch-'+branch+'-work_place') == true ){
				work_place_value = $('input#branch-'+branch+'-work_place').val();
			}else{
				work_place_value = '';
			}

			var work = '<div class="row"><div style="float:left;width: 220px;" class="label"><img src="/_images/content/ikona_kwadrat.gif" class="kwadrat_light_green"> '+title+': </div><div class="field"><input id="branch-'+branch+'-work_place" type="text" name="branch['+branch+'][work_place]" value="'+work_place_value+'" class="long"></div></div><div class="clear"></div>';
			work_place = work_place + work;
			display = 'block';
		});

		$('#work-place').children('div#work-branch').html('');
		$('#work-branch').append( $( work_place ) );
		$('#work-place').css('display', display );
	}

	$('#is_still_working, #is_still').change( function(){
		if( $(this).is(':checked') ){
			$('select#end_date-month').attr('disabled', 'disabled');
			$('select#end_date-year').attr('disabled', 'disabled');
		}
		else{
			$('select#end_date-month').removeAttr('disabled');
			$('select#end_date-year').removeAttr('disabled');
		}
	});

	/* END - UŻYTKOWNIK - wybór branży/formularz rejestracji/profil użytkownika */


	$.datepicker.regional['pl'] = {
		closeText: 'Zamknij',
		prevText: '&#x3c;Poprzedni',
		nextText: 'Następny&#x3e;',
		currentText: 'Dziś',
		monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec',	'Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
		monthNamesShort: ['Sty','Lut','Mar','Kwie','Maj','Cze','Lip','Sie','Wrz','Paź','Lis','Gru'],
		dayNames: ['Niedziela','Poniedzialek','Wtorek','Środa','Czwartek','Piątek','Sobota'],
		dayNamesShort: ['Nie','Pn','Wt','Śr','Czw','Pt','So'],
		dayNamesMin: ['N','Pn','Wt','Śr','Cz','Pt','So'],
		dateFormat: 'yy-mm-dd', firstDay: 1, isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['pl']);

	$(".datepicker").datepicker({
		changeMonth: true, changeYear: true,
		yearRange: "-100:+100", maxDate: '0D'
	});

	$(".datepicker_future").datepicker({
		changeMonth: true, changeYear: true,
		yearRange: "-10:+10", maxDate: '+10Y'
		});


});
