$(document).ready(function(){
/**************************************************************/
$("#contact_us, .contact_us").attr("href","mailto:jane@partnersonthepath.org?subject=Contact from Partners on the Path Website");


$("#nav_audience").hover(function() {
	$("#header #nav_audience li ul.sub_sec").css({'left': 'auto'});
},
function() {
	$("#header #nav_audience li ul.sub_sec").css({'left': '-999em'});
});
//$("#header #nav_audience li ul.sub_sec").mouseout(function() { $(this).css({'left': '-999em' });});



//ZEBRA STRIPE certain elements
$("form table tr:even, ul.questions li:even, ul.checklist li:even").addClass("even");

//NEW WINDOW LINKS: applied to any href that includes a colon (anything non-relative)
$("a[href*=':']").each(function(){ 	$(this).attr("target","_blank"); });


//PRINT BUTTON: take html of the form+answers and send to ../_print.asp to autoprint
$("button.print").live("click",function(){ 
	$(this).closest(".activity").find("textarea").each(function(){
		$(this).after($(this).val());
		$(this).remove();
	});
	z=$(this).closest(".activity").html();
	$("#footer").before('<form id="printform" style="display:none;" method="post" action="../_print.asp" target="_blank"><input type="text" name="html" value="" id="printhtml"/></form>');	
	z=z.replace(/<texta.*<\/textarea>/g,"");
	z=z.replace(/<input.*\/>/g,"");
	z=z.replace(/<button.*/g,"");
	z=z.replace(/<BUTTON.*/g,"");
	z="<script type='text/javascript'>window.print();</script>"+z
	$("#printhtml").val(z);	
	$("form#printform").submit();
    return false;
});


/********************************* start nav handler **/
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
/********************************* end nav handler **/


$("#sidebar form").submit(function(){
	thisform=$(this);
	$.post($(this).attr("action"), $(this).serialize(), function(returnedhtml) {
		thisform.html(returnedhtml);
	});
	return false;
});


/********************************* start activity form controls **/
activityindex=0;
$("#main_content h2:first").addClass("first"); //for margin styling purposes
$("div.activity:first").show(); //show first activity 
$("div.activity:last").after("<a href='#' class='continue'></a>"); //add continue link, but keep blank
if($("div.activity:first form").size()==0){ //add continue text into link if there's more than one form
	$("a.continue").html("Continue to Self-Care Activity #"+(activityindex+2)+" &raquo;");
}

$("div.activity form").submit(function(){
		thisform=$(this);
		//submit form, hide submit btn, fade in results, show continue button.
		$.post(thisform.attr("action"), thisform.serialize(), function(returnedhtml) {
			thisform.find("input[type='submit']").animate({opacity:0}).closest("p").hide();
			thisform.append("<div class='results' style='opacity:.0;'>"+returnedhtml+"</div>");
			thisform.find("div.results").css("background-color","#fff");
			thisform.find("div.results").animate({opacity:1.0},1500,function(){
			
if(thisform.hasClass("checklist1")){
				$("a.continue").html("Continue to Stressor Checklist #2 &raquo;");	
}
else if(thisform.hasClass("checklist2")){
				$("a.continue").html("Continue to Stressor Checklist #3 &raquo;");	
}
else if(thisform.hasClass("internal_stressors")){
				$("a.continue").html("Continue to External Stressors &raquo;");	
}
else{

			if(activityindex==$("div.activity").length-2){ //if review activity is next
				$("a.continue").html("Continue &raquo;");
				}
			else if(activityindex<$("div.activity").length-1){
				$("a.continue").html("Continue to Self-Care Activity #"+(activityindex+2)+" &raquo;");
				}
}

			
			});
		});
		return false;
});
		
	
$("a.continue").click(function(){
		activityindex++;
		$("div.activity").eq(activityindex).slideDown(500);	//show the next activity 

		if($("div.activity").eq(activityindex).find("form").size()!=0){ //if there's no form in this new activity,
			$(this).html("");//don't show continue button.
		}
		return false;
});
/********************************* end activity form controls **/

$("span.tooltip").each(function(){
	$(this).parent().mouseenter(function(){
		$(this).find("span.tooltip").fadeIn(333);
	});
	$(this).parent().mouseout(function(){
		$(this).find("span.tooltip").hide();
	});
});

$("ul#answers").mouseleave(function(){
		$("span.tooltip").hide();
});

/*** feedback ***/
$('#feedback a.cta').click(function(){	
	$("#feedback p:first").hide();
	$(this).hide();
	$('#feedback form').show();
	return false;
});

$('#share a.cta').click(function(){	
	$("#share p:first").hide();
	$(this).hide();
	$('#share form').show();
	return false;
});

	
$("a.member_login").click(function(){
	$("form#login").show();
	return false;
});
	

	
$("#cse-search-box input")
	.focus(function(){
		if($(this).val()=="Search"){
		$(this).val("");
		}
	})
	.blur(function(){
		if($(this).val()==""){
		$(this).val("Search");
		}
	});

	


/************** endnotes controller */	
$(".endnotes h3, .references h3").each(function(){
	m=$(this).html();
	$(this).html('<a href="#" class="shownotes"><strong>+</strong>'+m+'</a>');
});
	

	
$(".shownotes").toggle(function(){
		$(this).find("strong").html("-");
		$(this).closest("div").find("p").slideDown();
		return false;
	},function(){
		$(this).find("strong").html("+");
		$(this).closest("div").find("p").slideUp();
		return false;});
		

scrollfollow();	
	
});
/****************************************************************/
/***********end document ready, start defining functions*********/
/****************************************************************/

function scrollfollow(){
    var $sidebar   = $("#scrollfollow"),
        $window    = $(window),
        offset     = $sidebar.offset(),
        topPadding = 15;

    $window.scroll(function() {
	
	var t=setTimeout(scrollit,1000);
    });

	function scrollit(){		
        if ($window.scrollTop() > offset.top) {
            $sidebar.stop().animate({
                marginTop: $window.scrollTop() - offset.top + topPadding
            },200);
        } else {
            $sidebar.stop().animate({
                marginTop: 0
            });
        }
	}

}

function scrolltobottom(){
	window.scrollBy(0,50000);
}






