/*jslint laxbreak:true */
/*jslint laxcomma:true */
/*jslint loopfunc:true */
/*jslint strict:true */
/*jslint browser:true */
/*jslint devel:true */

define([
	"underscore"
	, "jquery"
	, "backbone"

	, "text!../../templates/client/browser-list.html"

	// Plugins
	, "jquery-layout"
]
, function (
_
, $
, Backbone

, tmplBrowserList
) {
	"use strict";

	var

	defaultDataTableOpts	= {
		sDom:				"TrtS"
		, bInfo:			false
		, bLengthChange:	false
		, bAutoWidth:		false
		, bFilter:			true
		, bSort:			false
		//, aaSorting:		[[0, "asc"]]
		//, aaSortingFixed:	[[0, "asc"]]

		, bDeferRender:		true
		, bStateSave:		false//true
		, oLanguage:	{
			sEmptyTable:	"&nbsp;"
			, sZeroRecords:	"&nbsp;"
		}

		, oTableTools:	{
			sRowSelect:			"single"
			, sSelectedClass:	"selected"
			, aButtons:			[]
			//, fnRowSelected:	function ( node ) {
			//	console.log( 'The row with ID '+node.id+' was selected', arguments );
			//}
			//, fnRowDeselected:	function ( node ) {
			//	console.log( 'The row with ID '+node.id+' was deselected', arguments );
			//}
		}
/*
		, aaData:			_.map(that.collectionGenres, function (item) {
			return [item];
		})
*/
		, aoColumns:		[{
			sTitle:			"Genres (<span class=\"list-count\">0</span>)"
			, sType:		"string"
		}]

		//, fnDrawCallback:	function (table) {
		//	$(table.nTableWrapper).find('span.list-count').text(table.aiDisplay.length);
		//}
	}

	, Browser	= Backbone.View.extend({
		dataTables:		{}

		, initialize:	function (options) {
			_.bindAll(this
				, "resize"
				, "render"
				, "show"
				, "hide"
			);

			var that	= this
			;

			Browser.__super__.initialize.apply(that, arguments);

			that.render();

			return this;
		}

		, resize:		function () {
			var that	= this
			;

			that.$el.find(".dataTables_scrollBody").height(that.$el.innerHeight() - 20);
			//that.$el.find(".dataTables_scrollBody").width(that.$el.innerWidth());
		}

		, render:		function () {
			var that	= this
			;

			_.each(that.options.collections, function __fnCbInitBrowserList(collection, name) {
				console.log(name);
				var tagName				= "daap_browse"+name.replace(/s$/, '')+"listing"
				,	el					= $(tmplBrowserList)
				,	dataTableOpts		= _.extend(defaultDataTableOpts, {
					sScrollY:			(that.$el.innerHeight() - 20)+"px"//that.initialHeight + "px"

					, aaData:			_.map(collection.get(tagName), function (item) {
						return [item];
					})

					, aoColumns:		[{
						sTitle:			name.ucfirst() + " (<span class=\"list-count\">0</span>)"
						, sType:		"string"
					}]
				})
				;

				that.$el.append(el);
				that.dataTables[name]	= el.find("table:first").dataTable(dataTableOpts);
			});
		}

		, show:		function () {
			var that	= this
			;

			that.$el.show();

			if (Object.keys(that.dataTables).length === 0) {
				that.render();
			} else {
				_.each(that.dataTables, function __fnCbShowBrowserDataTableDraw(dataTable, name) {
					dataTable.fnDraw();
				});
			}

			that.resize();
		}

		, hide:		function() {
			this.$el.hide();
		}
	});

	return Browser;
});