// Broadband.gov Quality Test

if ($('.qt').length > 0) {
	$('body').attr('id', $('#availableLabel').length > 0 ? 'no-connection' : 'quality-test');
	// preload "loading" image used on test pages:
	var imgLoading = new Image();
	imgLoading.src = '/images/ico_loading-white.gif';
}

// Tabs:
var $tabLinks = $('#tabs ul a'); // links to toggle the tabs
var $tabsWrapper = $('#tabs-wrapper'); // container for the tabs
var $tabs = $('#tabs-wrapper div'); // the individual tab containers

$tabs.addClass('tabs-closed'); // position the tabs offscreen to "hide" them
$tabsWrapper.css({ // style the tabs container to have a height of zero and overflow of hidden
	height: '0',
	overflow: 'hidden'
});

$tabLinks.click(function(){ // when each link is clicked, do the following:
	var $selTab = $tabs.filter(this.hash); // the tab targeted by the link
	if ( $(this).hasClass('selected') ) { // if the tab is already shown, hide it
		$tabsWrapper.stop().animate({height: '0'}, 100, function(){
			$selTab.addClass('tabs-closed');
			if (window != window.top) self.parent.sizeIframeBQT();
		});
		$(this).removeClass('selected');
	} else { // show the tab
		$tabs.addClass('tabs-closed');
		$tabLinks.removeClass('selected');
		$(this).addClass('selected');
		$selTab.removeClass('tabs-closed');
		$tabsWrapper.stop().animate({height: $selTab.height() + parseFloat($selTab.css('padding-top')) + parseFloat($selTab.css('padding-bottom'))}, 100, function(){
			$('#content').removeClass('condensed');
			if (window != window.top) self.parent.sizeIframeBQT();
		});
	}
	return false;
});

// function to show the "About" tab (e.g., after test completes):
function showAboutTab() {
	$('.tabs-nav a:contains("About")').not('.selected').click();
}
// end Tabs

// qualitytest (page-specific):
if (jQuery('body#quality-test').length) {
	/* The following was causing issues in IE due to security settings:
	if(!navigator.javaEnabled()) {
		$('.submit input').fadeTo(200, 0.25, function() {
			$(this).attr('disabled', 'true').css('cursor','default');
			$('.java-download').addClass('error').css({paddingLeft: 0});
		});
	}
	*/
	if (document.location.search.length > 0) {
		// check querystring for parameter to select engine:
		function queryParameters(query) {
			var keyValuePairs = query.split(/[&?]/g);
			var params = {};
			for (var i = 0, n = keyValuePairs.length; i < n; ++i) {
				var m = keyValuePairs[i].match(/^([^=]+)(?:=([\s\S]*))?/);
				if (m) {
					var key = decodeURIComponent(m[1]);
					(params[key] || (params[key] = [])).push(decodeURIComponent(m[2]));
				}
			}
			return params;
		}
		var params = queryParameters(document.location.search);
		var sCode = params['sCode'];
		if (sCode != '' && sCode != undefined) {
			sCode = sCode[0].substring(0,1).toUpperCase();
			if (sCode === 'M' || sCode === 'O') {
				$('input[id$="serviceCode"]').val(sCode);
			}
		}
	}
}
// end qualitytest

// noConnection (page-specific):
// toggle display of #typesLabel:
$('#typesLabel').height('0').find('input').attr('tabindex', '-1'); // hide by default
$('#availableLabel input').click(function() {
	var $interested = $('#interestedLabel');
	var $types = $('#typesLabel');
	if ($(this).val()=='N') {
		$types.find('input').attr('tabindex', '-1');
		$types.stop().animate({height: '0'}, 200, function(){
			$interested.stop().animate({height: $interested.children('fieldset:eq(0)').height()}, 200, function(){
				$interested.find('input').attr('tabindex', '0');
				if (window != window.top) self.parent.sizeIframeBQT();
			});
		});
	} else {
		$interested.find('input').andSelf().attr('tabindex', '-1');
		$interested.stop().animate({height: '0'}, 200, function(){
			$types.stop().animate({height: $types.children('fieldset:eq(0)').height()}, 200, function(){
				$types.find('input').attr('tabindex', '0');
				if (window != window.top) self.parent.sizeIframeBQT();
			});
		});
	}
});
if (window != window.top) $('#no-connection .return-link').click(function(){ return false });
if (navigator.platform.toLowerCase().indexOf('mac') != -1) $('#no-connection').addClass('mac'); 
// end noConnection

// frame functions:
if (window != window.top) {
	$('body').addClass('framed');
	$('h1').html($('h1 a').html()); // remove link from H1 content
	$(window).load(function(){
		self.parent.sizeIframeBQT();
	});
}
