// JavaScript Document
$(document).ready(function() {
	
	//gallery modal window
	$("a.zoom1").fancybox({
		titlePosition			: 'inside',
		title					:  this,
		'transitionIn'			: 'elastic',
		'transitionOut'			: 'elastic',
		'opacity'				: true,
		'hideOnContentClick'	: false
	});
	
	$("#submit-grant-question").click(submit_grant_question);
	$("#submit-community-question").click(submit_community_question);
	$("#submit-advisor-question").click(submit_advisors_question);
	$("#secure_login").click(set_community_url);
	
	$("#submit-comment").click(submit_blog_comment);
	
	$(".sub-accordion").accordion({ 
		header: 'li.more a.open', 
		autoHeight: false,
		event: 'mouseover',
		navigation: true,
		active: false, 
		animated: 'easeOutSine',
		duration:300
	});
	
	$(".site-map-accordion").accordion({ 
		header: 'li.more a.open', 
		autoHeight: false,
		event: 'click',
		navigation: true,
		active: false, 
		animated: 'easeOutSine',
		duration:450
	});

	$('.delete-comment').click(delete_comment);
	
	$("#submit-publication").click(submit_publication);
	
	$(".search-field").focus(clear_search);

});

function clear_search(event)
{
	var cur_val = $(this).val();
	
	 $(this).val('')
	 
	$(this).blur(function(){
		if($(this).val() == '')
		{
			$(this).val($(this).attr('title'));
		}
	});
}

function submit_publication(event)
{
	event.preventDefault();
	$("#publication-form-wrapper").fadeOut("fast");
	$('html, body').animate({
		scrollTop: $("#publication-form").offset().top
		
	}, 800, "easeOutSine");
	
	$("#publication-ajax-loader").fadeIn("fast", post_publication_form);
}

function post_publication_form()
{

	$.post("/publication/request/", 
	$("#publication-form-form").serialize(),
		//callback function with return data
		function(data)
		{
			$("#publication-ajax-loader").fadeOut("fast");
			$("#publication-form-wrapper").replaceWith(data);
			$("#publication-form-wrapper").hide();
			$("#submit-publication").click(submit_publication);
			$("#publication-form-wrapper").fadeIn("slow");
		}												  
	);
}

function delete_comment(event)
{
	event.preventDefault();
	var answer = confirm("Are you sure you want to delete this entry?")
	if (answer){
		var elementDelete = $("#"+$(this).attr('rel'));
		elementDelete.remove();
		$.post("/technicalassistance/delete_comment/" + $(this).attr('rel'));
	} else {
		event.stop();
	}
}

function submit_blog_comment(event)
{
	event.preventDefault();
	$("#comment-form-wrapper").fadeOut("fast", post_comment_form);
}

function post_comment_form()
{
	var entryID = $("#comment-form").attr('rel');
	
	$("#comment-ajax-loader").fadeIn("slow");
	$.post("/technicalassistance/comment/" + entryID, 
	$("#comment-form-ajax").serialize(),
		//callback function with return data
		function(data)
		{
			
			var formCheck = data.indexOf("comment-form-wrapper");
			if(formCheck != -1)
			{
				$("#comment-ajax-loader").fadeOut("fast");
				$("#comment-form-wrapper").replaceWith(data);
				$("#comment-form-wrapper").hide();
				$("#submit-comment").click(submit_blog_comment);
				$("#comment-form-wrapper").fadeIn("slow");
			}
			else
			{
				$("#comment-ajax-loader").fadeOut("fast");
				$("#new-comment").append(data);
				$("#new-comment").slideDown("slow");
				$("#comment-form-wrapper").replaceWith('<h6>Thank you for posting a comment</h6>');
				$("#comment-form-wrapper").fadeIn("slow");
			}
		}												  
	);
}

function submit_grant_question(event)
{
	event.preventDefault();
	$("#qa-form-wrapper").fadeOut("fast", post_qa_form);
}

function post_qa_form()
{
	
	$("#qa-ajax-loader").fadeIn("slow");
	$.post("/grants/ask/", 
	$("#grant-form").serialize(),
		//callback function with return data
		function(data)
		{
			$("#qa-ajax-loader").fadeOut("fast");
			$("#qa-form-wrapper").replaceWith(data);
			$("#qa-form-wrapper").hide();
			$("#submit-grant-question").click(submit_grant_question);
			$("#qa-form-wrapper").fadeIn("slow");
		}												  
	);
}

function submit_community_question(event)
{
	event.preventDefault();
	$("#qa-form-wrapper").fadeOut("fast", post_qa_community_form);
}

function post_qa_community_form()
{
	
	$("#qa-ajax-loader").fadeIn("slow");
	$.post("/community/ask/", 
	$("#community-form").serialize(),
		//callback function with return data
		function(data)
		{
			$("#qa-ajax-loader").fadeOut("fast");
			$("#qa-form-wrapper").replaceWith(data);
			$("#qa-form-wrapper").hide();
			$("#submit-community-question").click(submit_community_question);
			$("#qa-form-wrapper").fadeIn("slow");
		}												  
	);
}

function submit_advisors_question(event)
{
	event.preventDefault();
	$("#qa-form-wrapper").fadeOut("fast", post_qa_advisors_form);
}

function post_qa_advisors_form()
{
	
	$("#qa-ajax-loader").fadeIn("slow");
	$.post("/advisors/ask/", 
	$("#advisors-form").serialize(),
		//callback function with return data
		function(data)
		{
			$("#qa-ajax-loader").fadeOut("fast");
			$("#qa-form-wrapper").replaceWith(data);
			$("#qa-form-wrapper").hide();
			$("#submit-advisor-question").click(submit_advisors_question);
			$("#qa-form-wrapper").fadeIn("slow");
		}												  
	);
}

function set_community_url(event){
	event.preventDefault();
	$.post("/community/set_url/",
		{current_url: this.rel},
		//callback function with return data
		function(data)
		{
			window.location = "/login";

		}												  
	);
}