

var list_show = false;
var display = false;
var modal_display = false;
var current_ml = '';
var delta = 0;
var mouse_in_floater = false;
var password_ok=false;


/********************************/
/******* ONLOAD FUNCTIONS *******/
/********************************/
$(document).ready(function(){
	
	var mouse_location;
	$(document).mousemove(function(e){
		mouse_location=e;
	}); 
	
	/******* HEADER FUNCTIONS *******/
	$('#header .fields').hover(function(){
		$(this).parent().find('.button').addClass('hover');
	}, function(){
		$(this).parent().find('.button').removeClass('hover');
	});
		
	$('#header .main-menu UL LI, #header .search .fields').hover(function(){
		if ($(this).hasClass('parent')) $(this).addClass('iehover-parent');
		$(this).addClass('iehover');
	}, function(){
		if ($(this).hasClass('parent')) $(this).removeClass('iehover-parent');
		$(this).removeClass('iehover');
	});
	
	
	/******* TOOLBAR FUNCTIONS *******/
	$('.tool-bar .message-list UL LI').hover(function(){
		$(this).addClass('iehover');
	}, function(){
		$(this).removeClass('iehover');
	});
	
	$('.tool-bar A').click(function(){
		if ($(this).parents('.message-list').length) return true;
		// if ($(this).parents('.menu').length) return true;
		current_ml = $(this).attr('href');
		$('.message-list').hide();
		$(this).parent().find($(this).attr('href')).fadeIn('slow');
		$(current_ml).unbind('click');
		$(current_ml).click(function(){
			displaying_messages = true;
		});
		return false;
	});
	
	$('span.nickname').hover(function(){
		$(this).toggleClass('iehover'); 
	}, function() {
		$(this).toggleClass('iehover')
	});
	
	var mouse_in_message=false;
	$('.message-list').hover(function(){ 
        mouse_in_message=true; 
    }, function(){ 
        mouse_in_message=false; 
    });
	$('body').click(function(){
		if ( !mouse_in_message ) $('.message-list').fadeOut('slow');
	});
	
	
	/******* GENERAL FUNCTIONALITY *******/
	$("SELECT.selectbox").selectbox();
	
	$('.datepicker').datetimepicker({ dateFormat: 'yy-mm-dd' });
	
	if($('.discussion').length) {
		$('.discussion .row:first').css('display', 'block');
		$('.other-comments').click(function(){
			$('.discussion .row').css('display', 'block');
			return false;
		});
	}
	
	$('#second-phone').toggle(function(){
		$('.second-phone').show();	
	},function(){
		$('.second-phone').hide();	
	});
	
	if ($('.list INPUT:checked').length) {$('.delete-message').removeClass('inactive');}
	$('.list INPUT').click(function(){
		if ($('.list INPUT:checked').length) {$('.delete-message').removeClass('inactive');}
		else {$('.delete-message').addClass('inactive');}
	});
	
	/******* CATEGORY MENU FUNCTIONS *******/
	$('#category-menu-button').click(function(){
		if ( $('#category-menu').is(":visible") ) {
			$('#category-menu').fadeOut('slow');
		} else {
			$('#category-menu').fadeIn('slow');
		}
	});
	$('#category-menu').mouseleave(function(){
		$('#category-menu').fadeOut('slow');
	});
	
	/******* FLOATER FUNCTIONS *******/
    $('.floater-content').hover(function(){
        mouse_in_floater=true;
    },function(){
        mouse_in_floater=false;
    });
    $('.ui-datepicker').hover(function(){
        mouse_in_floater=true;
    },function(){
        mouse_in_floater=false;
    });
	$('.show-floater').click(function(){
		$('.floater').fadeIn('slow');
	});
	$('.hide-floater').click(function() {
		$('.floater').fadeOut('slow');
	});
	$('.show-secondary-floater').click(function(){
		$('.floater-secondary').fadeIn('slow');
	});
	$('.hide-secondary-floater').click(function() {
		$('.floater-secondary').fadeOut('slow');
	});
    $("body").mouseup(function(){
        if ( !mouse_in_floater ) $('.floater').fadeOut('slow');
        if ( !mouse_in_floater ) $('.floater-secondary').fadeOut('slow');
    });
		
	$('.focus-clear').focus(function(){
		if ( $(this).val()=='caption' ) $(this).val('');
	});
	
	
	/******* AVATAR FUNCTIONS *******/
	$('.avatar').mouseover(function(){
		$('#avatar-'+$(this).attr('rel')).addClass('show');
	})
	$('.avatar-box').mouseleave(function(){
		$(this).removeClass('show');
	});
	
	
	/******* JOIN FORM FUNCTIONS *******/
	$("#desired-username").keyup(function(){
		var new_username=$("#desired-username").val();
		if ( new_username.length>4 ) {
			$("#username-check").load("/ajax-api.php?check-username="+new_username);
		} else {
			$("#username-check").html("<span class='error'>Minimum 5 characters.</span>");
		}
	});
	
	$("#user-email").keyup(function(){
		var new_email=$("#user-email").val();
		var emailformat = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if ( emailformat.test( new_email ) ) {
			$("#email-check").load("/ajax-api.php?check-email="+new_email);
		} else {
			$("#email-check").html("<span class='error'>Invalid email address.</span>");
		}
	});
	
	$("#new-password-verify").keyup(function(){
		var new_password=$("#new-password").val();
		var new_password_verify=$("#new-password-verify").val();
		if ( new_password.length>5 ) {
			if ( new_password==new_password_verify ) {
				$("#password-check").html("<span class='success'>Password OK.</span>");
			} else {
				$("#password-check").html("<span class='error'>Passwords do not match.</span>");
			}
		} else {
			$("#password-check").html("<span class='error'>Password must be at least 6 characters.</span>");
		}
	});
	
	$("#send-join").click(function(event){
		var lead=$("#lead-select").val();
		var email=$("#user-email").val();
		if ( lead=='' ) {
			$("#join-error").html("<span class='error'>Please select a Lead Fiskateer that will be asked to approve your request to join.</span>");
			return false;
		} else {
			var emailformat = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			if ( !emailformat.test( email ) ) {
				$("#join-error").html("<span class='error'>Invalid email address.</span>");
				return false;
			} else {
				var username=$("#desired-username").val();
				var password=$("#new-password").val();
				var password_verify=$("#new-password-verify").val();
				if ( username!='' && password!='' && email!='' ) {
					var fname=$("#user_fname").val();
					var lname=$("#user_lname").val();
					var address=$("#user_address").val();
					var city=$("#user_city").val();
					var state=$("#user_state").val();

					/* the minumum age you want to allow in */
					var min_age = 13;

					/* change "age_form" to whatever your form has for a name="..." */
					var year = parseInt(document.forms["join-form"]["year"].value);
					var month = parseInt(document.forms["join-form"]["month"].value) - 1;
					var day = parseInt(document.forms["join-form"]["day"].value);

					var theirDate = new Date((year + min_age), month, day);
					var today = new Date;

					if ( ( today.getTime() - theirDate.getTime() ) < 0) {
						$("#join-error").html("<span class='error'>You must be over 13 years of age to join this site.</span>");
						return false;
					}

					if ( fname=='' ) {
						$("#join-error").html("<span class='error'>You must provide a first name.</span>");
						return false;
					}
					if ( lname=='' ) {
						$("#join-error").html("<span class='error'>You must provide a last name.</span>");
						return false;
					}
					if ( address=='' ) {
						$("#join-error").html("<span class='error'>You must provide an address.</span>");
						return false;
					}
					if ( city=='' ) {
						$("#join-error").html("<span class='error'>You must provide a city.</span>");
						return false;
					}
					if ( state=='' ) {
						$("#join-error").html("<span class='error'>You must provide a state.</span>");
						return false;
					}

					$.get("/ajax-api.php?validate-join=true&username="+username+"&email="+email+"&password="+password+"&password-verify="+password_verify, function(response){
						$("#join-error").html(response);
						if ( response=='Processing...' ) {
							document.getElementById('join').submit();
						}
					});
				} else {
					$("#join-error").html("<span class='error'>You must fill in all the required fields.</span>");
					return false;
				}
			}
		}
		return true;
	});
	
	$(".delete-message").click(function(){
		$(this).fadeOut();
		$("#message-"+$(this).attr('rel')).load("/ajax-api.php?delete-message="+$(this).attr('rel')).delay(2000).fadeOut();
	});
	
	$("#blog-form").submit(function(){
		var the_title=$("#blog_post_title").val();
		var the_content=tinyMCE.get("blog_post_content").getContent();
		if ( the_title.length<5 || the_content.length<10 ) {
			$("#blog_post_error").html("<div class='error'>Please enter at least 5 characters for the title and 10 for the content.</div>");
			$('html, body').animate({ scrollTop: 0 }, 'slow');
			return false;
		} else {
			return true;
		}
	});
	
	$("#event-form").submit(function(){
		var the_title=$("#event_title").val();
		var the_content=tinyMCE.get("event_content").getContent();
		if ( the_title.length<5 || the_content.length<10 ) {
			$("#event_error").html("<div class='error'>Please enter at least 5 characters for the event title and 10 for the content.</div>");
			$('html, body').animate({ scrollTop: 0 }, 'slow');
			return false;
		} else {
			return true;
		}
	});
	
	$("#board-form").submit(function(){
		var the_title=$("#forum_post_title").val();
		var the_content=tinyMCE.get("forum_post_content").getContent();
		if ( the_title.length<5 || the_content.length<10 ) {
			$("#forum_post_error").html("<div class='error'>Please enter at least 5 characters for the event title and 10 for the content.</div>");
			$('html, body').animate({ scrollTop: 0 }, 'slow');
			return false;
		} else {
			return true;
		}
	});
	
	$("#idea-form").submit(function(){
		var the_title=$("#idea_post_title").val();
		var the_content=tinyMCE.get("idea_post_content").getContent();
		if ( the_title.length<5 || the_content.length<10 ) {
			$("#idea_post_error").html("<div class='error'>Please enter at least 5 characters for the event title and 10 for the content.</div>");
			$('html, body').animate({ scrollTop: 0 }, 'slow');
			return false;
		} else {
			return true;
		}
	});
	
	$("#gallery-form").submit(function(){
		var the_title=$("#gallery_name").val();
		var the_content=tinyMCE.get("gallery_description").getContent();
		var the_image=$("#gallery_photo").val();
		if ( the_title.length<5 || the_content.length<10 || the_image.length<1 ) {
			$("#gallery_error").html("<div class='error'>Please enter at least 5 characters for the event title, 10 for the content, and choose at least one image.</div>");
			$('html, body').animate({ scrollTop: 0 }, 'slow');
			return false;
		} else {
			return true;
		}
	});
	
	$("#change-password-form").keyup(function(){
		var password=$("#new-password").val();
		var confirm_password=$("#confirm-password").val();
		if ( password.length>5 ) {
			re = /^[A-Za-z]+$/;
			if ( re.test(password) ) {
				$("#password-check").html("<span class='error'>At least 1 number.</span>");
				return false;
			} else {
				if ( password==confirm_password ) {
					$("#password-check").html("<span class='success'>Password valid.</span>");
					password_ok=true;
					return true;
				} else {
					$("#password-check").html("<span class='error'>Two passwords must match.</span>");
					return false;
				}
			}
		} else {
			$("#password-check").html("<span class='error'>Minimum 6 characters.</span>");
			return false;
		}
	});

	$("#change-password-form").submit(function(){
		if ( password_ok ) {
			return true;
		}
		return false;
	});

	$("#send-message-response").click(function(){
		var thread_id=$("#user_message_thread_id").val();
		var user_to=$("#user_message_to_id").val();
		var content=escape(tinyMCE.get("user_message_content").getContent());
		$.get( "/ajax-api.php?send-response=true&thread="+thread_id+"&user_to="+user_to+"&content="+content, function(data){
			$(data).appendTo("#message-response-result");
		});
		tinyMCE.get("user_message_content").setContent("");
	});
	
	$(".reset").click(function(){
		$('html, body').animate({ scrollTop: 0 }, 'slow');
	});
	
	/********** ADD FRIEND *********/
	$(".add-friend").click(function(){
		var friend_to_add=$(this).attr('rel');
		$(this).load("/ajax-api.php?request-friend="+friend_to_add);
	});
	
	/******* WYSIWYG EDITOR *******/
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		theme_advanced_buttons1 : "bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,|,link,unlink,media,|,removeformat,undo,redo",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		plugins : 'inlinepopups'
	});
	
	/******* ACCORDION MENU INIT *******/
	initMenu();

});



