$(document).ready(function () {
	
	$('#expand').click(function(){
		if($(this).text() == 'Раскрыть'){
			$(this).text('Скрыть');
			$('.open-catalog').removeClass('open-catalog').addClass('hide-catalog');
		}else{
			$(this).text('Раскрыть');
			$('.hide-catalog').removeClass('hide-catalog').addClass('open-catalog');
		}
		$('.catalog-menu-hide').toggle();
		$('select').toggle();
		
		return false;
	})
	
	function isset () {
    	var a=arguments, l=a.length, i=0;
    
	    if (l===0) {
	        throw new Error('Empty isset'); 
	    }
    
	    while (i!==l) {
	        if (typeof(a[i])=='undefined' || a[i]===null) { 
	            return false; 
	        } else { 
	            i++; 
	        }
	    }
    	return true;
	}
	
	$.fn.addRubrics = function(data) {
		return this.each(function() {
			var options = [];
            $.each(data, function(index, itemData) {
            	var rubric = itemData.ItemRubric;
            	options.push('<option value="' + rubric.id +'">' + rubric.caption + '</option>');
            });
            $(this).html(options.join(''));
        });
    };
    
    $.fn.addItems = function(data) {
		return this.each(function() {
			var options = [];
            $.each(data, function(index, itemData) {
            	var item = itemData.Item;
            	options.push('<option value="' + item.id +'">' + item.caption + '</option>');
            });
            options.push('<option value="-1">Другое наименование</option>');
            $(this).html(options.join(''));
        });
    };
	
    updateChildSelect = function(data) {
    	if(!isset(data['childList'][$('#topRubric').val()])){
			$('#childRubric').attr('disabled', 'disabled').html('<option value="0">Виды отсутствуют</option>');
		}else{
			$('#childRubric').removeAttr('disabled').addRubrics(data['childList'][$('#topRubric').val()]);
		}
		updateItemSelect();
    }
    
    updateItemSelect = function() {
    	if($('#childRubric').val() == 0){
    		var id = $('#topRubric').val();
    	}else{
    		var id = $('#childRubric').val();
    	}
    	if(isset(data['itemList'][id])){
    		$('#item').removeAttr('disabled').addItems(data['itemList'][id]);
    	}else{
    		$('#item').attr('disabled', 'disabled').html('<option value="0">Наименования отсутствуют</option>');
    	}
    }
    
    updateDeliverySection = function(deliver){
    	var fields = '#resp-post, #resp-name, #resp-phone, #address, #comment';
    	if(deliver == 0){
    		$(fields).attr('disabled', 'disabled');
    	}else{
    		$(fields).removeAttr('disabled');
    	}
    }
    
	checkChild = function(value){
		var result = false;
    	$.each(value, function(index, value){
    		if(parentId == value.ItemRubric.id){
    			result = true;
    		}	
    	});
    	return result;
    }
    
    updateSelects = function(data){
    	if((parentId != 0) && (isset(data['itemList'][parentId]))){
    		var topRubric = 0;
    		var nochild = false;
    		$.each(data['childList'], function(index, value){
    			if(checkChild(value) == true){
    				topRubric = index;
    			}
    		});
    		/*if(topRubric == 0){
    			topRubric = parentId;
    			nochild = true;
    		}*/
			$('#topRubric').val(topRubric);
			updateChildSelect(data);
			//if(!nochild){
				$('#childRubric').val(parentId);
			//}
			updateItemSelect();
			$('#item').val(itemId);
    	}
    }
    
    updateTopRubric = function(data){
    	if(toprubric != 0){
    		$('#topRubric').val(toprubric);
    		updateChildSelect(data);
    		updateItemSelect();
    	}
    }
    
	if($('#topRubric').length > 0){
		updateChildSelect(data);
		updateDeliverySection($('#delivery').val());
		updateSelects(data);
		updateTopRubric(data);
		$('#topRubric').change(function(){		
			updateChildSelect(data);
		});
		
		$('#childRubric').change(function(){		
			updateItemSelect();
		});
		
		$('#delivery').change(function(){
			updateDeliverySection($('#delivery').val());
		});
		
	}

});
