

Event.observe(window, 'load', function() {
	//gestion du lien +INFOS
	 $A(document.getElementsByClassName('more-infos')).each(function(e) {
		var mi = new MoreInfos(e);
	 }); 
	 
	 //gestion du js sur les checkbox des formats
	$A(document.getElementsByClassName('checkformat')).each(function(e) {
		var check = new CheckFormats(e);
	 }); 
	 
});

//classe représentant le lien +INFOS
var MoreInfos = Class.create();
MoreInfos.prototype = {
  initialize: function(element) {
  	pattern = "more-infos-";

	this.id = element.id;
	this.productId = this.id.substring(pattern.length, this.id.length);
	this.moreInfosDiv = $("more_infos_" + this.productId);
	
	this.eventClick = this.toggle.bindAsEventListener(this);	
	Event.observe(element, "click", this.eventClick);
  },
  toggle: function() {
	Effect.toggle(this.moreInfosDiv, 'blind', { duration: 0.2 }); 
  }
}


var CheckFormats = Class.create();
CheckFormats.prototype = {
  initialize: function(element) {

	this.elementId      = element.id;
	this.element      = element;	
    
	substr = this.elementId.substring(this.elementId.indexOf("_", 0), this.elementId.length);
	this.linkId = substr.substring(1, substr.indexOf("_", 1));
	this.productId = substr.substring(substr.indexOf("_", 1) + 1,  substr.length);
	

	this.links = Array();
	this.superiorLinks = Array();
	this.prices = Array();
	
	var re = new RegExp(/link_[\d]+/);

	for(i=0; i<document.forms['product_addtocart_form'].elements.length; i++)
	{
	  var node = document.forms['product_addtocart_form'].elements[i];
	  var nodeId = node.id;
	  var m = re.exec(nodeId);
	  
	  if (m != null) {
	  	substr = nodeId.substring(nodeId.indexOf("_", 0), nodeId.length);
		linkId = substr.substring(1, substr.indexOf("_", 1));
		productId = substr.substring(substr.indexOf("_", 1) + 1,  substr.length);
		
		if(productId == this.productId){
			this.links.push(node);
		}
	  }
	}	
	
	startChecking = false;
	for(i=0; i<this.links.length; i++) {
		var link = this.links[i];
//alert(link.id);		alert(this.elementId);	
		if (startChecking == true) {
			this.superiorLinks.push(link.id);
		}	
		if (link.id == this.elementId) {
			startChecking = true;
		}

		
	}
  
    this.eventClick = this.checkFormat.bindAsEventListener(this);

    this.registerEvents();
  },

  destroy: function() {
    Event.stopObserving(this.element, "click", this.eventClick);
  },

  registerEvents: function() {
    Event.observe(this.element, "click", this.eventClick);
  },

  checkFormat: function(event){
  
		element = $(this.elementId);
		var prices = this.prices;

		this.superiorLinks.each(function(item) {
			if(element.checked == true) {
				$(item).disabled = true;
				//on recherche le label associé à la checkbox
				span = $(item).nextSibling.nextSibling;

				prices[item] = span.innerHTML ;
				span.innerHTML = 'Free';
				
			}
			else {
				$(item).disabled = false;
				//on recherche le label associé à la checkbox
				span = $(item).nextSibling.nextSibling;
				
				span.innerHTML  = prices[item];
			}
				
		});
  }




}
