// JavaScript Document

$(document).ready(function() {

	// =================
	// NAVIGATION CODE
	// =================

	var current_nav_id = null;
	var subnav_over = false;

	$("div.navitem").mouseover(function() {
		var nav_id = $(this).attr('class').slice(-4);
		var x = $(this).offset().left;
		var w = $(this).width();
		$("div.navitem").removeClass('active');
		$(this).addClass('active');
		
		if(nav_id != 'nav1' && nav_id != 'nav5' && nav_id != 'nav7') {
			$('#' + nav_id).css("left",x + "px");
			$('.subnav').not('#' + nav_id).css("display","none");
			
			$('#' + nav_id).fadeIn(200);
			subnav_over = true;	
		}
	});
	
	$("div.navitem").mouseout(function() {
		subnav_over = false;
		current_nav_id = $(this).attr('class').slice(-4);
		window.setTimeout(function() {
			if(!subnav_over) {
				$('.subnav').css("display","none");
				$("div.navitem").removeClass('active');		
			}
		}, 200);
	});
	
	$(".subnav").mouseover(function() {
		subnav_over = true;	
	});
	
	$(".subnav").mouseout(function() {
		subnav_over = false;	
		window.setTimeout(function() {
			if(!subnav_over) {
				$('.subnav').css("display","none");
				$('#' + current_nav_id).css("display","none");
			}
		}, 200);
	});

	// YouTube clips
	$("a.youtube").YouTubePopup({ autoplay: 1 });

	// MAKE OFF-SITE LINKS OPEN IN NEW WINDOW
	$("a").filter(function() {
	    return this.hostname && this.hostname.replace('www.','') !== location.hostname.replace('www.','');
	}).attr('target', '_blank');

	// Make Newsletter images and PDFs pull from ServiceDevelopment.org site - where they live in /UserFiles/
	if ($.trim($(this).attr('title')) == 'Newsletters') {
		$('#main_column img[src^="UserFiles/"]').attr('src', function() {
			return this.src.replace('servite-org','servites');
		});

		$('#main_column a[href^="UserFiles/"]').attr('href', function() {
			return this.href.replace('servite-org','servites');
		});
	}

        //Remove time from date/time fields
        $('.date-format').each(function() {
            var d = new Date($(this).html());
            $(this).html((d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear());
        });

        //If 'Show Newsletters' flag is set (should be on About > Secular Order page only), move Newsletters list into placeholder DIV
        if($("#show-newsletters").text() =="Yes"){
            $("#newsletters_placeholder").html($("#newsletters_box").html());
        }


});

function popuptopicwh(title,topic,w,h,scrollbars)
	{
	window.open('http://gifts.servitedevelopment.org/popup.aspx?title=' + title + '&topic=' + topic,'Popup170','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=' + scrollbars + ',resizable=no,copyhistory=no,width=' + w + ',height=' + h + ',left=0,top=0');
	return (true);
	}