/********************************/
/******** ACCORDION MENU ********/
/********************************/
function initMenu() {
	$('.archive-menu ul').hide();
	$('.archive-menu li a').click( function() {
		$(this).next().slideToggle('normal');
	});
}


/********************************/
/********* UI CONTROLS **********/
/********************************/
function HideAvatarBox(obj){
	$(obj).parent().css({'position': 'static'});
	$(obj).hide();
}
function idea_next() {
	$('#idea-slider').goToNextSlide();
}



/********************************/
/****** GALLERY FUNCTIONS *******/
/********************************/
function delete_photo( photo_id, div_id ) {
	$('#photo-update-result').load('/ajax-api.php?delete-photo='+photo_id);
	$('#'+div_id).fadeOut('slow');
}


/********************************/
/******* EVENT FUNCTIONS ********/
/********************************/
function mark_attending( event_id) {
	$("#attending-"+event_id).load("/ajax-api.php?mark-attending="+event_id);
}



/********************************/
/*** FRIEND REQUEST FUNCTIONS ***/
/********************************/
function friend_request_accept( friend_id ) {
	$('#hidden-ajax-update').load('/ajax-api.php?accept-friend-request='+friend_id);
	$('#request-'+friend_id).html("<span class='success'>Friend Request Accepted.</span>").delay('1000').fadeOut('slow');
	$('#friend-request-'+friend_id).html("<span class='success'>Friend Request Accepted.</span>").delay('1000').fadeOut('slow');
	var count=$('#request-count').html();
	var new_count=count-1;
	$('#request-count').html(new_count);
	if ( new_count==0 ) {
		$('#request-listing').html("<h4>No Friend Requests Found.</h4>");
		$('#request-count-container').fadeOut('slow');
		$('#master-request-list').html("<h4>You have no friend requests right now.</h4>");
	}
}

