/*
	Rémy Savard, Jean-Philippe Sirois [iXmedia.com]
	global.js - Scripts
	Dernière modification: 21 juin 2011
------------------------------------------------- */
$(function(){

/* Gestion des liens externes {{{
******************************************************************************/
	window.domainWithoutSubdomain = function( domainWithSubdomain ) {
		var matches = domainWithSubdomain.match( DOMAIN_REGEX );
		return matches ? matches[0] : null;
	};

	var l = document.links.length, link;
	while (l--) {
		link = document.links[l];
		if ( !link.className.match(/cboxelement/) && !link.className.match(/fancybox/) && !link.href.match(/^(javascript:|mailto:)/) && (domainWithoutSubdomain(link.hostname) != domainWithoutSubdomain(location.hostname) || link.href.match( /\.(docx?|xlsx?|pptx?|pdf|eps|zip|vsd|vxd|rar|wma|mov|avi|wmv|mp3|mp4|mpg|mpeg|mpeg4|m4a|m4v|f4v|flv|csv|xml|ogg|oga|ogv|webm|jpg|jpeg|png|gif|webp|svg|ico|txt|css|js)$/ )) ) {
			link.target = '_blank';
			link.title += link.title ? ' – S’ouvre dans une nouvelle fenêtre.' : 'S’ouvre dans une nouvelle fenêtre.';
			link.className += link.className.indexOf('externe') == -1 ? ' externe' : '';
		}
	}

/*}}}*/

/* Colorbox {{{
******************************************************************************/

	$("a[rel='vignettes']").colorbox({
		current: ''
	});
	
	$().bind('cbox_open', function(){
        $('object').parent().css('display','none');
	});
	
	$().bind('cbox_closed', function(){
        $('object').parent().css('display','block');
	});

/*}}}*/

/* Ajustement de la taille de font {{{
******************************************************************************/

	if ($.cookie('ixfont_size')) {
		$('body').addClass($.cookie('ixfont_size'));
		if($('body').hasClass('a-grand')){
			$('#a-moyen').addClass('actif');
		}
	} else {
		var defaultfont = $('#font-size a[rel=default-font]').attr("id") || '';
		$('body').addClass(defaultfont);
	}
	
	// on bâtit un array de tailles disponibles
	var ixfont_tailles = new Array("a-moyen","a-grand");
	
	$('#font-size a').click(function() {
		// on enlève les classes existantes
		if($('body').hasClass("a-moyen")){
			var newclass = "a-grand";
			$(this).addClass('actif');			
		} else {
			var newclass = "a-moyen";
			$(this).removeClass('actif');
		}
		
		$.each(ixfont_tailles,function(i, n) {
			$('body').removeClass(n);
		});
		
		// on applique la nouvelle classe
		$('body').addClass(newclass);
		
		// on place le cookie
		$.cookie('ixfont_size', newclass, { expires: 365 });
		
		return false ;
	});

/*}}}*/

/* Tooltip {{{
******************************************************************************/

	$(".tip").tooltip({
		tip: '.reponse',
		position: "center left",
		opacity: 0.7,
		offset: [0, -15]
	});

/*}}}*/

/* Commande {{{
******************************************************************************/
  var total = 0
  
  if(window.location.href.indexOf("commande.html") != -1 ){

    $('#formualireCommande table .quantite input').change(function(){
      var quantite = parseInt( $(this).val() );
      if ( isNaN( quantite ) ) { quantite = 0; }

      var montant = parseFloat( $(this).parents('.quantite').prev().text().replace(/(\$|\s)/,'').replace(/,/,'.') );
      var total = ( quantite * montant ).toFixed(2);

      $(this).parents('.quantite').next().html(total+'&nbsp;$');
      recalculerTotalNew();
    }).trigger('change');

   /* $('#formualireCommande table .livraison input').click(function(){
      recalculerTotal();
    }).trigger('change');*/

    $('#formualireCommande .type-usager input').click(function(){
      ajusterPrix($('#formualireCommande .type-usager input:checked'));
    });

    $('#formualireCommande .type-usager input:checked').trigger('click');


  }


  function ajusterPrix($elem) {
    var membre = $elem.val(),
        prix = 'prix_membre';

    if (membre == '0') {
      prix = 'prix';
    }

    $.each(prixCommande, function(item){
      $('#' + item).html( prixCommande[item][prix].toFixed(2) + '&nbsp;$' );
    });

    $('#formualireCommande table .quantite input').trigger('change');
    recalculerTotalNew();
  }

  function recalculerTotalNew() {
    var sousTotal = 0,
        livraison = 0,
        tps = 0,
        tvq = 0,
        total = 0;

    $('td.quantite').next().each(function() {
      var montant = parseFloat( $(this).text().replace(/(\$|\s)/,'').replace(/,/,'.') );
      sousTotal += montant;
    });
    $('tr.sous-total th').eq(1).html( sousTotal.toFixed(2) + '&nbsp;$' );

    /*if (sousTotal > 0) {
      if (sousTotal < 25)
        livraison = 10;
      else if (sousTotal < 100)
        livraison = 15;
      else if (sousTotal <= 200)
        livraison = 17;
      else if (sousTotal > 200)
        livraison = 0;
    }
    $('tr.livraison th').eq(1).html( livraison.toFixed(2) + '$' );*/

    tps = ((sousTotal + livraison) * 0.05);
    $('tr.tps th').eq(1).html( tps.toFixed(2) + '&nbsp;$' );
    tvq = ((sousTotal + livraison + tps) * 0.085);
    $('tr.tvq th').eq(1).html( tvq.toFixed(2) + '&nbsp;$' );

    total = (sousTotal + livraison + tps + tvq);
    $('tr.total th').eq(1).html( total.toFixed(2) + '&nbsp;$' );
  }


/*}}}*/

/* Valeur par défaut dans les champs {{{
******************************************************************************/

	$(":input.valeurParDefaut").each(function() { 

		var classeCSS = "couleurValeurParDefault"; // optionel 
		var valeurParDefaut = $(this).attr("title"); 

		// Au chargement de la page 
		var valeurActuel = $(this).val(); 
		if (valeurActuel == "") { 
			$(this).val(valeurParDefaut); 
			$(this).addClass(classeCSS); // optionel 
		} 
		// Sur le focus 
		$(this).focus(function() { 
			var valeurActuel = $(this).val(); 
			if (valeurActuel == valeurParDefaut) { 
				$(this).val(""); 
				$(this).removeClass(classeCSS); // optionel 
			} 
		}); 
		// Sur le blur 
		$(this).blur(function() { 
			var valeurActuel = $(this).val(); 
			if (valeurActuel == "") { 
				$(this).val(valeurParDefaut); 
				$(this).addClass(classeCSS); // optionel 
			} 
		}); 
	}); 

/*}}}*/

});


