$(function() {
	$('tr[class^=child-]').hide().children('td');
	$('tr[class^=editchild-]').hide().children('td');
	
jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);

}; 
});

//Get cities from state
function list_cities(state, city) {
  if (state == '0') {
    $("#city").hide();
    return false;
  }
  $(".loading-state").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_cities/"+state+"/ajax/"+city,
    success: function(html){
      $("#city").html(html);
      $("#city").show();
      $(".loading-state").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.');
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-state").hide();
    }
  });
};

//if state = other, show custom option
function check_other(state) {
  if (state >= 10000) {
    $("#state_other").show();
    return true;
  } else {
    $("#state_other").hide();
    $("#city_other").hide();
    return true;
  }
};

//if city = other, show custom option
function check_other2(city) {
  if (city >= 10000) {
    $("#city_other").show();
    return true;
  } else {
    $("#city_other").hide();
    return true;
  }
};

//Get teams from event id
function list_teams(event_id) {
  if (event_id == '0') {
    $("#team_id").hide();
    return false;
  }
  $(".loading-event").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_teams/"+event_id+"/ajax/",
    success: function(html){
      $("#team_id").html(html);
      $("#team_id").show();
      $(".loading-event").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.');
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-event").hide();
    }
  });
};

//Get teams from event id
function list_teams2(event_id) {
  if (event_id == '0') {
    $("#team_id").hide();
    return false;
  }
  $(".loading-event").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_teams/"+event_id+"/ajax/true/",
    success: function(html){
      $("#team_id").html(html);
      $("#team_id").show();
      $(".loading-event").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.');
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-event").hide();
    }
  });
};

//Get times from event id
function list_time(event_id) {
  if (event_id == '0') {
    $("#time_10k").hide();
    return false;
  } else if (event_id == '3') {
    var tmp = '5k';
  } else if (event_id == '1' || event_id == '2') {
    var tmp = '10k';
  }
  $(".loading-event").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_time/"+event_id+"/ajax/",
    success: function(html){
      $("#time_name").html(tmp);
      $("#time_10k").html(html);
      $("#time_10k").show();
      $(".loading-event").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.'+event_id);
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-event").hide();
    }
  });
};

//Hides additional form input based on if runner or volunteer, plus it loads the tshirt sizes in case of volunteer
function hide_elements(type) {
  if (type == '2') {
    $("#runner").hide();
    $(".loading-type").show();
    //1 = Male, 2 = Female, 3 = Volunteer
    var type = parseInt(type) + 1;
    $.ajax({
      type: "GET",
      url: "/registration/list_tshirt/"+type+"/ajax/",
      success: function(html){
        $("#tshirt_size").html(html);
        $("#tshirt_size").show();
        $(".loading-type").hide();
      },
      error: function(obj1, e, obj2) {
        $("#error").html('An error was encountered.');
        $("#error").show();
        $.scrollTo("#error");
        $(".loading-type").hide();
      }
    });
  } else {
    $("#runner").show();
    $("#tshirt_size").html('');
    $("#tshirt_size").hide();
  }
};

//Loads tshirts corresponding to Male or Female
function load_tshirt(type) {
  $(".loading-sex").show();
  $.ajax({
    type: "GET",
    url: "/registration/list_tshirt/"+type+"/ajax/",
    success: function(html){
      $("#tshirt_size").html(html);
      $("#tshirt_size").show();
      $(".loading-sex").hide();
    },
    error: function(obj1, e, obj2) {
      $("#error").html('An error was encountered.');
      $("#error").show();
      $.scrollTo("#error");
      $(".loading-sex").hide();
    }
  });
};

