$(function(){
	/*
	jq-child-records
	jq-child-add
	jq-child-del
	jq-child-up
	jq-child-down
	*/
	resetBind();
});
function resetBind(){
	$('.jq-child-up, .jq-child-down, .jq-child-add, .jq-child-del').unbind();
	
	$('.jq-child-up').css({
		backgroundColor:'#006699',
		color:'#f9f9f6',
		cursor:'pointer'
	}).bind('click',function(){
		var myrec=$(this).parents('tr');
		myrec.animate({backgroundColor:'#006699'},'fast',function(){
			myrec.animate({backgroundColor:'#006699'},60,function(){
				myrec.prev().before( myrec ).end();
				myrec.animate({backgroundColor:'#006699'},60,function(){
					myrec.animate({backgroundColor:'#f9f9f6'},'fast')
				})
			})
		});
	});
	$('.jq-child-down').css({
		backgroundColor:'#006699',
		color:'#f9f9f6',
		cursor:'pointer'
	}).bind('click',function(){
		var myrec=$(this).parents('tr');
		myrec.animate({backgroundColor:'#006699'},'fast',function(){
			myrec.animate({backgroundColor:'#006699'},60,function(){
				myrec.next().after( myrec ).end();
				myrec.animate({backgroundColor:'#006699'},60,function(){
					myrec.animate({backgroundColor:'#f9f9f6'},'fast')
				})
			})
		});
	});
	$('.jq-child-add').css({
		color:'#000099',
		cursor:'pointer'
	}).bind('click',function(){
		var myrec=$(this).parents('tr');
		var copy=myrec.clone();
		copy.find(':input').each(function(){
			$(this).attr('value','');
		});
		myrec.after(copy);

		if($.fn.textbox){
			copy.find('.jq-box').textbox();
		}
		if($.fn.calendar){
			copy.find('.jq-calendar').calendar()
		}
		resetBind();
		
		copy.animate({backgroundColor:'#006699'},'fast',function(){
			copy.animate({backgroundColor:'#f9f9f6'},'fast')
		});
	});
	$('.jq-child-del').css({
		color:'#ff0000',
		cursor:'pointer'
	}).bind('click',function(){
		var myrec=$(this).parents('tr');
		var mydata=myrec.parents('.jq-child-records');
		myrec.animate({backgroundColor:'#ffcccc'},'fast',function(){
			if(mydata.children().length ==1){
				myrec.find('.jq-child-add').click();
			}
			myrec.remove();
		});
	});
}