jQuery(document).ready(function(){
	
	//Выбор категорий -----------------------------------
	var treeCount = 0;
	var tree_ids = [];
	
	function blockAll() {
		boxes = $('.chb-tree');
		$.each(boxes, function() {
			box = $(this);
			if (!box.is(':checked')) {
				box.attr('disabled', 'true');
				box.parent().css('color', 'gray');
			}
		});
	}
	
	function unBlockAll() {
		boxes = $('.chb-tree');
		$.each(boxes, function() {				
			box = $(this);
			box.attr('disabled', false);
			box.parent().css('color', 'black');				
		});			
	}
	
	function allTree() {
		if ($('#alltree').is(':checked')) {			
			boxes = $('.chb-tree');
			$.each(boxes, function() {				
				box = $(this);
				box.attr('disabled', true);
				box.attr('checked', true);
				box.parent().css('font-weight', 'normal');
				box.parent().css('color', 'gray');		
				$('#alltree').attr('checked', true);
			});									
		} else {
			boxes = $('.chb-tree');
			$.each(boxes, function() {				
				box = $(this);
				box.attr('disabled', false);
				box.attr('checked', false);
				box.parent().css('color', 'black');				
			});		
		}	
	}
	
	$('#treeok').click(function(){
		$('.smc-cont').fadeOut('fast', function() {});
		$('#shadowall').fadeOut('fast', function() {});
	});
	
	$("#tree-list").dialog({
		resizable: true,
		autoOpen: false,
		height: 480,
		width: 1100,
		modal: true,
		buttons: {
			'OK': function() {
				$(this).dialog('close');				
			}
		}
		
	});
	$('#dropdown').click(function(){
		$("#tree-list").dialog('open');
		return false;
	});

	
	$('#alltree').click(function(event){
		treeCount = 0;
		allTree();
	});
	
	$('.chb-tree').click(function(event){
		target = $(event.target);
		if (target.is(':checked')) {
			target.parent().css('font-weight', 'bold');
			treeCount++;	
		} else {
			target.parent().css('font-weight', 'normal');
			treeCount--;			
		}
		if (treeCount == 5 && target.is(':checked')) {
			blockAll();
		} else if (treeCount == 4 && !target.is(':checked')){
			unBlockAll();
		}
	});
	
	boxes = $('.chb-tree');
	$.each(boxes, function() {
		box = $(this);
		if (box.is(':checked')) {
			box.parent().css('font-weight', 'bold');
			treeCount++;
		}
	});
	
	if (treeCount == 0) {
		//$('#alltree').attr('checked', true);
		//allTree();
	} else if (treeCount == 5) {
		blockAll();
	}	
	
	$('#edit-form').submit(function(){
		var Form = document.getElementById('edit-form');
		boxes = $('.chb-tree');
		$.each(boxes, function() {
			box = $(this);
			if (box.is(':checked') && !box.is(':disabled')) {
				var Inner = document.createElement('input');
				Inner.type = 'hidden';
				Inner.value = box.val();
				Inner.name = 'tree_ids[]';
				Form.appendChild(Inner);			
			}
		});
	});
	//Выбор категорий -----------------------------------
	
	//------------------------------------------
	$('#country_id').change(function(){
		$('#region_id').attr('disabled', true);
		$('#region_id').html('<option>Загрузка...</option>');
		$.getJSON('/ajax/regions/country/' + $('#country_id').val() + '/', function(data) {
			$('#region_id').html('<option> -- Выберите регион </option>');
			$.each(data, function(i, item) {
				$('#region_id').append('<option value="' + item.region_id + '">' + item.region_name + '</option');
				$('#region_id').attr('disabled', false);
			});
		});
	});
			
		
	$('#delete-logo').click(function(){
		$("#dialog-confirm").dialog({
			resizable: false,
			height:190,
			modal: true,
			buttons: {
				'Нет': function() {
					$(this).dialog('close');
				},
				'Да': function() {
					$('#logo-img').load('/user/info/deleteimage/');
					$(this).dialog('close');
				}
			}
			
		});
		
		return false;
	});		
	
	
	//------------------------  price --------------------
	
	function fillCatSelect(id, data) {
		$.each(data, function(i, item) {
			$('#' + id).append('<option value="' + item.tree_id + '">' + item.name + '</option');
			$('#' + id).attr('disabled', false);
		});			
	}
	
	$('#root_tree_id').change(function() {
		$('#middle_tree_id').html('');
		rootVal = $('#root_tree_id').val();
		$.getJSON('/ajax/pricecategories/id/'+ rootVal + '/deep/1/', function(data) {
			$('#middle_tree_id').append('<option value="0"> - Выберите подраздел -</option');
			fillCatSelect('middle_tree_id', data);
			$('#tree_id').html('');
			$.getJSON('/ajax/pricecategories/id/'+ data[0].tree_id + '/deep/1/', function(inner_data) {
				$('#tree_id').append('<option value="0"> - Выберите категорию -</option');
				if (inner_data.length == 0) {
					fillCatSelect('tree_id', data);
				} else {
					fillCatSelect('tree_id', inner_data);
				}
			});
		});
	});
	
	$('#middle_tree_id').change(function() {
		rootVal = $('#middle_tree_id').val();
		$('#tree_id').html('');
		$.getJSON('/ajax/pricecategories/id/'+ rootVal + '/deep/1/', function(data) {
			if (data.length == 0) {
				$('#tree_id').append('<option value="' + rootVal + '">' + $('#middle_tree_id option:selected').html() + '</option');
			} else {
				$('#tree_id').append('<option value="0"> - Выберите категорию -</option');
				fillCatSelect('tree_id', data);
			}		
		});
	});	
	
	if ($('#price').val() == '0' ||  $('#price').val() == '') {
		$('#price').attr('disabled', true);
		$('#agreement-price').attr('checked', true);
	}
	
	if ($('#root_tree_id').val() < 1) {
		$('#middle_tree_id').attr('disabled', true);
	}
	
	if ($('#middle_tree_id').val() < 1) {
		$('#tree_id').attr('disabled', true);
	}
	
	
	//Вызов диалога логина если доступ закрыт
	$("#dialog-login-auto").dialog({
		resizable: false,
		modal: true,
		width: 270,
		buttons: {
			'Войти': function() {
				$('#login-form').submit();
			}
		}			
	});
		
});


