function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}



function dp_powiat( x ) {
$("select[name=powiat]").ajaxStart(function(){$('select[name=powiat]option:first').text('-- wczytywanie --');});
$('select[name=powiat]').find('option').remove().end().append('<option value="">-- wybierz województwo --</option>');
$('select[name=gmina]').find('option').remove().end().append('<option value="">-- wybierz powiat --</option>');
if (x) $.post("/jquery/powiat",{woj:x},function(json){
if (json) {
$('select[name=powiat] option:first').text('-- wybierz --');
for(var i=0;i<json.length;i++){$('select[name=powiat]').append('<option value=' + json[i]['pow'] + '>' + json[i]['nazwa'] + '</option>');}}}, 'json');
return true;
}

function dp_gmina( x ) {
$("select[name=gmina]").ajaxStart(function(){$('select[name=gmina] option:first').text('-- wczytywanie --');});
$('select[name=gmina]').find('option').remove().end().append('<option value="">-- wybierz powiat --</option>');
if (x) $.post("/jquery/gmina",{woj:$('select[name=wojewodztwo]').val(),pow:x},function(json){
if (json) {
$('select[name=gmina] option:first').text('-- wybierz --');
for(var i=0;i<json.length;i++){$('select[name=gmina]').append('<option value=' + json[i]['gmi'] + '>' + json[i]['nazwa'] + '</option>');}}}, 'json');
}

function dp_autocomplete( x, y, z ) {
$('select[name=powiat]').find('option').remove().end().append('<option value="">-- wybierz województwo --</option>');
$('select[name=gmina]').find('option').remove().end().append('<option value="">-- wybierz powiat --</option>');
if (x){ $('select[name=wojewodztwo] option[value="'+x+'"]').attr("selected","selected"); dp_powiat(x); }
if (y){ setTimeout('$(\'select[name=powiat] option[value="'+y+'"]\').attr("selected","selected");', 300); dp_gmina(y);}
if (z){ setTimeout('$(\'select[name=gmina] option[value="'+z+'"]\').attr("selected","selected");', 300); }

}


function format(row) {
	return '<span>' + row.nazwa + ' - <i>gm. ' + row.gmina + ', pow. ' + row.powiat + ', woj. '+ row.wojewodztwo + '</i></span>';
}

function nf( x ) {

var r=/[^0-9]+/g;

$(x).val($(x).val().replace(r,''));

kwota = number_format($(x).val(), 0, null, ",");

if ( $('p.'+x.name).length == 0)
$(x).after("<p style='padding-left:10px; display:inline;' class="+x.name+"></p>");

if ($(x).val())
$('p.'+x.name).text(" "+kwota+" €");
else
$('p.'+x.name).text('');

}


function szukaj_miejscowosci() {

$("input[class^=js_miejscowosc]").autocomplete('/jquery/miejscowosci', {
	dataType: "json",
	selectFirst: false,
	width: 418,
	delay: 400,
	parse: function(data) {
		return $.map(data, function(row) {
			return {
				data: row,
				value: row.nazwa,
				result: row.nazwa
			}
		});
	},
	formatItem: function(item) {
		return format(item);
	}
})

}

function form_kraj() {

if ($('select[class=js_panstwo]').val() != '1') {
$('select[class=js_wojewodztwo]').attr('disabled', true);
//$('select[class=js_wojewodztwo]').parent().parent().hide();	$('input[class^=js_miejscowosc]').parent().parent().hide();
}

if ($('select[class=js_wojewodztwo]').val() == '999')
$('input[class^=js_miejscowosc]').attr('disabled', true);

$('select[class=js_panstwo]').change( function (){
if ($(this).val() == '1') {
$('select[class=js_wojewodztwo]').attr('disabled', false);
//$('select[class=js_wojewodztwo]').parent().parent().find('b').html('<span class="req">*</span>'+$('select[class=js_wojewodztwo]').parent().parent().find('td[class=dl]').text());
//$('input[class^=js_miejscowosc]').parent().parent().find('b').html('<span class="req">*</span>'+$('input[class^=js_miejscowosc]').parent().parent().find('td[class=dl]').text());
//$('select[class=js_wojewodztwo]').parent().parent().show();	$('input[class^=js_miejscowosc]').parent().parent().show();
} else {
$('select[class=js_wojewodztwo]').attr('disabled', true);
$('select[class=js_wojewodztwo]').attr('value', '');	$('input[class^=js_miejscowosc]').attr('value', '');
//$('select[class=js_wojewodztwo]').parent().parent().find('b').html($('select[class=js_wojewodztwo]').parent().parent().find('td[class=dl]').html().replace('<span class="req">*</span>',''));
//$('input[class^=js_miejscowosc]').parent().parent().find('b').html($('input[class^=js_miejscowosc]').parent().parent().find('td[class=dl]').html().replace('<span class="req">*</span>',''));
//$('select[class=js_wojewodztwo]').parent().parent().hide();		$('input[class^=js_miejscowosc]').parent().parent().hide();
}
});

$('select[class=js_wojewodztwo]').change( function (){
if ($(this).val() == '999') {
$('input[class^=js_miejscowosc]').attr('disabled', true);
//$('input[class^=js_miejscowosc]').parent().parent().find('b').html($('input[class^=js_miejscowosc]').parent().parent().find('td[class=dl]').html().replace('<span class="req">*</span>',''));
}
else {
$('input[class^=js_miejscowosc]').attr('disabled', false);
//$('input[class^=js_miejscowosc]').parent().parent().find('b').html('<span class="req">*</span>'+$('input[class^=js_miejscowosc]').parent().parent().find('td[class=dl]').html().replace('<span class="req">*</span>',''));
}

});

}

