var nav_hover = false;

function hide_hover(use_this) {
	/*
	$(use_this).children(".nav_over").fadeOut('fast', 
		function() {
			$(use_this).children(".nav_over").remove();
		}
	);
	*/
	$(use_this).children(".nav_over").remove();
	$('.nav_bar_over').hide();
	$('.nav_bar_over').html("");
}

	$('.nav_entry_normal').hover(
		function() {
			if(nav_hover) hide_hover($(nav_hover));
			$(this).prepend("<div class='nav_over' style='display:none'><div class='nav_over_text'>"+$(this).html()+"</div></div>");
			$('.nav_over').show();
			
			button_left_pos = $(this).position().left;
			button_width = $(this).width();
			
			nav_hover = false;
			if($(this).children(".nav_sub").html() != undefined && nav_hover == false) {
				//alert($(this).position().left);
				
				
				nav_hover = $(this);
				$('.nav_bar_over').html("<span id='nav_bar_over_text'>"+$(this).children(".nav_sub").html()+"</span>");
				
				$('#nav_bar_over_text').children("a").after("<span class='nav_spacer'>|</span>"); // add our spacers
				$('#nav_bar_over_text span.nav_spacer:last-child').hide(); // remove the last spacer
				
				num_links = $('.nav_bar_over > a').length;
				left_pos = $(this).position().left - $('.nav_container').position().left; // the left pos of the main nav button pressed
				left_pos = button_left_pos  - $('.nav_container').position().left;
				
				//alert(left_pos);
				//alert($('#nav_bar_over_text').width());
				$('.nav_bar_over').show();
				
				text_length = $('#nav_bar_over_text').width();

				new_left_pos = left_pos - ((text_length - button_width) / 2) - 15;
				
				new_end_pos = text_length + new_left_pos;
				if(new_end_pos > $('.nav_bar_over').width()) new_left_pos -= new_end_pos - $('.nav_bar_over').width() + 100;
				if(new_left_pos < 0) new_left_pos = 1;
				
				$('#nav_bar_over_text').css('margin-left',new_left_pos);
				

			}
		}, 
		function() {
			if(!nav_hover) {
				hide_hover($(this));
			}
		}
	);
	
	$('.nav_bar_over').hover(
		function() {
			
		},
		function() {
			hide_hover($(nav_hover));
		}
	);
	
	function forgotpass() {
		
			$("#forgot_pass").overlay({
			    expose: { 
			        color: '#333', 
			        loadSpeed: 200, 
			        opacity: 0.9 
			    }, 
			    api:true,
			    top:200,
			    onLoad: function() {
			    		$('#email_forgot_pass').focus();
			    	},
			    	onBeforeLoad: function() {
			    		$('.forgot_sucess').hide();
			    		$('.forgot_error').hide();
			    	}
			}).load();
		
	}


	function email_password() {
		
    		$('.forgot_sucess').hide();
    		$('.forgot_error').hide();

		$.ajax({
			url: "ajax.php?cmd=email_password",
			data: {email:$('#email_forgot_pass').val()},
			type: "POST",
			cache:false,
			dataType: "xml",
			success: function(xml) {
				if($(xml).find('Result').text() == '0') {
					//$.prompt($(xml).find('ReturnMsg').text());
					$('.forgot_error').show();
					return;
				} else {
					//$.prompt($(xml).find('ReturnMsg').text());
					$('.forgot_success').show();
					return;
				}
				
				
			}
		});	
	}
	
	function email_password2() {
		

		$.ajax({
			url: "ajax.php?cmd=email_password",
			data: {email:$('#email_forgot_pass2').val()},
			type: "POST",
			cache:false,
			dataType: "xml",
			success: function(xml) {
				
				
				
				if($(xml).find('Result').text() == '0') {
					//$.prompt($(xml).find('ReturnMsg').text());
					$('#email_pass_result').html(' - Error');
					return;
				} else {
					//$.prompt($(xml).find('ReturnMsg').text());
					$('#email_pass_result').html(' - Success!');
					return;
				}
				
				
				
			}
		});	
	}
	
	function login_try(username, password) {
		var login_result = '';
		$.ajax({
			url: "ajax.php?cmd=login_attempt",
			data: {username:username,
				password: password},
			type: "GET",
			async: false,
			cache:false,
			dataType: "xml",
			success: function(xml) {
				if($(xml).find('Result').text() == '0') {
					//$.prompt($(xml).find('ReturnMsg').text());
					$('.forgot_error').show();
					return;
				} else {
					//$.prompt($(xml).find('ReturnMsg').text());
					//$('.forgot_success').show();
					login_result = $(xml).find('RedirectPage').text();
					return;
				}
				
				
			}
		});
		
		return login_result;
	}
	
	function login_ajax(username, password) {
		
		$('#login_submit').hide();
		$('#login_loader').show();
		
		var rslt = login_try(username,password);
		
		if(rslt != '') {
			window.location = rslt;
		} else {
			
			$('#login_loader').hide();
			$('#login_submit').show();
			
			$("#login_box").overlay({
			    expose: { 
			        color: '#333', 
			        loadSpeed: 200, 
			        opacity: 0.9 
			    }, 
			    api:true,
			    top:200,
			    onLoad: function() {
			    		$('#lb_username').val(username);
			    		$('#lb_password').focus();
			    	},
			    	onBeforeLoad: function() {

			    	}
			}).load();
		}
	}
	
	function overlay_login_ajax() {
		//alert($('#lb_username').val() + ' | ' + $('#lb_password').val());
		$('#invalid_holder').hide();
		$('#invalid_loader').show();
		
		var rslt = login_try($('#lb_username').val(), $('#lb_password').val());
		
		if(rslt != '') {
			window.location = rslt;
		} else {
			$('#invalid_loader').hide();
			$('#invalid_holder').show();
			
		}
		
	}
	
	function show_recent_newsletter() {
		
		window.open("organizer_current.php");
		return;
		$('#popup_content').html("<img src='images/loader.gif'>");
		
		$("#popup_box").overlay({
		    expose: { 
		        color: '#333', 
		        loadSpeed: 200, 
		        opacity: 0.9 
		    },
		    api:true,
		    onLoad: function() {

		    	},
		    	onBeforeLoad: function() {
		    		
				$.ajax({
					url: "ajax.php?cmd=show_current_newsletter",
					type: "POST",
					cache:false,
					dataType: "html",
					success: function(html) {
						$('#popup_content').html(html);
					}
				});	
		    		
				
		    	}
		}).load();
		
	}