$(document).ready(function() { 

  //Popup
    $('A[rel="external"]').click( function() {
        window.open( $(this).attr('href'),this,'width=700,height=380' );
        return false;
    });

  //AJAX forms
  $('#ajaxform #submit').live("click", function() {
    var data = $('#ajaxform').serialize();
    var url = $('#ajaxform').attr('action') + 'ajax/';
    var method = $('#ajaxform').attr('method');

    $("#submit").attr("disabled", true);
    $(".loading-submit").show();
    $("#error").fadeOut();

    $.ajax({
      url: url,
      type: method,
      data: data,
      dataType: 'json',
      success: function(data) {
        if (typeof data.error != 'undefined') {
          $("#error").html(data.error);
          $("#error").fadeIn();
          $.scrollTo("#error");
          $("#submit").attr("disabled", false);
        } else {
          $("#message").html(data.message);
          $("#message").fadeIn();
          $.scrollTo("#message");
          $('#ajaxform').fadeOut(1500);
        }
        $(".loading-submit").hide();
      },
      error: function(obj1, e, obj2) {
        $("#error").html('An error was encountered.');
        $("#error").fadeIn();
        $.scrollTo("#error");
        $(".loading-submit").hide();
        $("#submit").attr("disabled", false);
      }
    });
    
    return false;
  });
  
  //Delete member
	$('a.delete').click(function() {
		var msg = 'Are you sure?';
		if (confirm(msg)) {
			var element = $(this);
			var noteid = element.attr("id");
			var info = noteid;

			$.ajax({
				type: "GET",
        data: '',
        dataType: 'json',
				url: "/team/detail/delete/"+info+"/ajax/",
        success: function(data) {
          if (typeof data.error != 'undefined') {
            $("#error").html(data.error);
            $("#error").fadeIn();
            $.scrollTo("#error");
          } else {
						$('.child-'+noteid).fadeOut(3000);
						$(element).parents('tr').children('td').css('background-color', '#FF0000');
						$(element).parents('tr').fadeOut(3000);
          }
        },
				error: function(obj1, e, obj2) {
            $("#error").html('An error was encountered.');
            $("#error").fadeIn();
            $.scrollTo("#error");
					}
			 });
		}
			return false;
	});
	
  //Delete admin team
	$('a.team_delete').click(function() {
		var msg = 'Are you sure?';
		
		if (confirm(msg)) {
			var element = $(this);
			var noteid = element.attr("id");
			var info = noteid;

			$.ajax({
				type: "GET",
        data: '',
        dataType: 'json',
				url: "/admin/detail/delete/"+info+"/ajax/",
        success: function(data) {
          if (typeof data.error != 'undefined') {
            $("#error").html(data.error);
            $("#error").fadeIn();
            $.scrollTo("#error");
          } else {
						$(element).parents('tr').children('td').css('background-color', '#FF0000');
						$(element).parents('tr').fadeOut(3000);
          }
        },
				error: function(obj1, e, obj2) {
            $("#error").html('An error was encountered.');
            $("#error").fadeIn();
            $.scrollTo("#error");
					}
			 });
		}
			return false;
	});
	
  //Delete black list
	$('a.blacklist_delete').click(function() {
		var msg = 'Are you sure?';
		
		if (confirm(msg)) {
			var element = $(this);
			var noteid = element.attr("id");
			var info = noteid;

			$.ajax({
				type: "GET",
        data: '',
        dataType: 'json',
				url: "/admin/detail/blacklist_delete/"+info+"/ajax/",
        success: function(data) {
          if (typeof data.error != 'undefined') {
            $("#error").html(data.error);
            $("#error").fadeIn();
            $.scrollTo("#error");
          } else {
						$(element).parents('tr').children('td').css('background-color', '#FF0000');
						$(element).parents('tr').fadeOut(3000);
          }
        },
				error: function(obj1, e, obj2) {
            $("#error").html('An error was encountered.');
            $("#error").fadeIn();
            $.scrollTo("#error");
					}
			 });
		}
			return false;
	});

  //Delete coupon
	$('a.coupon_delete').click(function() {
		var msg = 'Are you sure?';
		
		if (confirm(msg)) {
			var element = $(this);
			var noteid = element.attr("id");
			var info = noteid;

			$.ajax({
				type: "GET",
        data: '',
        dataType: 'json',
				url: "/admin/detail/coupon_delete/"+info+"/ajax/",
        success: function(data) {
          if (typeof data.error != 'undefined') {
            $("#error").html(data.error);
            $("#error").fadeIn();
            $.scrollTo("#error");
          } else {
						$(element).parents('tr').children('td').css('background-color', '#FF0000');
						$(element).parents('tr').fadeOut(3000);
          }
        },
				error: function(obj1, e, obj2) {
            $("#error").html('An error was encountered.');
            $("#error").fadeIn();
            $.scrollTo("#error");
					}
			 });
		}
			return false;
	});

	//View member
	$('a.view').click(function() {
		$('.child-'+this.id).fadeToggle(500);
		$.scrollTo('#parent-'+this.id);
	});
	
	//Edit member
	$('a.edit').click(function() {
	  var id = this.id
	  var url = '/team/detail/edit_member/'+id;
	
	  $(".loading-edit-"+id).show();	

    $.ajax({
      url: url,
      type: 'get',
      dataType: 'json',
      success: function(data) {
        if (typeof data.error != 'undefined') {
          $("#error").html(data.error);
          $("#error").fadeIn();
          $.scrollTo("#error");
        } else {
          $("#edit-"+id).html(data.message);
          $("#edit-"+id).fadeToggle(500);
          $.scrollTo('#parent-'+id);
        }
        $(".loading-edit-"+id).hide();
      },
      error: function(obj1, e, obj2) {
        $("#error").html('An error was encountered.');
        $("#error").fadeIn();
        $.scrollTo("#error");
        $(".loading-edit-"+id).hide();
      }
    });
	});

	//View all members
	$('a.view_all').click(function() {
		$("tr[class^='child']").each(function() {
      $(this).fadeToggle(500);
    });
	});

}); 

