// Layer Stacks 

var Stack = {
	layers: new Array(),
	colors: $H(new Array()),
	lastBroughtToFront: null,
	layerMarkupClass: "empilable",
	contentsMarkupClass: "contenu",
	groupMarkupAttr: "stackgroup",
	colorMarkupClass: "cadre"
};

function onStackLayerMouseOver(e) {
	var layer = Stack.findStackableParent(Event.element(e));
	if (layer != null && Stack.lastBroughtToFront != layer) {
		Stack.bringToFront(layer);
		//Event.stop(e);
	}
}

Stack.init = function() {
  this.colors['saison'] = ['ffffff', 'ace5ac', '66cc66', '2db22d'];
  this.colors['auteur'] = ['ffffff', '96d0a9', '72c08b', '3ca35e'];
	this.colors['agenda'] = ['ffffff', 'c9d9a5', 'b6cc86', '95b257'];
	this.colors['video'] = ['ffffff', '8dd3be', '66c4a7', '2da882'];
	this.colors['pratique'] = ['ffffff', 'd9bc92', 'cca56d', 'b27f33'];
	this.colors['revue'] = ['ffffff', '94D2CB', '6FC2BA', '39A59A'];
	this.colors['public'] = ['ffffff', 'D896BB', 'CA71A4', 'B13B7E'];			
	this.colors['billetterie'] = ['ffffff', 'ADC3D9', '91AECC', '658BB2'];
	this.colors['brochure'] = ['ffffff', 'D4B3B2', 'C59896', 'A96E6C']; 
	this.colors['infos'] = ['ffffff', 'd9a68d', 'cc8766', 'b2592d'];
	this.colors['ressources'] = ['ffffff', '8dd3be', '66c4a7', '2da882'];
	this.colors['recherche'] = ['ffffff', 'cfbfa0', 'bea87e', 'a1834d'];
	this.colors['lienspartenaires'] = ['ffffff', 'd5bad8', 'c6a2ca', 'ab7cb1'];
	this.colors['mecenat'] = ['ffffff', 'c59ad8', 'b076ca', '8e42b1'];
	this.colors['telechargements'] = ['ffffff', 'cfb2d9', 'be96cc', 'a06cb2'];
	this.colors['technique'] = ['ffffff', 'cdbbd8', 'bca4ca', '9e7eb1'];
	this.colors['contact'] = ['ffffff', 'd9aca3', 'cc8e82', 'b26252'];
	this.colors['pedago'] = ['ffffff', 'abc7de', '78a2c4', '4980ae'];
	//alert(this.colors.inspect());
	this.layers = document.getElementsByClassName(this.layerMarkupClass);
	this.layers.sortBy(
		function(e,i) {
			return (e.style.zIndex != null) ? e.style.zIndex : 0;
		}
	);
	this.reorderLayers();
	this.layers.each(
		function(e,i) {
			Event.observe(e, "mouseover", onStackLayerMouseOver, true);
		}
	);
}
Stack.bringToFront = function (layer) {
  if (this.lastBroughtToFront != layer && layer != null) {
    this.lastBroughtToFront = layer;
  	this.layers = this.layers.without(layer);
  	this.layers.push(layer);
  	this.reorderLayers();
  }
}
Stack.reorderLayers = function () {
	var groupSizes = $H(new Array());
	//var reverseLayerStack = this.layers.reverse(false);
	var i;
	var e;
	var g;
	var groupDepths = new Array();
	for (i=0; i<this.layers.length; i++) {
	  e = this.layers[i];
		e.style.zIndex = i;
		//alert(e.id+" z-index:"+(Stack.layers.length-i)+" -> "+e.style.zIndex);
		g = e.getAttribute(this.groupMarkupAttr);
		if (g != null) {
			if (groupSizes[g] == undefined) {
				groupSizes[g] = 0;
			} else {
				groupSizes[g]++;
			}
			groupDepths[i] = groupSizes[g];
	  }
	}
	var color;
	var l;
	var groupSlice;
	var colorIndex;
	var colorRGB;
	for (i=this.layers.length-1; i>=0; i--) {
	  e = this.layers[i];
	  color = Stack.getLayerColor(e);
	  g = e.getAttribute(this.groupMarkupAttr);
	  if (Stack.colors[color] != undefined && g != null) {
	    l = Stack.getLayerContent(e);
	    if (l != null) {
  	    if (groupDepths[i] == groupSizes[g]) {
  	      colorRGB = Stack.colors[color][0];
  	    } else {
    	    groupSlice = Math.ceil(groupSizes[g] / 3);
    	    colorIndex = Math.ceil((groupSizes[g] - groupDepths[i]) / groupSlice);
    	    //alert("#"+i+" id="+e.id+" g="+g+" groups="+Stack.groups[g]+" depth="+groupDepths[i]+" sc="+sc);
    	    colorRGB = Stack.colors[color][ Math.min(colorIndex, Stack.colors[color].length-1) ];	      
  	    }
  	    l.style.backgroundColor = '#'+colorRGB;
      }
	  }
	}
}
Stack.findStackableParent = function (e) {
	while (e != null) {
		if (Element.hasClassName(e, this.layerMarkupClass)) {
			return e;
		}
		e = e.parentNode;
	}
	return null;
}
Stack.getLayerContent = function (layer) {
	var children = layer.getElementsByTagName("div");
	var i;
	for (i=0; i<children.length; i++) {
		if (Element.hasClassName(children[i], this.contentsMarkupClass)) {
			return children[i];
		}
	}
	return null;
}
Stack.getLayerColor = function (layer) {
	var children = layer.getElementsByTagName("div");
	var i;
	var j;
	var colorNames = this.colors.keys();
	//alert(colorNames);
	for (i=0; i<children.length; i++) {
		var f = children[i];
		if (Element.hasClassName(f, this.colorMarkupClass)) {
			var c = null;
			for (j=0; j<colorNames.length; j++) {	
				if (Element.hasClassName(f, colorNames[j])) {
					c = colorNames[j];
					break;
				}
			}
			return c;
		}
	}
	return null;
}


