

(function($){

	/**
	 * Listen for the highlight-article button click and highlight that article
	 *
	 * @author jON bEEBE
	 * @since  28.05.2009
	 */
	$.fn.AIBool = function(__key, callback, init)
	{
		var submiturl = '?ajax=TRUE&url=site_library/ajax/ajax_bool.php';
		var id = '';
		var key = __key;

		var handleclick = function(event) {
			// log('clicked:');
			// log(this);

			buttonID = $(this).attr('id');
			id = $(this).parent().attr('id');
			// log('id = ' + id);

			var vars = 'id=' + id + '&buttonID=' + buttonID + '&key=' + key;

			// If the button says "approve" then action = "approve"
			// else if the button says 'disapprove' ten action = 'disapprove'

			var value = $(this).attr('bool');
			value.toUpperCase();
			// log("value = " + value);

			if(value == 'TRUE') {
				vars += '&value=TRUE';
			}
			else if(value == 'FALSE') {
				vars += '&value=FALSE';
			}
			else {
				return false;
			}

			$.post(submiturl, vars, callback, 'json');

			// Be sure the browser does not do the default submit
			// since javascript handled it

			event.preventDefault();
			return false;
		}

		var onSuccess = function(data) {
			log('AIBool.onSuccess');

			log(data);
			log('id = ' + id);

			if(callback != null) callback(data);
		}

		//log('creating AIBool with key: ' + key);

		if(init != null) {
			for(var i = 0; i < this.length; i++) {
				init( $(this[i]) );
			}
		}

		return this.click(handleclick).removeClass('unprocessed');
	}

})(jQuery);

$(document).ready( function() {

	// Initialize all existing buttons

	function initPressButton(target) {
		target.css('display', 'block');

		if (target.attr('bool') == 'TRUE') {
			target.html('Add to Press');
		}
		else {
			target.html('Remove from Press');
		}
	}

	function initHighlightButton(target) {
		target.css('display', 'block');

		if (target.attr('bool') == 'TRUE') {
			target.html('Add to Highlights');
		}
		else {
			target.html('Remove from Highlight');
		}
	}

	function onPressSuccess(data) {
		log("callback called with");
		log(data);

		if(data.success) {
			log('should change button id:' + data.buttonID);

			if(data.value == 'FALSE') {
				$('[id='+data.buttonID+']').html('Add to Press').attr('bool', 'TRUE');
			}
			else if(data.value == 'TRUE') {
				$('[id='+data.buttonID+']').html('Remove from Press').attr('bool', 'FALSE');
			}
		}
		else {

		}
	}

	function onHighlightSuccess(data) {
		log("callback called with");
		log(data);

		if(data.success) {
			log('should change button id:' + data.buttonID);

			if(data.value == 'FALSE') {
				$('[id='+data.buttonID+']').html('Add to Highlight').attr('bool', 'TRUE');
			}
			else if(data.value == 'TRUE') {
				$('[id='+data.buttonID+']').html('Remove from Highlight').attr('bool', 'FALSE');
			}
		}
		else {

		}
	}

	// Initialize our buttons

	$('.highlight-article.unprocessed').AIBool('highlight', onHighlightSuccess, initHighlightButton);
	$('.press-article.unprocessed').AIBool('press', onPressSuccess, initPressButton);

	// Since these buttons live within a list item
	// listen for a new list item and initiate them if they exist in the new item

	$(document).bind('LIST_ITEM_ADDED', function(event, targetID) {
		$('[id='+targetID+']')
			.children('.highlight-article.unprocessed').AIBool('highlight', onHighlightSuccess, initHighlightButton).end()
			.children('.press-article.unprocessed').AIBool('press', onPressSuccess, initPressButton);
	});
});
