jQuery(document).ready(function() {

  /* IE6 hover4 fix */
  
  if (jQuery.browser.msie) {
    if(parseInt(jQuery.browser.version) == 6) {
	
	  $('#category_result li a').hover(function(){
		 $(this).addClass('list_over');
	  }, function() {
		 $(this).removeClass('list_over');
	  });
	  
	  $('.submit_button').hover(function(){
		 $(this).addClass('submit_btn_over');
	  }, function() {
		 $(this).removeClass('submit_btn_over');
	  });
		  
	  $('.random_button').hover(function(){
		 $(this).addClass('random_btn_over');
	  }, function() {
		 $(this).removeClass('random_btn_over');
	  });  	
       
	  $('.left_button').hover(function(){
		 $(this).addClass('left_cat_btn_over');
	  }, function() {
		 $(this).removeClass('left_cat_btn_over');
	  });
  
	  $('.middle_button').hover(function(){
		 $(this).addClass('middle_cat_btn_over');
	  }, function() {
		 $(this).removeClass('middle_cat_btn_over');
	  });       
	
	  $('.right_button').hover(function(){
		 $(this).addClass('right_cat_btn_over');
	  }, function() {
		 $(this).removeClass('right_cat_btn_over');
	  });    
  
    }
  } 
  
  
  /* IE6 PNG fix */
  
  $("#header_left_col,#header_right_col,#content_bottom,#footer_top,#footer_bottom").pngfix();
  $("#header_center_col,#content,#footer_content").pngfix({
	   sizingMethod: "scale"
	   });
  
  
  /* Preload button backgrounds to avoid initial flicker */   
  /*
  $.preloadCssImages();
  */
	  
  /* Autocomplete parameters and functions */
  
  $("#author_search").autocomplete("includes/autocomplete.php",{
      multiple: false,
      multipleSeparator: "~",
      mustMatch: true,
      width:150	
  });
  
  $("#author_search").result(function(event, data, formatted) {
	  if (data)
		  $(this).parent().next().find("input").val(data[1]);
  });
  
  function findValueCallback(event, data, formatted) {
	  $("<p>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#match_message");
  }  
  
  
  
  /* Search buttons with chained functions - will fade text to background color, load result,
  and change text back to original color - smoother result than using opacity esp. in IE */
  
  $('#quotesearch_button').click(function(){        
      var textval =$("#quotesearch_text").val();	
      $("#result,#result h6").animate({color: "result_fade_out"},500,				      
      function(){$("#result").load("includes/quotesearch.php",{searchstr: textval},
	      function(){$("#result").animate({color: "result_fade_in"},500);
                         $("#result h6").animate({color: "result_h6_fade_in"},500)
              })
      })        
  });
  
  $('#author_button').click(function(){	
      var authorval =$("#author_search").val();	
      $("#result,#result h6").animate({color: "result_fade_out"},500,				      
      function(){$("#result").load("includes/authorsearch.php",{searchstr: authorval},
	      function(){$("#result").animate({color: "result_fade_in"},500);
                         $("#result h6").animate({color: "result_h6_fade_in"},500)              
	      })
      })  
  });
     
  $('#random_button').click(function(){           
      $("#result,#result h6").animate({color: "result_fade_out"},500,				      
      function(){$("#result").load("includes/random_quote.php",
	      function(){$("#result").animate({color: "result_fade_in"},500);
                         $("#result h6").animate({color: "result_h6_fade_in"},500)              
	      })
      })         
  });   
      
  $('#a-d_button').click(function(){       	
      $("#category_result").animate({color: "cat_fade_out"},200,				      
      function(){$("#category_result").load("includes/categories_a-d.php",
	      function(){$("#category_result").animate({color: "cat_fade_in"},200);
	      })
      })    
  });
      
  $('#e-h_button').click(function(){           
      $("#category_result").animate({color: "cat_fade_out"},200,				      
      function(){$("#category_result").load("includes/categories_e-h.php",
	      function(){$("#category_result").animate({color: "cat_fade_in"},200)
	      })
      })     
  });
  
  $('#i-l_button').click(function(){             
       $("#category_result").animate({color: "cat_fade_out"},200,				      
      function(){$("#category_result").load("includes/categories_i-l.php",
	      function(){$("#category_result").animate({color: "cat_fade_in"},200)
	      })
      }) 	            
  });
      
  $('#m-p_button').click(function(){       
      $("#category_result").animate({color: "cat_fade_out"},200,				      
      function(){$("#category_result").load("includes/categories_m-p.php",
	      function(){$("#category_result").animate({color: "cat_fade_in"},200)
	      })
      })       
  });   
      
  $('#q-t_button').click(function(){       
	    $("#category_result").animate({color: "cat_fade_out"},200,				      
      function(){$("#category_result").load("includes/categories_q-t.php",
	      function(){$("#category_result").animate({color: "cat_fade_in"},200)
	      })
      }) 	  
  });
  
  $('#u-z_button').click(function(){       
      $("#category_result").animate({color: "cat_fade_out"},200,				      
      function(){$("#category_result").load("includes/categories_u-z.php",
	      function(){$("#category_result").animate({color: "cat_fade_in"},200)
	      })
      }) 	   
  });       
  
  $('#category_result a') 
      .livequery('click', function(event) { 
	  var category = event.target.id;	
	  $("#result,#result h6").animate({color: "result_fade_out"},500,				      
	  function(){$("#result").load("includes/category.php",{category: category},
		  function(){$("#result").animate({color: "result_fade_in"},500);
                             $("#result h6").animate({color: "result_h6_fade_in"},500)
		  })
	  }) 	
	  return false; 
      });  
    
  /* Displays loading gif whilst AJAX is working */  
      
  $().ajaxSend(function(r,s){  
  $("#loading_image_container").addClass("showLoadingImage");  
  });  
    
  $().ajaxStop(function(r,s){  
  $("#loading_image_container").removeClass("showLoadingImage");   
  });      
  

});        