// #############################################################################################
// #                                   initialisations                                         #
// #############################################################################################

jQuery.noConflict();

var mielapi = null;

var IE6 = false;

var strChUserAgent = navigator.userAgent;
var intSplitStart = strChUserAgent.indexOf("(",0);
var intSplitEnd = strChUserAgent.indexOf(")",0);
var strChStart = strChUserAgent.substring(0,intSplitStart);
var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
var strChEnd = strChUserAgent.substring(strChEnd);

if(strChMid.indexOf("MSIE 6") != -1) IE6 = true;

var NVO_OPEN = false;

// #############################################################################################
// #                                   definition init_sunny                                   #
// #############################################################################################

function init_mielapi()
{
  // -------------------------------------------------------------------------------------------
  //                                                                                      masque
  //

  this.fermer_bloc = function()
  { jQuery(".bloc").animate({opacity: 0}, 400, function() {jQuery(this).detach(); });
    cacher_masque();
    NVO_OPEN = false;
  }

  this.afficher_masque = function()
  { if(!jQuery.browser.msie || parseInt(jQuery.browser.version) != 7)
    { jQuery("body").append("<div id=\"masque\"></div>");
      jQuery("#masque").css({"opacity": 0.6}).fadeIn(500);
    }
  }

  this.cacher_masque = function()
  { if(!jQuery.browser.msie || parseInt(jQuery.browser.version) != 7)
    { jQuery("#masque").fadeOut(500);
      jQuery("#masque").detach();
    }
  }

  // -------------------------------------------------------------------------------------------
  //                                                                                  connection
  //

  this.afficher_compte = function()
  { jQuery("#mon_panier").css("z-index", 99);
    jQuery("#mon_compte").css("z-index", 101);
    afficher_masque();
    new Ajax.Request
    ( site_root + "index.php/jcatalog/login/login",
      { method: "post",
        parameters: {},
        onComplete: function(transport)
        { var response = eval('(' + transport.responseText + ')');
          var html = response["html"];
          jQuery(".contenu").first().append(html);
          jQuery("#bloc_compte").css("z-index", 101).fadeIn(500);
        }
      }
    );
  }

  this.cacher_compte = function()
  { jQuery("#bloc_compte").animate({height: "0"}, 400, function() { jQuery("#bloc_compte").detach(); });
    cacher_masque();
    NVO_OPEN = false;
  }

  // -------------------------------------------------------------------------------------------
  //                                                                                      panier
  //

  this.afficher_panier = function()
  { jQuery("#mon_panier").css("z-index", 101);
    jQuery("#mon_compte").css("z-index", 99);
    afficher_masque();
    new Ajax.Request
    ( site_root + "index.php/jcatalog/cart/cart",
      { method: "post",
        parameters: {},
        onComplete: function(transport)
        { var response = eval('(' + transport.responseText + ')');
          if(typeof(response["redirect"]) == "undefined")
          { var html = response["html"];
            jQuery(".contenu").first().append(html);
          }
          else document.location.href = response["redirect"];
        }
      }
    );
  }

  this.cacher_panier = function()
  { jQuery("#b_panier").animate({height: "0"}, 400, function() { jQuery("#b_panier").detach(); });
    cacher_masque();
  }

  this.add_product = function(i)
  { var form_inputs = {};
    jQuery("#product_atc_form_" + i).find("input").each
    ( function()
      { if(this.name.length > 0)
        { form_inputs[this.name] = this.value;
        }
      }
    );
    jQuery("#product_atc_form_" + i).find("select").each
    ( function()
      { if(this.name.length > 0)
        { form_inputs[this.name] = this.value;
        }
        form_inputs[this.name] = this.value;
      }
    );
    if(document.getElementById("mon_panier")) cacher_panier();
    jQuery("#mon_panier").css("z-index", 101);
    jQuery("#mon_compte").css("z-index", 99);
    afficher_masque();
    document.location.href = "#page";
    new Ajax.Request
    ( site_root + "index.php/jcatalog/cart/add",
      { method: "post",
        parameters: form_inputs,
        onComplete: function(transport)
        { var response = eval('(' + transport.responseText + ')');
          if(typeof(response["redirect"]) == "undefined")
          { var html = response["html"];
            jQuery(".contenu").first().append(html);
            if(typeof(response["cart_count"]) != "undefined") maj_cart_count(response["cart_count"]);
          }
          else document.location.href = response["redirect"];
        }
      }
    );
  }

  this.maj_cart = function()
  { cacher_panier();
    jQuery("#mon_panier").css("z-index", 101);
    jQuery("#mon_compte").css("z-index", 99);
    afficher_masque();
    var form_inputs = {};
    jQuery("#cart_form").find("input").each
    ( function()
      { if(this.type == "text") form_inputs[this.name] = this.value;
        else
        { if(this.type == "checkbox")
          { if(this.checked) form_inputs[this.name] = this.value;
          }
        }
      }
    );
    var f_content = "";
    for(var i in form_inputs) f_content += i + ": " + form_inputs[i] + "\n";
    new Ajax.Request
    ( site_root + "index.php/jcatalog/cart/update",
      { method: "post",
        parameters: form_inputs,
        onComplete: function(transport)
        { var response = eval('(' + transport.responseText + ')');
          var html = response["html"];
          jQuery(".contenu").first().append(html);
          if(typeof(response["cart_count"]) != "undefined") maj_cart_count(response["cart_count"]);
        }
      }
    );
  }

  this.maj_cart_count = function(cart_count)
  { cart_count = parseInt(cart_count);
    if(cart_count != "NaN")
    { if(jQuery("#cart_count").size() > 0)
      { jQuery("#cart_count").get(0).innerHTML = (cart_count == 0 ? "aucun" : "" + cart_count) + " article" + (cart_count > 1 ? "s" : "");
      }
    }
  }

  return this;

}

