var $ = jQuery;
$(document).ready( function() {

	$('#custom-event-next').click(function() {
		
		var name 		= $('#event-name').val();
		var desc 		= $('#event-desc').val();
		var address 	= $('#event-placedaddress').val();
		var startDate 	= $('#event-edtsd').val();
		var endDate 	= $('#event-edted').val();
		
		if(name != '' && desc != '' && address != '' && startDate != '' && endDate != '') {
			$('#custom-event-details').slideUp('slow');
			$('#custom-event-privacidad').slideDown('slow');
			$('#event-create-tabs li').eq(0).removeClass();
			$('#event-create-tabs li').eq(1).addClass('current');
		} else {
			$("#content-error").click();
		}
		
	});
	
	$('#custom-event-return').click(function() {
		$('#custom-event-details').slideDown('slow');
		$('#custom-event-privacidad').slideUp('slow');
		$('#event-create-tabs li').eq(0).addClass('current')
		$('#event-create-tabs li').eq(1).removeClass();
	});
	
	$("#content-error").fancybox({
		'autoScale' : true,
		'closeClick': true,
		'content': $('#content-error').text(),
		'padding': 15,
	});
	
	$('#invite-list li').click(function(){
		var friendId = $(this).attr('class');
		var eventId = $('#invite-list ul').attr('class');
		
		$('.'+friendId+' p').attr('id', 'loading-ajax-events');
		
		$.ajax({
			  type: "POST",
			  url: "wp-content/themes/cvp-irontec/events/custom/send-invites.php",
			  data: { 'idInvited': friendId.replace('friend-', ''), 'eventId' : eventId.replace('event-', '') }
			}).done(function( msg ) {
				$('.'+friendId+' p.invite').remove();
				$('.'+friendId+' p.send-invite').css('display','block');
				$('.'+friendId+' p').removeAttr('id', 'loading-ajax-events');
			});
	});
	
	$('#content-middle-add #comments-event-delete').click(function(){
		var commentDeleteId = $(this).attr('class').replace('comment-delete-', '');
		var commentEventId  = $('#comment-event-' + commentDeleteId);
		
		$('#'+commentEventId.attr('id')).attr('class', 'loading-ajax-comments');
		
		$.ajax({
			  type: "POST",
			  url: "wp-content/themes/cvp-irontec/events/custom/view/comments-ajax.php?deleteComment=true",
			  data: { 'commentId': commentDeleteId }
			}).done(function( msg ) {
				$('#comment-event-'+commentDeleteId).slideUp(function(){
						$('#comment-event-'+commentDeleteId).remove();
				});
			});
		
	});
	
	$('#admins-list span').click(function(){
		var userId  = $(this).attr('class');
		var eventId = $(this).attr('id').replace('event-', '');
		
		$('.user-'+userId.replace('normal-user-', '')).attr('id', 'loading-ajax-events-members');
		
		$.ajax({
			  type: "POST",
			  url: "wp-content/themes/cvp-irontec/events/custom/admin/members-ajax.php?normalizeUser=true",
			  data: { 'userId': userId.replace('normal-user-', ''), 'eventId' : eventId.replace('event-', '') }
			}).done(function( msg ) {
				$('.user-'+userId.replace('normal-user-', '')+' img').slideUp(function(){
						$('.user-'+userId.replace('normal-user-', '')+' img').remove();
				});
				$('.user-'+userId.replace('normal-user-', '')+' h5').slideUp(function(){
					$('.user-'+userId.replace('normal-user-', '')+' h5').remove();
				});
				
				$('.user-'+userId.replace('normal-user-', '')+' #user-info-event').slideDown('slow');
				$('.user-'+userId.replace('normal-user-', '')).removeAttr('id');
			});
	});
	
	$('#members-list span').click(function(){
		var userId  = $(this).attr('class');
		var eventId = $(this).attr('id').replace('event-', '');
		
		if (userId.indexOf('user-banned') != '-1') {
			
			$('.event-user-'+userId.replace('user-banned-', '')).attr('id', 'loading-members');
			
			$.ajax({
				  type: "POST",
				  url: "wp-content/themes/cvp-irontec/events/custom/admin/members-ajax.php?bannedUser=true",
				  data: { 'userId': userId.replace('user-banned-', ''), 'eventId' : eventId.replace('event-', '') }
				}).done(function( msg ) {
				
					$('.event-user-'+userId.replace('user-banned-', '')+' img').slideUp(function(){
						$('.event-user-'+userId.replace('user-banned-', '')+' img').remove();
					});
					$('.event-user-'+userId.replace('user-banned-', '')+' h5').slideUp(function(){
						$('.event-user-'+userId.replace('user-banned-', '')+' h5').remove();
					});
					
					$('.event-user-'+userId.replace('user-banned-', '')+' #user-info-event-banned').slideDown('slow');
					$('.event-user-'+userId.replace('user-banned-', '')).removeAttr('id');
			});
			
		}
		
		if (userId.indexOf('user-admin') != '-1') {

			$('.event-user-'+userId.replace('user-admin-', '')).attr('id', 'loading-members');
			
			$.ajax({
				  type: "POST",
				  url: "wp-content/themes/cvp-irontec/events/custom/admin/members-ajax.php?admiUser=true",
				  data: { 'userId': userId.replace('user-admin-', ''), 'eventId' : eventId.replace('event-', '') }
				}).done(function( msg ) {
			
				$('.event-user-'+userId.replace('user-admin-', '')+' img').slideUp(function(){
					$('.event-user-'+userId.replace('user-admin-', '')+' img').remove();
				});
				$('.event-user-'+userId.replace('user-admin-', '')+' h5').slideUp(function(){
					$('.event-user-'+userId.replace('user-admin-', '')+' h5').remove();
				});
				
				$('.event-user-'+userId.replace('user-admin-', '')+' #user-info-event-admin').slideDown('slow');
				$('.event-user-'+userId.replace('user-admin-', '')).removeAttr('id');
				
			});
		}
		
	});
	
	$('#banned-list span').click(function(){
		var userId  = $(this).attr('class');
		var eventId = $(this).attr('id').replace('event-', '');
		
		if (userId.indexOf('user-banned') != '-1') {
			
			$('.event-user-'+userId.replace('user-banned-', '')).attr('id', 'loading-members');
			
			$.ajax({
				  type: "POST",
				  url: "wp-content/themes/cvp-irontec/events/custom/admin/members-ajax.php?userNoBanned=true",
				  data: { 'userId': userId.replace('user-banned-', ''), 'eventId' : eventId.replace('event-', '') }
				}).done(function( msg ) {
				
					$('.event-user-'+userId.replace('user-banned-', '')+' img').slideUp(function(){
						$('.event-user-'+userId.replace('user-banned-', '')+' img').remove();
					});
					$('.event-user-'+userId.replace('user-banned-', '')+' h5').slideUp(function(){
						$('.event-user-'+userId.replace('user-banned-', '')+' h5').remove();
					});
					
					$('.event-user-'+userId.replace('user-banned-', '')+' #user-info-event-banned').slideDown('slow');
					$('.event-user-'+userId.replace('user-banned-', '')).removeAttr('id');
			});
			
		}
		
	});
	
	$('.custom-event-list-generic').on('click', 'a' ,function(event){
	    
	    if(event.preventDefault){
	        event.preventDefault();
	    }else{
	        event.returnValue = false; 
	    };
	    
		var eventId     = $(this).attr('attr');
		var userId      = $(this).attr('id');
		var actionEvent = $(this).attr('class');
		
		if (actionEvent == 'join-event') {
			
			$(this).slideUp('slow');
			$('#'+eventId.replace('event-', 'eventbutton-')+' .participacion-status').attr('id','loading-event-participacion');
			$.ajax({
				  type: "POST",
				  url: "wp-content/themes/cvp-irontec/events/custom/participacion.php?join-event=true",
				  data: { 'userId': userId.replace('user-', ''), 'eventId' : eventId.replace('event-', '') }
				}).done(function( msg ) {
					
					$('#'+eventId.replace('event-', 'eventbutton-')+' .participacion-status').removeAttr('id','loading-event-participacion');
					$('#'+eventId.replace('event-', 'eventbutton-')+' .leave-event').slideDown('slow');
					
			});
		}
		
		if (actionEvent == 'leave-event') {
			
			$(this).slideUp('slow');
			$('#'+eventId.replace('event-', 'eventbutton-')+' .participacion-status').attr('id','loading-event-participacion');
			
			$.ajax({
				  type: "POST",
				  url: "wp-content/themes/cvp-irontec/events/custom/participacion.php?leave-event=true",
				  data: { 'userId': userId.replace('user-', ''), 'eventId' : eventId.replace('event-', '') }
				}).done(function( msg ) {
					
					$('#'+eventId.replace('event-', 'eventbutton-')+' .participacion-status').removeAttr('id','loading-event-participacion');
					$('#'+eventId.replace('event-', 'eventbutton-')+' a.join-event').slideDown('slow');
			});
		}
		
	});
	
	
	
});
