Watermark = {
	map: new Array(),

	ShowAll: function(){
		for (var i=0;i< Watermark.map.length;i++){
			if(Watermark.map[i].obj.value == ""){
				Watermark.map[i].obj.value = Watermark.map[i].text;					
				Watermark.map[i].obj.css("color",Watermark.map[i].WatermarkColor);
			}else{
			    Watermark.map[i].obj.css("color",Watermark.map[i].DefaultColor);
			}
		}
	},

	HideAll: function(){
		for (var i=0;i<Watermark.map.length;i++){
			if(Watermark.map[i].obj.value == Watermark.map[i].text)
				Watermark.map[i].obj.value = "";					
		}
	},
	
	Watermark: function(input, text, color, align) {
		if (!color) {
			color = "#aaa";
		}

		if (!align) {
			align = "left";
		}

		var defaultColor = input.style.color;
		var defaultAlign = input.style.textAlign;

		Watermark.map[Watermark.map.length]={text:text,obj:input,DefaultColor:defaultColor,WatermarkColor:color};
		function clearMessage(){
			if(input.value == text) {
				input.value = "";
			}
			input.style.color = defaultColor;
			input.style.textAlign = defaultAlign;
		}

		function insertMessage(){
			if(input.value.length == 0 || input.value == text){
				input.value = text;
				input.style.color = color;
				input.style.textAlign = align;
			} else {
				input.style.color = defaultColor;
				input.style.textAlign = defaultAlign;
			}
		}

		Event.observe(input.id, "focus", clearMessage);
		Event.observe(input.id, "blur", insertMessage);
		Event.observe(input.id, "change", insertMessage);

		insertMessage();
	}
}

if (Prototype) {
	Element.addMethods(Watermark);
	Object.extend(Element, Watermark);
}


/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
*/

(function(){var a=navigator.userAgent.toLowerCase(),i,e,f,g,k,c;function l(){if(h("konqueror")){e="Konqueror";i="Linux"}else{if(h("safari")){e="Safari"}else{if(h("omniweb")){e="OmniWeb"}else{if(h("opera")){e="Opera"}else{if(h("webtv")){e="WebTV"}else{if(h("icab")){e="iCab"}else{if(h("msie")){e="Internet Explorer"}else{if(!h("compatible")){e="Netscape Navigator";f=a.charAt(8)}else{e="An unknown browser"}}}}}}}}if(!f){f=a.charAt(c+k.length)}if(!i){if(h("linux")){i="Linux"}else{if(h("x11")){i="Unix"}else{if(h("mac")){i="Mac"}else{if(h("win")){i="Windows"}else{i="an unknown operating system"}}}}}}function h(m){c=a.indexOf(m)+1;k=m;return c}Event.observe(window,"load",d,false);Event.observe(window,"load",l,false);var b=Class.create();window.lightbox=b;window.valid=null;b.prototype={yPos:0,xPos:0,initialize:function(m){this.content=m.href;Event.observe(m,"click",this.activate.bindAsEventListener(this));Event.observe("overlay","click",this.deactivate.bindAsEventListener(this))},activate:function(m){m.stop();if(e=="Internet Explorer"){this.getScroll();this.prepareIE("100%","hidden");this.setScroll(0,0);this.hideSelects("hidden")}this.displayLightbox("block");valid=this},prepareIE:function(m,p){var o,n;n=document.getElementsByTagName("html")[0];n.style.height=m;n.style.overflow=p},hideSelects:function(m){var o=document.getElementsByTagName("select");for(var n=0;n<o.length;n++){o[n].style.visibility=m}},getScroll:function(){if(self.pageYOffset){this.yPos=self.pageYOffset}else{if(document.documentElement&&document.documentElement.scrollTop){this.yPos=document.documentElement.scrollTop}else{if(document.body){this.yPos=document.body.scrollTop}}}},setScroll:function(m,n){window.scrollTo(m,n)},displayLightbox:function(m){$("overlay").style.display=m;$("lightbox").style.display=m;if(m!="none"){this.loadInfo()}},loadInfo:function(){new Ajax.Request(this.content,{method:"post",parameters:"",onComplete:this.processInfo.bindAsEventListener(this)})},processInfo:function(m){var n="<div id='lbContent'>"+m.responseText+"</div>";new Insertion.Before($("lbLoadMessage"),n);$("lightbox").className="done";this.actions()},actions:function(){lbActions=document.getElementsByClassName("lbAction");for(var m=0;m<lbActions.length;m++){Event.observe(lbActions[m],"click",this[lbActions[m].rel].bindAsEventListener(this))}},insert:function(n){var m=Event.element(n).parentNode;Element.remove($("lbContent"));new Ajax.Request(m.href,{method:"post",parameters:"",onComplete:this.processInfo.bindAsEventListener(this)})},deactivate:function(m){m.stop();Element.remove($("lbContent"));if(e=="Internet Explorer"){this.setScroll(0,this.yPos);this.prepareIE("auto","auto");this.hideSelects("visible")}this.displayLightbox("none")}};function d(){j();var m=document.getElementsByClassName("lbOn");for(var n=0;n<m.length;n++){valid=new b(m[n])}}function j(){var n,m,o;n=document.getElementsByTagName("body")[0];m=document.createElement("div");m.id="overlay";o=document.createElement("div");o.id="lightbox";o.className="loading";o.innerHTML='<div id="lbLoadMessage"><p>Loading</p></div>';n.appendChild(m);n.appendChild(o)}})();


document.documentElement.className += "js";

