// JavaScript Document

jQuery(document).ready(function(){
	
	// Apply a class of "even" to certain lists
	jQuery('#showlist dt:even, #showlist dd:even, #oldshowlist dt:even, #oldshowlist dd:even').addClass('even');
	
	// Put the back to top tab below the screen initially
	jQuery('#backtotop').css({
		'bottom' : '-41px'
	});
	
	var backToTopTimer = 0;
	
	jQuery(window).scroll(function () { 
		if ( jQuery(window).scrollTop() == 0 ) {
			clearTimeout(backToTopTimer);
			backToTopTimer = setTimeout('hideBackToTop()', 300);
		} else {
			clearTimeout(backToTopTimer);
			backToTopTimer = setTimeout('showBackToTop()', 300);
		}
    });
	jQuery('#backtotop').click(function () {
		jQuery('#container').scrollTo();
		return false;
	});

	// FAQ page
	if (jQuery('#faq').length != 0) {
		faq();
	}
	
	// Write PBS KIDS stuff
	if (jQuery('#writepbskids').length != 0) {
		writepbskids();
	}
	
});

function writepbskids() {
	jQuery('#writepbskids').hide();
	var newText = jQuery('p:Contains("write to PBS KIDS")').html().replace(/write\sto\sPBS\sKIDS/ig, '<a href="" id="write">write to PBS KIDS</a>');
	jQuery('p:Contains("write to PBS KIDS")').html(newText);
	jQuery('a#write').click(function(){
		jQuery('#writepbskids').slideDown();
		return false;
	});
	
	jQuery('#writetopbskids').submit(function(){
		var error = false;
		var errorMessage = '';
		
		// Test to make sure the user has chosen a subject
		if (jQuery('#writetopbskids select').val() == 'Choose a subject') {
			error = true;
			errorMessage = 'Please select a subject for your note.\n';
		}
		
		// Test to make sure the message has content
		if (jQuery('#writetopbskids textarea').val() == '') {
			error = true;
			errorMessage += 'Please type us a message before sending your note.\n';
		}
		
		if (error == true) {
			alert(errorMessage);
			return false;
		} else {
			return true;
		}
	});
	
}

function faq() {
	
	jQuery('#faq dt').hover(
		function() {
			jQuery(this).addClass('hover');
		},
		function() {
			jQuery(this).removeClass('hover');
		});
	jQuery('#faq-jumplinks a').click(function() {
			var dest = jQuery(this).attr('href');
			jQuery('#faq h3').show();
			jQuery('#faq-search input:text').val('').keyup();
			jQuery(dest).scrollTo();
			return false;
	});
	jQuery('#faq-jumplinks ul li ul').hide();
	
	jQuery('#faq dl dd').hide();
	jQuery('#faq dl dt').css({
		'cursor' : 'pointer'						
	});
	jQuery('#faq dl dt').click(function(){
		if (jQuery(this).hasClass('active')) {
			jQuery(this).next('dd').hide();
		} else {
			jQuery(this).next('dd').fadeIn('fast');
		}			
		jQuery(this).toggleClass('active');				
	});
	
	faqSearch();
	
	if (jQuery('#faq').length != 0) {
		jQuery('#content').css({'margin' : '0 0 800px -23px'});
		// For inbound jumplinks
		var url = window.location.href.split('#');
		if (url[1] != '') {
			jQuery('#' + url[1]).click();
			jQuery(window).scrollTop(jQuery('#' + url[1]).offset().top);
		};
	}
	
}

function hideBackToTop() {
	jQuery('#backtotop').stop().animate({
		'bottom' : '-41px'
	}, 'fast');
}

function showBackToTop() {
	jQuery('#backtotop').stop().animate({
		'bottom' : '0'
	}, 'fast');
}

function faqSearch() {
	
	var searchBox = '#faq-search input:text';
	var startSearchingLength = 2;
	var hideShowTimer = 0;
	
	jQuery(searchBox).focus(function() {
		if (this.value == 'Search the FAQ') {
			this.value = "";
			jQuery(this).addClass('active');
		}
	});
	
	jQuery(searchBox).blur(function() {
		if (this.value == "") {
			jQuery(this).removeClass('active');
			this.value = 'Search the FAQ';
		}
	});
	
	jQuery(searchBox).keyup(function() {
		if(this.value.length >= startSearchingLength) {
			
			// console.log('Search.');
			
			// var searchText = escape(this.value);
			if(this.value.substring(0, 7) == 'search=') {
				this.value = this.value.substring(7, this.value.length);
				this.value = this.value.replace(/\+/g, ' ');
			}
			searchText = this.value;
			// console.log(searchText);
			
			/*
			var regx = new RegExp("(%20)");
			searchText = searchText.replace(regx, '\s');
			*/
			
			// console.log(searchText);
			
			regx = new RegExp("(" + searchText + ")", 'gi');
			
			jQuery('#faq h3, #faq dd, #faq dt').removeClass('active').hide();
			jQuery('#faq dt:Contains(' + searchText + ')').show();
			jQuery('#faq dd:Contains(' + searchText + ')').show().each(function(){
				// console.log(jQuery(this).html());
				// console.log(jQuery(this).children().length);
				
				
				// var thisText = jQuery(this).html();
				// thisText = thisText.replace(regx, '<span class="highlight">$1</span>');
				// console.log(thisText);
				// jQuery(this).html(thisText);				
			}).prev('dt').addClass('active').show();
			
			// Make any options matching search string selected
			jQuery('#faq option:Contains(' + searchText + ')').each(function() {
				var index = jQuery(this).parent('select').children('option').index(this);
				jQuery(this).parent('select').attr({'selectedIndex' : index});
			});
			
			var matchesFound = jQuery('#faq dt:Contains(' + searchText + ')').length + jQuery('#faq dd:Contains(' + searchText + ')').length;
			jQuery('#faq-search p.status').text(matchesFound + ' matches found.');
		}
		if(this.value.length == 0) {
			jQuery('#faq dl dt').removeClass('active').show();
			jQuery('#faq dl dd').hide();
			jQuery('#faq-search p.status').text('Currently showing all FAQs.');
		}
	});
	
	jQuery(searchBox).each(function() {
		if(this.value != 'Search the FAQ') {
			jQuery(searchBox).keyup();
		}
	});
	
	jQuery('#faq-search input.clear').click(function(){
		jQuery(searchBox).val('').keyup();
		return false;
	});
		
}

// Create scrollTo Function
$.fn.extend({
	scrollTo: function() {
		var currentPosition = jQuery(window).scrollTop();
		var scrollTargetOffset = this.offset().top;
		// var scrollDuration = Math.abs(scrollTargetOffset - currentPosition);
		var scrollDuration = 1000;
		jQuery('html, body').animate({
			scrollTop : scrollTargetOffset
		}, scrollDuration);
	}
}); 

// Case insensitive "Contains" pseudoclass
jQuery.expr[':'].Contains = function(a,i,m){
    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
};
