/**
 * ===============================================================================================
 * 
 * Mayomo categories.lib.js Base classes definition
 * 
 * Modifications: 20090902 PPA Initial implementation 20090903 PPA REV: check
 * nullabily in private dependend method calls REV: callback new convention name
 * 
 * 20091029     SCP ADD:    closures
 * 
 * TODO: perhaps a better solution is to prefetch all the subcategories 
 * and to dismiss this calls. It will be faster and more reliable. Only
 * the change of language version of the site will refetch the categories
 * 
 * ===============================================================================================
 */
var Categories = {};
(function(c) {

	/**
	 * Categories class implements everything related to the category module
	 * 
	 * @public
	 */
	var Categories = {

		/**
		 * Prevents the default action of the categories link with 'return
		 * false'
		 * 
		 * @private
		 * @type {Boolean}
		 * @default true
		 */
		_preventDefault : true,

		/**
		 * Initializes the categories module, starts listening for
		 * reload-categories event, and fires category-change event on parent
		 * category different from citizen-journalism and it's sub categories
		 * 
		 * @public
		 * @param {object}
		 *            queryString
		 * @return {void}
		 */
		initialize : function(queryString) 
		{
			// bind an event listener for reloading the categories
			$(document).bind('reload-categories', function(e, categoryId) {
				Categories._fetch(categoryId);
			});

			// if not the default view trigger the category change event
			if (1 != $.inArray(queryString.category, [0, 9, 11, 12, 13, 14, 22, 23, 28, 
			                                          24, 25, 31, 34, 27, 38, 36, 26])
            ) {
				$(document).trigger('category-changed', queryString.category);
			}

			// bind event listeners for categories and subcategories
//			$("#categories a:not('.upload')").live('click', Categories._categoriesClick);
			$('#subcategories a').live('click', Categories._subcategoriesClick);
		},

		/**
		 * Fetches the sub categories of a given parent, rebinds the event
		 * listeners for mouse click.
		 * 
		 * @private
		 * @param {int} id
		 * @return {void}
		 */
		_fetch : function(id) 
		{
			// call a category list by giving a parent Id
			// the return string is text (number) fetched to Int
			// than load the subcategories
			$.ajax({
				url      : '/category/parent/id/' + id,
				dataType : 'text',
				success  : function(text) {
					parentId = +text;
					// activate the parent category
					$("#categories ul#tabs li").removeClass('active sprite');
					$("#categories ul#tabs li a").removeClass('li-active sprite');
					$("#categories ul li a[rel='" + parentId + "']").parent().addClass('active sprite');
					$("#categories ul li a[rel='" + parentId + "']").addClass('li-active sprite');
//					Categories._loadSubcategories(parentId);
				}
			});
		},

		/**
		 * Load the subcategories for a given parent category id
		 * 
		 * @private
		 * @param {Int} parentId
		 * @return {Void}
		 */
/*
		_loadSubcategories : function(parentId) 
		{
			// prepare the string for a sub category list
			queryString = '/category/list-sub/datatype/json/';

			if (parentId)
				queryString += 'parent/' + parentId + '/';

			$.ajax({
				dataType : 'json',
				url      : queryString,
				success  : function(data) {

					$('#subcategories').empty();
					
					for (var i = 0; i < data.length; i++) {
						$('#subcategories').append('|<a href="/#/category=' 
						  + data[i].Id 
						  + '/" rel="' 
						  + data[i].Id 
						  + '" ' 
						  + (BaseController.getQueryString().category == data[i].Id ? 'class="active sprite"' : '') 
						  + '>'
						  + data[i].Category 
						  + '</a>');
					}

					$('#subcategories').html($('#subcategories').html().substr(1));
				}
			});
		},
*/

		/**
		 * onClick handler on main category
		 * 
		 * @private
		 * @param {void}
		 * @return {Boolean}
		 */
/*
		_categoriesClick : function() 
		{
			var parentId = $(this).attr("rel");

			if (!parentId) {
				return true;
			}

			$('#subcategories').empty().html('Loading ...');

			$("#categories ul#tabs li").removeClass('active sprite');
			$('#categories ul#tabs li a').removeClass('li-active sprite');

			$(this).parent().addClass("active sprite");
			$(this).addClass("li-active sprite");

			BaseController.setQueryString({
				category : parentId
			});
			BaseController.rewriteUrl();

			$(document).trigger('category-changed', [parentId]);

			return !Categories._preventDefault;
		},
*/

		/**
		 * onClick handler on sub category
		 * 
		 * @private
		 * @param {void}
		 * @return {boolean}
		 */
		_subcategoriesClick : function() 
		{
			var id = $(this).attr("rel");

			if (!id) {
				return;
			}
			
			$("#subcategories *").removeClass('active');
			$(this).addClass('active');

            BaseController.setQueryString({category : id});

			BaseController.rewriteUrl();

			$(document).trigger('category-changed', [id]);

			return !Categories._preventDefault;
		},

		/**
		 * Enables the default action of categories links
		 * 
		 * @public
		 * @param {void}
		 * @return {void}
		 */
		disablePreventDefault : function() 
		{
			Categories._preventDefault = false;
		}
	};

	// export to public
	c.initialize            = Categories.initialize;
	c.disablePreventDefault = Categories.disablePreventDefault;

})(Categories);
