$(document).ready(function(){
    $('#search .show_hide_more').click(function(){
	$('#search .more').toggle();
	$('#search .show_hide_more span').toggle();
	
	if($('#search .more').css('display') == 'none')
	    $('#search form option:selected').attr('selected', false);
	
	
    });
    
    if($('#search form .more option:selected').size()>0)
	$('#search .show_hide_more').click();
    
    $('#search  .submit span').click(function(){
	 $('#search form option:selected').attr('selected', false);
	 $('#search form input[type=text]').val('');
	 $('#search form .wiek select:first option:first').attr('selected', true);
	 $('#search form .wiek select:last option:first').attr('selected', true);
    });
    
    

    
    $('#search .wiek select').each(function(){
	
	$(this).change(function(){
	    checkWiek();
	});

	
    });
    
    $('#search input[type=submit]').click(function(){
	
	fillLabels();
    
    });
    $('.dataBox').each(function(){
	var box = $(this);
	$(this).find('h6 span').click(function(){
	    box.find('.data .element').hide();
	    box.find('.data .'+$(this).attr('rel')).show();
	    box.find('h6 .active').removeClass('active');
	    $(this).addClass('active');
	    
	}).hover(function(){
	    $(this).addClass('hover');
	},function(){
	    $(this).removeClass('hover');
	});;
	
    });
    
    
    
    
    $('.listToCol').each(function(){
	var col = $(this).attr('rel');
	var cols = new Array();
	for(var i=0;i<col;i++){
	    cols[cols.length] = $('<div class="col"></div>');
	    
	}
	var count = $(this).find('div').size();
	var inCol = Math.ceil($(this).find('a').size()/col);
	
	var ii=0;
	for(var i=0;i<count; i++){
	    ii =$(this).find('div').eq(i).find('a').size()+ii;
	    col = Math.ceil(ii/inCol)-1;
	   
	    $(this).find('div').eq(i).clone().appendTo(cols[col]);
	}
	$(this).html('');
	for(var col in cols)
	    $(this).append(cols[col]);
	
	$(this).append('<div class="clear"></div>');
    });
    
    $('#listFiler input[type=text]').focus();
    
    $('#listFilerqa').submit(function(){

	var v = $(this).find('input[type=text]').val();
	
	$('.listToCol div').each(function(){
	    
	   if($(this).find('a:contains("'+v+'")').size()==0){
		$(this).hide();
		return true;
	   }
	   $(this).show();
	   $(this).find('li').hide();
	   $(this).find('li:contains("'+v+'")').show();
	   
	   
	   
	    return true;
	});
	
	
	
	return false;
    });
    
    
    
    $('#kalkulator input.txt').focus();
    $('#kalkulator form').submit(function(){
	var v = $(this).find('.txt').val();
	
	if(parseInt(v) != v){
	    alert('Wprowadź liczbę całkowitą');
	    return false;
	}
	
	document.location = 'kalkulator/'+v+'netto';

	return false;
	
    });
    
    

    
 
   $("#addForm input[name=stanowisko]").autocomplete("search/index/col/job_position", {
		width: 260,
		selectFirst: false
	});
   $("#addForm input[name=miasto]").autocomplete("search/index/col/job_city", {
		width: 260,
		selectFirst: false
	});    
   $("#addForm form").submit(function(){
	var error= new Array();
	$(this).find('input[type=text]').each(function(){
	    if($(this).val().length == 0){
		error.push($(this).parent().parent().find('label').html());
	    }
	});
	$(this).find('select').each(function(){
	    if($(this).val().length == 0){
		error.push($(this).parent().parent().find('label').html());
	    }
	});
	
	if(error.length>0){
	    
	    alert("Wypełnij poprawnie formularz: \n  - "+error.join("\n  - "));
	    return false;
	    
	}
	
   });
   
   
   
   $('#showInfo').click(function(){
    
	$('#info').fadeIn();
    
	return false;
   });
   $('#hideInfo').click(function(){
    
	$('#info').fadeOut();
    
	return false;
   });   
    
    
    $('.addForm').html($('#addForm').html());
    
    $('.resizer').each(function(){
    	var old = $(this).attr('size');
		$(this).attr('size', 10);
		var a = $('<a href="#" class="resize">Rozwiń listę</a>');
		a.insertAfter($(this));
		var s = $(this);
		a.click(function(){
			s.attr('size', old);
			return false;
		});
		
	
	});
    
    
    $('#reportSave').submit(function(){
      if($(this).find('input[type=text]').val().length == 0){
      
        alert("Wpisz nazwę");
        $(this).find('input[type=text]').focus();
        return false;
      } 
    
    });
    
    
});
var checkWiek = function(){
    
    	var from = $('#search .wiek select:first');
	var to = $('#search .wiek select:last');

	
	if(to.find('option:selected').val()<from.find('option:selected').val()){
	    
	    to.find('option:selected').attr('selected', false);
	    to.find('option[value='+from.find('option:selected').val()+']').attr('selected', true);
	}
}
var fillLabels = function(){
   
    $('#search select').each(function(){
	
	    var str = new Array();
	    $(this).find(':selected').each(function(){
	       str.push($(this).html()); 
		
	    });
	  
	    $(this).parent().find('input[type=hidden]').val(str.join(','));
	    
	
	  
    });
}