
// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  arguments.callee = arguments.callee.caller;  
  if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});


// work page filtering

$('#all').addClass('active');

$('#all').click(function() {
   
	  // Scrolls to the top off page on click
	  $.scrollTo( $('.brd_sml'), 100 );
	  
	  $('#the_hub_menu li').find('a').removeClass('active');
	  $(this).addClass('active');
	  
	  
	  $('#work_contents ul li').stop().fadeTo('slow', 0.2).attr('disabled', 'disabled');
	  $('#work_contents ul li.hideli').stop().fadeTo('slow', 1);

      return false;
});

$('#interaction').click(function() {
	
		
      // Scrolls to the top off page on click
	  $.scrollTo( $('.brd_sml'), 100 );
	  
	  $('#the_hub_menu li').find('a').removeClass('active');
	  $(this).addClass('active');
	  
	  $('#work_contents ul li').stop().fadeTo('slow', 0.2);
	  $('#work_contents ul li.interaction').stop().fadeTo('slow', 1);
	
  return false;
});

$('#brand').click(function() {
	
      // Scrolls to the top off page on click
	  $.scrollTo( $('.brd_sml'), 100 );
	  
	  $('#the_hub_menu li').find('a').removeClass('active');
	  $(this).addClass('active');
	  
	  $('#work_contents ul li').stop().fadeTo('slow', 0.2);
	  $('#work_contents ul li.brand').stop().fadeTo('slow', 1);
  return false;
});

$('#product').click(function() {
   
   // Scrolls to the top off page on click
   $.scrollTo( $('.brd_sml'), 100 );
   
   $('#the_hub_menu li').find('a').removeClass('active');
   $(this).addClass('active');
   
   $('#work_contents ul li').stop().fadeTo('slow', 0.2);
   $('#work_contents ul li.product').stop().fadeTo('slow', 1);
  return false;
});

 
	
// function to load elements in sequentially

function fadeResults(){
	
	$('#archive_contents .hideli').each(function(i){
										
		$(this).delay(i*100).fadeIn('slow');
	});
}

$("#search_button").click(function() {
 
$(this).fadeTo('slow', 0, function() {
								   
     $(this).hide();
	 $('.search_form').fadeIn();

});

	
});


function runSearch() {
	
	
	      //alert('running search');
		  //Empty Current Results 
		  $('#archive_contents').empty();
		  
		  //Add Search Loading Magnifier
		  $('#the_hub_search').addClass('');
		  
		  
		  //Catch the value you of the search box, strip the white space and replace with '+' creating the search string
		  var mySearch = $('#q').val().replace(/ /g,'+');
		  
		  
		  
		  
		  //Bind the search string to the sites url to access the sites search engine
		  var newSearch = 'http://www.fauvelkhan.com/?s='+mySearch;
		 
		  //load the new search and only load data from the ajaxLoad ID
		  $('#contentToLoad').empty().load(newSearch+' #ajaxLoad', function(){
			  
			  // Add the values from the list only
			  var $my_html = $('#contentToLoad #ajaxLoad').html();
			  
			  //Add No Results found search response
			  if ($my_html == "") {
				  $my_html = "<li>Sorry no items were found, please try another search.</li>";	
			  }
			  
			  //Append new content to the search results list
			  $('#archive_contents').empty().append($my_html);
			  
			  //Remove Search Loading Magnifier
			  $('#the_hub_search').addClass('');
			  
			  fadeResults();
  
		  });			  
		  
}



var runningRequest = false;
var request;
	
	function handleData(data) {	
		$('#results').html(data);
		fadeResults();
			
	}
	
	function fetchData(running) {

		if ( running == true) {
			//alert('aborted');
			request.abort();
			
			runSearch();
				
		} else {
			//alert('running');
			runSearch();
		
		}
		
}
	
//Normal Search
//Bind function to the submit of the form preventing normal behaviour
$('#hubSearch').submit(function() {
			
			runSearch();  
	        // Disable normal functionality		
			
	return false;
	
});


