// ütf-marker
var base = {
  basePath:  lp.substr(0, lp.lastIndexOf('/')),
  mediaPath: lp.substr(0, lp.lastIndexOf('/')) + '/media',
  userPath:  lp.substr(0, lp.lastIndexOf('/')) + '/user',
  locale:    lp.substr(lp.lastIndexOf('/') + 1, lp.length),
  ajaxSetup: {
    url: lp + '/ajax/',
    type: 'GET',
    dataType: 'html'
  },
  currCnt: 0,
  img: new Image(),
  timeout : 1000,
  imgid : 'big-img',
  jImg : null,
  getFormOptions : function() {
    return {
      url : base.ajaxSetup.url,
      data: {action : 'contact-form-box'},
      dataType: 'html',
      success: function(data) {
        var newForm = $(data).find('form');
        if(!newForm.length) {
          $('#contact-form-box-form').html(data);
        }
        else {
          $('#contact-form-box-form').replaceWith(newForm)
        }
        $.colorbox.resize();
      }
    }
  }
};




$(window).ready(function() {

  if(document.getElementById('bd-index'))
  {
    var heightMain = jQuery('#main').height();
    var homeBoxHeight = jQuery('.home-box-left').height()+parseInt(jQuery('.home-box-left').css('margin-top'));
    var propBoxHome = jQuery('#proposition-container').height();
    var propBoxMarginTop  = parseInt(jQuery('#proposition-container').css('margin-top'));

    if((heightMain  - homeBoxHeight) <= propBoxHome)
    {
      jQuery('#main').css('height', (heightMain-(heightMain - homeBoxHeight)+propBoxHome+propBoxMarginTop)+ 'px')
    }
  }




  $("a[rel='gallery']").colorbox();
  $('span.email').defuscate();
  base.preloadNextPicture('big-img');
  
  $('#email').attr('alt', 'Votre e-mail').hint();
  
  // form popup
  var jInfoBox = $('#info-box');
  if(jInfoBox.length) {
    $.ajaxSetup(base.ajaxSetup);
    
    $.ajax({
      data: {action : 'contact-form-box'},
      success:  function(data){ 
        $('body').append(data);
        
        $('#contact-form-box-form').live('submit', function() {
          $(this).ajaxSubmit(base.getFormOptions());
          return false
        }); 
        
        jInfoBox.colorbox({

          onClosed: function(){              
            $.ajax({                        
              data: {action : 'contact-form-box'},
              success:  function(data){
                $('#contact-form-box').replaceWith(data);
              }
            })
          },
          padding:  '20px', 
          inline: true, 
          href:   '#contact-form-box-form'
        });
      }
    })
  }
});

 
  

base.preloadNextPicture = function(imgId) {
  imgId = imgId || 'big-img';
  if(!base.jImg){ base.jImg = jQuery('#' + imgId); }

  if(slideshowPics.length == 1 || !base.jImg.length) {
    return false;
  }
  
  window.clearTimeout(base.timeout);
  var picPath    = base.userPath + '/';
  base.img.src   = picPath + slideshowPics[base.currCnt];
  base.currCnt   = base.currCnt == slideshowPics.length -1
                 ? 0
                 : base.currCnt + 1;
  
                 
  base.img.onload = function() { 
    base.jImg.parent().css('background-image','url(' +base.img.src+ ')') 
    base.togglePicture();
  }
}

base.togglePicture = function() {
  base.timeout = window.setTimeout (function(){   
    base.jImg.fadeOut(1000, function() { 
      this.src = base.img.src;
      base.jImg.fadeIn(2000, function() { 
        base.preloadNextPicture();
      })           
    }); 
  }, 4000);
}
