// Plugin to normalize heights of elements
$.fn.autoHeight = function() {
	var max = 0;
	return this.each(function() {
    	max = Math.max(max, $(this).height());
  	}).css("height", max + "px");
};


/*
$(function() {
	$(".pnx-highlight-1").autoHeight();
});
*/


// Wire up search box and global gateway
$(function() {
	
	var $tabs = $(".tabs-container"),
		$accordion = $(".accordion");
	

	if($tabs.length){
		$tabs.tabs();
	}
	
	if($accordion.length){
		$accordion.accordion({header: "h3", collapsible: true, animated: true, active: 2})
							.addClass('ui-accordion-home')
							.children('div').addClass('ui-accordion-panel');
	}
	
						
});

$(function() {
	var globalGateway = $("a.global-gateway"),
		searchTextInput = $("input.search-txt"),
		searchTextDefault = searchTextInput.val();

	searchTextInput
		.blur(function() {
			if (searchTextInput.val() === "") {
				searchTextInput.val( searchTextDefault );
			}
		
			searchTextInput.removeClass("search-txt-active");
		})
		.focus(function() {
			if (searchTextInput.val() === searchTextDefault) {
				searchTextInput.val( "" );
			}
		
			searchTextInput.addClass("search-txt-active");
		})
	
	// Add follow-through URL to global gateway
	globalGateway.attr("href", function() {
		return this.href + "?rtrn=" + window.location.href;
	});
});

// Call me
$(function() {
	var el = $("a.call-me");
	
	// Hijack link
	el.click(function(e) {
		// Add follow-through to end of URL
		var path = el.attr("href") + "&p_url=" + encodeURIComponent( window.location.href );
		
		// Open call-me page in popup window
		PNX.utils.Popup( path, "", {
			width: 825,
			height:600,
			scrollbars: 1,
			resizable: 1
		});
					
		return false;
	});
});


/** Global Header Actions**/
$(function() {
	$('.global-header-nav-primary li').hover(function() {
		// Stuff to do when the mouse enters the element;
		$(this).addClass('global-header-nav-primary-hover')
		.find('.global-header-nav-dropdown').show();
	}, function() {
		// Stuff to do when the mouse leaves the element;
		$(this).removeClass('global-header-nav-primary-hover')
		.find('.global-header-nav-dropdown').hide();
	});
});

// Global gateway
$(function() {
	// The global `countryInfo` is available after pnpr.js is
	// executed, so double-check that race conditions were met.
	if (countryInfo) {
		$(".global-gateway .country")
			.text( countryInfo.local_country_name );
	}
})
