$(document).ready(function() 
{
	$('#industry_select').change(function() {getIndustrySection(this);});
	
	function getIndustrySection(elem)
	{
		$.getJSON("/prax/getIndustrySection/"+$(elem).val(), function(result){

			$('#record_section_row').remove();
			
			/*
			$('#industry_select').parent().after('<div class="form_row" id="record_section_row"><label>Sekcia</label><select name="section" id="record_section"></select></div>');
			*/
			
			$('#industry_select').parent().after('<div id="record_section_row"><select name="section" id="record_section"></select></div>');
		   
		  	$.each(result,function(i,item)
		  	{
		  		$('#record_section').append('<option value="'+i+'">'+item+'</option>')
		  	})


		});
	}
	
	$('#project_industry_select').change(function() {
		
		$.getJSON("/prax/getIndustrySection/"+$(this).val(), function(result){

			$('#record_section_row').remove();
			$('#project_industry_select').parent().after('<div id="record_section_row"><select name="record[section]" id="record_section"></select></div>');
		   
		  	$.each(result,function(i,item)
		  	{
		  		$('#record_section').append('<option value="'+i+'">'+item+'</option>')
		  	})


		});
	   
	});
	
	//$('#industry_select').click(function(){alert('sel')});

	$('#offer_industry_select li a').click(function() {
		
		var ind_id =$($('#industry_select option').get($(this).attr('index'))).val();
		//alert(ind_id);
		
		//alert('ok');
		$.getJSON("/prax/getIndustrySection/"+ind_id, function(result){
		   
		  	$('#offer_section_select').html('');
		   	$('#offer_section_select').append('<select name="section" id="section_select"></select>');
		   
		  	$.each(result,function(i,item)
		  	{
		  		$('#section_select').append('<option value="'+i+'">'+item+'</option>')
		  	})

			$('#section_select').jqTransSelect();
			$('#offer_section_select .jqTransformSelectWrapper').css('z-index','9');

		});
	});
	
	//pridanie novej firmy
	$('#register_company_select').change(function(){
		
		var selected = $(this).val();
		if('other' == selected)
		{
			$('#select_company_type').slideDown(300);
		}
		else
		{
			$('#select_company_type').slideUp(300);
		}
	})
	
	$('#select_company_type input').click(function(){
		
		var company_type = $(this).val();
		if('prax' == company_type ){var form_url = '/prax/registracia-firmy'}
		if('veda-a-vyskum' == company_type ){var form_url = '/veda-a-vyskum/registracia-institucie'}

		location.href = form_url;
		/*
		$.ajax({
			   type: "GET",
			   url: form_url,
			   success: function(msg)
			   {
					$('#add_company_form').html(msg);
					$('#add_company_form').slideDown(300);
					$('#industry_select').unbind();
					$('#industry_select').change(function() {getIndustrySection(this);});
			   }
		   });	
		   */
	   

	})
	
	//kontrola dlzky
	$('#record_name').keydown(function(){inputCountdown(this,95,'name_countdown')})
	$('#record_name').focus(function(){inputCountdown(this,95,'name_countdown')})
	$('#record_description').keydown(function(){inputCountdown(this,600,'description_countdown')})
	$('#record_description').focus(function(){inputCountdown(this,600,'description_countdown')})
	
	
	function inputCountdown(elem,countdown_length,countdown_id)
	{
		$('#'+countdown_id).text(countdown_length-$(elem).val().length);
	}
	
	

	
});

