function setupSearchBox() {
	// Add correct z-indexes
	var zIndex = 999;
	$('#search ul li ul').parent().each(function(index) {
		$(this).css('z-index',zIndex)
		zIndex--;
	});
	
	// Zebra List
	$('#search div ul').each(function() {
		$(this).children('li:even').addClass('odd');
	});
	
	$('#search div ul li').click(function(){
		$('#search div ul').find('ul').delay(50).slideUp('fast');
		$(this).find('ul').slideDown(400);
	});	
}

$(document).ready(function(){

    // Tooltips
    $(".tooltip-link").tooltip({
        offset: [10, -60],
        effect: 'slide',
		relative: true
    });
	
    // Tabs
    $("#tabs ul.tabs").tabs("#tabs div.panes > div");
	
    // Accordion	
    $(".accordion").accordion({
        autoHeight: false,
		collapsible: true,
		active: false
    });

    // Truncate
    $(".truncate").truncatable({
        limit: 160,
        more: '... More +',
        less: true,
        hideText: ' Less'
    });

    // Landing Pages Slideshows
	if($('#landing-slides').length){
		// Tabs slideshow setup
		$(".slide-controls").tabs(".slides > div", {		
			effect: 'fade',
			fadeOutSpeed: 2000,
			fadeInSpeed: 2000,
			rotate: true
		}).slideshow({
			autoplay: true,
			autopause: false,
			clickable: false,
			interval: 8000
		});
		
		// API for tabs slideshow
		var api = $('.slide-controls').data('slideshow');
		
		// When button is clicked (cannot use onClick function of tabs as the slideshow emulates the clicking behaviour.)
        $('#landing-slides .slide-controls span').click(function(){		
            // Stop slideshow
            api.stop();
            // Highlight clicked button
            $('#landing-slides .slide-controls span').removeClass('active');
            $(this).addClass('active');
            // Show overlay for clicked item
			var clickedIndex = $(this).index() + 1;
            $('#landing-slides .slides .slide:nth-child(' + clickedIndex + ') .overlay').fadeIn();
        });
	}
	
    // Blocks image changer
	
	// Hide the anchor when javascript is present
	// because LI becomes link, no need for Anchor to exist
	// just causes issues with double links opening otherwise
	$('.block .block-list li').each(function(){
		var anchorText = $(this).find('a').text();
		$(this).find('a').hide();
		$(this).find('h4').append(anchorText);
	});
	
	// Hover effects
    $('.block .block-list li').hover(function(){	        
        var inMain = (($(this).parents("div#main").length) > 0);
        
        var liIndex = $(this).index();
		$(this).addClass('hovered');

        $(this).parents('.block').find('.block-img img').hide();		
        var chosenImg = $(this).parents('.block').find('.block-img img').get(liIndex);
        $(chosenImg).show();
    },
    function(){
		$(this).removeClass('hovered');
    });
	
	// Click events
    $('.block .block-list li').click(function(){
		var pageUrl = $(this).find('a').attr('href')
		if($(this).find('a').attr('target')=='_blank'){
			window.open(pageUrl);
		} else {
			document.location.href = pageUrl;
		}
    });
	
	// End Blocks image changer
	
    // Spotlight
	if($('#spotlight-company').length){
		$('.picker').show();
		$('#spotlight-company').scrollable({
			circular: false
		});
		var spotapi = $('#spotlight-company').data('scrollable');
		
		// Start slider on random slide
		var listLength = $('.picker ul li ul li').length;
		var numRand = Math.floor(Math.random()*listLength);
		spotapi.seekTo(numRand, 0);
		
		// Move slider to selected slide.
		$('.picker ul li ul li').click(function(){
			var spotPick = $('.picker ul li ul li').index(this);
			spotapi.seekTo(spotPick, 1000);
		});
	}
	
    // Superfish	
    $("ul.superfish").superfish().find('ul').bgIframe({
        opacity:false
    });
	
	// Share This
    stLight.options({
        publisher:'4f49ab62-5460-4de9-a3fb-bc4679961c8b'
    });	
	
    // Fancybox
    $('.fb').fancybox({
		'onComplete': function () {
			if($('.video-js-box').length){
				VideoJS.setupAllWhenReady();
			}		
		 }
	});
	
    // Products Drop-Down
    $('.products-dropdown .expander').click(function(){
        $('.products-dropdown .dropdown').slideDown();
		$('.products-dropdown .dropdown .close').fadeIn();
    });
    $('.products-dropdown .dropdown .close').click(function(){
        $('.products-dropdown .dropdown').slideUp();
		$('.products-dropdown .dropdown .close').fadeOut();
    });
	
	// Product tab inner select
	$('.tab-inner-select').change(function(){
		var selectIndex = $(".tab-inner-select option:selected").index();
		$('.tab-inner-selectview').hide();
		var tabToShow = $('.tab-inner-selectview').get(selectIndex);
		$(tabToShow).show();
	});
	
	// Missing URL
	$('a.noLink').click(function(){
		alert('Sorry, this link currently doesn\'t go anywhere. Please try again later.');
		return false;
	});
	
	// Glossary Highlight
	if($('#glossary').length){
		$('#left-sidebar ul li a').click(function(){
			var selectedID = $(this).attr('href');
			$('.line-blocks .line-block').removeClass('selected');
			$('.line-blocks ' + selectedID + '').addClass('selected');
		});
	}
	
	// Plastic Pipe Plumbing Menu
	$('#ppp-menu .block').hover(function(){
		$(this).addClass('hover');
	},
	function(){
		$(this).removeClass('hover');
	});
	
	// Plastic Piping Installation Instructions uniform height
	if($('.installation-instructions').length){
		var tallestHeight = '';
		var divHeight = '';
		$('.installation-instructions div div').each(function(){
			divHeight = $(this).height();
			if(divHeight>tallestHeight){
				tallestHeight = divHeight;
			}
		});
		$('.installation-instructions div div').css('height',tallestHeight + 'px');
	}
	
	// Plastic Piping Merchants Rollover
	if($('.merchant-items').length){
		$('.merchant-items .thumb').hover(function(){
			$(this).siblings('.zoom').fadeIn();
		},
		function(){
			$(this).siblings('.zoom').fadeOut();
		});
	}
	
	// Group > About > Design Develop Manufacture popups
	if($('#ddm-rollover-popups').length){
		$('#ddm-rollover-popups').hide();
	}
	
	// International Interactive Map
	if($('#international-interactive').length){
		$('#international-interactive a').click(function(){
			var current = $('#polytheme-lite').accordion('option','active');
			if($(this).hasClass('europe')){ selected = 1; }
			if($(this).hasClass('middle-east')){ selected = 2; }
			if($(this).hasClass('rest')){ selected = 3; }
			if(current==selected){
			} else {
				$('#polytheme-lite').accordion('activate',selected);
			}
			return false;
		});		
	}
		
	// SEARCH POLYPIPE POP-UP
	$('#search').hide();
	
	// Open
	$('.search').click( function() {
		// If the 'Search' box contents haven't been loaded, load them and set up event handler for 'close' button
		if ($('#search').html() == "") {
			$('#search').load('/common/search/popup-contents.php', function() {
				setupSearchBox();
				
				$('#search .close').click( function() {
					$('#search').fadeOut();
				});
				
				$('#search').fadeIn();
			});
		}
		// Otherwise, contents are already loaded - just show it
		else {
			$('#search').fadeIn();
		}
	});
	// Close
	
	// MORE KEYWORDS
	
	// Open Keywords
	$('.slidedown-keywords').click( function() {					
		$('#keywords-slide').slideDown();		
		return false;
	});
	
	// Add correct z-indexes
	var zIndex = 999;
	$('#keywords-slide ul li ul').parent().each(function(index) {
		$(this).css('z-index',zIndex)
		zIndex--;
	});
	
	// Zebra List
	$('#keywords-slide ul').each(function() {
		$(this).children('li:even').addClass('odd');
	});
	
	// Ul li animation
	$('#keywords-slide ul li').click(function(){
		$('#keywords-slide ul').find('ul').delay(50).slideUp('fast');
		$(this).find('ul').slideDown(400);
	});
	
	// Close keywords
	$('#keywords-slide .close').click( function() {
		$('#keywords-slide ul').find('ul').delay(50).slideUp('fast');
		$('#keywords-slide').slideUp();			
	});
});
