function navbar()
{
	var timeout    = 100;
	var closetimer = 0;
	var ddmenuitem = 0;
	
	function navbar_open()
	{  
	   navbar_canceltimer();
	   navbar_close();
	   ddmenuitem = $(this).find('ul').css('visibility', 'visible');
	}
	
	function navbar_close()
	{  
		if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
	}
	
	function navbar_timer()
	{  
		closetimer = window.setTimeout(navbar_close, timeout);
	}
	
	function navbar_canceltimer()
	{  
		if(closetimer)
	   {  
	   	  window.clearTimeout(closetimer);
	      closetimer = null;
	   }
	}
		
	$(document).ready(function()
	{  
	   $('#navbar > li').bind('mouseover', navbar_open)
	   $('#navbar > li').bind('mouseout',  navbar_timer)
	});
	
	document.onclick = navbar_close;	
}



function openDialog(){
	$('a[@rel*=resources]').modalPanel();
	$('a[@rel*=messages]').modalPanel();
	
}

function submitLogin()
{
	// Stuff for the login dialog form
	$('#indicator-login').fadeIn("fast");
	
	$('span.error').each(function(i) {
		$(this).css('display', 'none');
	});
	
	// Request
	$.post('/pastors/login/ajax_loginForm', $('#loginForm').serialize(),
		function(data) {

			// Valid form
			if(data.success == true) {			
				// Redirect to where they need to go.
				location.href = data.url;
				
			// Invalid form
			} else {
				// Construct the error message
				var html = '';
				$.each(data, function(i, val) {
					$('#error_' + i).css('display', 'block');
				});
				
				// Display and scroll to the error
				$('#message').css('display', 'none');
				$('#loginErrors').css('display', 'block');
				$('#loginErrors').append("We're sorry, we could not log you in.");
   				 
				$('#indicator').css('visibility', 'hidden');
			}
		}, "json");
	
}

(function($){
	$.fn.extend({
		modalPanel: function() {
			
			//Create our overlay object
			var overlay = $("<div id='modal-overlay'></div>");
			//Create our modal window
			var modalWindow = $("<div id='modal-window'><div id='modal-close'></div><div id='modal-content'></div></div>");
			//Create close button
			var closeButton = $("<img id='close-btn' src='http://2009.dev.oneprayer.com/ext/img/close.png' alt='Close' />");
			
			
			
			return this.each(function() {
				//Listen for clicks on objects passed to the plugin
				$(this).click(function(e) {
					
					if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
						$("body","html").css({height: "100%", width: "100%"});
						$("html").css("overflow","hidden");
					}
					
					//Append the overlay to the document body
					$("body").append(overlay.click(function() { modalHide(); }))
					
					//Set the css and fade in our overlay
					overlay.css("opacity", 0.8);
					overlay.fadeIn(150);
					
					//Prevent the anchor link from loading
					e.preventDefault();
					
					//Activate a listener 
					$(document).keydown(handleEscape);	
					
					// Open the window and insert the content from the url
					$("body").append(modalWindow);
					modalWindow.fadeIn(150);
					$('div#modal-close').append(closeButton.click(function() { modalHide(); }));						
					$('div#modal-content').load(this.href+"/ajax");					
				});
			});
			
			
			
			
			//Our function for hiding the modalbox
			function modalHide() {
				$(document).unbind("keydown", handleEscape)
				var remove = function() { $(this).remove(); };
				overlay.fadeOut(remove);
				modalWindow
					.fadeOut(remove)
			}
			
			//Our function that listens for escape key.
			function handleEscape(e) {
				if (e.keyCode == 27) {
					modalHide();
				}
			}
		}
	});
})(jQuery);




function resToggle(selector) {
			if ($("ul#"+selector).is(":hidden")) {
			  $("ul#"+selector).slideDown("slow");
			} else {
			  $("ul#"+selector).slideUp("slow");
			}
}

function swapPassword(goto) {
	$('div#modal-content').load("/pastors/password/"+goto+"/ajax");					
	
	
}


function swapLogin(goto) {
	$('div#modal-content').load("/pastors/login/"+goto+"/ajax");					
}



function getPassword()
{
	// Stuff for the login dialog form
	$('#indicator-login').fadeIn("fast");
	
	$('span.error').each(function(i) {
		$(this).css('display', 'none');
	});
	
	// Request
	$.post('/pastors/login/password/ajax_getPassword', $('#loginForm').serialize(),
		function(data) {

			// Valid form
			if(data.success) {
				// Redirect to where they need to go.
				$('#loginErrors').html(data.message);
				$('#message').css('display', 'none');
				$('#loginErrors').css('display', 'block');
				
				
			// Invalid form
			} else {
				// Construct the error message
				var html = '';
				$.each(data, function(i, val) {
					$('#error_' + i).css('display', 'block');
				});
				
				// Display and scroll to the error
				$('#message').css('display', 'none');
				$('#loginErrors').css('display', 'block');
				$('#loginErrors').html("We're sorry, we could not find that email.");
   				 
				$('#indicator').css('visibility', 'hidden');
			}
		}, "json");
	
}






$(document).ready(function(){
	navbar();
	openDialog();
});


