/* 
	Hire and Rental Industry Association Australia
	Javascript functions for HRIA member search
*/
	

// list of nav items
var headNav = {'':'head-nav-01', 'member-services':'head-nav-02', 'job-vacancies':'head-nav-03', 'useful-links':'head-nav-04', 'contact-hria':'head-nav-05', 'privacy-policy':'head-nav-06', 'terms-of-use':'head-nav-07'};

document.observe('dom:loaded', function() { 
	if ($('head-nav')) {
		var qs = document.location.href;
		if (qs != '') {
			var parts = qs.split('/');
			var first = (parts.length > 2) ? parts[3] : "";
			if (headNav[first] != '') {
				if ($(headNav[first])) {
					$(headNav[first]).addClassName('active');
				}
			}
		}
	}
	
	if ($('find')) {
		/*
		new SearchHint("find", "find-hint", "/ajax_search.php", {
			paramName: 'find', 
			indicator: 'find-progress', 
			minChars: 3, 
			callback: function(el, inp) {
				var append = ($('frmSearch').elements['findBy'][1].checked) ? 'who=business&' : 'what=equip&';
				return 'format=ul&whatALL='+encodeURI('... match ')+'&'+append+inp;
			},
			afterUpdateElement: function(inp, sel) {
				if (inp.value != '') {
					if ((inp.value).substr(0, 10) == '... match ') {
						inp.value = (inp.value).substr(10);
					}
				}
			}
		});
		*/
		
		new LocalSearchHint("find", "find-hint", keyALL, { 
			paramName: 'find', 
			indicator: 'find-progress', 
			minChars: 3, 
			choices: 20
		});
	}
	
	if ($('find-bus')) {
		new LocalSearchHint("find-bus", "find-bus-hint", busALL, { 
			paramName: 'find', 
			indicator: 'find-bus-progress', 
			minChars: 3, 
			choices: 20
		});
	}
	
	if ($('find-supplier')) {
		new LocalSearchHint("find-supplier", "find-supplier-hint", suppALL, { 
			paramName: 'find', 
			indicator: 'find-supplier-progress', 
			minChars: 3, 
			choices: 20
		});
	}
	
	if ($('location')) {
		new SearchHint("location", "location-hint", "/ajax_search.php", {
			parameters: 'where=loc&format=ul&', 
			paramName: 'location', 
			indicator: 'location-progress', 
			minChars: 3
		});
	}
	
});

var SearchHint = Class.create(Ajax.Autocompleter, {
	updateChoices: function($super, choices) {
		choices = choices.replace(/<ul>/, "<ul><li>"+this.element.value+"</li>");
		choices += '<div class="hint-tail"></div>';
		$super(choices);
		if (this.element.value != '' && this.entryCount > 0) {
			var searchedFor = this.element.value;
			var searchedPat = new RegExp(searchedFor, "gi");
      for (var i=0; i<this.entryCount; i++) {
        var entry = this.getEntry(i);
				if (entry.innerHTML != '') {
					entry.innerHTML = (entry.innerHTML).replace(searchedPat, function(m) {
						return '<b>'+m+'</b>';
					});
				}
				
      }
    }
	},
	show: function($super) {
		try {
			$(this.update.id+'-top').show();
		} catch(e) {}
    $super();
  },
	hide: function($super) {
		try {
			$(this.update.id+'-top').hide();
		} catch(e) {}
    $super();
  }
});

var LocalSearchHint = Class.create(Autocompleter.Local, {
	updateChoices: function($super, choices) {
		choices = choices.replace(/<ul>/, "<ul><li>"+this.element.value+"</li>");
		choices += '<div class="hint-tail"></div>';
		$super(choices);
	},
	show: function($super) {
		try {
			$(this.update.id+'-top').show();
		} catch(e) {}
    $super();
  },
	hide: function($super) {
		try {
			$(this.update.id+'-top').hide();
		} catch(e) {}
    $super();
  }
});

function updateFinder(h, s) {
	if ($(h) && $(s)) {
		$(h).hide();
		$(s).show();
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
