var items = []; // Declare item array up here so our custom function can use it as well as doc-ready stuff
var $container = jQuery('#container'); // cache the isotope container item because it is called multiple times


$(document).ready(function(){

      $('#search').defaultValue();

      var $container = $('#container');
      
      $container.isotope({
        itemSelector: '.element'
      });
      
			// stick items into array declared above as a series of objects
        // we could include more than just the name in here, to build up a bigger search 'index'
        $('div.content').each(function(){
                var tmp = {};
                tmp.id = $(this).parent().attr('id');
                tmp.name = ($(this).text().toLowerCase());
                items.push( tmp );
        });
                
        // User types in search box - call our search function and supply lower-case keyword as argument
        $('#search').bind('keyup', function() {
                isotopeSearch( $(this).val().toLowerCase() );
        });
				
				
		
		// filter items when filter link is clicked
$('#filters a').click(function(){
  var selector = $(this).attr('data-filter');
  $container.isotope({ filter: selector });
	
	var $this = $(this);
        // don't proceed if already selected
        if ( $this.hasClass('selected') ) {
          return false;
        }
        var $optionSet = $this.parents('.option-set');
        $optionSet.find('.selected').removeClass('selected');
        $this.addClass('selected');
});



  return false;
      

});


/**
 * Function takes single keyword as argument,
 * checks array of item objects and looks for substring matches between item.name and keyword,
 * if matches are found calls isotope.filter() function on our collection.
 */
function isotopeSearch(kwd)
{
        // reset results arrays
        var matches = [];
        var misses = [];

        $('.element').removeClass('match miss'); // get rid of any existing classes
				$("body p").unhighlight(); //remove custom highlightclass
        $('#noMatches').hide(); // ensure this is always hidden when we start a new query

        if ( (kwd != '') && (kwd.length >= 2) ) { // min 2 chars to execute query:

						
                // loop through items array             
                _.each(items, function(item){
                        if ( item.name.indexOf(kwd) !== -1 ) { // keyword matches element
                                matches.push( $('#'+item.id)[0] );
																
                        } else {
                                misses.push( $('#'+item.id)[0] );
                        }
                });
                
                // add appropriate classes and call isotope.filter
								$(".desc").highlight(kwd); //add custom highlight class
                $(matches).addClass('match');
                $(misses).addClass('miss');
                $container.isotope({ filter: $(matches) }); // isotope.filter will take a jQuery object instead of a class name as an argument - sweet!
                
                if (matches.length == 0) {
                        $('#noMatches').show(); // deal with empty results set
                }
                
        } else {
                // show all if keyword less than 2 chars
                $container.isotope({ filter: '.element' });
        }
				

}
