$(function () {
  $('.flyout').each(function () {
    var distance = -10;
    var time = 250;
    var hideDelay = 100;
    var hideDelayTimer = null;
    var beingShown = false;
    var shown = false;
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);
    $([trigger.get(0), popup.get(0)]).mouseover(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;
		popup.css({
			position: 'absolute',
			top: 280,
			left: trigger.offset().left -60,
			display: 'block'
        })
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          beingShown = false;
          shown = true;
        });
      }
    }).mouseout(function () {
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '+=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          shown = false;
          popup.css({'display' : 'none', 'top' : 215 });
        });
      }, hideDelay);
    });
  });
});


$(document).ready(function() {
	$(".rollover img").hover(function() {
		if ($(this).attr("src").indexOf("-off.") > 0) {
			$(this).attr("src", $(this).attr("src").replace("-off.","-on."));
		}
	},function() {
		if ($(this).attr("src").indexOf("-on.") > 0) {
			$(this).attr("src", $(this).attr("src").replace("-on.","-off."));
		}
	});
	if ($("#other-sites-top img").length>0) {
		$("#other-sites-top-ddl li:first").addClass("first-item");
        $("#other-sites-top img").hover(function() {
		var intTop = $("#other-sites-top img").offset().top;
		var intLeft = $("#other-sites-top img").offset().left;
		$("#other-sites-top-ddl").css({ top: intTop, left: intLeft});  
		$("#other-sites-top-ddl").css("display","inline");
		$("#other-sites-top-ddl").mouseout(function() {
		$("#other-sites-top-ddl").css("display","none");
		}).mouseover(function() {
			$("#other-sites-top-ddl").css("display","inline");
		});
		}, function() { });
	}
	// search box focus/blur actions
	$("#search").focus(function() {
		$(this).val((($(this).val() == "Search") ? "" : $(this).val()));
	});
	$("#search").blur(function() {
		$(this).val((($(this).val() == "") ? "Search" : $(this).val()));
	});
	$("#AtoZAlphaList_txtSBISBN").focus(function() {
		$(this).val((($(this).val() == "Search By Product #") ? "" : $(this).val()));
	});
	$("#AtoZAlphaList_txtSBISBN").blur(function() {
		$(this).val((($(this).val() == "") ? "Search By Product #" : $(this).val()));
	});
	$("#nav-tabs td").hover(function() {
		if (window.attachEvent) {
			$(this).addClass("sfHover");		
		}
		var objOffset = $(this).offset();
		$(".sub-nav-bg-bottom",this).css("top",$("#header").height() + "px");
		$(".sub-nav-bg-bottom", this).css("left",objOffset.left + "px");
	},function() {
		if (window.attachEvent) {
			$(this).removeClass("sfHover");
		}
		$(".sub-nav-bg-bottom",this).css("left","-999px");
	});
	var strJumpHTML = ""
	var intJumpCount = 0;
	$(".collapsible .collapse-link").attr("href","javascript:void(0)");
	$(".collapsible h2 .collapse-link").each(function() {
		$(this).attr("name","jumpnav" + intJumpCount);
		$(this).attr("id","jumpnav" + intJumpCount);
		strJumpHTML += "<li><a href=\"javascript:void(0)\" id=\"-jumpnav" + intJumpCount + "\">"+$(this).html()+"</a>";
		if (($("#plcCATemp").length>0) && ($(this).parent().parent().find("h3 .collapse-link").length > 0)) {
			intSubJumpCount = 0;
			strJumpHTML += "<ul style=\"margin-top: 6px;margin-bottom: 0px; padding-bottom: 0px;\">";
			$(this).parent().parent().find("h3 .collapse-link").each(function() {
				$(this).attr("name","jumpnav" + intJumpCount + "-" + intSubJumpCount);
				$(this).attr("id","jumpnav" + intJumpCount + "-" + intSubJumpCount);
				strJumpHTML += "<li style=\"font-size: 11px; margin: 0px; padding: 1px 0px 1px 8px;\"><a href=\"javascript:void(0)\" id=\"-jumpnav" + intJumpCount + "-" + intSubJumpCount + "\">"+$(this).html()+"</a></li>";
				intSubJumpCount++;
			});
			strJumpHTML += "</ul>";
		}
		strJumpHTML += "</li>";
		intJumpCount++;
	});
	$("#jump-nav ul").html(strJumpHTML);
	
	//jump to links
	$("#jump-nav a").click(function() {
		window.scrollTo(0,$("#" + $(this).attr("id").substr(1)).offset().top);
	});
	
	//back to top links
	$(".product-information .collapsible .content .collapsible .content").append("<div class=\"back-to-top\"><a href=\"javascript:void(0);\">Back to Top</a></div>");
	$(".back-to-top a").click(function() {
		window.scrollTo(0,$("#product-detail-body").offset().top);
	});
	
	//rewrite back to top links in Content leafs
	$(".product-information p.blueLinkBoldBig a.blueLinkBoldBig:contains('Back to Top')").text("Back to Content List");
		
	$(".collapsible .collapse-link").click(function() {
		$(this).parent().toggleClass("expanded");
		$(this).parent().parent().find(".content:first").toggle();
	});
	
	//clean up some empty divs
	//$("#LeftNavigation__lbladd1:empty").parent.hide();
	//$("#LeftNavigation__lbladd2:empty").parent.hide();
});


function goExpand(argName) 
{
	c = $("h3>a:equals('" + argName + "')");
	if (!c.parent().hasClass("expanded")) c.parent().addClass("expanded");
	c.parent().parent().find(".content:first").show();
	window.scrollTo(0,c.offset().top);
}

function goExpandAnchor(argName, argExpand) 
{
	//alert('goExpandAnchor(' + argName + ',' + argExpand + ')');
	c = $("a[name='" + argName + "']");
	//alert (c.length);
	
	if (argExpand) {
		b = $(c.parent("div.content"));
		//set styles - prev h3, parent collpsible block
		b.prev("h3").addClass("expanded");	//h3
		b.parent().addClass("expanded");	//collpsible block
		
		//show content
		b.show();
	}
	window.scrollTo(0,c.offset().top);
}


/* legacy function for product page pop-up */
function openimagesPage(openurl) 
	{
		window.open(openurl,'ProductImages','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=550,top=125,left=200');
		return;
	}


function goSearchUS_Submit()
{		
	var element;
	element = document.getElementById("search");		
	var searchText = element.value;
	
	
	if (searchText.length > 0 && searchText != 'Search')
	{
		//Redirect to the Seach Page, passing in the Search Criteria
		window.location.href= "/hai/SearchResults.aspx?Search=" + searchText;
		
	} else {
		alert('Please enter a search string in the Search box.');
	}
}
function goSearchEnterUS_InnerPage(e,ctrl)
{        
	if ( e.keyCode == 13 )
	{
		//alert('search');         
		//e.keyCode = 0;
		e.returnValue = false;
		e.cancelBubble = true;
		//goSearchUS_Inner(ctrl);
		
		var searchText = ctrl.value;
	
		if (searchText.length > 0 && searchText != 'Search')
		{
			//alert('redirect');
			//Redirect to the Seach Page, passing in the Search Criteria
			window.location.href= "/hai/SearchResults.aspx?Search=" + searchText;
			
		} else {
			alert('Please enter a search string in the Search box.');
		}
		//return false;
	}
}

function getContactLink()
{
	var element = document.getElementById('PageFooter_lnkContact').href;

	if (element != "")
	{
		location.replace(element);
		//alert("You entered: " + element);
	}
}