// JavaScript Document

/* lien popup copyright */

var nouvelleFenetre = null;
function fermerFenetre(){
	if (nouvelleFenetre != null){
		if(!nouvelleFenetre.closed)
			nouvelleFenetre.close();
	}
}
function ouvrirFenetre(strURL,strType,strHeight,strWidth) {
	fermerFenetre();
	var strOptions="";
	if (strType=="minimal") strOptions="resizable,scrollbars,height="+strHeight+",width="+strWidth;
	if (strType=="statut") strOptions="status,resizable,height="+strHeight+",width="+strWidth;
	if (strType=="navigateur") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
	nouvelleFenetre = window.open(strURL, 'newWin', strOptions);
	nouvelleFenetre.focus();
}

/**
 * On lancer plusieurs chose au chargement de la page
 */ 
jQuery(window).ready( function () {
  /**
   * Menu horizontal
   */   
  jQuery('#nav_list li,#nav_list li div.level2').mouseover( function () {
    if(!jQuery(this).parent().hasClass('default'))
      jQuery(this).parent().addClass('current');
    
    jQuery('#nav_list li.default').removeClass('current');  
    
  });
  jQuery('#nav_list li div.level2').mouseout( function () {
    if(!jQuery(this).parent().hasClass('default'))
      jQuery(this).parent().removeClass('current');
    
    jQuery('#nav_list li.default').addClass('current');
  });
  
  /**
   * Animation sur les picto promo dans les pages produits
   */     
  if(jQuery('#cartPromo img').length > 0) {
    changeSize(jQuery('#cartPromo img'), '-');
  }
    
  function changeSize(el, sens) {
    jQuery(el).animate({
      width: sens + '=2'
    }, 500, function() {
      changeSize(el, sens == '+' ? '-' : '+');
    });
  }
  
  /**
   *   Scroll News
   */   
  jQuery('.scrolling').each( function() {
    var vheight = jQuery(this).height();
    var vmargintop = '0';
    
    scrollText(jQuery(this), 80, vheight, vmargintop);
    jQuery(this).mouseover(function() { jQuery(this).stop() });
    jQuery(this).mouseout(function() { 
      var top = parseInt(jQuery(this).css('margin-top'));

      scrollText(jQuery(this), 80, vheight, vmargintop); 
    });
    
  });
  
  /**
   * Page famille pour lire la suite des descriptions
   */   
  jQuery('#family_intro .btn_lire_suite a').click( function(event) {
    event.preventDefault();
    jQuery('#family_intro').hide();
    jQuery('#family_intro_long').show();
  });
  jQuery('#family_intro_long .btn_cacher_suite a').click( function(event) {
    event.preventDefault();
     jQuery('#family_intro_long').hide();
    jQuery('#family_intro').show();
  });
  
  // Recherche avancée
  jQuery('#filter_lib,#filter_ref,#filter_aucun').click(function() {
    jQuery('#filterSearch').val(jQuery(this).val());
  });

  // Rollover descente produit
  jQuery('.products_list .product').each( function() {
    jQuery(this).mouseover(function() {
      jQuery(this).css('background-position', '0 -173px');
    }).mouseout( function() {
      jQuery(this).css('background-position', '0 0');
    });
  });
  
  jQuery('#in_type_devis_1').click( function(event) {
     jQuery('#formdevischantier').hide();
     jQuery('#formdevisremotorisation').show();
  });  
  
  jQuery('#in_type_devis_2').click( function(event) {
    jQuery('#formdevisremotorisation').hide();
    jQuery('#formdevischantier').show();
  });   
  
  // Calcul TOTAL H.T. fiche produit
  getProductPriceHt();
  jQuery('#qte').bind( 'change, keyup', function() { 
    getProductPriceHt();
  });
  
  // Centrage des images fiche produit
  if(jQuery('#product_slider').length > 0) {
    var containerwidth = jQuery('#product_slider').width();
    var liwidth = jQuery('#product_slider ul li:first').outerWidth(true);
    var linumber = jQuery('#product_slider ul li').length;
    var width = liwidth * linumber;
    if(width > containerwidth) width = containerwidth;
    jQuery('#product_slider ul').width(width);
  }
  
  // Slider produit associé
  sliderProductAssoc();
  autoNext = setTimeout("jQuery('#prod_connexe .pconnexe_nav a.slider_next:not(.inactive)').trigger('click', ['auto'])", 3000);
  
  // Survols de la liste des commandes
  var bgcolor = '';
  jQuery('.cmd_list tr').mouseover( function() {
    bgcolor = jQuery(this).css('background-color');
    if(!jQuery(this).children().is('th'))
      jQuery(this).css('background-color', '#FFF2B0');
  }).mouseout( function() {
    if(!jQuery(this).children().is('th'))
      jQuery(this).css('background-color', bgcolor);
  });
  
  jQuery('#franco').click( function() {
      
      if(jQuery('#franco').is(':checked') == true)showFranco();
  });  
  
});

