var curType = 1;
var curnum = 10;

$(document).ready(function(){
	$('#hidC').focusout(function(){
		if ($('#hidC').val() != ''){
			$('#catother'+curType).html($('#hidC').val());
		} else {
			$('#catother'+curType).html('другое');
		}
		
		$('#hidC').hide();
		$('#o_cat').val($('#hidC').val());
	});

	$('#mainblock').height(document.body.scrollHeight - 51);

	$('.onerecord').hover(function(){
		$(this).find('.hidden').show();	
	}, function(){
		$(this).find('.hidden').hide();
	});
	
	$('#feedback').submit(function(){
		$.ajax({ 
			url: '/ajax.php', 
			data:{
				message: $('#feedback #message').val(),
				action: 'feedback'
			},
			success: function(data){
				 $('#feedback').html(data);
			}
		});
		
		return false;
	});
});

function deleteRecord(id){
	$.ajax({ 
		url: '/ajax.php', 
		data:{
			id: id,
			action: 'deleteRecord'
		},
		success: function(data){
			$('#form'+id).delay(300).fadeOut(function(){
				$('#form'+id).remove();
				$('#onerow'+id).delay(300).animate({
					backgroundColor: '#dc8e8e'},
					200)
					.delay(20)
					.animate({
						backgroundColor: '#ffffff'},
						200,
						function() {
							$(this).hide();
					});
			});
		}
	})
}

function closeForm(id){
	$('#form'+id).delay(300).fadeOut(function(){
		$('#form'+id).remove();
	});
}

function editRecord(id){
	$.ajax({ 
		url: '/ajax.php', 
		data:{
			params: 'id='+id,
			f: 'form_out',
			action: 'ajax'
		},
		success: function(data){
			$('#form').after(data);
			eform = $('#form'+id);
			line = $('#onerow'+id);
			
			eform.css('position','absolute');
			eform_left = line.offset().left;
			eform_top = line.offset().top;
			eform.css('left',eform_left-22);
			eform.css('top',eform_top-15);
			eform.fadeIn();
			eform.css('opacity',0.99);
		}
	})
}

function tenmore(){
	$.ajax({ 
		url: '/ajax.php', 
		data:{
			action: 'load10records',
			count: 10,
			offset: curnum
		},
		success: function(data){
			curnum += 10;
			$('#records .onerecord').last().after(data);

			$('.onerecord').hover(function(){
				$(this).find('.hidden').show();	
			}, function(){
				$(this).find('.hidden').hide();
			});
			
			$('#mainblock').height(document.body.scrollHeight - 51);
		}
	})
}

function refreshAccount(accId){
	$('#accounts #account'+accId+' .money').html('<img src="/img/icons/loading.gif" border=0 />');
	$.ajax({ url: "/ajax.php", method:'POST', data: {
			'action' : 'ajax',
			'f' : 'refreshAccountCount',
			'params' : 'accId='+accId
		}, success: function(res){
			$('#accounts #account'+accId+' .money').html(res);
		}
	});
}

function editCategory(catId){
	$('#onecat'+catId+' .cat-icon img').attr('src','/img/icons/loading.gif');
	
	$.ajax({ url: "/ajax.php", method:'POST', data: {
			'action' : 'load_catEdit',
			'id' : catId
		}, success: function(res){
			$('#onecat'+catId).html(res);
		}
	});
}

function delCategory(catId){
	$('#onecat'+catId+' .cat-icon img').attr('src','/img/icons/loading.gif');
	
	$.ajax({ url: "/ajax.php", method:'POST', data: {
			'action' : 'delCat',
			'id' : catId
		}, success: function(res){
			$('#onecat'+catId).delay(300).animate({
				backgroundColor: '#dc8e8e'},
				200)
				.delay(20)
				.animate({
					backgroundColor: '#ffffff'},
					200,
					function() {
						$(this).hide();
				});
		}
	});
}

function uniteCategory(catId,newcatId){
	$('#onecat'+catId+' .cat-icon img').attr('src','/img/icons/loading.gif');
	
	$.ajax({ url: "/ajax.php", method:'POST', data: {
			'action' : 'uniteCat',
			'id' : catId,
			'newcatId' : newcatId
		}, success: function(res){
			$('#onecat'+catId).delay(300).animate({
				backgroundColor: '#dc8e8e'},
				200)
				.delay(20)
				.animate({
					backgroundColor: '#ffffff'},
					200,
					function() {
						$(this).hide();
				});
		}
	});
}



