/**
 * Lowdowns
 *
 * @author Bram Van Damme <bramus@netlash.com>
 * @author Bert Pattyn <bert@netlash.com>
 */

/**
 * JS_NETLASH Object
 */
if (!JS_NETLASH) { var JS_NETLASH = new Object(); }

/**
 * JS_NETLASH - Lowdowns object
 */
JS_NETLASH.lowdowns = {

	/**
	 * Datamembers
	 */
	debug: false,

	/**
	 * init
	 *
	 * @return void
	 */
	init: function() {
		JS_NETLASH.lowdowns.commentsHandler.init();
	}
}



/**
* JS_NETLASH - Lowdowns object - commentsHandler
*/
JS_NETLASH.lowdowns.commentsHandler = {

	/**
	 * Datamembers (config)
	 */
	blob			: '<li><a class="userImage" href="{url}"><img src="/modulefiles/profiles/avatars/38x38/{avatar}" alt="{username}" title="{username}" /></a><h4><a href="{url}" title="{username}">{username}</a></h4><p>{text}</p></li>',
	spinner			: '<img src="/modules/core/layout/images/spinner.gif" id="spinner_{id}" alt="" title="" />',


	/**
	 * init - hook ourselves to the form!
	 *
	 * @return void
	 */
	init: function() {

		// hook submit button
		if ($('#btnAddComment').length > 0) {
			$('#btnAddComment').bind('click', JS_NETLASH.lowdowns.commentsHandler._initAddComment);
		}

		// hook flagLinks
		$('p.flagLink a').bind('click', JS_NETLASH.lowdowns.commentsHandler._initFlagComment);
	},


	/**
	 * _initAddComment - Handle the add comment button click event
	 *
	 * @return void
	 */
	_initAddComment: function(evt) {

		// debug
		if(JS_NETLASH.lowdowns.debug) console.log('_initAddComment');

		// don't submit!
		evt.preventDefault();

		// disable the button & show the spinner
		$('#btnAddComment').attr('disabled','disabled');
		$('#spinner').css('visibility','visible');

		// define postData
		postData	= $('#addCommentForm').serialize();

		// 	Now make the ajax call
		JS_NETLASH.lowdowns.commentsHandler._doAddComment(postData)
	},


	/**
	 * _doAddComment - do the Ajax Dance
	 *
	 * @return void
	 */
	_doAddComment: function(postData) {

		$.ajax({
			url:		'/ajax.php?module=lowdowns&action=add_comment',
			type:		'post',
			dataType:	'json',
			cache:		false,
			data:		postData,
			// success making call
			success:	function(json) {
				// process the ajax response
				JS_NETLASH.lowdowns.commentsHandler._doneAddComment(json);
			},
			// error making call - something went horribly wrong!
			error:		function(xhr,err,e) {
				// give notice
				alert(err + ' ' + e, 'Critical Error');
				// reload the page
				if(!JS_NETLASH.lowdowns.debug) window.location.reload();
			}
		});

	},


	/**
	 * _doneAddComment - process the Ajax response
	 *
	 * @return void
	 */
	_doneAddComment: function(json) {

		// debug
		if (JS_NETLASH.lowdowns.debug) {

			console.log('_doneAddComment');

			console.log(json);

		}

		// what what (in the butt)?
		switch (parseInt(json.status.code)) {
			// OK and NOK (added // spammed)
			case 200:
			case 400:
				// define what to insert
				toInsert	= JS_NETLASH.utils.string.assignFromObject(JS_NETLASH.utils.string.assignFromObject(JS_NETLASH.lowdowns.commentsHandler.blob, json.content.user), json.content.comment);
				// debug
				if (JS_NETLASH.lowdowns.debug)	console.log(toInsert);
				// insert comment into dom
				if ($('#shoutbox-comments ul').length > 0) $('#shoutbox-comments ul').append(toInsert);
				else {
					
					$('#no_sidebar_comments').hide();
					
					$('#shoutbox-comments').prepend('<ul class="clearfix">' + toInsert + '</ul>');
				}
				// reset the form
				$('#addCommentForm').get(0).reset();
				// no need to rehook the flagLinks as we can't report our own comment ;-)
				break;
			// ERROR - Something went wrong (most likely insufficient params)
			case 500:
			default:
				// Give notice
				alert(json.status.text);
				break;
		}
		// reactivate form
		$('#btnAddComment').removeAttr('disabled');
		$('#spinner').css('visibility','hidden');
	},


	/**
	 * _initFlagComment - Handle the flag comment link click event
	 *
	 * @return void
	 */
	_initFlagComment: function(evt) {

		// debug
		if(JS_NETLASH.lowdowns.debug) console.log('_initFlagComment');

		// don't jump to the top
		evt.preventDefault();

		// define the id
		var id = evt.target.id.toString().substring(13);
		
		// inject spinner
		$(evt.target).parent().html(JS_NETLASH.utils.string.replaceAll(JS_NETLASH.lowdowns.commentsHandler.spinner, '{id}', id));
		
		// define postData
		postData	= 'comment_id=' + id;
		
		// 	Now make the ajax call
		JS_NETLASH.lowdowns.commentsHandler._doFlagComment(postData, id);
	},


	/**
	 * _doFlagComment - do the Ajax Dance
	 *
	 * @return void
	 */
	_doFlagComment: function(postData, id) {

		$.ajax({
			url:		'/ajax.php?module=lowdowns&action=flag_comment',
			type:		'post',
			dataType:	'json',
			cache:		false,
			data:		postData,
			// success making call
			success:	function(json) {
				// process the ajax response
				JS_NETLASH.lowdowns.commentsHandler._doneFlagComment(json, id);
			},
			// error making call - something went horribly wrong!
			error:		function(xhr,err,e) {
				// give notice
				alert(err + ' ' + e, 'Critical Error');
				// reload the page
				if(!JS_NETLASH.lowdowns.debug) window.location.reload();
			}
		});

	},


	/**
	 * _doneFlagComment - process the Ajax response
	 *
	 * @return void
	 */
	_doneFlagComment: function(json, id) {
		// debug
		if (JS_NETLASH.lowdowns.debug) {

			console.log('_doneFlagComment');

			console.log(json);
		}

		// what what (in the butt)?
		switch (parseInt(json.status.code)) {

			// OK - Flagged
			case 200:
				// update text (and thus remove spinner)
				$('#flagLink_' + id).html(json.status.text);
				break;

			// NOK - Already flagged
			case 400:
				// Just update it too
				$('#flagLink_' + id).html(json.status.text);
				break;

			// ERROR - Something went wrong (most likely insufficient params)
			case 500:
			default:
				// Give notice
				alert(json.status.text);
				// reload the page
				window.location.reload();
				break;
		}
	},


	/**
	 * end of object
	 */
	_eoo: true
}



/**
 * Main Run : Init objects when document is loaded
 */
$(document).ready(function() {
	JS_NETLASH.lowdowns.init();
});


