/**
 * Permet de switcher d'images quand on consulte un
 * produit configurable.
 * @author MLE
 * @date 23/08/2010
 */

if(typeof selectedAssocProducts=='undefined') {
var selectedAssocProducts = {};
}

/*** Produits configurable ***/
Product.Config.prototype.configureElement = function(element) {
this.reloadOptionLabels(element);
if(element.value){
this.state[element.config.id] = element.value;
if(element.nextSetting){
element.nextSetting.disabled = false;
this.fillSelect(element.nextSetting);
this.resetChildren(element.nextSetting);
}
}
else {
this.resetChildren(element);
}
this.reloadPrice();
//  Calculator.updatePrice();
/***** Load Associated Image : This should come after this.resetChildren is called *****/
// If an option doesnt have a value attribute, it'll take its innerHTML as its value - hence the reason for || element.value.substr(0,6) == 'choose'
if (!element.value || element.value.substr(0,6) == 'choose') return; // Selected "choose option"
var attributeId = element.id.replace(/[a-z]*/, '');
for (var a in this.config.attributes)
{
for (i = 0; i < this.config.attributes[a].options.length; i++)
{
if (this.config.attributes[a].options[i].id != element.value) continue;
selectedAssocProducts[a] = this.config.attributes[attributeId].options[i].products;
}
}
var productNo = intersect(selectedAssocProducts) || selectedAssocProducts[attributeId][0];
$$('.product-picture img')[0].src = assocIMG[productNo];
$$('.button-zoom a')[0].href = originalImg[productNo];
thumbs = $$('.article-picture .thumbs ul')[0];
thumbs.childElements().invoke('remove');
j=0; for (var i in thumbsImg[productNo]) j++;
for( i=0; i< j; i++ )
{
curr = thumbsImg[productNo][i];
thumbs.insert("<li><a href='"+curr["href"]+"' rel='lightbox[rotation]'><img width='48' height='48' longdesc='"+curr["longdesc"]+"' alt='' src='"+curr["src"]+"' /></a></li>");
}
if( thumbs.childElements().length > 0 )
{
thumbs.childElements()[0].addClassName('first');
effectThumbs();
}
}

Product.Config.prototype.resetChildren = function(element){
	delete selectedAssocProducts[element.config.id]; // Added
if(element.childSettings) {
for(var i=0;i<element.childSettings.length;i++){
element.childSettings[i].selectedIndex = 0;
element.childSettings[i].disabled = true;
delete selectedAssocProducts[element.childSettings[i].config.id]; // Added
if(element.config){
this.state[element.config.id] = false;
}
}
}
}

function intersect(ar) // ar can be an array of arrays or an asssociative array
{
if (ar == null) return false;
var a = new Array();
if (ar.length == undefined) // Associate Array
{
for (var i in ar)
a.push(ar[i]);
}
else
a = ar;
if (a.length == 1) return false; // Single array ? Nothing to intersect with
var common = new Array();
function loop(a, index, s_index, e_index)
{ 
if (index == null) index = 0;
if (s_index == null) s_index = 0;
if (e_index == null) e_index = a[index].length;
if (index == a.length - 1) return;           
for (var i = s_index; i < e_index; i++)
{
if (common.indexOf(a[index][i]) != -1) continue;
for (var j = 0; j < a[index + 1].length; j++)
{
if (a[index][i] != a[index+1][j]) continue;
loop(a, index + 1, j, j + 1);
if (index + 1 == a.length - 1) { common.push(a[index][i]); break; }
}
}
}       
loop(a);
return common;
}
