<?php
/**
* @package linea21\plugins\l21_prettyDate
* @author $Author$ - Simon Georget <simon@linea21.com>
* @version $Id$ 
* @access public
* @license http://opensource.org/licenses/gpl-3.0.html
*/
_debug(__FILE__ . ' loaded');
global $plug;

// we load javascript file
// The file include 4 locales :  grunt embedLocales --embedLocales fr,it,es,en
// footerAddJS($plug->getVar('relative_url').'js/moment-2.9.0/min/moment-with-customlocales-min.js');
footerAddJS($plug->getVar('relative_url').'js/moment.min.js');

// We prepare the JS content
$js = 	'moment.locale("'.U_L.'");

				 $( ".published_by" ).each(function( index ) {

				 		var $el 				=  $(this).find("span.published-on");
				 		var $el2 				=  $(this).find("span.date");

						if($($el2).attr("data-published-date")) {
							var dateZ 			= 	$($el2).attr("data-published-date");
							var format = "detailed";
						} else {
							var dateZ 			= 	$el2.text();
							var format = "simple";
						}
						// alert(format + " - " + dateZ);
						
						// if dateZ does not exists we exit
						if(!dateZ) return;
						var zone = "Europe/Paris";

						if (format == "detailed") 
							var prettyDate 	= 	moment(dateZ, "DD-MM-YYYY hh:mm:ss").fromNow();
						else 
							var prettyDate 	= 	moment(dateZ, "'.strtoupper(DATE_FORMAT).'").fromNow();
							
						$el.replaceWith("<span class=\"pretty-published-on\">'._t('statut', 'published_on'). '</span>");
						$el2.replaceWith("<span class=\"pretty-date\">" + prettyDate + "</span>");

				 		// finally we attach the original date as title to the span
						$(this).find("span.pretty-date").attr("title", dateZ);

				});

      	';

footerAddInlineJS($js);
?>