// #############################################################################################
// #                                   document.ready                                          #
// #############################################################################################

jQuery(document).ready
( function()
  { // ------------------------------ comportement pour tous les navigateurs
    mielapi = init_mielapi();
    jQuery("#menu_miel .parent").hover
    ( function()
      { jQuery(this).find(".enfant").fadeIn(200);
      },
      function()
      { jQuery(this).find(".enfant").fadeOut(0);
      }
    );
    // ------------------------------ comportement pour tous les navigateurs sauf IE 6
    if(!IE6)
    { jQuery(".connexion_link").click
      ( function()
        { if(jQuery(".contenu").find("#bloc_compte").length === 0) mielapi.afficher_compte();
          else mielapi.cacher_compte();
        }
      );
      jQuery("#nvo_client").live
      ( "click",
        function()
        { if(NVO_OPEN) { NVO_OPEN = false; jQuery(".bloc_compte_nvo").fadeOut(0); }
          else { NVO_OPEN = true; jQuery(".bloc_compte_nvo").fadeIn(400); }
        }
      );
      jQuery("#mon_panier").click
      ( function()
        { if(jQuery(".contenu").find("#b_panier").length === 0) mielapi.afficher_panier();
          else mielapi.cacher_panier();
        }
      );
      jQuery("#masque, .fermer_bloc").live("click", function() { mielapi.fermer_bloc(); });
    }
    // ------------------------------ comportement pour IE 6
    else
    { jQuery('.img_png').css("behavior", "url(" + site_root + "skin/frontend/default/mielapiculteur/iepngfix/iepngfix.htc)");
      jQuery('.connexion_link').click
      ( function()
        { document.location.href = site_root + "customer/account";
        }
      );
      jQuery("#mon_panier").click
      ( function()
        { document.location.href = site_root + "checkout/cart/";
        }
      );
    }
  }
);

function add_to_cart(form_id)
{ if(!IE6) mielapi.add_product(form_id);
  else document.forms["product_atc_form_" + form_id].submit();
}

function ajouterPan(produit, quantite)
{ if(miels_regionaux[produit])
  { document.forms["product_atc_form_carte"].product.value = miels_regionaux[produit];
    document.forms["product_atc_form_carte"].qty.value = quantite;
    add_to_cart("carte");
  }
}

