$(document).ready(function () {
	
	$(".submitButton,.linkButton").hover(function() {
		$(this).find(".buttonBG").each(function() {
			var newBG = $(this).css("background-image").replace("-off", "-on");
			$(this).css("background-image", newBG);
		});
	}, function() {
		$(this).find(".buttonBG").each(function() {
			var newBG = $(this).css("background-image").replace("-on", "-off");
			$(this).css("background-image", newBG);
		});
	});
	
	// SIGN UP / SETTINGS, Cancer Type Picker
	$("#cancer_types_root").val("");
	$(".cancer_types").change(function() {
		var current_offset = $(this).offset();
		$(this).siblings(".cancer_types:visible").each(function() {
			var other_offset = $(this).offset();
			if (other_offset.top > current_offset.top) {
				$(this).val("");
				$(this).removeAttr("name");
				$(this).hide();
			}
		});
		var type = $(this).val();
		$("#type_" +  type).show();
		var sub_id = $("#type_" +  type).attr("id");
		$("#type_" +  type).attr("name",sub_id);
	});
	
	// PROFILE, Recent Activity:
	$(".message").hover(function(){
		$(this).find(".delete_message").show();
	}, function() {
		$(this).find(".delete_message").hide();
	})
	$(".delete_message").click(function() {
		var name = $(this).attr("name");
		var name_arr = name.split("_");
		var id = name_arr[1];
		var parent = $(this).closest(".message");
		$.post("user/_ajax_returns.php", { 'pg': 'user_profile', 'submited': 'true', 'type': 'delete_message', 'delete_id': id }, function(){
			parent.remove();
		});
	});
	
	// PRIVATE MESSAGES
	$(".private_message").hover(function(){
		$(this).find(".delete_private_message").show();
	}, function() {
		$(this).find(".delete_private_message").hide();
	})
	$(".delete_private_message").click(function() {
		var name = $(this).attr("name");
		var name_arr = name.split("_");
		var id = name_arr[1];
		var parent = $(this).closest(".private_message");
		$.post("user/_ajax_returns.php", { 'pg': 'user_private_messages', 'submited': 'true', 'type': 'delete_private_message', 'delete_id': id }, function(data){
			parent.remove();
		});
	});
	
	// DAILY TRACKER, Sliders:
	$("div.slider").slider({
		value:	0,
		min:	0,
		max:	10,
		step:	1,
		stop:	function(event, ui) {
			$("body").css('cursor', 'default');
		},
		slide:	function(event, ui) {
			$(this).siblings("input").val(ui.value);
			$("body").css('cursor', 'w-resize');
		}
	});
	$(".range input").each(function() {
		var thisval = $(this).val();
		if (thisval == '') {
			thisval = 0;
		}
		$(this).siblings("div.slider").slider( "option", "value", thisval );
	});

	
	// Ajax Forms
	$("form.ajaxForm").ajaxForm({
		beforeSubmit:	preSubmit,
		success:		ajaxForm_postSubmit,
		dataType:		'json'
	});
	$("form.ajaxPrepend").ajaxForm({
		beforeSubmit:	preSubmit,
		success:		ajaxPrepend_postSubmit
	});
	$("form.ajaxAppend").ajaxForm({
		beforeSubmit:	preSubmit,
		success:		ajaxAppend_postSubmit
	});
	$("form.ajaxUpdate").ajaxForm({
		beforeSubmit:	preSubmit,
		success:		ajaxUpdate_postSubmit
	});
	
});

function preSubmit(formData, $form, options) {
	//alert($.param(formData));
	$form.find("input[type='image'],input[type='submit']").attr("disabled", "disabled");
	return true; 
}
function ajaxForm_postSubmit(data, statusText, xhr, $form)  {
	if (data.success == 1) {
		if (data.url != '') {
			window.location = data.url;
		} else {
			$("#ajaxoutput1").css("color","#337D2B");
			$("#ajaxoutput1").html(data.feedback);
			$(".hideOnSuccess").hide();
			$("html,body").animate({scrollTop: 0}, 'fast');
		}
	} else {
		$("#ajaxoutput1").css("color","red");
		$("#ajaxoutput1").html(data.feedback);
		$form.find("input[type='image'],input[type='submit']").removeAttr("disabled");
		$("html,body").animate({scrollTop: 0}, 'fast');
	}
}

function ajaxPrepend_postSubmit(data, statusText, xhr, $form)  {
	$form.resetForm();
	$form.find("input[type='image'],input[type='submit']").removeAttr("disabled");
	$form.find(".ajax_return").prepend($(data).fadeIn('slow'));
	$(".message").hover(function(){
		$(this).find(".delete_message").show();
	}, function() {
		$(this).find(".delete_message").hide();
	})
}

function ajaxAppend_postSubmit(data, statusText, xhr, $form)  {
	$form.resetForm();
	$form.find("input[type='image'],input[type='submit']").removeAttr("disabled");
	$form.find(".ajax_return").append($(data).fadeIn('slow'));
	$(".message").hover(function(){
		$(this).find(".delete_message").show();
	}, function() {
		$(this).find(".delete_message").hide();
	})
}

function ajaxUpdate_postSubmit(data, statusText, xhr, $form)  {
	$form.resetForm();
	$form.find("input[type='image'],input[type='submit']").removeAttr("disabled");
	$form.find(".ajax_return").html($(data).fadeIn('slow'));
	$(".message").hover(function(){
		$(this).find(".delete_message").show();
	}, function() {
		$(this).find(".delete_message").hide();
	})
}
