<?php
	$footerScripts .= "<script>
		$(document).on('click', '.admin', function (e) {
			var modalID = '#adminModal';
			var adminType = $(this).data('admintype');
			var adminAct = $(this).data('adminact');
			var editID = $(this).data('editid');
			var page = $(this).data('page');
			var section = $(this).data('section');
			var infoLink = 'admin/admin.modal.ajax.php';
			var processLink = 'admin/process/' + adminType + '.php';
			
			console.log('adminAct: ' + adminAct);
			console.log('editID: ' + editID);
			console.log('page: ' + page);
			console.log('section: ' + section);
			
			if (adminAct === 'delete') {
				var url = 'admin/process/'+ adminType +'.php';
				if(confirm('Are you would like to delete this ' + adminType + '?')) {
					$.ajax({
						type: 'POST',
						url: url,
						data: 'adminType=' + adminType + '&op=' + adminAct + '&page=' + page + '&section=' + section + '&editID=' + editID,
						dataType: 'json',
						async: true,
						cache: false,
						timeout: 10000,
						success: function(data) {
							if (data.output.updateOP === 'delete') {
								var updateItem = '';
								updateItem = updateItem  + '#' + data.output.update;
								console.log(updateItem);
								$(updateItem).animate({ backgroundColor: '#fbc7c7' }, 'fast').animate({ opacity: 'hide' }, 'slow');
							}
						},
						error: function() {
							console.log('ERROR');
						}
					});
					e.preventDefault();
				};
				return false;
			} else {
				//console.log(section);
				
				// Populate the Modal Form
				$.ajax({
					type: 'POST',
					url: infoLink,
					data: 'adminType=' + adminType + '&adminAct=' + adminAct + '&editID=' + editID + '&page=' + page + '&section=' + section,
					dataType: 'json',
					async: true,
					cache: false,
					timeout: 10000,
					success: function(content) {
						$(modalID).show();
						$('#adminModal .modal-title').show().html(content.content.title);
						$('#adminModal #formContent').show().html(content.content.content);
					}
				});
				
				// Slider Scripts
				$('.slider').each(function(i){
					$(this).slider({
						min: parseInt($(this).attr('min')), 
                        max: parseInt($(this).attr('max')), 
                        value: parseInt($(this).closest('td').find('input').val()),
                        step: parseInt($(this).attr('inc')),
                        slide: function( event, ui ) {
                            //$(this).closest('.sliderValue').val(ui.value);
							$(this).closest('td').find('input').val(ui.value);
                        }
                    });
				});
				
				// Validate & Process the Modal Form
				//var panel = $(this).closest('div.panel');
				//var currentId = $(this).closest('div.panel').attr('id');
				$('#adminForm').validate({
					errorClass: 'formError',
					submitHandler: function() {
						//console.log('SUBMIT');
						$.ajax({
							type: 'POST',
							url: processLink,
							data: $('#adminForm').serialize(),
							dataType: 'json',
							async: true,
							cache: false,
							timeout: 10000,
							success: function(data) {
								//console.log('SUCCESS');
								//reload_notifications();
								$('.modal').modal('hide');
								
								if (data.output.update){
									var updateItem = '#' + data.output.update;
									var updateSection = '#' + data.output.section;
									if (data.output.updateOP === 'delete') {
										$(updateItem).animate({ backgroundColor: '#fbc7c7' }, 'fast').animate({ opacity: 'hide' }, 'slow');
									} else if (data.output.updateOP === 'add') {
										if (data.output.updateString) {
											$(updateSection).append(data.output.updateString).hide().fadeIn('slow');
										}
									} else if (data.output.updateOP === 'edit') {
										if (data.output.updateString) {
											$(updateItem).fadeOut('slow', function(){
												$(this).replaceWith(data.output.updateString);
												$(this).fadeIn('slow');
											});
										}
									}
								} else {
									//console.log('Not Finding It');
								}
							},
							error: function() {
								console.log('ERROR');
								$('.modal').modal('hide');
								//$('.feedback.negative').slideDown(200).delay(3000).slideUp(200);
							}
						});
						return false;
					}
				});
			};
		});
		
		/*function reload_notifications() {
			$('#adminArea').load('". $URL ."/admin/inc/preview/notifications.php');
		}*/
		/*".  $modalScripts ."*/
	</script>";
?>