<?php 


function perfarttimeline_links__locale_block(&$vars) {
  // global $language;
  // foreach ($vars['links'] as $lang => $link) {
  //   $vars['links'][$lang]['title'] = $lang;
  //   // if($lang == $language->language)
  //   //   unset($vars['link'][$lang]);
  // }
  $content = theme_links($vars);
  return $content;
}


/**
 * Returns HTML for an image using a specific image style.
 *
 * @param $variables
 *   An associative array containing:
 *   - style_name: The name of the style to be used to alter the original image.
 *   - path: The path of the image file relative to the Drupal files directory.
 *     This function does not work with images outside the files directory nor
 *     with remotely hosted images. This should be in a format such as
 *     'images/image.jpg', or using a stream wrapper such as
 *     'public://images/image.jpg'.
 *   - width: The width of the source image (if known).
 *   - height: The height of the source image (if known).
 *   - alt: The alternative text for text-based browsers.
 *   - title: The title text is displayed when the image is hovered in some
 *     popular browsers.
 *   - attributes: Associative array of attributes to be placed in the img tag.
 *
 * @ingroup themeable
 */
function perfarttimeline_image_style($vars) {
  // dsm($vars);
  
  $real_path = $vars['path'];

  _perfarttimeline_getImageStyleDimensions($vars);
  $figure = '<figure style="width:'.$vars['width'].'px;height:'.$vars['height'].'px;">';

  $styles = array('grid_small', 'grid_medium', 'grid_large');
  if(in_array($vars['style_name'], $styles)){
    foreach ($styles as $style) {
      $vars['attributes']['class'] = 'image '.$style;
      if($style == $vars['style_name']){
        $vars['path'] = image_style_url($vars['style_name'], $real_path);
        $figure .= theme('image', $vars);
      }else{
        $hidden_vars = array_merge(array(), $vars);
        $hidden_vars['style_name'] = $style;
        _perfarttimeline_getImageStyleDimensions($hidden_vars);
        $hidden_vars['attributes']['path'] = image_style_url($style, $real_path);
        $hidden_vars['path'] = "";
        $figure .= theme('image', $hidden_vars);
      }
    }
  }else{
    $vars['attributes']['class'] = 'image '.$vars['style_name'];
    $vars['path'] = image_style_url($vars['style_name'], $real_path);
    $figure .= theme('image', $vars);
  }
  
  #gif
  $gifvariables = array_merge(array(), $vars);
  $gifvariables['path'] = '/'. drupal_get_path("theme", "perfarttimeline") .'/images/blank.gif';
  $gifvariables['attributes']['class'] = 'blank';
  $gifvariables['title'] = $gifvariables['alt'] = '';
  $figure .= theme('image', $gifvariables);

  #figcaption
  $figure .= '<figcaption>'.$vars['title'].'</figcaption>';

  $figure .= '</figure>';
  
  return $figure;
}

function _perfarttimeline_getImageStyleDimensions(&$vars){
  $dimensions = array(
    'width' => $vars['width'],
    'height' => $vars['height'],
  );
  image_style_transform_dimensions($vars['style_name'], $dimensions);
  $vars['width'] = $dimensions['width'];
  $vars['height'] = $dimensions['height'];
}

function perfarttimeline_menu_tree__main_menu($variables) {
  // dsm($variables);
  global $language;
  preg_match_all('/<li[^>]+><a[^>]+>/', $variables['tree'], $links);
  // dsm($links, 'links');
  if(isset($links[0])){
    foreach ($links[0] as $link) {
      preg_match('/href="([^"]+)"/', $link, $path);
      $normal_path = drupal_get_normal_path( preg_replace('/^\/fr\/|\/en\//', '', $path[1]) );
      // dsm($normal_path, 'normal_path');
      preg_match('/node\/([0-9]+)/', $normal_path, $matches);
      // dsm($matches2, 'matches2');
      if(isset($matches[1])){
        $node = node_load($matches[1]);
        if(in_array($node->type, array('temoignage', 'biographie', 'document_video'))){
          $prenom = field_get_items('node', $node, 'field_prenom', $language->language);
          $nom = field_get_items('node', $node, 'field_nom', $language->language);
          // dsm($nom, 'nom');
          // dsm($prenom, 'prenom');
          $new_link = preg_replace('/(<li[^>]+)>/', '${1} prenom="'.$prenom[0]['value'].'" nom="'.$nom[0]['value'].'">', $link);
          // dsm($new_link, 'new_link');

          $variables['tree'] = str_replace($link, $new_link, $variables['tree']);
        }
      }
    }
  }

  return '<ul class="menu main-menu">' . $variables['tree'] . '</ul>';
}



