jQuery.extend(jQuery.expr[':'], {
		exact: function(a,i,m)
		{
			if( (a.textContent||a.innerText||jQuery(a).text()||'') == m[3] )
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	});

function in_array(string, array)
{

	for (i = 0; i < array.length; i++)
	{
		if(array[i] == string)
		{
			return i;
		}
	}
	return -1;
}

function is_cas( term )
{

	if( term.match(/[a-zA-Z']+/g) )
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

//a Javascript Trim function equivalent;
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

//display XML object as a string (for debugging).
function xml2Str(xmlNode)
{
  try {
    // Gecko-based browsers, Safari, Opera.
    return (new XMLSerializer()).serializeToString(xmlNode);
  }
  catch (e) {
    try {
      // Internet Explorer.
      return xmlNode.xml;
    }
    catch (e)
    {//Strange Browser ??
     alert('Xmlserializer not supported');
    }
  }
  return false;
}

function pageload( hash )
{
	if( !hash )
	{
		hash = "";
	}

	facets.hash = hash;

	if( facets.xml )
	{
		set_existing_selections( facets.hash );
		search( facets.hash.trim() );
	}
	return false;
}

//this is supposed to correct history browsing in Opera. In my tests, it does nothing.
history.navigationMode = 'compatible';

$(document).ready(
	function()
	{

		if( cas_array !== undefined )
		{
			for( i = 0; i < cas_array.length; i++ )
			{
				$("td[rel='"+cas_array[i]+"']").each( function()
				{
					$(this).html( "<b>"+$(this).text()+"</b>" );
				});
			}
		}
		/*$('table#search_result_table').dataTable(
		{
			"aaSorting": [ ],
			"sDom": '<"wrapper"ipt<"btm_pag"pl<"btmclear">>>',
			"iDisplayLength": 25,
			"sPaginationType": "full_numbers",
			"oLanguage": {
			"sSearch": "Quick Filter:",
			"sInfo":  "_START_ - _END_ of _TOTAL_"
			}
		});*/

		if ( ( $.browser.msie && parseInt( $.browser.version, 10 ) <= 7 ) || ( $.browser.msie && parseInt( $.browser.version, 10 ) <= 8 && $('table#search_result_table').find("tr").length > 350 ) ) 
		{
		}
		else
		{
			$('table#search_result_table').dataTable(
					{
						"bAutoWidth": false,
						"bSort": true,
						"bSortClasses": false,
						 "bDeferRender": true,
						"aoColumns": [
							{ "sWidth": null, "bSortable": false },
							{ "bVisible": false, "bSortable": false },
							{ "bVisible": false, "bSortable": false }
						],

						"aaSorting": [[1,'asc'], [2,'desc']],
						"sDom": '<"wrapper"ilpt<"btm_pag"p<"btmclear">>>',
						"sPaginationType": "full_numbers",
						"iDisplayLength": 25,
						"oLanguage":
						{

						"sInfo":  "_START_ - _END_ of _TOTAL_"
						}
					});
		}

		$('input#searchfield').keyup( function(e)
		{

			if( e.keyCode == 13 )
			{
				loc = window.location.href;
				hasharray = loc.split('?');
				hashstring= hasharray[1];
				new_term = $("input#searchfield").val();
				new_term = new_term.trim();
				regex = /(,(\s+)(and|on)(\s+)|,(\s)+|(\s)+(and|on)(\s)+)/g;
				new_term = new_term.replace( regex, "::");
				regex = /(analysis|analyses)/gi;
				new_term = new_term.replace( regex, "");
				if( hashstring != undefined && hashstring != "s=" )
				{
					hashstring += "::" + new_term;
				}
				else
				{
					hashstring = "s=" + new_term;
				}

				document.location = hasharray[0]+"?"+hashstring;
				//$.historyLoad(facets.hash);
				return false;
			}
		});
		$('input#addsearchterms').click(function()
		{
			//build_hash();
			//$.historyLoad(facets.hash);

			loc = window.location.href;
				hasharray = loc.split('?');
				hashstring= hasharray[1];
				new_term = $("input#searchfield").val();
				new_term = new_term.trim();
				regex = /(,(\s+)(and|on)(\s+)|,(\s)+|(\s)+(and|on)(\s)+)/g;
				new_term = new_term.replace( regex, "::");
				regex = /(analysis|analyses)/gi;
				new_term = new_term.replace( regex, "");

				if( hashstring != undefined && hashstring != "s=" )
				{
					hashstring += "::" + new_term;
				}
				else
				{
					hashstring = "s=" + new_term;
				}

				document.location = hasharray[0]+"?"+hashstring;
				//$.historyLoad(facets.hash);
				return false;
			return false;
		});
	});

$(".more_toggle").live("hover", function(e)
{
	$(this).css("cursor", "pointer");

	return false;
},
function(e)
{
	$(this).css("cursor","default");
	return false;
});
$(".more_toggle").live("mouseup", function(e)
{
	cgram_num = $(this).attr('rel');
	details_box = $(this).siblings(".more_details");
	toggle = this;

	if( $(details_box).html() == "" )
	{

		$.get('/chromatogram/details', {s:cgram_num}, function( msg )
		{
			if( msg != "" )
			{

				$(toggle).removeClass("view_more").addClass("view_less").siblings(".more_details").html('<img src="/images/cgram/'+cgram_num.toLowerCase()+'_thumb.png"/><div class="details_data">'+msg+'<br/><a href="/chromatogram/view/'+cgram_num+'">View Full Chromatogram</a></div>').show();
			}
		});
	}
	else
	{
		if( $(this).is(".view_less" ) )
		{
			$(details_box).hide();

			$(this).removeClass("view_less").addClass("view_more");
		}
		else
		{
			$(details_box).show();
			$(this).removeClass("view_more").addClass("view_less");
		}
	}
});

