/**
 * @author aprian
 */


/*
 * Function Create Event Dialog
 * 		- elLink: element that trigger the dialog show up
 */
function createEventDialog(elLink){
	
	// link = trigger for the dialog	
	$(elLink).click(function(e) {
		
		// preparation
		e.preventDefault();
		$("#modal").empty();
		
		// get modal content using ajax
		var URL = $(this);
		var ID = URL.getUrlParam("id");
		
		$.get(
			"/application/library/cfc/eventProxy.cfc?id=" + ID,
			{method : 'getEventDialog', returnFormat : 'plain'},
			function(data){
				
				// put modal content in it's placeholder
				$("#modal").html(data);
				
				// open the modal
				$('#modal').dialog('open');		
				
			},'text');
		
	})
	
	$.fx.speeds._default = 1000;
	$.fx.speeds.fold = 1000;
	
	$("#modal").dialog({
		autoOpen: false,
		closeOnEscape: true,
		width: 675,
		minHeight: 400,
		show: 'fold',
		hide: 'blind',
		bgiframe: true,
		modal: true,
		resizable: false,
		position: ['center',50]
	});
		
}

/*
 * Function Create Event Form Dialog 
 * 		- elLink: element that trigger the dialog show up
 */
function createEventFormDialog(elLink){
	
	// link = trigger for the dialog	
	$(elLink).click(function(e) {
		
		// preparation
		e.preventDefault();
		$("#modal").dialog('destroy').empty();
		
		// get modal content using ajax
		var URL = $(this);
		var formTitle = URL.getUrlParam("a");
		var ID = URL.getUrlParam("id");
		
		createEventForm(formTitle,ID)
		
		$.fx.speeds._default = 1000;
		$.fx.speeds.fold = 1000;
		
		$("#modal").dialog({
			autoOpen: true,
			closeOnEscape: true,
			width: 450,
			minHeight: 150,
			show: 'fold',
			hide: 'blind',
			bgiframe: true,
			modal: true,
			resizable: false,
			position: ['center',175]
		});
	
	});
}


/*
 * Function Create Event Form
 * 		- formTitle : title of the form (rsvp, tellFriend)
 * 		- ID : ID of the events
 */
function createEventForm(formTitle,ID){
	
	var options = { 
        target: '.form_message',
        success: showResponse,
        dataType: 'json' 
    }; 
	
	// kill some notification
	delNotification();
	
	// get next and prev form css ID
	var prevFormId = $(".wrapper_form").attr('id');
	var nextFormId = 'wrapper_' + formTitle;
	
	if ( prevFormId !== nextFormId ){
		
		// get the form
		$.get(
			"/application/scripts/events/view/form-" + formTitle + ".cfm?id=" + ID,
			function(data){
				
				
				
				// put modal content in it's placeholder
				$("#modal").append(data);
				
				// get the animation, slideup prevFormId and slidedown nextFormId
				if( typeof(prevFormId)== "undefined" || prevFormId == ''){
					
					$("#" + nextFormId).slideDown(1100);
						
				} 
				else {
				
					$("#" + prevFormId).slideUp(800, function(){
						
							$("#" + nextFormId).slideDown(1100, function(){
								$("#" + prevFormId).remove();
							});
							
					});
					
				}
				
				// bind ajax submit event
				$("#form_" + formTitle).submit(function(){
					delNotification();
					$(this).ajaxSubmit(options);
					return false;
				});
				
			},'text');
			
	}
			
}

/*
 * Function Get Event Form
 * 		- elLink: element that trigger the dialog show up
 * 
 */
function getEventForm(elLink){
	
	$(elLink).live('click', function(e){
			
	
		/*
		 	preparation
		 		- prevent link default action
		 		- get ID & formTitle to define which form to be called
		 		- define ajax form submit options
		*/ 
		e.preventDefault();
		var URL = $(this);
		var formTitle = URL.getUrlParam("a");
		var ID = URL.getUrlParam("id");

		createEventForm(formTitle,ID);
					
	});
}

/* 
 * Function showResponse after form submit
 */
function showResponse(response, statusText)  {
	
	if( response.ERROR == 1){
		var responseText = '<div class="notification error">' + response.MESSAGE + '</div>';
	}
	else {
		var responseText = '<div class="notification success">' + response.MESSAGE + '</div>';
		$(".wrapper_form form").slideUp(800, function(){
			$(this).parent().remove()
		});
	}
	$(".wrapper_form").before(responseText);
	$(".notification").fadeIn(800);
	
};	


/*
 * Function delete notification created after form submit
 */
function delNotification(){
	$(".notification").fadeOut(200, function(){
		$(this).remove();
	});
}