// code to execute on all pages
$(document).ready(function() {
	$("#frmDrugSearch--sSearchPhrase").autocomplete("/ajax/index.cfm?fuseaction=MM2.Ajax.Search.Suggest", {
		width: 200,
		selectFirst: false,
		cacheLength: 1
	});
	
	$("#frmDrugSearch--sSearchPhrase").result(function(event, data, formatted) {
		$("#result").html( !data ? "No match!" : "Selected: " + formatted);
	});
	
	function formatItem(row) {
		return row[0] + " (<strong>id: " + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}
	
	
	// if have javascript - show the radio buttons div for "Find Drugs By"
	$('#findDrugs').removeClass('hide');
	
	// "Find Drugs By" alpha link actions
	$('#alphabet li a').each(function(index) {
		$(this).click(function() {
			var sFindDrugsBy				= $("input[@name='sFindDrugsBy']:checked").val();
			var sFindDrugsURL				= '/Drugs/';
			var sManufacturersURL			= '/Manufacturers/';
			var sGoToURL					= '';
			
			// check if searching by name or manufacturer
			if (sFindDrugsBy == 'Manufacturer') {
				sGoToURL					= sManufacturersURL;
			}
			// go to find drugs by alpha
			else {
				sGoToURL					= sFindDrugsURL;
			}
			
			// add the alpha char to search by
			sGoToURL						= sGoToURL + this.name + '/';
			
			window.location					= sGoToURL;
			
			
			return false;
		});
	});
	
	// button rollover for all browsers   
	$('.button').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	
	//output the user's name in the header if logged in
	if(USER_LOGGEDIN){
		if($("#midNavInner p").length < 1){
			var USERFIRSTNAME = "";
			var USERLASTNAME = "";
			
			if(getCookie("USERFIRSTNAME") != null){
				USERFIRSTNAME = getCookie("USERFIRSTNAME");
			}	
			if(getCookie("USERLASTNAME") != null){
				USERLASTNAME = getCookie("USERLASTNAME");
			}
			
			sHTML	= "<p>You are logged in as <span>"+USERFIRSTNAME+" "+USERLASTNAME+"</span> [<a href=\""+MYHCRURL+"\" title=\"edit my details\">edit my details</a>]</p>"; 
			
			$("#midNavInner").prepend(sHTML);
		}
	}
	//output the user's name in the header if logged in
});