jQuery(document).ready(function() {

	// Catalogue admin main menu allows "dropdown" categories
	jQuery('.nodelinks').click(function(){
		var node = jQuery(this).attr('href').split('node=')[1]
		var nd = jQuery('#node'+node)
		if(nd.length == 0) {
			var x = jQuery(this).parent().parent()
			x.append('<div id="node' + node +'" class="nodes"><' + '/div>')
			
			nd = jQuery('#node'+node)
			nd.load('inc/ajloadnode.php?a=ajax&node='+node, '', subNodeLinks)
			nd.show()
		} else {
			nd.slideToggle('fast');//toggle(jQuery(this).hide(),jQuery(this).show())
		}

		return false
	})

	// This function needed to run for sublevels as the initial JS doesn't seem to apply to it.
	function subNodeLinks(x, y, z) {
		jQuery('.subnodelinks').click(function() {
			var node = jQuery(this).attr('href').split('node=')[1]
			var nd = jQuery('#node'+node)
			if(nd.length == 0) {
				var x = jQuery(this).parent().parent()
				x.append('<div id="node' + node +'" class="nodes"><' + '/div>')

				nd = jQuery('#node'+node)
				nd.load('inc/ajloadnode.php?a=ajax&node='+node)
				nd.show()
			} else {
				nd.slideToggle('fast'); //toggle(jQuery(this).hide(),jQuery(this).show())
			}

			return false	
		})
	}

	// Forces confirmation for delete category
	jQuery('.delcategory').click( function() {
		return confirm('Delete Category and all sub categories - are you sure?')
	});

	// Forces confirmation for delete items
	jQuery('.delitem').click(function() {
		return confirm('Delete item details permanently - Are you sure?');
	});

	// disallows 'blank' search field
	jQuery('#itemModifySearch').submit(function(){
		if($('#search').val() == '')
			return false;
		else
			return true;
	});

	// Select all and select none check box link options
	jQuery('#selall').click(function() {
		$('#selected_items').checkCheckboxes();
		return false;
	})

	jQuery('#remall').click(function() {
		$('#selected_items').unCheckCheckboxes();
		return false;
	})

	// removes the pagination identifier from the query string as a step before adding a new one
	function removePP(str) {
		var url = new String(str);
		var parts = new Array();
		var qstr = new Array();

		parts = url.split('?'); // split the url into the main url and query string
		qstr = parts[1].split('&'); // split the query string into pairs

		var len = qstr.length; // optimise loop by not evaluating length each iteration
		for(var i = 0; i < len; i++) {
			var found = qstr[i].search(/num_pp/);
			if(found >= 0)
				qstr.splice(i, 1);  // If we find the pagination query pair, remove from array
		}

		return parts[0] + '?' + qstr.join('&'); // rejoin the arrays back into our full URL
	}

	// change number of items displayed on selection or on prev / next in the following 2 functions
	jQuery('#num_pp').change(function(){
		var url = removePP(document.location) + '&num_pp=' + jQuery(this).val();
		document.location = url;
		return false;
	});

	jQuery('#next_p').click(function(){
		var url = removePP(jQuery(this).attr('href')) + '&num_pp=' + jQuery('#num_pp').val();
		document.location = url;
		return false;
	});

	jQuery('#prev_p').click(function(){
		document.location = removePP(jQuery(this).attr('href')) + '&num_pp=' + jQuery('#num_pp').val();
		return false;
	});
	
	// Force confirmation on admin logout
	jQuery('.adminonlinelogout').click(function(){
		return confirm('Logout - are you sure?');
	});
})