
(function(){

  var $ = document.id;

  window.addEvents({
    'domready': function() {
      if(!Browser.Engine.trident) { init(); }
    },
    'load': function() {
      if(Browser.Engine.trident) { init(); }
    }
  });
  
  function init() {  
  
    // Focus
    if(document.hostname != 'blogg.mimersbrunn.se') {
      if($('loginCompte') != null) {
        ($('loginCompte').value == '') ? $('loginCompte').focus() : $('passwdCompte').focus();
      }
    }
  
    // Visionneuse  
    if($type(Carousel) == 'class' ) { new Carousel('last-pic-player', {autostart: true,mouseover: true}); }
    
    // Highlights
    //if($type(inMoo) == 'class' ) { new inMoo('inMoo', {duration: 750}); }
    if($('inMoo') != null) {
    
      var hl_cur = 0;  
      var hl_max = $$('ul#inMoo li').length;
      var hl_thumb = $('inMoo').getFirst('li').getWidth();
      $('inMoo').setStyle('width',hl_max * hl_thumb +'px');
      var inMooFx = new Fx.Morph($('inMoo'), {duration: 250, transition: Fx.Transitions.linear});
      var hl_ctx = $$('div#highlights_ctx div.ctx')[0].getWidth();
      var hl_show = parseInt(hl_ctx / hl_thumb);
      var hl_diff = ( (hl_ctx / hl_thumb) - parseInt(hl_ctx / hl_thumb) ) * hl_thumb;
      
      var hl_right = function() {
        $('inMooRight').removeEvent('click',hl_right);
        if((hl_cur+1) <= (hl_max -hl_show)) {
          inMooFx.cancel();         
          hl_cur++;
          hl_start_pos = $('inMoo').getStyle('margin-left').toInt();
          hl_end_pos = $('inMoo').getStyle('margin-left').toInt() - hl_thumb;
          if( (Math.abs(hl_start_pos) + hl_ctx) > (hl_max * hl_thumb - hl_thumb) ) { hl_end_pos = hl_end_pos + hl_diff; }
          inMooFx.start({'margin-left':[hl_start_pos,hl_end_pos]}).chain(function(){$('inMooRight').addEvent('click',hl_right);});
        } else {
          $('inMooRight').addEvent('click',hl_right);
        } 
      }
      
      var hl_left = function() {      
        $('inMooLeft').removeEvent('click',hl_left);      
        if(hl_cur > 0) {
          inMooFx.cancel();
          hl_cur--;         
          hl_start_pos = $('inMoo').getStyle('margin-left').toInt();
          hl_end_pos = $('inMoo').getStyle('margin-left').toInt() + hl_thumb;            
          if(Math.abs(hl_start_pos) < hl_thumb) { hl_end_pos = hl_end_pos - hl_diff; }            
          inMooFx.start({'margin-left':[hl_start_pos,hl_end_pos]}).chain(function(){$('inMooLeft').addEvent('click',hl_left);});
        } else {
          $('inMooLeft').addEvent('click',hl_left);
        }
      }
      
      $('inMooRight').addEvent('click',hl_right);
      $('inMooLeft').addEvent('click',hl_left);
      
    }    
    
    // Selects Custom
    if($type(Selects) == 'class' ) { new Selects($$('select.selectform')); }
    
    // affichage des carrés infos (bug IE6)
    if(Browser.Engine.trident4) {
      $$('span.help').extend($$('span.arrow')).each(function(it) {
        $(it).setStyle('display','block');
      });
    }
    
    // infobulle d'aide
    $$('span.help').each(function(item) {
      item.addEvents({
        'mouseover': function() {
          arrow = item.getNext('span.arrow');
          $$('#helptool table.tooltext td.mm')[0].set('html','<strong>'+$(item).getPrevious('a').get('html')+'</strong><span class="txt">'+$(item).get('html')+'</span>');
          $('helptool').setStyles({
            'top': this.getCoordinates().top  - 10 +'px', 
            'left': arrow.getCoordinates('global_ctx').left - $('helptool').getDimensions().width - 3 +'px' 
          });
          $('helptool').setStyle('display','block');    
        },
        'mouseleave': function() {
          $('helptool').setStyle('display','none');
        }
      });
    });
    
    // thumbnails
    $$('a.thumbnails').each(function(item) {
      item.addEvents({
        'mouseenter': function() {
          var thumb_url = item.getChildren('img')[0].src.replace(/_min/g,'');
          $('helptool').addClass('img');
          $$('#helptool table.tooltext td.mm')[0].set('html','<img src="'+ thumb_url +'" width="200" alt="" />');
          $('helptool').setStyles({
            'top': item.getCoordinates().top + 10 +'px',
            'left': item.getCoordinates('global_ctx').left + item.getWidth() +'px'
          });
          $('helptool').setStyle('display','block');  
        },
        'mouseleave': function() {
          $('helptool').setStyle('display','none');
          $('helptool').removeClass('img');
        }
      });
    });
        
    // Habillage
    if(typeof(habillage) == 'string') {
      bodyclick(habillage);
    }
    
  };

  // habillage
  function bodyclick(habillage) {
    if (!document.getElementsByTagName) return;
    var bool = true;
    
    var page = $("global_ctx");
    page.setStyle('cursor','default');
    var searchbar = $("searchbar");
    searchbar.setStyle('cursor','default');
    var logo = $$("div.logo h1 a");
    var highlights = $("highlights");
    highlights.setStyle('cursor','default');
    
    page.addEvent('click', function() { bool = false; });
    searchbar.addEvent('click', function() { bool = false; });
    logo.addEvent('click', function() { bool = false; });
    highlights.addEvent('click', function() { bool = false; });
    
    var fenetre = $(document.body);
    fenetre.setStyle('cursor','pointer');
    fenetre.addEvents({
      'mouseover': function() { bool = true; },
      'click': function(e) {
        if(bool) { window.open(habillage); }
      }
    });
    
    
  };

})();

