(function ($)
{
	// This works on <ul> tags with indefinite children 
	$.fn.navigationMenu = function()
	{		
		var navigation = $(this);
		$('li:has(ul)',this).children('ul').hide().end()
			.each(function(i){
				$(this).mouseover(function(event){
					if(event.target){
						var menu;
						menu = $(this).children('ul');
						if($(menu).is(':hidden')){
						//$('span#test').text(i);
							//console.log($('li',menu));
							
							$(menu).slideDown(100);
							menuHandler(menu);
						}
					}
				});
			});
				
		function menuHandler(menu)
		{
			var timerID = 0;
			$(menu).parent().hover(function(event){
				if(timerID){ clearTimeout(timerID) };
				timerID  = 0;
				$(this).siblings().children('ul').slideUp(100);
			},
			function(){
				timerID = setTimeout(function(){$(menu).slideUp(100)},300);
			});
		};
	};
	
	// Depends on jquery.XMLUtils.pack.js
	$.fn.JSONReader = function(callerSettings)
	{
		var settings = $.extend({
			url: false,
			data: false,
			sortName: 'title',
			sortOrder: 'ASC'
		}, callerSettings || {});
		
		var object = this;
		var ret=0;
		$.ajax({
			type: 'POST',
			timeout: 5000,
			async:false,
			url: settings.url,
			data:settings.data,
			dataType: 'json',
			error: function(xhr,textStatus,errorThrown){
				$('#error-message').empty().append('There was a problem signing in. Try again later.').fadeIn(function(){
					setTimeout(function(){$('#error-message').fadeOut()},5000);
				});
			},
			success: function(json) {
				// Grab the display template
				if(json.message){
					$('#error-message').empty().append(json.message).fadeIn(function(){
						setTimeout(function(){$('#error-message').fadeOut()},5000);
					});
				}else{
					//window.location.href = "http://community.teensforlife.com/service/displayMyPlace.kickAction?as=99668";
					ret = 1;
				}
			}
		});
		
		return ret;
	};
	
})(jQuery);

// Plugin Calls Here
$(document).ready(function(){
	$('#secondaryNav ul').navigationMenu();
	$('#form-login').submit(function(){
		var submitForm = $(this).JSONReader({url:'/testcurl.php',data:$(this).serialize()})
		if(submitForm==0){
			return false;
		}
	});
});