jQuery(document).ready(function($){
	// add a lead photo
	$('.showcalendar').live('click',function(){
		var id=$(this).attr('id');
		var page=document.location.href;
		// make the db change to add the lead photo
		$.post(page,
			{
				'ajaxrequest':'displayfunctioncalendar',
				'id':id
			},
			function (data){
				$('#showcal').html(data);
			}
		);

		$.post(page,
			{
				'ajaxrequest':'displayevents',
				'id':id
			},
			function (data){
				$('#callist').html(data);
			}
		);
		$('html, body').animate({scrollTop:0}, 'fast');

	});// end showcalendar

	$('.showevents').live('click',function(){
		var id=$(this).attr('id');
		var page=document.location.href;
		page=page.replace("#","");

		// make the db change to add the lead photo
		$.post(page,
			{
				'ajaxrequest':'displayevents',
				'id':id
			},
			function (data){
				$('#callist').html(data);
			}
		);
  		$('html, body').animate({scrollTop:0}, 'fast');

	});// end showevents

	$('.show-participant-form').live('click', function(){
		var checkboxid=$(this).attr('id');
		idarray=checkboxid.split("_");
		var membernameid='#member-name_' + idarray[1];
		var memberid='#mid_' + idarray[1];
		var participantnameid='#participant-name_' + idarray[1];// the participant name field
		var participantnameidkey=participantnameid + 'key';
		var membercheckbox="#checkbox_" + idarray[1];
		var friendcheckbox="#friend-checkbox_" + idarray[1];// the checkbox for the friend
		var gidid='#gid_' + idarray[1];
		var hoursassignedid='#hours_' + idarray[1];
		var tableid='#table_' + idarray[1];
		var page=document.location.href;
		page=page.replace("#","");

		if($(membercheckbox).is(':checked')){// deal with the member checkbox being checked
			$(participantnameid).val($(membernameid).val());// transfer the member name to the signup box
			$(participantnameid + 'key').val($(memberid).val());// transfer the member id number to the hidden field

			//add the member to the staffing plan
			$.post(page,{
				'ajaxrequest':'staffingplan',
				'mid':$(memberid).val(),
				'name':$(participantnameid).val(),
				'gid':$(gidid).val(),
				'hoursassigned':$(hoursassignedid).val(),
				'reference':idarray[1]
   			});// end add the member to the staffing plan

		}

		if($(friendcheckbox).is(':checked')){// deal with the friend checkbox being checked
			$(participantnameid).val('');// clear the field
			$(participantnameid).attr('readonly',false);// remove the read only status so that the visitor can enter a name
			$(participantnameid).focus();// put the focus on the field
			var divid='participant-name_' + idarray[1] + 'div';
			$(participantnameid).after("<div id=" + divid + " ></div>");
			$.post(page,{
				'ajaxrequest':'showaddform',
				'reference':idarray[1]
				},
				function(data){
					$('#' + divid).html(data);
				}
			);// end post

			$(".autocomplete").autocomplete(page, {//autocomplete the field
				width: 260,
				selectFirst: false
			});

			$(".autocomplete").result(function(event, data, formatted) {// if there is a result, transfer the member id number to the hidden id field
				if (data){
					$(participantnameidkey).val(data[1]);// place the value in the mid field

					// add the contact information for this person
					$.post(page,
						{
						'ajaxrequest':'addsignupcontact',
						'mid':data[1]
						},function(data){
							dataarray=data.split(':');
							$('#add-email_' + idarray[1]).val(dataarray[0]);
							$('#add-phone_' + idarray[1]).val(dataarray[1]);
						}
					);// end post

					//add the friend to the staffing plan
					$.post(page,{
						'ajaxrequest':'staffingplan',
						'mid':data[1],
						'name':$(participantnameid).val(),
						'gid':$(gidid).val(),
						'hoursassigned':$(hoursassignedid).val(),
						'reference':idarray[1]
	   				});// end add a friend to the staffing plan

				}
			});

			$('.required-field').blur(function(){// prevent the visitor from clearing the name box
				if($(this).val()==''){// if the field is blank
					alert('You must enter a name to use this option.');// display an alert
					$(membercheckbox).attr('checked',false);// uncheck the member id box
					$(friendcheckbox).attr('checked',false);// uncheck the friend id box
					$(participantnameid).val('Participant Name');// insert the default value
					$(participantnameid + 'key').val('');// clear the value in the id field
					$('#' + divid).empty();// clear out the div that was added earlier
				}
				else{
					// check for a member id number.  If there is one, add the member to the staffing plan; if there is not, show a data collection form
				}
			});

			$('.addcontactform').live('click',function()
				{
					$.post(page,{
						'ajaxrequest':'modifysignupinfo',
						'name':$(participantnameid).val(),
						'mid':$(participantnameidkey).val(),
						'email':$('#add-email_' + idarray[1]).val(),
						'phone':$('#add-phone_' + idarray[1]).val(),
						'gid':$(gidid).val(),
						'hoursassigned':$(hoursassignedid).val(),
						'reference':idarray[1]

					   },
						function(data){
							dataarray=data.split(":");
							if(dataarray[0]=='error'){
								alert(dataarray[1]);
							}
							else{// there was no error, so close the drop down box
								divid='#participant-name_' + idarray[1] + 'div';
								$(divid).hide('slow');
							}
						}
					);

				}
			);// end addcontactform click event

		}

	});// end show-participant-form


});// end of jquery block

