// No librairy conflict
jQuery.noConflict();
     
// HrefBase pour le domaine en cours
// Les autres GLOBALS sont :
//  - espace_type
//  - espace_code
var base = jQuery('base').attr('href');

jQuery(document).ready(function(){
    // Initialise le menu
   jQuery("#aot-scroll-page-left-menu").aotMenu({
    level_0: '.aot-menu-level_0',
    level_0_sub: '.aot-menu-level_0_sub',
    level_1: '.aot-menu-level_1',
    level_1_sub: '.aot-menu-level_1_sub',
    level_2: '.aot-menu-level_2',
    active: 'active'
   });
   
   jsAutoRo();
   
   // News handling => icone images-multiples
   if (jQuery('#news-single-img2 img').length >= 1) {
	   jQuery('.news-single-imgcaption-multi').html((jQuery('#news-single-img2 img').length+1))
	                                          .attr('title',"Cliquez pour voir les "+(jQuery('#news-single-img2 img').length+1)+" images associées a cette actualité.");
   }else {
     jQuery('.news-single-imgcaption-multi').html('1')
                                            .attr('title',"Cliquez pour voir l'image associée a cette actualité.");
   }
   jQuery('.news-single-imgcaption>*').click(function() {jQuery(this).parent().parent().find('a[rel]').click();});
   jQuery('.news-single-imgcaption').click(function() {jQuery(this).parent().find('a[rel]').click();});
   
   jQuery("ul.aot-page-main-stdbloc-tabs").tabs("div.aot-page-main-stdbloc-panes > div");

});

function jsAutoRo() {
  // Allow js-auto-ro => rollover pour les images.
   jQuery('.js-auto-ro>img, img.js-auto-ro').filter('img').each(function() {
      var tmp = jQuery(this).attr('src');
      var path = tmp;
      // Soustraction du chemin
      tmp = tmp.split('/');
      file = tmp[tmp.length-1];
      // Séparation du nom de fichier et de l'extension
      file = file.split('.');
      ext = file[file.length-1];
      file = file[0];
      // Reduction du nom de fichier
      file = file.split('_');
      if (file[0] == 'ro') {
        // Si ro_xxxx_yyyy => xxxx est le nom de reference
        file = file[1];
      }else {
        // Si xxxx_yyyy => xxxx est le nom de réference
        file = file[0];
      }
      // Répertoire
      path = path.split('/');
      var tpath = '';
      for (i=0; i < (path.length-1); i= i+1) {
        if (i > 0) {
          tpath = tpath+'/';
        }
        tpath = tpath+path[i];
      }
      
      // Tentative de récupération de l'image RO dans le même répertoire...
      fetchRoImg(tpath,file,ext,this);
      
      // Sauvegarde du chemin etat NO
      jQuery(this).attr('no',jQuery(this).attr('src'));
   });
   // Bind du rollover
   jQuery('.js-auto-ro').live('mouseenter',function() {
      jQuery(this).filter('img').attr('src',jQuery(this).attr('ro'));
      jQuery(this).find('img').attr('src',jQuery(this).find('img').attr('ro'));
   }).live('mouseleave',function() {
      jQuery(this).find('img').attr('src',jQuery(this).find('img').attr('no'));
      jQuery(this).filter('img').attr('src',jQuery(this).attr('no'));
   });
}

function fetchRoImg(path,file,ext,obj) {
  jQuery.ajax({
    type : 'GET',
    url  : base+path+'/ro_'+file+'.'+ext,
    error: function() {
      var tmp = path.split('/');
      if (tmp[0] != 'fileadmin' || (tmp[tmp.length-1] != '32x32' && tmp[tmp.length-1] != '17x17')) {
        fetchRoImg('fileadmin/templates/gui/images/'+espace_type+'/btn/32x32',file,ext,obj);
      }else if (tmp[tmp.length-1] != '17x17') {
        fetchRoImg('fileadmin/templates/gui/images/'+espace_type+'/btn/17x17',file,ext,obj);
      }else {
        // Pas d'image RO disponible => RO == NO
        jQuery(obj).attr('ro',jQuery(obj).attr('no'));
      }
    },
    success: function() {
      jQuery(obj).attr('ro',path+'/ro_'+file+'.'+ext);
    }        
  });
}