var autoNext;
var autoPrev;
function getProductPriceHt() {
  if(jQuery('#productPriceHidden').length > 0 && jQuery('#qte').length > 0) {
    var price = jQuery('#productPriceHidden').val();
    var qte = jQuery('#qte').val();
    if(qte > 0)
      jQuery('#cartProdCalculatedHt').html(Math.round((price * qte)*100)/100);
  }
}

/** 
 * Génère un slider sur les produits associés 
**/
function sliderProductAssoc() {
  var linumber = jQuery('#prod_connexecontent .products_list li').length;
  var liwidth = jQuery('#prod_connexecontent .products_list li:first').outerWidth(true);
  jQuery('#prod_connexecontent .products_list').width(linumber * liwidth);
  jQuery('#prod_connexe .pconnexe_nav a').click( function(event) { event.preventDefault() });
  
  // si pas assez de produit connexes, on désactives le slider
  if(linumber <= 4) {
    jQuery('#prod_connexe .pconnexe_nav a.slider_next').addClass('inactive');
    jQuery('#prod_connexe .pconnexe_nav a.slider_next').addClass('inactive').css('background-position', '0 0');
    
  }
  
  // Bouton précédent
  jQuery('#prod_connexe .pconnexe_nav a.slider_prev:not(.inactive)').live( 'click', function(event, isAuto) {
    listenPrevClick(jQuery(this), liwidth, event, isAuto);
  });
  // Bouton suivante
  jQuery('#prod_connexe .pconnexe_nav a.slider_next:not(.inactive)').live( 'click', function(event, isAuto) {
    listenNextClick(jQuery(this), liwidth, event, isAuto);
  });
}

function listenNextClick(el, liwidth, event, isAuto) {
    event.preventDefault();
    if(!isAuto) {
      clearTimeout(autoPrev);
      clearTimeout(autoNext);
    }

    jQuery('#prod_connexecontent .products_list').animate({
      left: '-=' + liwidth
    }, 600, function() {
      jQuery('#prod_connexe .pconnexe_nav a.slider_prev').css('background-position', '0 -32px');
      jQuery('#prod_connexe .pconnexe_nav a.slider_prev').removeClass('inactive');
      
      var ulwidth = jQuery('#prod_connexecontent .products_list').width();
      var ulleft = parseInt(jQuery('#prod_connexecontent .products_list').css('left'));
      if(ulwidth + ulleft <= 4 * liwidth) {
        var linumber = jQuery('#prod_connexecontent .products_list li').length;
        var liinvisible = linumber - 4;
        jQuery('#prod_connexecontent .products_list').css('left', (-liinvisible * liwidth) + 'px');
        jQuery(el).addClass('inactive').css('background-position', '0 0');
      }else if(isAuto)  
        autoNext = setTimeout("jQuery('#prod_connexe .pconnexe_nav a.slider_next:not(.inactive)').trigger('click', ['auto'])", 3000);
      
      if(jQuery('#prod_connexe .pconnexe_nav a.slider_next').hasClass('inactive') && isAuto)
        autoPrev = setTimeout("jQuery('#prod_connexe .pconnexe_nav a.slider_prev:not(.inactive)').trigger('click', ['auto'])", 3000);
    });
}

function listenPrevClick(el, liwidth, event, isAuto) {
    event.preventDefault();
    if(!isAuto) {
      clearTimeout(autoPrev);
      clearTimeout(autoNext);
    }

    jQuery('#prod_connexecontent .products_list').animate({
      left: '+=' + liwidth
    }, 600, function() {
      jQuery('#prod_connexe .pconnexe_nav a.slider_next').css('background-position', '0 -32px');
      jQuery('#prod_connexe .pconnexe_nav a.slider_next').removeClass('inactive');

      if(parseInt(jQuery('#prod_connexecontent .products_list').css('left')) >= 0) {
        jQuery('#prod_connexecontent .products_list').css('left', '0');
        jQuery(el).addClass('inactive').css('background-position', '0 0');
      }else if(isAuto)
        autoPrev = setTimeout("jQuery('#prod_connexe .pconnexe_nav a.slider_prev:not(.inactive)').trigger('click', ['auto'])", 3000);

      if(jQuery('#prod_connexe .pconnexe_nav a.slider_prev').hasClass('inactive') && isAuto)
        autoNext = setTimeout("jQuery('#prod_connexe .pconnexe_nav a.slider_next:not(.inactive)').trigger('click', ['auto'])", 3000);
    });
}

/**/
