var gridFilters = function () {
	var _isOpen = false;


	var _initialize = function () {
		$('.filter_check').live('mouseenter', function () {alert('marking');
			var $this = $(this);
			$this.find('.mark').show();
		}).live('mouseleave', function () {
			var $this = $(this);
			if ($this.attr('rel') != 'selected') {
				$this.find('.mark').hide();
			}
		});
	};
	
	
	
	var _setOpen = function (opened) {
		_isOpen = opened;
		if (_isOpen) {
			$(document).scrollTop(0);
			$('#grid_filter_button').hide();
			$('#grid_filter_close_button').show();
			$('#container_grid_filters_overlay').show();
			$('#container_filters_popup').show();
		} else {
			$('#grid_filter_button').show();
			$('#grid_filter_close_button').hide();
			$('#container_grid_filters_overlay').hide();
			$('#container_filters_popup').hide();
		}
	};
	
	// notice the cookieVal value.  that is the value of the user's cookie
	// parse this out - it is split by pipe "|" and these are the pre-selected clip types

	
	var _toggleFilter = function (type, id) {
		if (id == 'all') {
			$(this).find('a').addClass('overSel');
			var filter_check = $('#filter_check_'+ type +'_all');
			if (filter_check.attr('rel') == 'selected') {
				filter_check.removeAttr('rel');
				$('.filter_check').each(function() {
					if($(this).attr('id').indexOf(type) >=0) {
						//alert($(this).attr('id')); 
						$(this).removeAttr('rel');
						$(this).attr('asfd','asdf');
					}
					
				}); 
					cookieVal ='';
			} else {
				$('.filter_check').each(function () {
					var $this = $(this);
					if ($this.attr('id').indexOf(type) >= 0) {
						$this.attr('rel', 'selected');
						cookieVal = cookieVal + $(this).attr('id').replace('filter_check_type_','') + "|";
						cookieVal = cookieVal.replace('all|','');
					}
				});
				
				if (type == 'type') {
					$('.filter_check_prompt').show();
					$('.filter_check_question').show();
				}
			}
		} else {
			var filter_check = $('#filter_check_'+ type +'_'+ id);
			if (filter_check.attr('rel') != 'selected') {
				filter_check.attr('rel', 'selected');
				if (type == 'type' && id == 5) {
					$('.filter_check_prompt').show();
					$('.filter_check_question').show();
				}
				cookieVal = cookieVal + id + "|";
			} else {
				filter_check.removeAttr('rel');
				$('#filter_check_'+ type +'_all').removeAttr('rel');
				if (type == 'type' && id == 5) {
					$('.filter_check_prompt').hide();
					$('.filter_check_question').hide();
				}
				cookieVal = cookieVal.replace(id+'|','');
			}
		}
		_reflectFilters();
	};
	
	
	
	var _clearFilter = function (type, id) {
		var filter_check = $('#filter_check_'+ type +'_'+ id);
		filter_check.removeAttr('rel');
		_reflectFilters();
	};
	
	
	
	var _reflectFilters = function () {
		$('.filter_check').each(function () {
			var $this = $(this);
			if ($this.attr('rel') == 'selected') {
				
				$this.find('a').addClass('overSel');
			} else {
				
					$this.find('a').removeClass('overSel');
			}
		});
	};
	
	
	
	var _submitFilters = function () {
		
		var clipTypeFilters			= [];
		var promptCategoryFilters	= [];
		var questionFilters			= [];
		$('.filter_check').each(function () {
			var $this = $(this);
			if ($this.attr('rel') == 'selected') {
				var typeIdString	= $this.attr('id').replace('filter_check_', '');
				var typeIdArray		= typeIdString.split('_');
				var type			= typeIdArray[0];
				var id				= typeIdArray[1];

				switch (type) {
					case 'type':
						clipTypeFilters.push(id);
						break;
					
					case 'prompt':
						promptCategoryFilters.push(id);
						break;
						
					case 'question':
						questionFilters.push(id);
						break;
				}
			}
		});
		
		// Join clip types into string
		var clipTypeString = clipTypeFilters.join(',');
		
		// Add to form element
		$('#filter_form_clip_types').val(clipTypeString);
		
		// Submit form
		$('#form_vidfilter').submit();
	};
	
	
	return {
		initialize:function () {
			_initialize();
		},
		toggleFilter:function (type, value) {
			_toggleFilter(type, value);
		},
		clearFilter:function (type, value) {
			_clearFilter(type, value);
		},
		submitFilters:function () {
			_submitFilters();
		},
		setOpen:function (opened) {
			_setOpen(opened);
		}
	};
}();
