
(function($){
	
	/**
	 * Listen for the highlight-article button click and highlight that article
	 *
	 * @author jON bEEBE
	 * @since  28.05.2009
	 */
	$.fn.ApproveContent = function()
	{
		var submiturl = '?ajax=TRUE&url=site_library/ajax/ajax_linkApproveContent.php';
		var id = '';
		
		var handleclick = function(event) {
			log('clicked:');
			log(this);
			
			// Get the id of the item to process from the parent LI element's id
			
			id = $(this).parent().attr('id');
			//log('id = ' + id);
			
			var vars = 'id=' + id;
			
			// If the button says "approve" then action = "approve"
			// else if the button says 'disapprove' ten action = 'disapprove'
			
			var action = $(this).html();
			log("action = " + action);
			
			if(action == "Approve") {
				vars += '&action=approve';
			}
			else if(action == "Disapprove") {
				vars += '&action=disapprove';
			}
			else {
				return false;
			}
			
			$.post(submiturl, vars, onSuccess, 'json');
			
			// Be sure the browser does not do the default submit
			// since javascript handled it
			
			event.preventDefault();
			return false;
		}
		
		var onSuccess = function(data) {
			// log(data);
			// log('id = ' + id);
			if(data.success == true) {
				
				// Set this buttons name to the opposite state
				// e.g. an approve button becomes 'unapprove'
				
				var parent = $('[id=' + data.elemid + ']');
				
				log('parent');
				log(parent);
				log(parent[0]);
				
				var button = parent.children('.approve');
				log("button");
				log(button);
				log(button[0]);
				
				var action = button.html();
				log("action = " + action);
			
				if(action == "Approve") {
					button.html( "Disapprove" );
				}
				else if(action == "Disapprove") {
					button.html( "Approve" );
				}
				else {
					return false;
				}
			
			}
		}
		
		return this.click(handleclick).removeClass('unprocessed');
	}
	
})(jQuery); 

$(document).ready( function() {
	
	// Initialize all existing buttons
	
	$('.approve.unprocessed').ApproveContent();
	
	// Since these buttons live within a list item
	// listen for a new list item and try to 
	$(document).bind('LIST_ITEM_ADDED', function(event, targetID) {
		$('[id='+targetID+']').children('.approve-article.unprocessed').ApproveContent()
	});
});