// ütf-marker
/*
<p><object height="573" width="678" style="background: none repeat scroll 0% 0% rgb(255, 255, 255);" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" type="application/x-shockwave-flash" data="http://localhost/welter/media/flash/map.swf">
<param value="false" name="allowFullScreen" />
<param value="transparent" name="wmode" />
<param value="always" name="allowScriptAccess" />
<param value="http://localhost/welter/media/flash/map.swf" name="src" /> </object></p>
<script type="text/javascript">
function open_popup(jsonObject )
{
	base.interactiveMapSubmit(jsonObject);
}
</script>
*/
var base = {
  
  // paths
  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),
  
  // ajax
  ajaxSetup: {
    url: lp + '/ajax/',
    type: 'GET',
    dataType: 'html'
  },
  
  //SlideShow
  currCnt: 0,
  img: new Image(),
  timeout : 1000,
  imgid : 'big-img',
  jImg : null,

  assignFormSubmit : function(pageId){
    //fix ie
    jQuery('#colorbox-inner form').submit(function(){
      jQuery(this).ajaxSubmit({
        url : base.ajaxSetup.url,
        data: {action : 'estimate-of-costs', pageId: pageId},
        dataType: 'html',
        success: function(data) {
          $('#colorbox-inner').html(data);
          //fix ie
          base.assignFormSubmit(pageId);
          $.colorbox.resize();
        }
      });
      return false;
    });
  }
};

