// TODO implement _t()
var i18n = i18n || {};

;(function($) {
	
	/**
	 * Works on a nested unordered list with "category" and "subcategory"
	 * classes on the <li> elements. Adds a "toggle" link to all parents with children,
	 * based on their checkbox selection state. The list is restricted to a two-level (parent>child)
	 * relationship.
	 * 
	 * Note: This behaviour is augmented by SupplierControl.js and DemandCategoryControl.js,
	 * where most of the checkbox magic happens. 
	 */
	$.fn.DemandMap_collapsible = function() { 
		return this.each(function(){
			
			var self = $(this);
			
			var toggleCategory = function(category) {
				if($(category).hasClass('expanded')) hideCategory(category);
				else showCategory(category);
			};
			
			var showCategory = function(category) {
				$(category).find('.subcategory').show();
				$(category).find('.toggle a').html($.i18n._t('COLLAPSE'));
				$(category).addClass('expanded');
			};
			
			var hideCategory = function(category) {
				$(category).find('.subcategory').hide();
				$(category).find('.toggle a').html($.i18n._t('EXPAND'));
				$(category).removeClass('expanded');
			};
			
			// if any children(checkbox) of 'li ul' is/are selected then collapse 
			$(this).find('.category').each(function(){			
				// add "toggle" link if the category has any subcategories
				if($(this).find('.subcategory').length > 0) {
					$(this).children('label').after('<small class="toggle">(<a href="#"></a>)</small>');
				}
				
				if($(this).find('.subcategory :checked').length == 0) hideCategory(this);
				else showCategory(this);
			});
			
			$('.category .toggle', this).livequery('click', function(e) {
				toggleCategory($(e.target).parents('.category'));
				
				return false; 
			});
		
			/**
			 * If a parent category is ticked, select all children and auto-expand
			 * the them.
			 */
			$('.category > .checkboxHolder :checkbox', this).livequery('click', function(e) {
				var eventData = {added: [], removed: []};
				var category = $(e.target).parents('.category');
				var subCheckboxes = category.find('.subcategory :checkbox');
				var subcategories = category.find('.subcategory');
				
				// if its checked, tick all children and expand.
				// otherwise, we assume some children might be ticked
				if($(e.target).is(':checked')) {
					showCategory(category);
					subCheckboxes.attr('checked', 'checked');
					eventData.added = $.makeArray(subcategories.add(category));
				} else {
					hideCategory(category);
					subCheckboxes.removeAttr('checked');
					eventData.removed = $.makeArray(subcategories.add(category));
				}
				
				self.trigger('selected', eventData);
			});
			
			/**
			 * If all subcategories are selected, toggle the containing category checkbox as well.
			 */
			$('.subcategory :checkbox', this).livequery('click', function(e) {
				var eventData = {added: [], removed: []};
				var category = $(this).parents('.category'), subcategory = $(this).parents('.subcategory');
				var categoryCheckbox = category.find(':checkbox:first');
				var unselectedSiblings = subcategory.siblings('.subcategory').andSelf().filter(function() {
					return ($(this).find(':checked').length == 0);
				});

				// if all are checked, the group should be checked as well
				if(unselectedSiblings.length == 0) {
					categoryCheckbox.attr('checked', 'checked');
				} else {
					categoryCheckbox.removeAttr('checked');
					eventData.removed = $.makeArray(category);
				}
				
				if($(this).is(':checked')) eventData.added.push(subcategory);
				else eventData.removed.push(subcategory);
				
				self.trigger('selected', eventData);
			});

		});
	}
})(jQuery);