// Fix background image flickering in IE
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) { }

// IE6 alpha PNG support
// http://www.dillerdesign.com/experiment/DD_belatedPNG/
var DD_belatedPNG;
if (DD_belatedPNG) {
	DD_belatedPNG.fix('body.home #Content .rightColumn a.newsFeed, body.home #Content .rightColumn a.facebook, body.home #Content .rightColumn a.twitter, body.home #Content .rightColumn a.linkedIn');
}

/* begin - Navigation Primary */
var NavigationPrimary = {
  init: function() {    
		var c = this;
		var n = $('#NavigationPrimary');
		
		// Use bgiframe to fix dropdowns over selects if it's available.
		$.fn.bgiframe && $('li', n).bgiframe();

		// Mark parent elements so that we can style them with CSS
		$('> li', n).each(function() { c.markParents(this); });
  	
  	// Use the hoverintent plugin if it's available
  	if ($.fn.hoverIntent)
    {
  	  $('li', n).hoverIntent( {
  	    sensitivity: 1,
  	    over: this.mouseenter,
  	    out: this.mouseleave,
  	    timeout: 100
  	  });
    }
  	else
  	  $('li', n).hover(this.mouseenter, this.mouseleave);
  },
  markParents: function(li) {
    var c = this;
    if ($('> ul', li).size() > 0)
	    $(li).addClass('parent').find('> a').addClass('parent').end().find('> ul li').each( function() { c.markParents(this); });
	},
	mouseenter: function() { $(this).addClass('over').children('a').addClass('over'); },
	mouseleave: function() { $(this).removeClass('over').children('a').removeClass('over'); }
	
};
$(function() { NavigationPrimary.init(); });
/* end - Primary Navigation */

/* begin - Navigation Secondary */
var NavigationSecondary = {
  init: function() {    
		var c = this;
		var n = $('#NavigationSecondary');
		
		// Use bgiframe to fix dropdowns over selects if it's available.
		$.fn.bgiframe && $('li', n).bgiframe();

		// Mark parent elements so that we can style them with CSS
		$('> li', n).each(function() { c.markParents(this); });
  	
  	// Use the hoverintent plugin if it's available
  	if ($.fn.hoverIntent)
    {
  	  $('li', n).hoverIntent( {
  	    sensitivity: 1,
  	    over: this.mouseenter,
  	    out: this.mouseleave,
  	    timeout: 100
  	  });
    }
  	else
  	  $('li', n).hover(this.mouseenter, this.mouseleave);
  },
  markParents: function(li) {
    var c = this;
    if ($('> ul', li).size() > 0)
	    $(li).addClass('parent').find('> a').addClass('parent').end().find('> ul li').each( function() { c.markParents(this); });
	},
	mouseenter: function() { $(this).addClass('over').children('a').addClass('over'); },
	mouseleave: function() { $(this).removeClass('over').children('a').removeClass('over'); }
	
};
$(function() { NavigationSecondary.init(); });

// Assign the "first" class to the first secondary nav item
$(document).ready(function(){
	$('#NavigationSecondary ul li:first').addClass("first");
});

/* end - Secondary Navigation */


/* begin - Activate Tabs */
$(document).ready(function(){
	$("#ProductTabsContainer").tabs();
	$("#Library").tabs();
});



/* end - Activate Tabs */

/* begin - Preload images */
if ($.xLazyLoader) {
	$.xLazyLoader({
		image: ['../images/home/semiconductor-over.gif', 
						'../images/home/life-science-over.gif', 
						'../images/home/multi-length-scale-imaging-over.gif', 
						'../images/home/oil-gas-over.gif', 
						'../images/home/advanced-materials-over.gif',
						'../images/home/SemiconductorContent-bg.gif', 
						'../images/home/LifeScienceContent-bg.gif', 
						'../images/home/ScaleImagingContent-bg.gif', 
						'../images/home/OilGasContent-bg.gif', 
						'../images/home/AdvancedMaterialsContent-bg.gif'
					 ]
	});
}
/* end - Preload images */


