//
// pnfsearch.js
// Paul Dragicevich, 29/05/2009
//
// Dynamically creates a search box with what the user was looking for.
//
$(function(){
	PNFSearch.Init();
});
var PNFSearch = {
	ItemRE: /item\=.*\/([^&]+?)(\..*)?&/i,
	// Set everything up.
	Init: function(){
		$('#homeContent tbody:first').prepend('<tr id="PNFSsearchbox"><td width="170" height="33" align="left" valign="bottom"><h6>Try a search</h6></td><td width="230" height="33" align="left" valign="bottom"><input type="text" id="PNFSquery" /><input type="button" value="Search" id="PNFSgo" /></td></tr>');
		$('#PNFSgo').click(PNFSearch.Go);
		PNFSearch.ParseQS();
	},
	// Execute the search.
	Go: function(){
		var searchUrl = '/Global/Search/SearchResult.aspx?search=' + escape($('#PNFSquery').val());
		window.location = searchUrl;
		return false;
	},
	// Parse the QS for the page they were trying to access.
	// Use to prepopulate the search box.
	ParseQS: function(){
	   var s = unescape(window.location.search);
	   var itemResult = PNFSearch.ItemRE.exec(s);
		if (itemResult){
		   var term = itemResult[1];
		   term = term.replace('-',' ','g');
		   term = term.replace('_',' ','g');
		   term = term.replace(/\s+/,' ','g');
			$('#PNFSquery').val(term);
		}
	}
};






