$(document).ready(function() {
	// Add First and Last Classes to Top Nav
	$("#menu-header-nav a").first().addClass("first");
	$("#menu-header-nav a").last().addClass("last");
	
	// Add Pipes to Sub and Footer Nav
	$("#menu-footer-nav li").not("#menu-footer-nav li:last").after("<li>|</li>");
	$("#subNav li").not("#subNav li:last").after("<li>|</li>");
	
	// Repopulate Stories Listing
	$("#cat").change( function() {
		$.post( ajaxURL, { action: 'get_stories', category: $(this).val() }, function(response) {
			$("#categories ul").html(response);
		});
	});
	
	// Ghost Text
	$("#input_1_1").attr("title", "Enter Your Email Address Here");
    $(".populate input").each(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
        }
    });
    $(".populate input").focus(function() {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
        }
    });
    $(".populate input").blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
        }
    });

	// Encrypt Email Addresses
	$(".mask").each(function(){
		var ats, dots, address, i;
		ats = [ ' at ', ' (at) ', ' [at] ' ];
		dots = [ ' dot ', ' (dot) ', ' [dot] ' ];
		address = $(this).html();
		for ( i = 0; i < ats.length; i++ ) {
			address = address.replace(ats[i], '@');
		}
		for ( i = 0; i < dots.length; i++ ) {
			address = address.replace(dots[i], '.');
		}
		$(this).html('<a href="mailto:' + address + '">' + address + '</a>');
	});
	
	// Facts Rotator
	setInterval( function() {
		$('.facts li').filter(':visible').fadeOut(500,function(){
			if($(this).next('li').size()){
				$(this).next().fadeIn(500);
			}
			else{
				$('.facts li').eq(0).fadeIn(500);
			}
		});
	}, 7000);
	
	// Region Filters
	$(".regions a").click( function() {
		$(".regions li.active").removeClass("active");
		$(this).parent("li").addClass("active");
		var region = $(this).attr("class");
		$(".story-archive li").hide();
		$(".story-archive li").each( function() {
			if( $(this).hasClass( region ) ) {
				$(this).show();
			}
		});
			
		return false;
	});
	
	// YouTube Lightbox
	$(".youtube").click( function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
				 	'wmode'				: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
		return false;
	});
	
	// Expand Comments Section
	$("#commentsLink").click( function(){
		$("#comments").slideToggle();
		return false;
	});
	
	// Tray Animation
	$('#tray .content li').hover(function() { 
		$(this).addClass('hover');
		trayChosen();
	}, function() {
		$(this).removeClass('hover');
	});
	
	// Adjust Height of Content to Match Sidebar
	if( $("#sidebar").outerHeight() > $("#content").outerHeight() ) {
		var contentOuterHeight = $("#content").outerHeight();
		var contentInnerHeight = $("#content").height();
		var contentHeightDiff = contentOuterHeight - contentInnerHeight;
		var sidebarOuterHeight = $("#sidebar").outerHeight();
		var sidebarInnerHeight = $("#sidebar").height();
		
		$("#content").css( "min-height", sidebarInnerHeight - contentHeightDiff - 15 );
	};
	
	// FAQ Collapsible Content
	$(".page-template-page-faqs-php .entry").hide();
	$(".page-template-page-faqs-php h3").click( function() {
		$(this).next(".entry").toggle();
	});
});

function trayChosen(){ $('#tray .info li').removeClass('block'); $('#tray .info li.' + $('#tray .content li.hover').attr('id')).addClass('block'); }
