$(function(){
	// Wyszukiwarka
	var default_values = new Array();
    $('.on-focus-change').focus(function(){
    	if (!default_values[this.name]) {
        	default_values[this.name] = this.value;
        }
    	this.value = '';
    });
    $('.on-focus-change').blur(function(){
    	if (this.value == ''){
    		$(this).attr('value', default_values[this.name]);
    	}
    });
    
    // Colorbox
    $('a[rel="colorbox"]').colorbox();
	
	// Formularz kategorii i dodatkowych pol formularza
	var defaultNames = new Array();
    $('.select-category').each(function(){
    	defaultNames[this.id] = this.name;
    });  
	
	if ($('.categoryPath').size() > 1)
	{
		lastCategoryPath = $('.categoryPath').last().val();
		$('.categoryPath').each(function(){
			parentId = $(this).next('.categoryPathParent').val();
			categoryId = $(this).val();
			
			$('#Subcategory' + parentId).val(categoryId).parent('div').show();
			
			if ($(this).val() == lastCategoryPath)
			{
			    if ($('#' + categoryId).length > 0) // sprawdzenie czy dana kategoria ma subkategorie
			    {
				    $('.select-category').each(function(){
				    	$(this).attr('name', defaultNames[this.id]);
					});
				    $("#additional-forms").html('');
			    	$('#' + $(this).val()).show().children('.select-category').attr('name', 'data[CurrentCategory]').focus();
			    } else {
				    $('.select-category').each(function(){
				    	$(this).attr('name', defaultNames[this.id]);
					});
				    
				    // załadowanie dodatkowych pol formularza
				    $("#additional-forms").load(SITE_ADDRESS + "additional_forms/ajax_form_load/" + categoryId, function(){
				    	// Wprowadzanie danych do dodatkowych pol formularza, kiedy istnieje tablica $_POST
				    	$('.selected-additional-forms').each(function(){
				    		var newValue = $(this).val();
				    		var additionalFormId = $(this).attr('title');
				    		var additionalForm = $('#AdditionalForm' + additionalFormId);
				    
				    		if (additionalForm.is('select,input[type=text]'))
				    		{
				    			additionalForm.val(newValue);
				    		} else {
				    			var additionalForm = $('#AdditionalForm' + additionalFormId + newValue);
				    			
				    			if (additionalForm.is('input[type=radio],input[type=checkbox]'))
				    			{
				    				additionalForm.attr('checked', 'checked');
				    			}
				    		}
				    	});
				    });
				    
				    $('option[value=' + categoryId + ']').parent('.select-category').attr('name', 'data[CurrentCategory]');
			    }
			}
		});
	} else {
		$('.maincategory-dropdown').children('.select-category').attr('name', 'data[CurrentCategory]');	
	}

	$('.select-category').live('change', function(){	    
		// pobranie aktualnego levelu
		level = $(this).prev('input[type=hidden]').val();

		// usuniecie divow zawierajacych input hidden (level) z wartoscia większą niz obecna
		$('input[type=hidden]').each(function(){
			if ($(this).val() > level)
			{
				$(this).next().find('option:first').attr('selected', 'selected');
				$(this).parent('div').hide();
			}
		});

	    if ($('#' + $(this).val()).length > 0) // sprawdzenie czy dana kategoria ma subkategorie
	    {
		    $('.select-category').each(function(){
		    	$(this).attr('name', defaultNames[this.id]);
			});
		    $("#additional-forms").html('');
	    	$('#' + $(this).val()).slideDown('fast').children('.select-category').attr('name', 'data[CurrentCategory]').focus();
	    } else {
		    $('.select-category').each(function(){
		    	$(this).attr('name', defaultNames[this.id]);
			});
		   
		    // załadowanie dodatkowych pol formularza
		    $("#additional-forms").load(SITE_ADDRESS + "additional_forms/ajax_form_load/" + $(this).val());
		    
		    $(this).attr('name', 'data[CurrentCategory]');
	    }
	});
});
