/**
 * @author Tyler Gaw <tylerg@arc90.com>
 *		   Alex Gutierrez <alexg@arc90.com>
 *         
 *
 * Javascript document for Site-wide note tables.
 *
 */

GradeGuru.noteFilters = {
	table: {},
	
	// Options to refine the filtering
	// right now only columns is in use. Set the column index for
	// rating on 'type', 'semester', 'rating'
	opts: {},

	// @param OBJ noteTable - a jQuery dataTable object
	// @param INT defaultRating the minimum rating threshold
	// @param OBJ options - {columns: {'type': int, 'semester': int, 'rating': int}}
	init: function (defaultRating, noteTable, options)
	{
		this.table = noteTable;
		this.opts = options;
		this.selects.init();
		this.ratingSlider.init(defaultRating);
	},
	
	// Filter the note table by the selected criteria
	filter: function (filterOn, input, column)
	{
		var table = GradeGuru.noteFilters.table;
		
		switch (filterOn) {
		case 'rating':
			table.fnFilter("^(<[a-zA-Z<>=\"\' ]*>)?([" + input + "-5](.[0-9])?)|New", column, false);
			break;
		case 'semester':
		case 'note-types':
			table.fnFilter(input, column, false);
			break;
		}
	},

	// The multi-select dropdowns used to filter
	selects: {
		init: function ()
		{
			var optAll = $("div.filter-cell li.drop-menu-items a:contains('All')"),
				$this = GradeGuru.noteFilters.selects;
			
			// Set the first item to active
			//
			optAll.parent().toggleClass('active').attr('rel', 'all-select');
			
			// Click event for each item in the drop down
			//
			$('div.filter-cell li.drop-menu-items li').click(
				function () 
				{	
					var thisMenu = $(this).parent().parent(),
						thisDrop = thisMenu.parent(),
						filterOn = thisDrop.attr('rel');
					
					if ($(this).attr('rel') === 'all-select')
					{
						if (!$(this).hasClass('active'))
						{
							thisDrop.find('li.drop-menu-title').removeClass('active');
							thisMenu.find('li').removeClass('active');
							$(this).toggleClass('active');
						}
					}
					else
					{
						thisMenu.find("li[rel='all-select']").removeClass('active');
						$(this).toggleClass('active');
						
						if (thisMenu.find("li.active").length === 0)
						{
							thisMenu.find("li[rel='all-select']").addClass('active');
							thisDrop.find('li.drop-menu-title').removeClass('active');
						}
						else
						{
							thisDrop.find('li.drop-menu-title').addClass('active');
						}
					}
					
					$this.buildQuery(filterOn);
					return false;
				}
			);
		},

		// Create the filter string and pass it to the filtering function
		buildQuery: function (filterOn)
		{
			var filterInput = "", 
				selections = $('ul.drop-menu[rel="' + filterOn + '"]').find('li.active'), 
				column,
				baseObj = GradeGuru.noteFilters;
	
			selections.each(
				function (i)
				{
	
					if ($(this).attr('rel') !== 'all-select')
					{
						filterInput += $(this).text();
	
						if (i !== (selections.length - 1))
						{
							filterInput += "|";
						}
					}
				}
			);
			switch (filterOn) {
			case 'semester':
				column = baseObj.opts.columns.semester;
				break;
			case 'note-types':
				column = baseObj.opts.columns.type;
			}
			
			baseObj.filter(filterOn, filterInput, column);
		}
	},
	
	// Rating slider to filter by minimum rating
	//
	ratingSlider: {
		
		init: function (rating)
		{
			var baseObj = GradeGuru.noteFilters;
			
			if ($('#rating')[0])
			{
				$('#rating').selectToUISlider(
					{
						labels: 0,
						tooltip: false,
						sliderOptions: {
							range: 'min',
							slide: function (event, ui)
							{
								baseObj.ratingSlider.updateDisplay(ui.value);
							},
							change: function (event, ui)
							{
								baseObj.filter('rating', ui.value, baseObj.opts.columns.rating);
							}
						}
					}
				);
			}
	
			// Each time a star rating is clicked,
			// update the star display based on the selected item
			// and update the position of the handle
			$('#rating-feedback li').click(
				function ()
				{
					var value = $('#rating-feedback li').index(this);
					baseObj.ratingSlider.updateDisplay(value);
					$('#rating').attr('selectedIndex', value).change();
					return false;
				}
			);
	
			// Set a default value
			$('#rating-feedback li:eq(' + rating + ')').trigger('click');
		},
		
		// Update the visual display of the selected minimum rating
		updateDisplay: function (selectedIndex)
		{
			var collection = $('#rating-feedback li');
	
			//Toggle star active class
			$.each(collection,
				function (i, val)
				{
					i <= selectedIndex ? $(this).find('a').addClass('active') : $(this).find('a').removeClass('active');
				}
			);
	
			//Set the slider indicator width
			$('#rating-slider .ui-slider .ui-slider-range-min').width(((selectedIndex / (collection.length - 1)) * 100) + '%');
			
			// Close any open drop down menus
			$('body').trigger('clearDropMenus');
		}
	}
};