$(function() {
	var rhresources=$('#rh-resources');
	var addresourcedialog=$('#addresourcedialog');
	var intContentID=0;

	initRHResources(rhresources);
	
	$('#addresourcedialog').jqm({modal: true, overlay:40});
	
	$('.addtoresources').click( function() {
		$('#addresourcesuccess', addresourcedialog).hide();
		$('#addresourcefailure', addresourcedialog).hide();
		$('#addresourcebutton', addresourcedialog).hide();
		$('#addresourcedisabled', addresourcedialog).show();
		$('#addresource-fields', addresourcedialog).show();
		clearRadioButtons();
		$('#newcategoryname' ,addresourcedialog).val('');
	
		intContentID=getID(this.id);
		
		$.ajax({
			type: 'POST',
			url: 'index.php',
			data: 'action=ajaxcontentxml&contentid='+intContentID,
			async: false,
			success: function(xml) {
				if (''!=$('Title', xml).text())
				{
					$('.adding .name', addresourcedialog).text($('Title', xml).text());
					$('.adding .info', addresourcedialog).text(
						$('ext', xml).text()
						+' | '
						+$('filesize_string', xml).text()
					);
				
					addresourcedialog.jqmShow();
				}
			}
		});
	
		
		return false;
	}
	);
	
	$('input[type=radio]', addresourcedialog).bind('click', function() {
		enableSubmit()
	})
	
	$('#newcategoryname', addresourcedialog).keyup( function() {
		enableSubmit();
	}
	);
	
	$('#addresourcebutton').click( function() {
		var intCategoryID=$('input[type=radio]:checked', addresourcedialog).attr('value');
	
		$('#addresourcesuccess', addresourcedialog).hide();
		$('#addresourcefailure', addresourcedialog).hide();
	
		$.ajax({
			type: 'POST',
			url: 'index.php',
			data: 'action=adduserresource&contentid='+intContentID+'&categoryid='
				+intCategoryID+'&newcategoryname='+$('#newcategoryname' ,addresourcedialog).val(),
			async: false,
			success: function(xml) {
				if (1==parseInt($('Success', xml).text(), 10))
				{
					$('#addresource-fields', addresourcedialog).hide();
					$('#addresourcesuccess', addresourcedialog).show();
					reloadUserResources();
				}
				else
				{
					$('#addresourcefailure', addresourcedialog).show();

					$('#addresourcedisabled', addresourcedialog).hide();
					$('#addresourcebutton', addresourcedialog).show();
				}
			}
		});
		
		return false;
	}
	);
	
});

function enableSubmit()
{
	var addresourcedialog=$('#addresourcedialog');

	if (0!=$('input[type=radio]:checked', addresourcedialog).length || ''!=$('#newcategoryname' ,addresourcedialog).val())
	{
		$('#addresourcedisabled', addresourcedialog).hide();
		$('#addresourcebutton', addresourcedialog).show();
	}
	else
	{
		$('#addresourcebutton', addresourcedialog).hide();
		$('#addresourcedisabled', addresourcedialog).show();

	}
}

function clearRadioButtons()
{
	$('#addresourcedialog input[type=radio]').each( function()
	{
		$(this).attr('checked', false);
	}
	);
}

function reloadUserResources()
{
	$.ajax({
		type: 'POST',
		url: 'index.php',
		data: 'action=getuserresourceshtml&forcereload=1',
		dataType: 'html',
		success: function(html) {
			if (0!=$('#rh-resources').length)
			{
				$('#rh-resources').replaceWith(html);
			}
			else
			{
				$('#loggedin').after(html);
			}
			initRHResources($('#rh-resources'));
			
			$.ajax({
				type: 'POST',
				url: 'index.php',
				data: 'action=getuserresourcecatshtml',
				dataType: 'html',
				success: function(html) {
					$('#existingcategories').replaceWith(html);

					$('#addresourcedialog input[type=radio]').bind('click', function() {
						enableSubmit()
					})
				}
			});
		}
	});
	
}


function initRHResources(rhresources)
{
	$('.resources', rhresources).hide();
	$('.category', rhresources).addClass('clickablecategory').addClass('closedcategory');
	$('.categoryname', rhresources).addClass('closedcategoryname');

	$('#rh-resources .category').bind ('click', function() {
		intID=getID(this.id);

		if (0!=$('.closedcategoryname', $(this)).length && 0!=$('ul.resources', $(this)).length)
		{
			$('#resources_'+intID, rhresources).slideDown('fast');
			$(this).removeClass('closedcategory').addClass('opencategory');
			$('.categoryname', $(this)).removeClass('closedcategoryname').addClass('opencategoryname');
		}
		else
		{
			$('#resources_'+intID, rhresources).slideUp('fast');
			$(this).removeClass('opencategory').addClass('closedcategory');
			$('.categoryname', $(this)).removeClass('opencategoryname').addClass('closedcategoryname');
		}
	}
	);
}
