(function($) {
	if (!JS_AB) { var JS_AB = new Object(); }
	JS_AB.abradio = {
		debug : false,
		init : function() {
			JS_AB.abradio.windowPopup.init();
			JS_AB.abradio.commentsHandler.init();
			JS_AB.abradio.quickSearchHandler.init();
			JS_AB.abradio.hoverHandler.init();
			JS_AB.abradio.player.init();
		}
	}

	JS_AB.abradio.windowPopup = {
		init: function() {
			$('#playerNewWindow').click(function() {
				window.open(this.href,'AB Radio','width=301,height=150,menubar=no,toolbar=no,location=no,scrollbars=no,resizable=no'); return false;
			});

			$('#playerNewWindowBig').click(function() {
				window.open(this.href,'AB Radio','width=584,height=550,menubar=no,toolbar=no,location=no,scrollbars=no,resizable=no'); return false;
			});
		}
	}
	
	JS_AB.abradio.quickSearchHandler = {
		minChars: 1,
		maxItemsToShow: 50,
		init: function() {
			if ($('#search').length > 0) {
				$('#search').autocomplete("/ajax.php?module=abradio&action=autocomplete", {
					matchContains: true,
					minChars: JS_AB.abradio.quickSearchHandler.minChars,
					dataType: 'json',
					maxItemsToShow: JS_AB.abradio.quickSearchHandler.maxItemsToShow,
					parse: JS_AB.abradio.quickSearchHandler._parseAutoComplete
				});
				$('#search').result(JS_AB.abradio.quickSearchHandler._handleAutoComplete);
				$('#searchArtists').bind('submit', function(evt) {
					evt.preventDefault();
					window.location = $('#searchArtists').attr('action') + JS_NETLASH.utils.string.urlise($('#search').val());
				});
			}
		},
		_parseAutoComplete: function(json) {
			var parsed = [];
			if (json.content.count > 0) {
				for (i in json.content.data) {
					var entryName 	= JS_NETLASH.utils.string.html_entity_decode(json.content.data[i].name);
					var entryUrl	= json.content.data[i].url;
					parsed[parsed.length] = {
						data	: [entryName],
						value	: entryUrl,
						result	: entryName
					};
				}
			}
			return parsed;
		},
		_handleAutoComplete: function(event, data, formatted) {
			if (formatted)	window.location = formatted;
		},
		_eoo				: true
	}
	JS_AB.abradio.hoverHandler = {
		init: function() {
			if ($('#abtv-images a').length > 0) {
				$('#abtv-images a').hover(
					function(evt) {
						$('#row-' + $(this).attr('id').replace('image-', '')).addClass('hover'); 
						$(this).addClass('hover'); 
					},
					function(evt) { 
						$('#row-' + $(this).attr('id').replace('image-', '')).removeClass('hover'); 
						$(this).removeClass('hover'); 
					}
				);
				$('#abtv-datagrid tbody tr td').hover(
					function(evt) { 
						$('#image-' + $(this).parent('tr').attr('id').replace('row-', '')).addClass('hover');
						$(this).parent('tr').addClass('hover');
					},
					function(evt) { 
						$('#image-' + $(this).parent('tr').attr('id').replace('row-', '')).removeClass('hover'); 
						$(this).parent('tr').removeClass('hover'); 
					}
				);
			}
		},
		_eoo				: true
	}	
	JS_AB.abradio.commentsHandler = {
		blob : '<div class="shoutbox-comment secondarycontent clearfix"><div class="avatar"><a href="{url}" class="noborder" title="{nick}"><img src="/modulefiles/profiles/avatars/48x48/{avatar}" alt="{nick}" title="{nick}" /></a></div><div class="message"><p class="user"><a href="{url}" title="{nick}">{nick}</a></p><p>{text}</p></div></div>',
		spinner : '<img src="/modules/core/layout/images/spinner.gif" id="spinner_{id}" alt="" title="" />',
		init : function() {
			if ($('#btnAddComment').length > 0) { $('#btnAddComment').removeAttr('disabled').bind('click', JS_AB.abradio.commentsHandler._initAddComment); }
			$('p.flagLink a').bind('click', JS_AB.abradio.commentsHandler._initFlagComment);
			$('p.deleteLink a').bind('click', JS_AB.abradio.commentsHandler.deleteComment);
		},
		_initAddComment : function(evt) {
			evt.preventDefault();
			$('#btnAddComment').attr('disabled','disabled');
			$('#spinner').css('visibility','visible');
			postData = $('#addCommentForm').serialize();
			JS_AB.abradio.commentsHandler._doAddComment(postData)
		},
		_doAddComment : function(postData) {
			$.ajax({
				url: '/ajax.php?module=abradio&action=add_comment',
				type: 'post',
				dataType: 'json',
				cache: false,
				data: postData,
				success: function(json) { JS_AB.abradio.commentsHandler._doneAddComment(json); },
				error: function(xhr,err,e) { if(JS_AB.abradio.debug) { alert(err + ' ' + e, 'Critical Error'); }}
			});
		},
		_doneAddComment : function(json) {
			if (JS_AB.abradio.debug) console.log(json);
			switch (parseInt(json.status.code)) {
				case 200:
				case 400:
					toInsert = JS_NETLASH.utils.string.assignFromObject(JS_NETLASH.utils.string.assignFromObject(JS_AB.abradio.commentsHandler.blob, json.content.user), json.content.comment);
					if (JS_AB.abradio.debug) console.log(toInsert);
					if ($('#no_sidebar_comments')) { $('#no_sidebar_comments').remove(); }
					$('#shoutbox-comments').prepend(toInsert);
					$('#addCommentForm').get(0).reset();
				break;
				case 500:
				default:
					if(JS_AB.abradio.debug) { alert(json.status.text); }
				break;
			}
			$('#btnAddComment').removeAttr('disabled');
			$('#spinner').css('visibility','hidden');
		},
		_initFlagComment : function(evt) {
			evt.preventDefault();
			if (JS_AB.abradio.debug) console.log(evt);
			var id = evt.target.id.toString().substring(13);
			$(evt.target).parent().html(JS_NETLASH.utils.string.replaceAll(JS_AB.abradio.commentsHandler.spinner, '{id}', id));
			postData = 'comment_id=' + id;
			JS_AB.abradio.commentsHandler._doFlagComment(postData, id);
		},
		_doFlagComment : function(postData, id) {
			$.ajax({
				url: '/ajax.php?module=abradio&action=flag_comment',
				type: 'post',
				dataType: 'json',
				cache: false,
				data: postData,
				success: function(json) { JS_AB.abradio.commentsHandler._doneFlagComment(json, id); },
				error: function(xhr,err,e) { if(JS_AB.abradio.debug) { alert(err + ' ' + e, 'Critical Error'); }}
			});
		},
		_doneFlagComment : function(json, id) {
			if (JS_AB.abradio.debug)	console.log(json);
			switch (parseInt(json.status.code)) {
				case 200:
					$('#flagLink_' + id).html(json.status.text);
				break;
				case 400:
					$('#flagLink_' + id).html(json.status.text);
				break;
				case 500:
				default:
					if(JS_AB.abradio.debug) { alert(json.status.text); }
				break;
			}
		},
		deleteComment: function(evt) {
			evt.preventDefault();
			if(confirm('Ben je zeker?')) {
				var id = $(this).attr('id').replace('delete_comment_', '');
				$.ajax({ url: '/ajax.php?module=abradio&action=delete_comment', 
						type: 'post', dataType: 'json', cache: false, 
						data: 'comment_id='+ id,
						success: function(json) { $('#comment-' + id).slideUp(); }
				});
			}
		},
		_eoo : true
	},
	JS_AB.abradio.player = {
	    init: function() {
			if (!$('body').hasClass('accessibility')) {
		    	$('#actualControls').hide();
			} else {
				$('#actualControls').show();
			}

			$('#playerControls a').bind('click', function(evt){
			    evt.preventDefault();
		    	$('#actualControls').toggle();
			});
	    },
	    _eoo: true
	}
	$(document).ready(function() { JS_AB.abradio.init(); });
})(jQuery)
