$(document).ready(function(){

	// Create fadeToggle function
	jQuery.fn.fadeToggle = function(speed, easing, callback) { 
   		return this.animate({opacity: 'toggle'}, speed, easing, callback); 
	};
	
	// Hide Resource Sitemap
	$("li a:contains('All')").parent().empty();
	
	// Hide Extra Industries Sitemap
	$(".related.ind").nextAll(".related.ind").remove();
		
	// Apply active state to search inputs
	$("input#keywords, input#resource-key").focus(function () {
         $(this).addClass("focus");
    });
    
    // Apply text to search inputs
	$("input#keywords").focus(function () {
		 if (this.value == 'Search' ) {
		 	$(this).val("");
		 }
    });
    $("input#keywords").blur(function () {
        if (this.value == "" ) {
		 	$(this).val("Search");
		 	$(this).removeClass("focus");
		 }
    });
    $("input#resource-key").focus(function () {
		 if (this.value == 'Search all resources' ) {
		 	$(this).val("");
		 }
    });
    $("input#resource-key").blur(function () {
        if (this.value == "" ) {
		 	$(this).val("Search all resources");
		 	$(this).removeClass("focus");
		 }
    });
    
    // Create popups
    $(".land em img, .dropdown .drop img").click(function(){
		$(".popup").fadeToggle("fast");
	});
         
	// Create first and last items where required
	$('.content p:first, .overview p:first-child, .mission p:first, .history p:first-child, .full-width p:first, .recommended ul li:first-child').addClass('lead');
	
	$('.menu ul li:first-child, #sidebar .connect ul li:first-child').addClass('first');
	$('.menu ul li:nth-child(3), #sidebar .connect ul li:last-child').addClass('last');
	$('#subnav ul li:last-child').addClass('last');
	$('blockquote p:first-child').addClass('first');
	$('.grid ul:first-child').addClass('first');
	$('.grid ul:last-child').addClass('last');
	$('.grid ul li:first-child').addClass('first');
	$('.grid ul li:last-child').addClass('last');
	$('.partners .row .block:last-child').addClass('last');
	$('.material-spotlight .story p:first-child').addClass('first');
	$('#content h2 + p').addClass('lead');
	$('#sidebar form input:first-child').addClass('first');
	$('.side .related:last-child').css({'border-bottom' : 'none', 'margin-bottom' : '4px'});
	$('#news-section .story:last, #stories .story:last').addClass('last');
	$('.locations p:first, .jobs p:first').addClass('intro');
	$('ul.category li:nth-child(3n+1)').addClass('i');
	
	// Article order styling logic
	$("#news:not(.archive) #left .article:nth-child(2)").removeClass('article').addClass('article-latest');
	$("#news:not(.archive) #left .article:nth-child(3), #news:not(.archive) #left .article:nth-child(4)").removeClass('article').addClass('article-recent');
	$("#left .article-recent:nth-child(4)").addClass('border');
	$("#left .article-latest p:nth-child(2), #left .article-latest p:nth-child(3)").addClass('intro');
	
	//$(".archive #left .article:nth-child(2)").removeClass('article-latest').addClass('article');
	//$(".archive #left .article:nth-child(3), .archive #left .article:nth-child(4)").removeClass('article-recent').addClass('article');
	
	// Menu view alls
	/*$('#menu-one span.more a').click(function(){
		$('#menu-one ul li').slideDown("fast");
		$('#menu-one ul li.last').removeClass('last');
		$('#menu-one ul li:last-child').addClass('last');
		return false;
	});
	$('#menu-two span.more a').click(function(){
		$('#menu-two ul li').slideDown("fast");
		$('#menu-two ul li.last').removeClass('last');
		$('#menu-two ul li:last-child').addClass('last');
		return false;
	});
	$('#menu-three span.more a').click(function(){
		$('#menu-three ul li').slideDown("fast");
		$('#menu-three ul li.last').removeClass('last');
		$('#menu-three ul li:last-child').addClass('last');
		return false;
	});*/
	
	// Contact page tab widget
	$('a#email-us-tab').click(function(){
		$('#tabs a').removeClass('active');
		$(this).addClass('active');
		$('#tabs #make-enquiry, #tabs #feedback').hide();
		$('#email-us').show();
		return false;	
	});
	$('a#make-enquiry-tab').click(function(){
		$('#tabs a').removeClass('active');
		$(this).addClass('active');
		$('#tabs #email-us, #tabs #feedback').hide();
		$('#make-enquiry').show();
		return false;	
	});
	$('a#feedback-tab').click(function(){
		$('#tabs a').removeClass('active');
		$(this).addClass('active');
		$('#tabs #email-us, #make-enquiry').hide();
		$('#feedback').show();
		return false;	
	});
	
	// Search results lister magic
	$('#search dl.about').wrapAll('<div class="search-section"></div>');
	$('#search dl.about:first').before('<h4>/ <a href="/about/">About Us</a></h4>');
	
	$('#search dl.products').wrapAll('<div class="search-section"></div>');
	$('#search dl.products:first').before('<h4>/ <a href="/products/">Products</a></h4>');
	
	$('#search dl.solutions').wrapAll('<div class="search-section"></div>');
	$('#search dl.solutions:first').before('<h4>/ <a href="/solutions/">Solutions</a></h4>');
	
	$('#search dl.industries').wrapAll('<div class="search-section"></div>');
	$('#search dl.industries:first').before('<h4>/ <a href="/industries/">Industries</a></h4>');
	
	$('#search dl.home').wrapAll('<div class="search-section"></div>');
	$('#search dl.home:first').before('<h4>/ <a href="/">Home</a></h4>');
	
	$('#search dl.connect').wrapAll('<div class="search-section"></div>');
	$('#search dl.connect:first').before('<h4>/ <a href="/connect/">Connect</a></h4>');
	
	$('#search dl.news').wrapAll('<div class="search-section"></div>');
	$('#search dl.news:first').before('<h4>/ <a href="/about/news/">News</a></h4>');
	
	$('#search dl.resources').wrapAll('<div class="search-section"></div>');
	$('#search dl.resources:first').before('<h4>/ <a href="/connect/resources/">Resources</a></h4>');
	
	// Create job interactions
	$('.jobs a.show').click(function(){
		$(this).parent().parent().children('.description').slideToggle("fast");
		$(this).parent().parent().toggleClass('active');
		return false;
	});
	$('.jobs a.apply').click(function(){
		var jobtitle = $(this).parent().parent().children('div.left').children('h4').html();
		$('#job-apply input.jobtitle').val(jobtitle);
		$('#job-apply span.here').show();
		return false;
	});

});
