// ---------- PngHack ----------

Event.observe(window, 'load', function() {
  $$('img.png').invoke('pngHack');
  $('articles').hide();
  //Event.observe('panier', 'mouseover', panier.ouvre, false);
  //Event.observe('panier', 'mouseout', panier.ferme, false);
});


// ---------- Recherche ----------

function focusSearch() {
  if ($F('query') == 'Search...') {
    $('query').value = '';
    $('query').removeClassName('blur').addClassName('focus');
  }
}

function blurSearch() {
  if ($F('query') == '') {
    $('query').value = 'Search...';
    $('query').removeClassName('focus').addClassName('blur');
  }
}

Event.observe(window, 'load', function() {
  if ($F('query') == '') blurSearch();
  $('query').observe('focus', focusSearch);
  $('query').observe('blur', blurSearch);
});


// ---------- Création compte ----------

function recopieAdresse () {
  if ($F('recopie') != undefined) {
    $('adresse_livraison').value = $F('adresse_facturation');
    $('codepostal_livraison').value = $F('codepostal_facturation');
    $('ville_livraison').value = $F('ville_facturation');
    $('pays_livraison').value = $F('pays_facturation');
    $('tel_livraison').value = $F('tel_facturation');
    $('fax_livraison').value = $F('fax_facturation');
  } else {
    $('adresse_livraison').value = '';
    $('codepostal_livraison').value = '';
    $('ville_livraison').value = '';
    $('pays_livraison').value = '';
    $('tel_livraison').value = '';
    $('fax_livraison').value = '';
  }
}


// ---------- Tarifs livraison ----------

function updateTarifsLivraison() {
  new Ajax.Updater('livraison','/en/company/update-postal-charges.php?rand=' + Math.random(), {
    encoding: 'iso-8859-1',
    method: "get",
    parameters: $("frais").serialize()
  });
}


// ---------- Gestion panier ----------

var panier = {
  timeout : null,
  ouvre : function() {
    clearTimeout(this.timeout);
    if($('articles').style.display == 'none') {
      this.timeout = setTimeout(function(){new Effect.BlindDown('articles', {duration:0.3, fps:50})},75);
    }
  },
  ferme : function() {
    if($('articles').style.display == 'none') {
      clearTimeout(this.timeout);
    } else {
      this.timeout = setTimeout(function(){new Effect.BlindUp('articles', {duration:0.3, fps:50})},150);
    }
  }
}

function ajoutePanier(ref) {
  new Ajax.Updater('panier','/en/products/ajoute-panier.php?rand=' + Math.random(), {
    encoding: 'iso-8859-1',
    method: "get",
    parameters: $(ref).serialize(),
    onComplete:function() {
      new Effect.SlideUp('articles', {duration: 0.3, delay: 1 });
    }
  });
}

function modifiePanier(ref) {
  new Ajax.Updater('panier','/en/products/modifie-panier.php?rand=' + Math.random(), {
    encoding: 'iso-8859-1',
    method: "get",
    parameters: $(ref).serialize(),
    onComplete: function() {
      updateItem(ref);
      updateTotalPanier();
      new Effect.SlideUp('articles', {duration: 0.3, delay: 1 });
    }
  });
}

function updateFiche(ref) {

}

function updateItem(ref) {
  new Ajax.Updater(ref,'/en/products/update-item.php?rand=' + Math.random(), {
    encoding: 'iso-8859-1',
    method: "get",
    parameters: $(ref).serialize()
  });
}

function updateTotalPanier() {
  new Ajax.Updater('commande_total','/en/products/update-total-panier.php?rand=' + Math.random(), {
    encoding: 'iso-8859-1',
    method: "get"
  });
}

function updateTotalCommande() {
  var transporteur = $F('moyen_livraison');
  var paiement = $F('moyen_paiement');
  new Ajax.Updater('commande_total','/en/products/update-total-commande.php?rand=' + Math.random(), {
    encoding: 'iso-8859-1',
    method: "get",
    parameters: 'transporteur=' + transporteur + '&paiement=' + paiement
  });
  return
}

function fichemoins1(ref) {
  qte = $F('qte_' + ref);
  if (!isNaN(qte)) {
    nbre = parseInt(qte) - 1;
    if (nbre < 0) nbre = 0;
    $('qte_' + ref).value = nbre;
  }
}

function ficheplus1(ref) {
  qte = $F('qte_' + ref);
  if (!isNaN(qte)) {
    nbre = parseInt(qte) + 1;
    $('qte_' + ref).value = nbre;
  }
}

function paniermoins1(ref) {
  nbre = parseInt($F('qte_'+ref)) - 1;
  if (nbre < 0) nbre = 0;
  $('qte_'+ref).value = nbre;
  modifiePanier(ref);
}

function panierplus1(ref) {
  nbre = parseInt($F('qte_'+ref)) + 1;
  $('qte_'+ref).value = nbre;
  modifiePanier(ref);
}


// ---------- Commande directe ----------

function afficheContenances() {
  new Ajax.Updater('reference','/en/products/update-contenances.php?rand=' + Math.random(), {
    encoding: 'iso-8859-1',
    method: "get",
    parameters: 'produit=' + $F('produit')
  });
}

function ajouteProduit() {
  ref = $F('reference');
  qte = $F('qte_quantite');
  if ((ref != '') && !isNaN(qte) && (qte != 0)) {
    new Ajax.Updater('commande','/en/products/update-commande-directe.php?rand=' + Math.random(), {
      encoding: 'iso-8859-1',
      method: "get",
      parameters: 'reference=' + ref + '&qte=' + qte,
      insertion: 'bottom',
      onComplete: function() {
        $('qte_quantite').value = 0;
      }
    });
  }
}


// ---------- Commande ----------

function changeCodePromo() {
  new Ajax.Updater('resultatcodepromo','/en/products/update-codepromo.php?rand=' + Math.random(), {
    encoding: 'iso-8859-1',
    method: "get",
    parameters: 'code=' + $F('code_promo'),
    onComplete: function() {
      updateTotalCommande();
    }
  });
}

function changeLivraison() {
  $('livraison').setStyle({ backgroundImage: 'url(/images/transporteurs/transporteur-' + $F('moyen_livraison') + '.gif)' });
  updateTotalCommande();
}

function changePaiement() {
  $('paiement').setStyle({ backgroundImage: 'url(/images/paiements/' + $F('moyen_paiement') + '.gif)' });
  updateTotalCommande();
}
