/**
 * @author DIGIO
 */
$(document).ready(function()
{
	// select source code for easy copy&paste //
    $('textarea.sourcecode').click(function()
    {
        $(this).select();
    });
	
	// set contact pop-up //
    $('#button-contact').click(function(){
		msgbox($(this.rel),'',{overlay:{stack:false}});
		return false;
    });
	
	//set share link by mail pop-up
    	$('#link_by_mail').click(function(){
		msgbox($('#mail_link_overlay'), '',{overlay:{stack:false}});
		$('#form_mail_link').yav({
			errorMessage: msg.ERRORMESSAGE,
			errorPosition: 'after',
			errorDiv: 'errorDiv_mail_link',
			custom: 
			{
				antiSpam: function(return_value)
				{
					var emailList = $('#' + return_value.id).val().replace(';', ',');
					var emailListLength = emailList.split(',').length;
					if(emailListLength > 5)
					{
						return return_value; //validation ERROR
					}
					else
					{
						return null; //validation OK	
					}
				}
			},
			onOk: function(the_form){
				$('.error').remove();
				$.post(noCache(the_form.action), $('#form_mail_link').serialize(), function(response)
				{
					var data= evalJson(response);
					
					if (data.stat == 'OK')
					{
						$('#errorDiv_mail_link')
							.append($('<p></p>')
								.addClass('ok')
								.text(msg.MAIL_SENT))
	                        .fadeIn('slow')
	                        .animate({opacity: 1}, 3000)
	                        .fadeOut('slow', function() {
								$(this).empty()
								try{
									msgbox_close('all');
								}catch(e){}
							});
					}
					else
					{
	                    $('#errorDiv_mail_link')
							.append($('<p></p>')
								.text(msg[data.msg]));
					}
				});
				return false;
			}
		})
		return false;
	}); 
	
	// handle "registration" button
	if($('#inscripcion').hasClass('external'))
	{
		$('#inscripcion').click(function() {
			msgbox_confirm(msg.REDIRECTION_NOTICE, msg.REDIRECTION_NOTIFY, function(result){
				if(result == true){
					window.open($('#inscripcion').attr('href'));
				}
			});
			return false;
		});
	}
	else
	{
		if($('#inscripcion').hasClass('do_login')) // 1) si el usuario no ha iniciado sesion:
		{
			$('#inscripcion').click(function(){
				document.location.href = $('#inscripcion').attr('href') + '?redirect_to=@event_detail@' + $('#eventid').val() + '@detail?open_registration';
				return false;
			});
		}
		else // 2) si el usuario esta autentificado:
		{
			if(!$('#inscripcion').hasClass('user_is_attending')) // 2.1) si el usuario aun no se ha inscrito:
			{
				if(!$('#inscripcion').hasClass('registration_form')) // 2.1.1) si el evento carece de formulario de inscripcion:
				{
					$('#inscripcion').click(function(){
						choose_attendance_type();
						return false;
					});

					// open registration form if user just signed up in order to register for the event
					if(document.location.href.indexOf('?open_registration') > 0)
					{
						$('#inscripcion').click();
					}
				}
				else // 2.1.2) si el evento tiene formulario de inscripcion:
				{
					$('#inscripcion').click(function(){
						msgbox_confirm(msg.REDIRECTION_NOTICE, msg.REGISTRATION_FORM_REDIRECTION, function(result){
							if(result == true){
								document.location.href = $('#inscripcion').attr('href');
							}
						});
						return false;
					});

					// open registration form if user just signed up in order to register for the event
					if(document.location.href.indexOf('?open_registration') > 0)
					{
						$('#inscripcion').click();
					}
				}
			}
		}
	}
});

// elegir tipo de asistencia e inscribirse en el evento
function choose_attendance_type(){
	$.get(noCache('/user_attending_events/prepare_attendance_form/' + $('#eventid').val()), function(response){
		var data = evalJson(response);
		
		if (data.stat == 'OK') {
			if (data.param.is_overlapped) {
				$('#forceAttending').addClass('required');
				$('#overlap_warning').show();
			}
			else {
				$('#forceAttending').removeClass('required');
				$('#overlap_warning').hide();
			}
			
			if (data.param.is_payment) {
				$('#payment_warning').show();
			}
			else {
				$('#payment_warning').hide();
			}
			
			$('#form_enrolment').yav({
				errorMessage: msg.ERRORMESSAGE,
				errorPosition: 'after',
				errorDiv: 'errorDiv_enrolment',
				onOk: function(the_form){
					msgbox_close('all');
					$('p.error', the_form).remove();
					
					// inscribir usuario a evento
					$.post(noCache(the_form.action), $(the_form).serialize(), function(response){
						var data = evalJson(response);
						if (data.stat == 'OK') {
							// 2.2) usuario ya inscrito:
							$('#inscripcion').attr({
								href: '/user/myattendings.html'
							});
							$('#inscripcion').text(msg.EDIT_ENROL);
							$('#inscripcion').unbind('click');
							
							msgbox_success(msg.SUCCESS);
						}
						else {
							msgbox_alert(msg[data.msg], false);
						}
					});
					
					return false;
				}
			}); 
			
			// open pop-up
			msgbox($('#form_enrolment_overlay'), '', {
				overlay: {
					stack: false
				}
			});
		}
		else {
			msgbox_alert(msg[data.msg], false);
		}
	});
}
