var $nlm = jQuery.noConflict();

$nlm(document).ready(function(){

	$nlm("#nojs").hide();
	$nlm("ul.topnav").show();
	$nlm("ul.topnav li:first").remove();


        /* Temporary hacks */
	/* Correct linked image in #footer-usa to usa.gov*/
	$nlm("#footer-usa a").attr("href", "http://www.usa.gov/"); 
        /* Correct the search input box */
        $nlm("#search").attr("name", "query");

	
	$nlm.ajax({
		type: "GET",
				url: "/sitenav.xml",
				dataType: "xml",
				success: function(xml) {
					$nlm(xml).find('nav').each(function(){
					var topnav = $nlm(this).find('topnav').text();
					var content = '<a href="#">'+topnav+'</a><ul class="subnav">';
						$nlm(this).find('subnav').each(function(){
							var subtitle = $nlm(this).find('subtitle').text();
							var location = $nlm(this).find('location').text();
							    content += '<li><a href="'+location+'">'+subtitle+'</a></li>';
						});
					content += '</ul>';
					$nlm('<li></li>').html(content).appendTo('.topnav');
					});
	addMenuBehaviors();  
	}
			});
	
// Clear Search on focus - add text on blur
	$nlm('input.search-input').each(function(){
			$nlm(this)
			.data('default',$nlm(this).val())
			.addClass('inactive')
			.focus(function(){
				$nlm(this).removeClass('inactive');
				if($nlm(this).val()==$nlm(this).data('default')||''){
					$nlm(this).val('');
				}
			})
			.blur(function(){
				var default_val = $nlm(this).data('default');
				if ($nlm(this).val()==''){
					$nlm(this).addClass('inactive');
					$nlm(this).val($nlm(this).data('default'));
				}
			});
		});
});

if("PIE" in window) {  
	$nlm(function() {
		$nlm('#main-body').each(function() {PIE.attach(this);});
		$nlm('.search-input').each(function() {PIE.attach(this);});
		$nlm('ul.topnav').each(function() {PIE.attach(this);});
		$nlm('#barbranding').each(function() {PIE.attach(this);});
		$nlm('#footer').each(function() {PIE.attach(this);});
		$nlm('#footer-sub').each(function() {PIE.attach(this);});
		$nlm('#portal').each(function() {PIE.attach(this);});
	});
}


function addMenuBehaviors() 
{
// Dropdown Menu

$nlm("ul.topnav li a").hover(function() 
{ //When trigger is clicked...
//Following events are applied to the subnav itself (moving subnav up and down)

 $nlm(this).parent().find("ul.subnav").stop(true, true).delay(200).slideDown(200); //Drop down the subnav on click *** Changed to Hover
 
 $nlm(this).parent().hover(function() {
}, function()
{

  $nlm(this).parent().find("ul.subnav").stop(true, true).slideUp('fast'); //When the mouse hovers out of the subnav, move it back up

 });
 
//Following events are applied to the trigger (Hover events for the trigger)
 }).hover(function() 
 {
  $nlm(this).addClass("subhover"); //On hover over, add class "subhover"
 }, function()
 {	//On Hover Out
  $nlm(this).removeClass("subhover"); //On hover out, remove class "subhover"
 });
	
 $nlm("ul.topnav li a").focus(function() { //When trigger is clicked...
//Following events are applied to the subnav itself (moving subnav up and down)
 $nlm(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click *** Changed to Hover
 $nlm(this).parent().focus						(function() {
 }, function()
 {
  $nlm(this).parent().find("ul.subnav").slideUp('fast').hide(); //When the mouse hovers out of the subnav, move it back up
 });

});
}

function openPopup(POPUP)
  {
  var popup =  window.open(POPUP,'popup','resizable=yes,scrollbars=yes,width=400,height=600');
  popup.focus();
  }
  

//
// JavaScript loader
// based on http://friendlybit.com/js/lazy-loading-asyncronous-javascript/
// and http://www.nczonline.net/blog/2009/07/28/the-best-way-to-load-external-javascript/
//
function loadScript(url, callback){

    var script = document.createElement("script");
	script.type = "text/javascript";
        script.async = true;

    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
		script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function(){
            callback();
        };
    }

    script.src = url;
    var x = document.getElementsByTagName("script")[0];
    x.parentNode.insertBefore(script, x);

    return script;

} // loadScript


function loadStyle(url, media)
{
  var css = document.createElement("link");
  css.rel  = "stylesheet";
  css.href = url;
  css.type = "text/css";
  if(media) {
      css.media = media;
  }
  document.getElementsByTagName("head")[0].appendChild(css);

  return css;

} // loadStyle

//
// Load search autocomplete
//
loadScript("/core/jquery-ui/1.8/jquery-ui.min.js", function() {
    loadScript("/core/nlm-autocomplete/1.0/nlm-autocomplete.js", function() {
        $nlm(document).ready(function() {
            nlm.autocomplete.add({element: "#search", dictionary: "nlm-ac-dictionary", width: 0});
        });
    });
});
loadStyle("/core/jquery-ui/1.8/jquery-ui.css");
loadStyle("/core/nlm-autocomplete/1.0/nlm-autocomplete.css");


//
// Load AddThis
//
/*
$nlm(window).load(function() {
    loadScript(location.protocol + "//s7.addthis.com/js/250/addthis_widget.js#username=nationallibraryofmedicine", function() {
        // call AddThis API here rather than rely on class based configuration?
    });
});
*/


