/**
  * [PUBLIC] Draw an Image Gallery
  *
  * @param STRING $y_title									:: a title for the gallery
  * @param STRING $y_dir										:: path to scan
  * @param *OPTIONAL[yes/no] $y_process_previews_and_images	:: If = 'yes', will create previews for images and videos (movies) and will create conformed images
  * @param *OPTIONAL[yes/no] $y_remove_originals				:: If = 'yes', will remove original (images) after creating previews [$y_process_previews_and_images must be yes]
  * @param *OPTIONAL[>=0]	$y_display_limit				:: Items limit to display
  */
 public function draw($y_title, $y_dir, $y_process_previews_and_images = 'no', $y_remove_originals = 'no', $y_display_limit = '0')
 {
     //--
     $y_title = (string) $y_title;
     //--
     $y_dir = (string) $y_dir;
     //--
     $y_process_previews_and_images = (string) $y_process_previews_and_images;
     if ((string) $y_process_previews_and_images != 'yes') {
         $y_process_previews_and_images = 'no';
     }
     //end if
     //--
     $y_display_limit = Smart::format_number_int($y_display_limit, '+');
     //--
     //--
     if ((string) $this->use_secure_links == 'yes') {
         if ((string) $this->secure_download_link == '' or (string) $this->secure_download_ctrl_key == '') {
             return '<h1>WARNING: Media Gallery / Secure Links Mode is turned ON but at least one of the: download link or the controller was NOT provided ...</h1>';
         }
         //end if
     }
     //end if
     //--
     //--
     if (!SmartFileSysUtils::check_file_or_dir_name($y_dir)) {
         return '<h1>ERROR: Invalid Folder for Media Gallery ...</h1>';
     }
     //end if
     //--
     $y_dir = SmartFileSysUtils::add_dir_last_slash($y_dir);
     SmartFileSysUtils::raise_error_if_unsafe_path($y_dir);
     //--
     if (!is_dir($y_dir)) {
         return '<h1>WARNING: The Folder for Media Gallery does not exists ...</h1>';
     }
     //end if
     //--
     //--
     $this->gallery_items = 0;
     //--
     //-- constraint of params
     if ((string) $y_process_previews_and_images != 'yes') {
         $y_remove_originals = 'no';
     }
     //end if
     //--
     if (strlen($this->preview_formvar) > 0) {
         // avoid processing if it is displayed in a form
         $y_process_previews_and_images = 'no';
         $y_remove_originals = 'no';
     }
     //end if
     //--
     //-- some inits ...
     $out = '';
     $arr_files = array();
     $processed = 0;
     //--
     //--
     $arr_storage = (array) (new SmartGetFileSystem(true))->get_storage($y_dir, false, false);
     $all_mg_files = (array) $arr_storage['list-files'];
     //--
     //--
     for ($i = 0; $i < Smart::array_size($all_mg_files); $i++) {
         //--
         $file = (string) $all_mg_files[$i];
         $ext = strtolower(SmartFileSysUtils::get_file_extension_from_path($file));
         //--
         if (substr($file, 0, 1) != '.' and strpos($file, '.#') === false and strpos($file, '#.') === false) {
             //--
             if (is_file($y_dir . $file) and ((string) $ext == 'jpeg' or (string) $ext == 'jpg' or (string) $ext == 'gif' or (string) $ext == 'png')) {
                 //--
                 if (SmartFileSysUtils::version_check($file, 'mg-preview')) {
                     //-- it is an image preview file
                     if (!is_file($y_dir . SmartFileSysUtils::version_add($file, 'mg-image'))) {
                         SmartFileSystem::delete($y_dir . $file);
                         // remove preview if orphan
                     }
                     //end if
                     //--
                 } elseif (SmartFileSysUtils::version_check($file, 'mg-image')) {
                     //-- it is an image file
                     if ((string) $y_process_previews_and_images == 'yes') {
                         //--
                         $tmp_file = $y_dir . SmartFileSysUtils::version_add($file, 'mg-preview');
                         //--
                         if (!is_file($tmp_file)) {
                             //--
                             $out .= $this->img_preview_create($y_dir . $file, $tmp_file) . '<br>';
                             $processed += 1;
                             //--
                         }
                         //end if
                         //--
                     }
                     //end if
                     //--
                     $arr_files[] = $file;
                     $this->gallery_items += 1;
                     //--
                 } elseif (SmartFileSysUtils::version_check($file, 'mg-vpreview')) {
                     //-- it is a movie preview file
                     if (stripos($file, '.#tmp-preview#.jpg') === false) {
                         //--
                         $tmp_linkback_file = SmartFileSysUtils::get_noext_file_name_from_path(SmartFileSysUtils::version_remove($file));
                         //--
                         if (!is_file($y_dir . $tmp_linkback_file)) {
                             SmartFileSystem::delete($y_dir . $file);
                             // remove if orphan
                         }
                         //end if
                         //--
                     }
                     //end if
                     //--
                 } else {
                     // unprocessed image
                     //--
                     if ((string) $y_process_previews_and_images == 'yes') {
                         //--
                         $tmp_file = $y_dir . SmartFileSysUtils::version_add($file, 'mg-image');
                         //--
                         if (!is_file($tmp_file)) {
                             //--
                             if ((string) $y_dir . $file != (string) $y_dir . strtolower($file)) {
                                 SmartFileSystem::rename($y_dir . $file, $y_dir . strtolower($file));
                                 // make sure is lowercase, to be ok for back-check since versioned is lowercase
                             }
                             //end if
                             //--
                             $out .= $this->img_conform_create($y_dir . $file, $tmp_file) . '<br>';
                             $processed += 1;
                             //--
                         } else {
                             //--
                             if ((string) $y_remove_originals == 'yes') {
                                 //--
                                 SmartFileSystem::delete($y_dir . $file);
                                 $out .= '<table width="550" bgcolor="#FF3300"><tr><td>removing original image: \'' . Smart::escape_html($file) . '\'</td></tr></table><br>';
                                 $processed += 1;
                                 //--
                             }
                             //end if
                             //--
                         }
                         //end if else
                         //--
                     }
                     //end if
                     //--
                 }
                 //end if else
                 //--
             } elseif (is_file($y_dir . $file) and ((string) $ext == 'webm' or (string) $ext == 'ogv' or (string) $ext == 'mp4' or (string) $ext == 'mov' or (string) $ext == 'flv')) {
                 // WEBM, OGV, MP4, MOV, FLV
                 //-- process preview FLV / MOV ...
                 if ((string) $y_process_previews_and_images == 'yes') {
                     //--
                     $tmp_file = $y_dir . SmartFileSysUtils::version_add($file, 'mg-vpreview') . '.jpg';
                     //--
                     if (!is_file($tmp_file)) {
                         //--
                         if ((string) $y_dir . $file != (string) $y_dir . strtolower($file)) {
                             SmartFileSystem::rename($y_dir . $file, $y_dir . strtolower($file));
                             // make sure is lowercase, to be ok for back-check since versioned is lowercase
                         }
                         //end if
                         //--
                         $out .= $this->mov_preview_create($y_dir . strtolower($file), $tmp_file) . '<br>';
                         $processed += 1;
                         //--
                     }
                     //end if
                     //--
                 }
                 //end if
                 //--
                 $arr_files[] = $file;
                 $this->gallery_items += 1;
                 //--
             }
             //end if else
             //--
         }
         //end if
         //--
     }
     //end for
     //--
     //--
     $out .= '<!-- START MEDIA GALLERY -->' . "\n";
     //--
     if ((string) $this->use_styles == 'yes') {
         $out .= '<div id="mediagallery_box">' . "\n";
     }
     //end if
     //--
     //--
     $out_arr = array();
     //--
     if ($processed <= 0) {
         //--
         $arr_files = Smart::array_sort($arr_files, 'natsort');
         //--
         $max_loops = Smart::array_size($arr_files);
         if ($y_display_limit > 0) {
             if ($y_display_limit < $max_loops) {
                 $max_loops = $y_display_limit;
             }
             //end if
         }
         //end if
         //--
         for ($i = 0; $i < $max_loops; $i++) {
             //--
             $tmp_the_ext = strtolower(SmartFileSysUtils::get_file_extension_from_path($arr_files[$i]));
             // [OK]
             //--
             if ((string) $tmp_the_ext == 'webm' or (string) $tmp_the_ext == 'ogv' or (string) $tmp_the_ext == 'mp4' or (string) $tmp_the_ext == 'mov' or (string) $tmp_the_ext == 'flv') {
                 $out_arr[] = $this->mov_draw_box($y_dir, $arr_files[$i], $tmp_the_ext);
             } else {
                 $out_arr[] = $this->img_draw_box($y_dir, $arr_files[$i]);
             }
             //end if
             //--
         }
         //end for
         //--
         $out .= '<div title="' . Smart::escape_html($this->gallery_show_counter) . '">' . "\n";
         //--
         if ((string) $y_title != '') {
             $out .= '<div id="mediagallery_title">' . Smart::escape_html($y_title) . '</div><br>';
         }
         //end if
         $out .= '<div id="mediagallery_row">';
         for ($i = 0; $i < Smart::array_size($out_arr); $i++) {
             $out .= '<div id="mediagallery_cell">';
             $out .= $out_arr[$i];
             $out .= '</div>' . "\n";
         }
         //end for
         $out .= '</div>';
         //--
         $out .= '</div>' . "\n";
         //--
     }
     //end if
     //--
     $out_arr = array();
     //--
     //--
     if ((string) $this->use_styles == 'yes') {
         $out .= '</div>' . "\n";
     }
     //end if
     //--
     $out .= '<!-- END MEDIA GALLERY -->' . "\n";
     //--
     //--
     if ((string) SMART_FRAMEWORK_DEBUG_MODE != 'yes') {
         if ($processed > 0) {
             $out = '<img src="' . $this->pict_reloading . '" alt="[Reloading Page ...]" title="[Reloading Page ...]"><script type="text/javascript">setTimeout(function(){ self.location = self.location; }, 2000);</script>' . '<br><hr><br>' . $out;
             define('SMART_FRAMEWORK__MEDIA_GALLERY_IS_PROCESSING', $processed);
             // notice that the media galery is processing
         }
         //end if
     }
     //end if
     //--
     //--
     return $out;
     //--
 }