IntuiCat.CGI = 'cp';

IntuiCat.PageUpdate_Obj_Shell = function (element, property, type, value){ // Implémenter pour debug
};

var CP = {  /* == this sauf exception*/ 
  menuAlter: function(event, menu, on){
    if (menu.tagName.toLowerCase() == 'h1'){
      
      // couleur de police
      menu._color = menu._color || menu.getStyle('color');
      menu.setStyle({color: (on ? (['contact', 'coords', 'lettre', 'charte'].include(menu.id) ? 'black' : 'white') : menu._color)})
      
      // préfixe '>' et image
      var img = menu.down('img')
        , lImg = img && img.id !== 'imgrand'
        , span = menu.down('span');
      if (on) {
        if (!span) {
          span = '<span style="font-weight:bold; clear:left;">&gt;&nbsp;</span>';
          if (img) {
            if (lImg) {
              img.insert({after:span});
              if (Prototype.Browser.IE && ! menu.hasClassName('axe')) {
                menu._paddingTop = menu.getStyle('paddingTop');
                menu.setStyle({paddingTop:0});
              }
            }
            img.show();
          } 
          else {
            menu.innerHTML = span + menu.innerHTML;
          }
		      menu.siblings().each(function(menus){return this.menuAlter(event, menus);}, this); /* éteint les menus voisins */
      	  if (['moteur', 'editorial', 'agenda', 'lettre', 'contact'].include(menu.id))
            $$('h1.axe').each(function(axe){return this.menuAlter(event, axe);}, this); /* éteint les menus d'axe */
          $('docPreview').hide();
        }
      }
      else {
        if(span) {span.remove();} 
        if(lImg){
          if(Prototype.Browser.IE && menu._paddingTop) menu.setStyle({paddingTop: menu._paddingTop});
          img.hide();
        }
      }
  
      // contenu
      var content = $(menu.id + 'Content');
      if (event && content){
        if(on){
          $('editorialContent').hide();
          $('articleContent').hide();
          content.show(); // 
        }
        else content.hide();
      }
    }
  },
  
 axeMenuAlter: function(event, td, high) {  /* allume | éteint le picto d'axe */
   var menu = $(td.className);
   menu = menu ? menu : $(td.className.replace('row', '').strip());
   if (menu) this.menuAlter.bind(this)(event, menu, high);
  },

  menuOver: function(event, menu){
    menu.menuOverTO = this.menuAlter.bind(this).delay(.20, event, menu, true);
  },

  menuOut: function(event, menu){
    window.clearTimeout(menu.menuOverTO);
  },
  
  menuObserve: function(menu){
    menu.observe('mouseover', this.menuOver.bindAsEventListener(this, menu))
        .observe('mouseout', this.menuOut.bindAsEventListener(this, menu));
  },

/* relatedTarget 
Used to identify a secondary EventTarget related to a UI event. Currently this attribute is used 
with the mouseover event to indicate the EventTarget which the pointing device exited 
and with the mouseout event to indicate the EventTarget which the pointing device entered.
*/
  
  tdOver: function (event, td){ /* la souris ENTRE dans une cellule de tableau */
    if (!td._up){
      td.setStyle({backgroundColor:td.backgroundColor()});
      this.axeMenuAlter.bind(this)(event, td, true);
      td._up = true;
    }
    td._previewTO = this.docPreviewShow.bind(td).delay(.8); // seconde
  },
  
  tdOut: function (event, td){  /* la souris SORT d'une cellule de tableau */
    var rel = event.relatedTarget;
    if (td._up && ! (rel == td || rel.descendantOf(td))) {
      if (td._keepState) td._keepState = false; /*cf. this.docPreviewShow()*/
      else this.axeMenuAlter.bind(this)(event, td, false);
      td.setStyle({backgroundColor:'transparent'});
      td._up = false;
    }
    if (td._previewTO) window.clearTimeout(td._previewTO);
  },
  
  tdClick: function(event, td){
    if (td._previewTO) window.clearTimeout(td._previewTO);
  },
  
  tdObserve: function (td){
   var anchor = td.down('a');
   if (anchor) {td._cpdoc = anchor.href.replace(/.*CPDoc=(\d+)/, '$1');}
   td.observe('mouseover', this.tdOver.bindAsEventListener(this, td))
     .observe('mouseout', this.tdOut.bindAsEventListener(this, td))
     .observe('click', this.tdClick.bindAsEventListener(this, td))
     .setStyle({cursor:'pointer'})
     .backgroundColor = function (){
       return (this.className.indexOf('enjeux') > -1) ? '#a8b5b9' 
        : ((this.className.indexOf('actes') > -1) ? '#fbb988' 
        : '#ece3c1');
      }.bind(td);
     ;
  },
  
  docPreviewShow: function(){ /*this == td*/
   if (this._cpdoc) {
    new Ajax.Request('DocPreviewGet.cp?CPDoc=' + this._cpdoc,{
      asynchronous:false, /* "Since synchronous usage is rather unsettling, and usually bad taste, you should avoid changing this. Seriously." - I did it! */
      method:'get',
      onComplete:function(transport){
        var contOffsets = $('contents').cumulativeOffset();
        var thisOffsets = this.cumulativeOffset();
        var docPreview = $('docPreview');
        docPreview.setStyle({
             top: parseInt(thisOffsets.top - this.getHeight()/2 - contOffsets.top, 10) + 'px'
           , left: parseInt(thisOffsets.left - (docPreview.getWidth()-this.getWidth())/2 - contOffsets.left, 10) + 'px'});
        $('docPreviewContent').update(transport.responseText.stripScripts()).setStyle({backgroundColor:this.getStyle('backgroundColor')});
        this._keepState = true;
        docPreview.show(); /*Effect.Appear(docPreview, {duration:.25})*/
      }.bind(this)
    });
   }
  },

  formSubmit: function (form, reply){
   reply = $(reply);
   reply.update('Requête en cours ...').show();
   form = $(form);
   if(form){
     if (! form.butteauxcailles) {form.insert('<input type="hidden" name="butteauxcailles" value="butteauxcailles">');}
     return form.request({
      asynchronous:false,
      onComplete:function(transport){
         var response = transport.responseText.stripScripts()
          , warning = '/!\\'
          , lwarning = response.startsWith(warning);
         response = lwarning ? response.substr(warning.length) : response;
       if (response.stripTags() == response) {reply.setStyle({color: lwarning ? 'red' : 'green'});}
       reply.update(response);
       return lwarning;
       }
      }
     )    
   }
  },

  commentSubmit: function(){
    var form = $('commentForm');
    var reply = document.createElement('div');
    form.insert({after:reply});
    if (this.formSubmit(form, reply)) {form.hide();}
  },

  pageImgObserve: function(img, srcOn, srcOff){
   	img.observe('mouseover', this.pageImgOver.bindAsEventListener(this, img, 'Images/' + srcOn));
   	img.observe('mouseout', this.pageImgOut.bindAsEventListener(this, img, 'Images/' + srcOff));
  },
  
  pageImgOver: function(event, img, src){
  	img.src = src;
  },
  pageImgOut: function(event, img, src){
  	img.src = src;
  },
  
  bodyOnLoad: function (){ /* charge les handlers d'événements*/
   $$('h1.menu').each(this.menuObserve, this); /*h1.axe*/
   $$('td').each(this.tdObserve, this);
   new Draggable('docPreview');
   $$('a>img.pages.oldest').each(function(img){this.pageImgObserve(img,'retourdebut-on.png','retourdebut-off.png')}, this);
   $$('a>img.pages.older').each(function(img){this.pageImgObserve(img,'retour-on.png','retour-off.png')}, this);
   $$('a>img.pages.newer').each(function(img){this.pageImgObserve(img,'avance-on.png','avance-off.png')}, this);
   $$('a>img.pages.newest').each(function(img){this.pageImgObserve(img,'avancefin-on.png','avancefin-off.png')}, this);
  }
}
CP.bodyOnLoad.bind(CP)();