function friend_request_decline( friend_id ) {
	$('#hidden-ajax-update').load('/ajax-api.php?decline-friend-request='+friend_id);
	$('#request-'+friend_id).html("<span class='error'>Friend Request Declined.</span>").delay('1000').fadeOut('slow');
	$('#friend-request-'+friend_id).html("<span class='error'>Friend Request Declined.</span>").delay('1000').fadeOut('slow');
	var count=$('#request-count').html();
	var new_count=count-1;
	$('#request-count').html(new_count);
	if ( new_count==0 ) {
		$('#request-listing').html("<h4>No Friend Requests Found.</h4>");
		$('#request-count-container').fadeOut('slow');
		$('#master-request-list').html("<h4>You have no friend requests right now.</h4>");
	}
}



/********************************/
/**** JOIN REQUEST FUNCTIONS ****/
/********************************/

function join_request_accept( user_id ) {
	$('#hidden-ajax-update').load('/ajax-api.php?accept-join-request='+user_id);
	$('#jrequest-'+user_id).html("<span class='success'>Join Request Accepted.</span>").delay('1000').fadeOut('slow');
	$('#join-request-'+user_id).html("<span class='success'>Join Request Accepted.</span>").delay('1000').fadeOut('slow');
	var count=$('#join-request-count').html();
	var new_count=count-1;
	$('#join-request-count').html(new_count);
	if ( new_count==0 ) {
		$('#join-request-listing').html("<h4>No Join Requests Found.</h4>");
		$('#join-request-count-container').fadeOut('slow');
		$('#master-join-request-list').html("<h4>You have no join requests right now.</h4>");
	}
}

function join_request_decline( user_id ) {
	$('#hidden-ajax-update').load('/ajax-api.php?decline-join-request='+user_id);
	$('#jrequest-'+user_id).html("<span class='error'>Join Request Declined.</span>").delay('1000').fadeOut('slow');
	$('#join-request-'+user_id).html("<span class='error'>Join Request Declined.</span>").delay('1000').fadeOut('slow');
	var count=$('#join-request-count').html();
	var new_count=count-1;
	$('#join-request-count').html(new_count);
	if ( new_count==0 ) {
		$('#join-request-listing').html("<h4>No Friend Requests Found.</h4>");
		$('#join-request-count-container').fadeOut('slow');
		$('#master-join-request-list').html("<h4>You have no friend requests right now.</h4>");
	}
}



