$(document).ready(function(){
	
	$('#coming-soon').css('opacity', .9);
/* 	$('.datepicker').datepicker(); */
	
	$('.userEvents').tablesorter({
		headers: { 
			3: { 
				sorter:false
			}
		}
	});
	$('.adminEvents').tablesorter({
		headers: { 
			5: { 
				sorter:false
			}
		}
	});
	$('.adminUsers').tablesorter({
		headers: { 
			4: { 
				sorter:false
			}
		}
	});
	$('.adminSwimmers').tablesorter({
		headers: { 
			4: { 
				sorter:false
			},
			5: { 
				sorter:false
			}
		}
	});
	
	$('.tip').tipsy();

	$(".phone").mask("(999) 999-9999");

	// Alert the user that they are using an unsupported browser  | IE6
	if(jQuery.browser.msie && jQuery.browser.version < 7) {
		$("#wrapper").prepend('<div class="alert notice">You are using an unsupported web browser. Please download <a href="http://www.getfirefox.com">FireFox</a> for free!</div>');
	}


	$(".paidToggle").live("click", function() {
		if($(this).hasClass('freeUser'))
		{
			alert('This user\'s payment cannot be toggled as their swimmers are free.');
		}else{
			var answer = confirm("Are you sure you want to toggle this user's payment status?");
		}
		var amount = $(this).attr("rel");
		if($(this).hasClass('paid')) {
			var paid = true;
		}else{
			var paid = false;
		}

		if (answer) {
			$.ajax({
				type: "GET",
				url: $(this).attr('href'),
				cache: false,
				success: function(data){
				}
			});
			if(!paid){
				$(this).addClass('paid')
				$(this).html('<span style="color: #333 !important;">Yes ('+amount+')</span>');
			}else{
				$(this).removeClass('paid')
				$(this).html('<span style="color: #b70000 !important;">No ('+amount+')</span>');
			}
		}
		return false;
	});

	$(".removeCatBtn").live("click", function() {
		var answer = confirm("Are you sure you want to remove this category and all it's associated events?")
		if (answer) {
			return true;
		} else {
			return false;
		}
	});

	/* REMOVE event from user's listing
	-------------------------------------------------------- */
	$(".removeBtn").live("click", function() {
		var answer = confirm("Are you sure you want to remove this event from your events list?")
		if (answer) {
			var event_id = $(this).attr("rel");
			var row = $(this).parent().parent();
			$.ajax({
				type: "GET",
				url: "/user/drop_event/"+event_id+"/",
				cache: false,
				success: function(data){
					if(data == '1') {
						refreshDashboard();
						row.fadeOut("fast", function(){
							row.remove();
						});
						
						return false;
					}
				}
			});
		}
		return false;
	});
	
	
	/* EDIT event button
	-------------------------------------------------------- */
	$(".editEventBtn, .addEventBtn, .addCategoryBtn, .viewUserInfo, .editSwimmerBtn, .addSwimmerBtn, .editUserBtn").live("click", function(){
		var goto = $(this).attr("href");
		$.facebox.loading();
		$.ajax({
			url : goto, 
			cache: false,
			success : function(data){
				$.facebox.reveal(data);
			}
		});
		return false;
	});
	
	$(".addEventBtn").live("click", function(){
		var goto = $(this).attr("href")+"/";
		$.facebox.loading();
		$.ajax({
			url : goto, 
			cache: false,
			success : function(data){
				$.facebox.reveal(data);
			}
		});
		return false;
	});
	$(".addCategoryBtn").live("click", function(){
		var goto = $(this).attr("href")+"/";
		$.facebox.loading();
		$.ajax({
			url : goto, 
			cache: false,
			success : function(data){
				$.facebox.reveal(data);
			}
		});
		return false;
	});
		
	
	function refreshDashboard (){
		$("#myEvents").append('<div class="loadingSmall">&nbsp;</div>');
		$("#myEvents").children(".loadingSmall").css("opacity", .5);
		$("#myEvents").animate({opacity: 1}, 500, '', function(){ 
			//$.get("dashboard/index/my_events", {}, function(data){ $("#myEvents").html(data); $(this).children(".loadingOverlay").remove();});
			$.ajax({
				cache: false,
				url: "dashboard/index/my_events/",
				success: function(data){
					 $("#myEvents").html(data); 
					 $("#myEvents").children(".loadingOverlay").remove();
					 }
			});
		});
		$("#eventSignUp").append('<div class="loadingSmall">&nbsp;</div>');
		$("#eventSignUp").children(".loadingSmall").css("opacity", .5);
		$("#eventSignUp").animate({opacity: 1}, 500, '', function(){ 
			//$.get("dashboard/index/categories", {}, function(data){ $("#eventSignUp").html(data); $(this).children(".loadingOverlay").remove();});
			$.ajax({
				cache: false,
				url: "dashboard/index/categories/",
				success: function(data){
					 $("#eventSignUp").html(data); 
					 $("#eventSignUp").children(".loadingOverlay").remove();
					 }
			});
		});
		$("#hoursLeft").append('<div class="loadingSmall">&nbsp;</div>');
		$("#hoursLeft").children(".loadingSmall").css("opacity", .5);
		$("#hoursLeft").animate({opacity: 1}, 500, '', function(){ 
			//$.get("dashboard/index/hours_left", {}, function(data){ $("#hoursLeft").html(data); $(this).children(".loadingOverlay").remove();});
			$.ajax({
				cache: false,
				url: "dashboard/index/hours_left/",
				success: function(data){
					 $("#hoursLeft").html(data); 
					 $("#hoursLeft").children(".loadingOverlay").remove();
					 }
			});
		});
	}
	
/* 	$('a[rel*=facebox]').facebox(); */
	
	$('.viewEvents').live("click", function(){
		$(this).parent().parent().css('background-color', "#e8f9fc");
		var goto = $(this).attr("href")+"/";
		$.facebox.loading();
		$.ajax({
			url : goto, 
			cache : false,
			success : function(data){
				$.facebox.reveal(data);
			}
		});
		return false;
	});
	
	$(".alert .close").live("click", function() {
		$(this).parent().hide('blind',{},100); 
		return false;
	});
	
	$("ul#menu li").hover(function(){
		$(this).children("ul:hidden").slideToggle(50);
		return false;
	}, function(){
		$(this).children("ul:visible").slideToggle(50);
	});
	
	/*  <-----  REGISTRATION FORM  -----> */
	$(".newswimmers:last").children(".close").show();

	$(".newswimmers .close").live("click", function() {
		$(this).parent().effect('highlight', {color: '#f9e1e1'}, 300).hide('blind',{},100, function(){ 
			$(this).prev(".newswimmers").children(".close").show();
			$(this).remove();
		});
		return false;
	});
	
	$(".addSwimmer").click(function() {
		newfield = $(".swimmers").clone();
		$(':input', newfield).each(function(){
			$(this).val('');
		});
		$(".newswimmers .close").hide();
		newfield.removeClass("swimmers").removeClass("errorMessage").addClass("newswimmers");
		newfield.children("input,select").val('');
		newfield.children(".close").show();
		insertafter = $(".swimmers").parent();
		newfield.appendTo(insertafter).show('blind',{},200, function(){
			$(this).effect("highlight", {}, 300);
		});
		return false;
	});	
	
	$("#physical_problems_check").click(function() {
		$("#physical_problems").toggle('blind',{},200);
	});


	$("#signInEmail,#signInPassword").focus(function(){
		$(".loginHelper").show();
	});
	
});