$(function() {
	an_postPrintHandler = function() {
  	// "Unmark" the tabs as tabs so that the tabs JS doesn't hide the content.
  	$('ul.tabs').hide();
		$('.ui-tabs-hide').removeClass('ui-tabs-hide');

		// Convert h2 tags to h3 in the tab content
		$('#ProductTabsContainer, #Library').find('h2').each(function(i) {
			var text = $(this).text();
			$(this).replaceWith('<h3 class="paddingBottom">' + text + '</h3>');
		});
		
		// tab navigation not in a tabHeader should be inserted before it's content
		$('ul.tabs').each(function(i) {
			$(this).find('a').each(function(j) {
				$(this.hash).prepend('<h2>' + $(this).text() + '</h2>');
			});
		});
		if (window.print && jQuery.getURLParam("print")==1) {
      setTimeout("window.print()", 500); // dialog on demand - delayed so that page loads before dialog appears
		}
  };
	
	if ($.fn.popupwindow)
  	$('#PrintLink').popupwindow(); 
	if ($.fn.printPage) {
	  $('#ContentChannel a').not($('ul.tabs a'))
		                    .printPage({ // Footnote the content
													postPrintHandler: an_postPrintHandler,
													footnoteLinks: false
	                      });
	}
	//$('#PrintLink').popupwindow(); // Make the print page link open in a new window
	//$('#ContentChannel a').printPage(); // Footnote the content
});

/* begin - open new window for external links and pdfs */
	$(function() {
		$('#WrapperTop a[href^=http://]')
			.not($('a.noPopup'))
			.not($('a[href$=.pdf]'))
			.popupwindow(); // Fully qualified links
		$('#WrapperTop a[href^=https://]').not($('a[href$=.pdf]')).popupwindow(); // Secure HTTP connection links
		$('#WrapperTop a[href$=.pdf]').popupwindow(); // PDFs by URL
		$('#WrapperTop a.popup').popupwindow(); // specific internal links
		
	});
/* end - open new window for external links and pdfs */

/* begin - Image Gallery */
var ImageGallery = {
	init: function() {
		$('a.imageGallery').click(ImageGallery.click);
	},
	showDialog: function() {
		var $div = $('#ImageGalleryModal');
		if ($div.length == 0)
			return;
		
		$div.dialog({
					draggable: false,
					modal: true,
					resizable: false,
					width: 900,
					// title: 'Image Gallery',
					closeText: 'Close Window',
					close: function(event, ui) {
						$div.remove();
					},
					open: function(event, ui) {
							$('#GalleryContainer').tabs();
							//ui.dialog.find('.ui-dialog-titlebar').prepend('<div class="galleryNav"><ul class="tabs"><li id="AdvancedMatrialsPubGallery"><a href="#AdvancedMaterialsGallery">Advanced Materials</a></li><li id="LifeSciencePubGallery"><a href="#LifeScienceGallery">Life Science</a></li><li id="OilGasPubGallery"><a href="#OilGasGallery">Oil &amp; Gas</a></li><li id="SemiconductorPubGallery"><a href="#SemiconductorGallery">Semiconductor</a></li></ul></div>');
					}
		});
	},
	click: function(event) {
		var cookie_name = 'ImageGallery'; 
		event.preventDefault();
		var $div = $('#ImageGalleryModal');
		if ($div.length == 0) {
			$div = $('<div style="display: none;" id="ImageGalleryModal"></div>').appendTo('body');
			if ($.cookie(cookie_name)) {
				$div.load('/image-gallery/registered.php?_=' + (new Date()).getTime(), ImageGallery.showDialog);
			} else {
				$div.load('/image-gallery/public.php?_=' + (new Date()).getTime(), ImageGallery.showDialog);
			}
		} else {
			ImageGallery.showDialog();
		}
	}
};
$(function() {
	ImageGallery.init();
});
/* end - Image Gallery */


/* begin - Clear search box text on focus only if default text is present */
$(function() {
	$('#Search .query').focus(function() {
		if (this.value=="Search") {
			this.value = "";
		}
	});
	$('#Search .query').blur(function() {
		if (this.value=="") {
			this.value = "Search";
		}
	});
});
/* end - Clear search box text */