var rep
var firstname
var lastname
var email
var phone
var partner
var street
var city
var state
var zip

jQuery(function () {
	
	/**
	****** @HighCharts
	**/
	var participant_ratio_options = {
		chart: {
			backgroundColor: 'none',
			renderTo : 'participants_graph_piechart',
			spacingTop: -40,
			spacingRight: 150,
			spacingLeft: 0,
			spacingBottom: 0
		},
		title : {
			text: null,
			floating: true
		},
		credits: {
			enabled: false
		},
		tooltip: {
	         formatter: function() {
	            return '<b>'+ this.point.name +'</b>: '+ this.y;
	         }
	      },
		plotOptions: {
			pie: {
	            allowPointSelect: true,
				borderWidth: 0,
	            cursor: 'pointer',
	            dataLabels: {
	               enabled: false
	            },
	            showInLegend: true
	         }
		},
		legend: {
			borderWidth: 0,
			layout: 'vertical',
			margin: 0,
			verticalAlign: 'middle',
			x: 130,
			y: 0
		},
		series: [{
			type: 'pie',
			name: 'Participant Ratio',
			data: [{
	            name: 'Filled',
	            color: '#29d5c7',
	            y: parseInt($("#filledSpaces").text())
	        }, {
	            name: 'Needed',
	            color: '#505050',
	            y: parseInt($("#totalSpaces").text())
	        }]
		}]
	}
	
	if ($('#participants_graph_piechart').length > 0) {
		var participant_ratio_chart = new Highcharts.Chart(participant_ratio_options);		
	};

  $("#user_global_admin").change( function(){
    if ($("#user_global_admin").attr('checked') == true) {
      $("#user_admin").attr('checked', 'checked');
    }
  });

  /* show new partner or category dropdowns on a new project if checked */

  $("#project_new_partner").change( function(){
    if ($("#project_new_partner").attr('checked') == true) {
      $("#project_partner_id").attr('disabled', true);
      $('#new_partner').load('/new_partner');
    } else {
      $("#project_partner_id").removeAttr('disabled');
      $("#new_partner").html("");
    };
  });

  $("#project_new_category").change( function(){
    if ($("#project_new_category").attr('checked') == true) {
      $("#project_category_id").attr('disabled', true);
      $('#new_category').load('/new_category');
    } else {
      $("#project_category_id").removeAttr('disabled');
      $("#new_category").html("");
    };
  });

  /* colorbox for changing location */

  $(".changeLocation").click( function(){
    $(".changeLocation").colorbox({width:'300px'});
  });

  /* pre-populate contact info with partner/representative info */

  $("#project_partner_id").change( function(){
    var partner_id = $('#project_partner_id').val();
    $.ajax({
      url: "/admin/get_partner_info",
      data: { "partner_id" : partner_id },
      success: function(data) {
        partner = data.partner.partner;
        $('#project_street').val(partner.street);
        $('#project_city').val(partner.city);
        $('#project_state').val(partner.state);
        $('#project_zip').val(partner.zip);
        
        if (data.representative == null) {
        $('#project_contact_name').val("");
        $('#project_contact_phone').val("");
        $('#project_contact_email').val("");
        } else {
        rep = data.representative.user;
        $('#project_contact_name').val(rep.first_name + " " + rep.last_name);
        $('#project_contact_phone').val(rep.phone);
        $('#project_contact_email').val(rep.email);
        };
      },
      dataType: "json"
    });
  });

  /* if a role is checked, show the dropdown.  if unchecked, reset the dropdown so no value gets passed */

  $("#user_coordinator").change( function(){
    if ($("#user_coordinator").attr('checked') == true) {
      $("#church_select").removeAttr('style');
    } else {
      $("#church_select").hide();
      $("#user_church_id").val(null);
    };
  });
  
  $("#user_representative").change( function(){
    if ($("#user_representative").attr('checked') == true) {
      $("#partner_select").removeAttr('style');
    } else {
      $("#partner_select").hide();
      $("#user_partner_id").val(null);
    };
  });
  
  $("#user_leader").change( function(){
    if ($("#user_leader").attr('checked') == true) {
      $("#project_select").removeAttr('style');
    } else {
      $("#project_select").hide();
      $("#user_project_id").val(null);
    };
  });
  
  $("#user_coleader").change( function(){
    if ($("#user_coleader").attr('checked') == true) {
      $("#project_select").removeAttr('style');
    } else {
      $("#project_select").hide();
      $("#user_project_id").val(null);
    };
  });
  
  $("#notification_recipient_project_participants").change( function(){
    if ($("#notification_recipient_project_participants").attr('checked') == true) {
      $("#project_select").removeAttr('style');
    } else {
      $("#project_select").hide();
      $("#user_project_id").val(null);
    };
  });

  /* if user is already in a role, show the dropdown for that role from the beginning */

  if ($("#user_coordinator").attr('checked') == true) {
      $("#church_select").removeAttr('style'); 
  };

  if ($("#user_representative").attr('checked') == true) {
      $("#partner_select").removeAttr('style'); 
  };

  if ($("#user_leader").attr('checked') == true) {
      $("#project_select").removeAttr('style'); 
  };

  if ($("#user_coleader").attr('checked') == true) {
      $("#project_select").removeAttr('style'); 
  };

  /*hide spouse & kids until requested */

  $("#spouse").click( function(){
    $("#add_spouse").removeAttr('style');
    $("#spouse").hide();
    return false;
  });

  $("#show_kids").click( function(){
    $("#kids_form").removeAttr('style');
    $("#show_kids").hide();
    return false;
  });

  /*match slug to region name*/

  $("#region_name").change( function(){
    var slug;
    slug = $('#region_name').val().replace(/ /g, "_").toLowerCase();
    $("#region_slug").val(slug);
  });

 if ($('#map').length > 0) {
   $('#map').jMapping(); 
 };

 $('#project_age_limit').change(function () {
   if (parseInt($('#project_age_limit').val()) < 18 && $('#project_category_id').val() != "4") {
     alert("Please note: Families can only sign up together if this project is designated to in the Family-friendly category.")
   }
 });

 $(".helper").tooltip({

   position: "top left",

   // a little tweaking of the position
   offset: [50, -20],

   // use the built-in fadeIn/fadeOut effect
   effect: "fade",

   // custom opacity setting
   opacity: 1.0

 });
 

})