function update_page_contact() {
  $$('.toggle_update_page_contact').each(Element.hide);

  switch(parseInt($F('form_contact_orientation'), 10)){
  case 0:
    break;
  case 1: // infos sur vos commandes
    $('contact_status_commande').show();
    break;
  case 2: // infos sur votre compte
    $('contact_compte').show();
    break;
  case 5: // retours
    $('contact_retour').show();
    break;	
  default: 
    $('form_contact_motif').value = $('form_contact_orientation').options[$('form_contact_orientation').selectedIndex].text;
    $('form_contact_motif_id').value = $('form_contact_orientation').value;
    $('contact_form').show();
    break;
  }
}


document.observe("dom:loaded", function() {
  var form;
  
  // Contacts
  form = $$("form.contacts").first();
  if(form) {
    var foot = form.select("thead tr:first-child").first(),
        checkboxes = form.select("tbody input[type=checkbox]"),
        tr = new Element("tr"),
        td = new Element("td", {colspan:4}),
        a = new Element("a", {href: "#"}),
        actions = {
          contacts_all_label: function() {
            checkboxes.each(function(checkbox){
              checkbox.checked = true;
            })
          },
          contacts_none_label: function() {
            checkboxes.each(function(checkbox){
              checkbox.checked = false;
            })
          },
          contacts_invert_label: function() {
            checkboxes.each(function(checkbox){
              checkbox.checked = !checkbox.checked;
            })
          }
        },
        i = 0;
    
    td.insert(contacts_label+" ");
    $H(actions).each(function(item){
      var label = window[item[0]],
          callback = item[1],
          anchor = a.cloneNode(false).update(label);
      
      anchor.observe("click", function(event){
        callback();
        event.stop();
      });
      
      td.insert(anchor);
      td.insert(i < 2 ? ", " : ".");
      i+=1;
    });
    foot.insert({after: tr.insert(td)});
  }

  // Parrainage
  form = $$("form.parrainage").first();
  if(form) {
    var lines = form.select("tbody tr"),
        foot = form.select("tfoot tr:last-child").last(),
        counter = lines.length,
        line = lines.last(),
        model = line.cloneNode(true),
        tr = new Element("tr"),
        td = new Element("td", {colspan: 3}),
        a = new Element("a", {href:"#"}).update(parrainage_label);
    
    a.observe("click", function(event){
      var newline = model.cloneNode(true);
      
      counter += 1;

      var text = newline.select("input[type=text]").first();
      text.setAttribute("id", "parrainage["+counter+"][email]");
      text.setAttribute("name", "parrainage["+counter+"][email]");
      text.className = "";
      text.value = "";

      newline.select("input[type=radio]").each(function(item){
        item.setAttribute("id", "parrainage["+counter+"][langue]");
        item.setAttribute("name", "parrainage["+counter+"][langue]");
        item.className = "";
        item.checked = (item.getAttribute("value") == document.documentElement.lang)
      });
      
      line.insert({after: newline});
      line = newline;
      event.stop();
    });

    foot.insert({before: tr.insert(td.insert(a))});
  }

  // Parrainage express
  if(typeof parrainage_label !== "undefined") {
    $('email_express').Watermark(parrainage_label);
    Event.observe($('parrainage_express_form'), 'submit', function(element){	    Watermark.HideAll();
    })
  }

  var clickmes = $$(".clickme");
  clickmes.each(function(clickme) {
    clickme.observe("mouseover", function() {
      this.addClassName("clickme-hover")
    }.bind(clickme)).observe("mouseout", function() {
      this.removeClassName("clickme-hover")
    }.bind(clickme)).observe("click", function() {
      this.addClassName("clickme-active");
      document.location.href = clickme.select("a:first-child").first().href
    }.bind(clickme));
  });

  var code_avantage = $("code_avantage");
  if(code_avantage && !navigator.userAgent.match(/(iPhone|Android)/)) {
    var a = document.createElement("a"),
        offset = code_avantage.positionedOffset(),
        width = code_avantage.offsetWidth;

    a.appendChild(document.createTextNode(code_avantage_label));

    a.className = "code_avantage";
    a.href = "#";
    a.style.width = width + "px";
    a.style.top = offset.top + "px";
    a.style.left = "0px";

    code_avantage.style.visibility = "hidden";

    Element.observe(a, "click", function(e) {
      a.parentNode.removeChild(a);
      code_avantage.style.visibility = "visible";
      e.stop()
    });
    
    $("panier_container").appendChild(a)
  }

  var sign = $("signup"),
      items = ["input[name='login']", "input[name='password']"];
  if(sign && !navigator.userAgent.match(/(iPhone|Android|iPad|iPod)/)){
    $A(items).each(function(selector) {
      var n = sign.select(selector).first(),
          l = n.previousSiblings("label").first();
      
      // http://avatraxiom.livejournal.com/97999.html
      (function() {
        var input = n, label = l;
        window.setTimeout(function() {
          if(input.value !== '')
            label.hide();
        }, 500)
      })();

      n.observe("focus", function() {
        l.hide()
      });
      n.observe("blur", function() {
        if(this.value !== '')
          l.hide();
        else
          l.appear()
      });
    });

    // MSIE autocompletion works differently
    sign.select(items[0]).first().observe("propertychange", function(e) {
      var l = sign.select(items[1]).first().previousSiblings("label").first();
      if(e.target.value !== '')
        l.hide();
      else
        l.appear()
    });
    
    sign.addClassName("covered")
  }
});
