
	/*
	*	These functions control the form helpers and AJAX tools used to add/remove/modify 
	*	albums and album attributes (cover photo, etc.)
	*/
	
	$('#city').change(function() 
	{
		show_areas($(this).val());
		show_locations($(this).val(),false);
	});
	
	function show_areas(city_ID)
	{
		$('#area_row').show();
		$('#pform_areas')
			.load("/photos/getselects/areas/" + city_ID, false);
		return false;
	}
	
	function show_locations(city_ID,area_ID) 
	{
		$('#locations_row')
			.show();
		$('#pform_locations')
			.load("/photos/getselects/locations/" + city_ID + "/" + area_ID);
		return false;	
	}

	function upload_location_image (item_id) {
		var upload = $('#choose_photos');
		upload.uploadifySettings('scriptData', { photo_id : item_id });
		$('#choose_photos').uploadifyUpload();
		//console.log(item_id);
	}

	// submit form data
	$('.fs-ajax-location').click(function(e) 
	{	
		// return false if form is disabled
		if($(this).hasClass('disabled')) return false;
		
		// vars
		var location_id = $(this).attr('location_id');
		var item_id = $(this).parents('.form').find('input[name="event_id"]').attr('value');
		var form_name = $(this).attr('form_name');
		var post_data = $('#' + form_name + ' form').serializeArray();
		var pattern = /--postid=/;
		var insert_id = '';

		// remove html error input from previous function calls
		$('.errors').remove();
		
		// Submit data to moderate controller
		// Moderate controller will create a new item in moderation or return an error
		$.post('/contribute/moderate/' + location_id + '/' + form_name, post_data, function(data) 
		{
			// Debug
			//console.log(data);
			
			// If success pattern is not found in data, show errors
			if(! pattern.test(data))
			{
				$(data).insertAfter('#' + form_name + ' .form_header');
			}
			
			// No errors - append submitted data to end of item list
			else
			{
				// assign insert id so user can view items after creating them
				insert_id = parseInt(data.replace('--postid=', ''));

				// if there is an image, we need to upload the image using insert_id
				if($('.uploadifyQueueItem').length > 0) {
					upload_location_image(item_id);
				}

				// If the form submitted is the main details form, disable on success
				if(form_name == 'location_details') {
					$(	'#' + form_name + ' form input, ' 	+ 
						'#' + form_name + ' form select, ' 	+
						'#' + form_name + ' textarea')
							.attr("disabled", "disabled")
							.css('color', 'gray');
							
					$('#' + form_name + ' form .fs-ajax-location')
						.removeClass('.fs-ajax-location')
						.addClass('cancel-hover disabled');
				}
				
				// If the form allows multiple inputs (i.e. weekly events, related links,
				// display grayed out entries for items that have been submitted
				else {
					$.post('/contribute/success/' + location_id + '/' + form_name + '/' + insert_id, post_data, function(data) {
						// Debug
						//console.log(data);

						// Apend data to list below form
						$('#' + form_name + ' .modify-wrapper').append(data);
						
						// Reset form
						$('#' + form_name + ' form').get(0).reset()
					});
				}
			}
		});
		
		e.preventDefault();
	});

	// select existing item and make it editable
	$('.modify-link').live('click', function(e)
	{	
		var action = $(this).attr('action');
		var type = $(this).attr('type');
		var status = $(this).attr('status');
		var form = $(this).parents('.form').attr('id');
		var hasImage = $(this).attr('has_image');
		var imgBaseUrl = $(this).parents('.form').attr('baseImageUrl');
		var insert_id = $(this).attr('insert_id');
		
		// If an item has just been created the id will be stored in insert_id attribute
		// existing items have their id populated in rel attribute
		var itemID = insert_id
			? insert_id
			: $(this).attr('rel');
		
		// Check if the link clicked was edit or delete
		if(action == 'edit')
		{	
			// Get data associated with the link that was clicked	
			$.getJSON('/contribute/getdata/' + itemID + '/' + type + '/' + status, function(data) 
			{
				// Debug
				//console.log(data);
				
				switch (form)
				{
					case 'weekly_events':

						// Populate form with data / image (if applicable)
						$('#' + form + ' select option[value="' + data.weekday + '"]').attr('selected', 'selected');
						$('#' + form + ' input[name="name"]').val(data.title);
						$('#' + form + ' textarea[name="description"]').val(data.description);
						$('#' + form + ' input[name="event_id"]').val(data.ID);

						if (hasImage == 1) {
							$('#' + form + ' img.location-image-logo')
								.attr('src', imgBaseUrl + itemID + '_thumb.jpg')
								.show();
						} else {
							$('#' + form + ' img.location-image-logo')
								.hide();
						}

						// if the item has not yet been approved, do not allow edit - only allow delete
						if(data.approved == 0)
						{
							$(	'#' + form + ' form input, ' 	+ 
								'#' + form + ' form select, ' 	+
								'#' + form + ' textarea')
									.attr("disabled", "disabled")
									.css('color', 'gray');
									
							$('#' + form + ' form .fs-ajax-location')
								.removeClass('.fs-ajax-location')
								.addClass('cancel-hover disabled');
						}
						
						// If we've disabled the form and the user selects an approved item, re-enable form
						else {
							$(	'#' + form + ' form input, ' 	+ 
								'#' + form + ' form select, ' 	+
								'#' + form + ' textarea')
									.removeAttr('disabled')
									.css('color', '#333');
									
							$('#' + form + ' form .fs-ajax-location')
								.addClass('.fs-ajax-location')
								.removeClass('cancel-hover disabled');
						}
					
					break;
					
					case 'related_links':
					
						// Populate form with data
						$('#' + form + ' select option[value="' + data.website_id + '"]').attr('selected', 'selected');
						$('#' + form + ' input[name="elsewhere_link"]').val(data.website);
						$('#' + form + ' input[name="elsewhere_id"]').val(data.elsewhere);
						
						// if the item has not yet been approved, do not allow edit - only allow delete
						if(data.approved == 0)
						{
							$(	'#' + form + ' form input, ' 	+ 
								'#' + form + ' form select')
									.attr("disabled", "disabled")
									.css('color', 'gray');
									
							$('#' + form + ' form .fs-ajax-location')
								.removeClass('.fs-ajax-location')
								.addClass('cancel-hover disabled');
						}
						
						// If we've disabled the form and the user selects and approved item, reenable form
						else {
							$(	'#' + form + ' form input, ' 	+ 
								'#' + form + ' form select')
									.removeAttr('disabled')
									.css('color', '#333');
									
							$('#' + form + ' form .fs-ajax-location')
								.addClass('.fs-ajax-location')
								.removeClass('cancel-hover disabled');
						}
						
					break;
				}
			});
		}
		
		else if (action == 'delete')
		{
			//console.log('delete');
		}
		
		e.preventDefault();

	});

	$('#featured_event_upgrade').click(function(e) {
		$('.upgrade_to_featured').slideToggle();
		$(this).hide();
		e.preventDefault();
	});





