 var IMG_PATH = "/globals/style-3.0/images/search/";
 var SEARCH_URL = "/globals/style-3.0/programming/php/search-proxy.php";

 var COMPONENT_SITES= new Array();
     COMPONENT_SITES['www.tamhsc.edu']="Health Science Center";
     COMPONENT_SITES['bcd.tamhsc.edu']="Baylor College of Dentistry";
     COMPONENT_SITES['medicine.tamhsc.edu']="College of Medicine";
     COMPONENT_SITES['nursing.tamhsc.edu']="College of Nursing";	
     COMPONENT_SITES['gsbs.tamhsc.edu']="Graduate School of Biomedical Sciences";	 
     COMPONENT_SITES['ibt.tamhsc.edu']="Institute of Biosciences &amp; Technology";
     COMPONENT_SITES['pharmacy.tamhsc.edu']="Rangel College of Pharmacy";
     COMPONENT_SITES['srph.tamhsc.edu']="School of Rural Public Health";
	  
  var COMPONENT_SITES_IMGS = new Array();
      COMPONENT_SITES_IMGS['medicine.tamhsc.edu']= IMG_PATH + "com-icon.png";
      COMPONENT_SITES_IMGS['bcd.tamhsc.edu']= IMG_PATH + "bcd-icon.png";
      COMPONENT_SITES_IMGS['www.tamhsc.edu']= IMG_PATH + "hsc-icon.png";
      COMPONENT_SITES_IMGS['nursing.tamhsc.edu']= IMG_PATH + "con-icon.png";
      COMPONENT_SITES_IMGS['gsbs.tamhsc.edu']= IMG_PATH + "gsbs-icon.png";
      COMPONENT_SITES_IMGS['ibt.tamhsc.edu']= IMG_PATH + "ibt-icon.png";
      COMPONENT_SITES_IMGS['pharmacy.tamhsc.edu']= IMG_PATH + "cop-icon.png";
      COMPONENT_SITES_IMGS['srph.tamhsc.edu']= IMG_PATH + "srph-icon.png";
	  
  var queried = false;
   
   
  $(document).ready(function(){
		
	hideResultsTab();
	
	
	$('input.searchField').bind('focus click',function() {
		$(this).addClass("searchFocused");
	});
	
	$('input.searchButton').bind('mouseover mouseenter',function() {
		$(this).attr("src", IMG_PATH + "go-hover.png");										 
	});
	
	$('input.searchButton').bind('mouseout mouseleave',function() {
		$(this).attr("src", IMG_PATH + "go.png");												 
	});
	
	$('input.searchField').bind('keyup',function() {
			QuickSearch.query = $(this).val();
			QuickSearch.componentSites = jQuery.extend({}, COMPONENT_SITES);
			QuickSearch.interceptKeypress();	
	});
	
	$('input.searchField').bind('blur',function() {
  		setTimeout("fadeResultsTab()",1000);
		if($(this).val().length == 0)
			$(this).removeClass("searchFocused");
	});
	
  });
  
  function hideResultsTab(){	 
	$('#search-drop-results').hide().html("");
	$('#search-drop-tab').addClass("hidden");
  }
  
   function fadeResultsTab(){
	$('#search-drop-results').fadeOut("fast",function(){
		$(this).html("");
	});
	$('#search-drop-tab').addClass("hidden");
  }
  
  function showResultsTab(){
	$('#search-drop-results').fadeIn();
	$('#search-drop-tab').removeClass("hidden");	
  }
  
  
  QuickSearch = {
 
 	   query : "",
	   interval : 1000,	   
	   lastKeypress : null,
	   componentSites : null,
	   interceptKeypress : function() {
		  this.lastKeypress = new Date().getTime();
		  var that = this;
		  setTimeout(function() {
			 var currentTime = new Date().getTime();
			 if(currentTime - that.lastKeypress > that.interval) {
				 if(queried == false){
					that.quickSearch();
					queried = true;
				 }					
			 }else{ queried = false; }
		  }, that.interval + 100);
	   },
	   
	   quickSearch : function(){
		   
		   $("#qsearch_viewall").remove();
		   $('#search-drop-results').html("");
		   
		   if(this.query.length == 0 ){	
				fadeResultsTab();
			}else{
				lastquery = $(this).val();				
				// ensure current component site is on top by searching for it first
				// all other results are posted in the order they arrive
				if(COMPONENT_SITES[window.location.hostname] != null)
				{					
					this.siteSearch(window.location.hostname);
				}else{				
					for(var site in this.componentSites){ //only send first
						this.siteSearch(site);
						break;	
					}
				}
				showResultsTab();
				
			}
	   },
	   
	   siteSearch : function(site){
		 	var results =  $('#search-drop-results');
			var siteId = 'qsearch_' + site.replace(/\./g,'');		
			
			var that = this;
			
			$.ajax({
				type: "GET",
				url: SEARCH_URL + "?q=" + this.query + "&sitesearch=" + site,
				dataType: "xml",
				//error: function(xmlrequest,message){alert(message);},
				success: function(xml) {
							

					if($(results).find("#results_title").length == 0 )
						$(results).append("<h3 id='results_title'>Top Results</h3>");			

					$(xml).find('R').each(function(){
						
						
						if($(results).find("#" + siteId).length == 0 )
							$(results).append("<h3 class='title' id='" + siteId + "'>" + COMPONENT_SITES[site] + "</h3>");
										   
						var id = $(this).attr('N');
						var title = $(this).find('T').text();
						var url = $(this).find('U').text();
						var summary = $(this).find('S').text();
						
						queryResultId = siteId + '_qresult_' + id;
						
						if(title.length > 35)
							title = title.substring(0,35) + "..."
						
						htmlResult = '<div id="' + queryResultId + '" class="result"><img src="' + COMPONENT_SITES_IMGS[site] + '" alt="HSC logo"><h4>' + title + '</h4><span>' + summary.substring(0,80) +  '...</span></div>';
						
						if($(results).find("#" + queryResultId).length == 0 ){
							$(results).find("#" + siteId).after(htmlResult);
							$(results).find("#" + queryResultId).click(function(){
								window.location.href = url;
							});
						}
						else{
							$(results).find("#" + queryResultId).replaceWith(htmlResult);
							$(results).find("#" + queryResultId).click(function(){
								window.location.href = url;
							});
						}
			
					});
					
					if( getLast(that.componentSites) == site ){
						$(results).append('<a id="qsearch_viewall" href="#" class="all">view all search results</a>');
						$(results).find("#qsearch_viewall").click(function(){
							$("#gsaSearchForm").submit();
						});
					}
	
					
					delete that.componentSites[site];									
					
					
					for(var otherSite in that.componentSites){
						that.siteSearch(otherSite);
						break;
					}
									
				}
			});
			
			return true;
	   }
	   
	   
 
  }
  
  function getLast(array){
	var last = null;
	for(var obj in array){
		last = obj;
	}
	return last;
  }

$.fn.makeAbsolute = function(rebase) {
    return this.each(function() {
        var el = $(this);
        var pos = el.position();
        el.css({ position: "absolute",
            top: pos.top, left: pos.left });
        if (rebase)
            el.remove().appendTo("body");
    });
}



$.fn.makeRelative = function(rebase) {
    return this.each(function() {
        var el = $(this);
        el.css({ position: "relative",
            top: 0, left: 0 });
        if (rebase)
            el.remove().appendTo("body");
    });
}


