jQuery.fn.highlight = function (str)
{    
	var regex = new RegExp(str, "g\i");    
	return this.each(function ()
	{        
		this.innerHTML = this.innerHTML.replace(
           	regex,
                "<span class='highlight'>" + str + "</span>"
                );
         });
};


jQuery.fn.removeHighlight = function() {
 return this.find("span.highlight").each(function() {
  this.parentNode.firstChild.nodeName;
  with (this.parentNode) {
   replaceChild(this.firstChild, this);
   normalize();
  }
 }).end();
};


$(document).ready(function() {
	
	
	//default each row to visible
	$('#schedule .months').addClass('visible');
	
	//overrides CSS display:none property
	//so only users w/ JS will see the
	//filter box
	$('#filterBox').show();
      	$('#schedule').removeHighlight();
	
	$('#schedule h3').each(function(){
		$('#monthFilter').append("<option value='"+$(this).html() +"'>"+$(this).html() +"</option>");
	});
	
	$('#monthFilter').bind("change keyup", function(event) {
		//if esc is pressed or nothing is entered
    		if ($(this).val() == '') {
      			$('#schedule').removeHighlight();
    			$('#stateFilter').val('');
    			$('.noresults').hide().removeClass('visible');
    			$('.noresults-state').hide().removeClass('visible');
	      		$('#schedule .months').removeClass('visible').show().addClass('visible');
		} else {
			$('.noresults').hide().removeClass('visible');
    			$('.noresults-state').hide().removeClass('visible');
		      	$('#schedule .months').removeClass('visible').show().addClass('visible');
		      	$('#schedule .months .event').removeClass('visible').show().addClass('visible');
			$('#keywordFilter').val('');
    			$('#stateFilter').val('');
      			$('#schedule').removeHighlight();
      			filter('#schedule .months h3', $(this).val());
    		}
	});


	$('#stateFilter').bind("change keyup", function(event) {
		//if esc is pressed or nothing is entered
    		if ($(this).val() == '') {
      			$('#schedule').removeHighlight();
    			$('#monthFilter').val('');
    			$('.noresults').hide().removeClass('visible');
    			$('.noresults-state').hide().removeClass('visible');
    			$('.noresults-state').hide().removeClass('visible');
		      	$('#schedule .months').removeClass('visible').show().addClass('visible');
		      	$('#schedule .months .event').removeClass('visible').show().addClass('visible');
		} else {
			$('.noresults').hide().removeClass('visible');
    			$('.noresults-state').hide().removeClass('visible');
		      	$('#schedule .months').removeClass('visible').show().addClass('visible');
		      	$('#schedule .months .event').removeClass('visible').show().addClass('visible');
			$('#keywordFilter').val('');
    			$('#monthFilter').val('');
      			$('#schedule').removeHighlight();
      			stateFilter('#schedule .months .event .state', $(this).val());
    		}
	});


	
	$('#keywordFilter').keyup(function(event) {
		if (event.keyCode == 27 || $(this).val() == '') {
			$(this).val('');
      			$('#schedule').removeHighlight();
      			window.location.reload();
    			$('#monthFilter').val('');
    			$('#stateFilter').val('');	
		      	$('#schedule .months').removeClass('visible').show().addClass('visible');
		      	$('#schedule .months .event').removeClass('visible').show().addClass('visible');
    			$('.noresults-state').hide().removeClass('visible');
		      	$('.noresults').hide().removeClass('visible')
    		
    		} else {
    		$('.noresults').hide().removeClass('visible')
    		$('.noresults-state').hide().removeClass('visible');
    		$('#stateFilter').val('');	
    		$('#monthFilter').val('');
      		$('#schedule').removeHighlight();
      		keywordFilter('#schedule .months .event', $(this).val(), $(this).val());
    	}

	});
	
});


//filter results based on query
function filter(selector, query) {
  
  $(selector).each(function() {
    ($(this).text().search(new RegExp(query, "i")) < 0) ? $(this).parent().hide().removeClass('visible') : $(this).parent().show().addClass('visible');
  });
}

function stateFilter(selector, query) {
  $(selector).each(function() {
    ($(this).text().search(new RegExp(query, "i")) < 0) ? $(this).parents('.event').hide().removeClass('visible') : $(this).parents('.event').show().addClass('visible');  
  });
  
  $(".event")
    .filter(function(){
      return this.style.display == 'none';
    })
    .parent()
    .not($('.event').filter(function(){
      return this.style.display != 'none';
    }).parent())
    .css('display', 'none');

     if($('#schedule .months .event .state').text().search(new RegExp(query, "i"))<0){
	     	$('.noresults-state').show().addClass('visible');
	}

}

function keywordFilter(selector, query, highlights) {
	query	=	$.trim(query); //trim white space
	highlights	=$.trim(highlights); //trim white space



  $(selector).each(function() {
    ($(this).text().search(new RegExp(query, "i")) < 0) ? $(this).hide().removeClass('visible') : $(this).show().addClass('visible');
  });
  
    $('.months').each(function() {
    ($(this).text().search(new RegExp(query, "i")) < 0) ? $(this).hide().removeClass('visible') : $(this).show().addClass('visible');
  });
  
  $('#schedule').each(function() {
     ($(this).text().search(new RegExp(query, "i")) < 0) ? $('.noresults').show().addClass('visible') : $('.noresults').hide().removeClass('visible');
});

if (query.length >2) {
                        $('#schedule').highlight(highlights); 
}

}

