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

jQuery.noConflict();

//var site_root = "/www.lunedemiel.fr/web/";
var site_root = "/";

var sunny = 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;

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

function init_sunny()
{

  // -------------------------------------------------------------------------------------------
  //                                                                        fix z-index pour IE7
  //

  if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 7)
  { var zIndexNumber = 1000;
    jQuery("div").each(function() { jQuery(this).css("zIndex", zIndexNumber); zIndexNumber -= 10; });
  }

  // -------------------------------------------------------------------------------------------
  //                                                                                      masque
  //

  jQuery("#masque, .fermer_bloc").live("click", function() { fermer_bloc(); });

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

  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
  //

  jQuery(".connexion_link").click
  ( function()
    { if(jQuery(".contenu").find("#bloc_compte").length === 0) afficher_compte();
      else cacher_compte();
    }
  );

  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();
  }

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

  jQuery("#mon_panier").click
  ( function()
    { if(jQuery(".contenu").find("#b_panier").length === 0) afficher_panier();
      else cacher_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()
  { if(IE6)
    { jQuery('.img_png').css("behavior", "url(" + site_root + "skin/frontend/default/sunny/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/";
        }
      );
    }
    else sunny = init_sunny();
  }
);