base.interactiveMapSubmit = function(jsonObject)
{
  var jQuotationBox = $('#quotation-box');
  var  pageId = 'interactive-map';

	var citiesArray = eval('(' + jsonObject + ')');
  jQuery.ajax({
    type: 'POST',
    data:     {action: 'estimate-of-costs', pageId: pageId, chargement: citiesArray[0], dechargement: citiesArray[1]},
    dataType: 'html',
    success:  function(data){
      jQuery('#colorbox-inner').html(data);
      $.colorbox({
        padding:  '20px',
        inline: true,
        href:   '#colorbox-inner',
        onClosed: function() {
          jQuery.ajax({
            data:     {action: 'estimate-of-costs', pageId: 'carte-interactive'},
            dataType: 'html',
            success:  function(data){
              $('#colorbox-inner').html(data);
              //fix ie
              base.assignFormSubmit('carte-interactive');
            }
          });
        }
     });
		base.assignFormSubmit('carte-interactive');
    }
  });
  return false;
}
  
  
$(window).ready(function() {

  //configure ajax
  $.ajaxSetup(base.ajaxSetup);

  var tbodys = $('table:not(.truck-props)').find('tbody');
  tbodys.find('tr:odd').addClass('odd');
  tbodys.find('tr:even').addClass('even');
  
  //Slideshow
  base.preloadNextPicture('big-img');
  
  // decode emails
  $('span.email').defuscate();
  
  
  /* contact page */
  var jHeadings = $('#bd-contacts #content-left h2')
  var jHeadingsA = $('a', jHeadings);
  jHeadingsA.click(function() {
    if( jQuery(this).hasClass( 'active' ) ) return false;
    this.blur();
    jHeadings.removeClass('active');
    jHeadingsA.removeClass('active');
    $(this).addClass('active');
    $(this).parent().addClass('active');
    $('ul.section').hide();
    $('ul.section li').removeClass('highlight');
    $(this).parent().next().fadeIn(1800);
    return true;
  });

  /* filiales page */
  var jHeadings2 = $('#bd-nos-filiales #content-left h2')
  var jHeadings2A = $('a', jHeadings2);
  jHeadings2A.click(function() {
    if( jQuery(this).hasClass( 'active' ) ) return false;
    this.blur();
    jHeadings2.removeClass('active');
    jHeadings2A.removeClass('active');
    $(this).addClass('active')
    $(this).parent().addClass('active');
    $('.section').hide();
    $(this).parent().next().fadeIn(1800);
    return true;
  });
  
  if( window.location.hash ) {
    var hash = window.location.hash.substr(1);
    var hashArr = hash.split('_');
    if(hashArr.length == 2) $('#' + hash).addClass('highlight');
    $('#' + hashArr[0] + ' a').trigger('click');
  }  
  
  
  /* categorie selector on truck listing */
  $('#category-filter strong').mousedown(function(event){
    $('#category-filter ul').slideToggle()
  });
  
  $('#category-filter').mouseleave(function(event) {
    $(this).find('ul').hide();
  });
  
  
  /* colorbox on truck listing */
  jQuery('a[rel=gallery]').colorbox({
    slideshowStart:voc.slideshowStart,
    slideshowStop:voc.slideshowStop,
    current: voc.current,
    previous: voc.previous,
    next: voc.next,
    close: voc.close,
    innerWidth: '620px',
    innerHeight: '530px',
    onComplete: function(){
      var jContainer = $('#cboxLoadedContent');
      $('<h3>' + $(this).parents('dl').prev().text() + ' - ' + $(this).find('img').attr('title') + '</h3>').appendTo(jContainer);
      $(this).parents('dd').find('table').each(function() {
        $(this).clone().appendTo(jContainer).find('td.icon').removeClass('icon').addClass('label');
      })
      jContainer.find('img').css({margin:0})
    }
  });


  jQuery('#historic-navi .year a').bind( 'click' , function(){
    var jLink = jQuery(this);
    jLink.blur();
    var date = jLink.attr('rel');
    var jContent = jQuery('#historic-content');
    jContent.addClass('waiting');

    jQuery('#historic-navi .year').removeClass('active');
    jLink.parent().addClass('active');

    jQuery.ajax( {
      data:     'action=historic&date='+date,
      dataType: 'json',
      success:  function(data){
        jQuery('#historic-description').html(data.description);
        jQuery('#historic-date').html(date);
        var newPicture = new Image();
        newPicture.onload = function(){
          jContent.removeClass('waiting');
        };
        newPicture.src = base.userPath + '/historic_pictures/'+data.picture;
        jQuery('#historic-big-picture').attr('src', newPicture.src);
      }
    } );
    
    return false;
  } );

  /************** Gallery **************/
  var pic    = document.getElementById('content-header-img');
  var picCnt = 1;

  if( slideshowPics.length > 1 ) {

    //create arrows
    var arrowLeft = document.createElement('div');
    var arrowRight = document.createElement('div');
    arrowLeft.id = 'arrow-left';
    arrowRight.id = 'arrow-right';
    jQuery('#content-picture-box').append(arrowLeft).append(arrowRight);
    //add events on arrows
    var arrows = {prev : jQuery('#arrow-left'),
                  next : jQuery('#arrow-right')} ;

    for(arrow in arrows) {
      // show
      arrows[arrow].mouseover(function() {
        this.style.opacity = 0.9;
        this.style.filter  = 'alpha(opacity=100)';
      });
      // hide
      arrows[arrow].mouseout(function() {
        this.style.opacity = 0;
        this.style.filter  = 'alpha(opacity=40)';
      });

      arrows[arrow].click(function() {
        var throbberElement = document.createElement('div');
        throbberElement.className = 'throbber';
        jQuery('#content-picture-box').append(throbberElement);
        if(this.id == 'arrow-right') {
          picCnt = picCnt < slideshowPics.length - 1 ? picCnt + 1 : 0;
        }
        else {
          picCnt = picCnt > 0 ? picCnt - 1 : slideshowPics.length - 1;
        }

        var newPicture = new Image();
        newPicture.onload = function(){
          jQuery('#content-picture-box .throbber').remove();
        };
        newPicture.src = base.userPath + '/' + slideshowPics[picCnt];
        pic.src = newPicture.src;
      })
    }
  }
  /**********************************/

  /************ quotation ***********/
  var jQuotationBox = $('#quotation-box');
  var  pageId = $('#quotation-box').children('a').attr('rel');
 
  if(jQuotationBox.length && document.body.id != 'bd-commercial') {
    jQuery.ajax({
      data:     {action: 'estimate-of-costs', pageId: pageId },
      dataType: 'html',
      success:  function(data){
        jQuery('body').append('<div id="colorbox-inner" />');
        jQuery('#colorbox-inner').append(data);
        //fix ie
        base.assignFormSubmit(pageId);

        jQuotationBox.colorbox({
          padding:  '20px',
          inline: true,
          href:   '#colorbox-inner',
          onClosed: function() {
            jQuery.ajax({
              data:     {action: 'estimate-of-costs', pageId: pageId },
              dataType: 'html',
              success:  function(data){
                $('#colorbox-inner').html(data);
                //fix ie
                base.assignFormSubmit(pageId);
              }
            });
          }
        });
      }
    });
    return false;
  }

  /**********************************/

  jQuery('.topic-box').bigFatLink();
  
});

base.preloadNextPicture = function(imgId) {
  imgId = imgId || 'big-img';
  if(!base.jImg) base.jImg = $('#' + imgId);
  if(slideshowHeaderPics.length <= 1 || !base.jImg.length) return false;
  window.clearTimeout(base.timeout);
  base.img.src   = base.userPath+'/'+slideshowHeaderPics[base.currCnt];
  base.currCnt   = base.currCnt == slideshowHeaderPics.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( 1000, function() {base.preloadNextPicture();} );
    } ); 
  }, 2000 );
}