function highlight_active_links() {

  $("a").each(function(){
    var current_href = 'http://89.75.217.211:1007'+$(this).attr("href");

    if (document.location == current_href) {

      $(this).blur();
      $(this).addClass("active");
    }
  });
}

$(function() {
// datepicker

	$('.box_logowanie input[type="text"], .box_logowanie input[type="password"]').each(function(){
	if (this.name=='email')
	  if(this.value==''){this.value='Email...'}
	if (this.name=='password')
	  if(this.value==''){this.value='Hasło...'}
	});
	
	$('.box_logowanie input[type="text"], .box_logowanie input[type="password"]').focus(function(){
	  if(this.value=='Email...' || this.value=='Hasło...'){ this.value='' }
	});
   
	$('.box_logowanie input[type="text"], .box_logowanie input[type="password"]').blur(function(){
	if (this.name=='email')
	  if(this.value==''){this.value='Email...'}
	if (this.name=='password')
	  if(this.value==''){this.value='Hasło...'}
	});

$('.box_logowanie .link_pracodawca').addClass('active');

$('.link_kandydat, .link_pracodawca').click(function() {

if ($(this).attr('class') == 'link_kandydat lb') {
var w1 = /azienda/;
var w2 = 'candidato';
} 

if ($(this).attr('class') == 'link_pracodawca lb') {
var w1 = /candidato/;
var w2 = 'azienda';
}

$('.lb').removeClass('active');
$(this).addClass('active');


$('.box_logowanie a').each(function(){ this.href = this.href.replace( w1, w2 ); });
$('.box_logowanie form').each(function(){ this.action = this.action.replace( w1, w2 ); });

});


$('textarea.resizable:not(.processed)').TextAreaResizer();

$('.page_content_left .najnowsze_oferty .nb2').hide();

$('.page_content_left .najnowsze_oferty .head a').click(function() {

$('.page_content_left .najnowsze_oferty .head a').removeClass('active');
$(this).addClass('active');

box = '.' + $(this).attr('alt');

$('.page_content_left .najnowsze_oferty .content').hide();
$('.page_content_left .najnowsze_oferty '+ box).show();


})


$.datepicker.setDefaults({yearRange: '-40:0',
changeMonth: true,
changeYear: true,
showOn: 'both',
buttonText: ' Scegli ',
buttonImage: 'http://www.praca4u.pl/gfx/silk_png/calendar.png',
buttonImageOnly: true,
dateFormat: 'dd-mm-yy',
//appendText: ' Format daty (dd-mm-yyyy)',
regional: 'pl'
});

$("input[name=data_urodzenia]").datepicker({dateFormat: 'yy-mm-dd'});
$("input[name=data_rozpoczecia_nauki]").datepicker({dateFormat: 'yy-mm-dd'});
$("input[name=data_zakonczenia_nauki]").datepicker({dateFormat: 'yy-mm-dd'});
$("input[name=data_rozpoczecia_pracy]").datepicker({dateFormat: 'yy-mm-dd'});
$("input[name=data_zakonczenia_pracy]").datepicker({dateFormat: 'yy-mm-dd'});
$("input[name=data_ogloszenia_od]").datepicker({minDate: 0, dateFormat: 'yy-mm-dd'});
$("input[name=data_ogloszenia_do]").datepicker({minDate: 0, dateFormat: 'yy-mm-dd'});

// autocomplete
if ($("#miejscowosc").length)
$("#miejscowosc").autocomplete('/jquery/miejscowosci', {
	dataType: "json",
	selectFirst: false,
	width: 418,
	delay: 400,
	parse: function(data) {
		return $.map(data, function(row) {
			return {
				data: row,
				value: row.nazwa,
				result: row.nazwa
			}
		});
	},
	formatItem: function(item) {
		return format(item);
	}
}).result(function(e, row) {

dp_autocomplete(''+row.id_wojewodztwo+'',''+row.id_powiat+'',''+row.id_gmina+'');

});

form_kraj();
szukaj_miejscowosci();
highlight_active_links();

$('#captchaImg').click(function() {
$(this).attr('src', 'captcha.html?t='+ Math.random( ));
});

$('.rekrutuja marquee').marquee('pointer').mouseover(function () {
	$(this).trigger('stop');
}).mouseout(function () {
	$(this).trigger('start');
}).mousemove(function (event) {
	if ($(this).data('drag') == true) {
		this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
	}
}).mousedown(function (event) {
	$(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
}).mouseup(function () {
	$(this).data('drag', false);
});

$('#checkAll').click(function() {

$('input[type=checkbox]').not(':checked').attr('checked', true);
$('input[type=checkbox]').is(':checked').attr('checked', false);

});

});

function wybierz_pakiet(e) {
$('.po').hide();
$('.p'+e).show();
}