<?php
/**
 * SocialEngine
 *
 * @category   Application_Core
 * @package    Authorization
 * @copyright  Copyright 2006-2020 Webligo Developments
 * @license    https://www.socialengine.com/license/
 * @version    $Id: update-member-level.tpl 9747 2012-07-26 02:08:08Z john $
 * @author     John
 */
?>
<script>
	scriptJquery(document).ready(function () {
		var selectedId = scriptJquery("#image_id").val();
		if (selectedId) {
			var radio = scriptJquery(".user_avatar_popup").find("input[name='avatar'][value='" + selectedId + "']");
			if (radio.length) {
				radio.prop("checked", true);
				radio.closest('label, li').addClass('selected_avatar');
			}
		}
	});

	scriptJquery(document).off('submit', '#user_avatar').on('submit', '#user_avatar', function(e) {
		e.preventDefault();
		
		var selected = scriptJquery(this).find("input[name='avatar']:checked");

		if (selected.length === 0) {
			alert('<?php echo $this->translate($this->string()->escapeJavascript("Please choose avatar.")); ?>');
			return false;
		} else {

			var avatarId = selected.val();
			var avatarUrl = selected.parent().find('img').attr('src');

			//Apply as background
			var $target = scriptJquery('#lassoImg');
			if ($target.is('img')) {
				//If it's an <img>
				$target.attr('src', avatarUrl);
			} else {
				//If it's div/span/etc
				$target.css({
					'background-image': 'url(' + avatarUrl + ')',
					'background-size': 'cover',
					'background-position': 'center'
				});
			}
			$target.removeClass('bg_item_nophoto');
			scriptJquery('#current-element').find('.image-preview-main').remove();

			scriptJquery("#image_id").val(avatarId);

			scriptJquery('#user_avatar_overlay').hide();
			scriptJquery('#ajaxsmoothbox_container').html("<div id='success_message' class='success_msg m-2'><span><?php echo $this->translate('Your avatar is successfully added.'); ?></span></div>");
			scriptJquery('#success_message').fadeOut("slow", function(){
				setTimeout(function() {
					ajaxsmoothboxclose();
					scriptJquery('.user_avatar_popup').remove();
				}, 1000);
			});
		}
	});
</script>
<div class="user_avatar_popup">
  <div class="core_loading_cont_overlay" id="user_avatar_overlay" style="display:none;"></div>
  <div class="user_avatar_popup_content">
    <?php if(empty($this->is_ajax) ) { ?>
      <?php echo $this->form->render($this);?>
    <?php } ?>
  </div>
</div>
<?php die; ?>