// JavaScript Document



function rePosHandlers(blocsImgs, contener){
	blocsImgs.each(function(bloc){
		if(bloc.getPrevious('.handler')){
			handler = bloc.getPrevious('.handler');
		
			handler.setStyle('top',  bloc.getPosition(contener).y-3);
		}
	});
}


function makeSlider(contener, maxHeight, minHeight, handlerHeight){
	var handlers = new Array();
	
	//INITIALIZATION
	if(!contener.getElement('.elem')) return;
	var initHeight = contener.getStyle('height').toInt();

	var blocsImgs = contener.getElements('.elem');
	var paddingBlocImgs = contener.getElement('.elem').getStyle('padding').toInt();

	var nbreImgs = blocsImgs.length;
	
	
	var handlerHeight = contener.getElement('.handler').getSize().y;
	
	var realMinHeight = ((initHeight - (maxHeight+paddingBlocImgs*2) - (nbreImgs-1)*(paddingBlocImgs+3))/(nbreImgs-1))-paddingBlocImgs;

	blocsImgs[0].setStyles({'height':maxHeight, 'margin-top':'0px'});
  
	if(realMinHeight < minHeight-paddingBlocImgs){
		var newContenerHeight = ((minHeight+(paddingBlocImgs+3))*(nbreImgs-1))+maxHeight+paddingBlocImgs*2;
		contener.getParent('.content').getElements('.element').setStyle('height', newContenerHeight);
		contener.getParent('.content').getElements('.texte').setStyle('height', newContenerHeight-paddingBlocImgs*2);
		contener.getParent('.content').getElements('.track').setStyle('height', newContenerHeight-paddingBlocImgs);
		realMinHeight = minHeight-paddingBlocImgs;
	}
	
	initY = maxHeight + paddingBlocImgs*2;
	blocsImgs[0].getNext('.handler').setStyle('top', initY);
	for(var i = 1; i < blocsImgs.length; i++){
		

		blocsImgs[i].setStyle('height', realMinHeight);
		initY += realMinHeight+paddingBlocImgs*2+3;
		if(blocsImgs[i].getNext('.handler')){
			
			blocsImgs[i].getNext('.handler').setStyle('top', initY);
			
		}
		
	}
	
	contener.getElements('.handler').each(function(handler){
			var handler = makeHandler(handler, maxHeight, realMinHeight, handlerHeight, contener);
			
			handlers.push(handler);
	});
	//alert(handlers.length);
	blocsImgs.each(function(elem){
		elem.addEvent('click', function(e){
			e.stop();
			blocsImgs.each(function(otherElem){
				if(otherElem != elem){
					otherElem.setStyle('height', realMinHeight);
				}
			});
			elem.setStyle('height', maxHeight);
			rePosHandlers(blocsImgs, contener);
		
		});
	});

}

function makeHandler(handler, maxDeploy, minDeploy, handlerHeight, contener){
	 
		var blocsImgsUp = new Array();
		var blocsImgsDown = new Array();
		
		
		handler.getAllNext('.elem').each(function(bloc){
			blocsImgsDown.push(bloc);
			
		});
		handler.getAllPrevious('.elem').each(function(bloc){
			
			blocsImgsUp.push(bloc);
			
			
			
			
		});
		
		var initY = handler.getStyle('top').toInt();
		
		
		var dragHandler = handler.makeDraggable({
				snap:0,
				limit:{x:[0,0],y:[initY-maxDeploy+minDeploy ,initY]},
				onStart : function(handler){
					
					initY = handler.getStyle('top').toInt();
					
				},
				onDrag : function(handler){
	 				var deplacement = handler.getStyle('top').toInt()-initY;
					initY = handler.getStyle('top').toInt();
					
					if(deplacement > 0){
						var blocsImgs = blocsImgsDown;
						var elemToScale = blocsImgsUp[0];
						var resize = deplacement;
					}else{
						var blocsImgs = blocsImgsUp;
						var elemToScale = blocsImgsDown[0];
						var resize = -deplacement;
					}
					
					var currentHeight = elemToScale.getStyle('height').toInt();
					
					if(currentHeight < maxDeploy){	
						
						elemToScale.setStyle('height', currentHeight+resize);//ELEM SCALING UP
						
						for(var i=0; i<blocsImgs.length; i++){
							var cHeight = blocsImgs[i].getStyle('height').toInt();
							if(cHeight > minDeploy){//ELEM SCALING DOWN
								blocsImgs[i].setStyle('height', cHeight-resize);	
								break;
							}else{//HANDLERS MOVE
								var handlerToMove = null;
								if(deplacement > 0 && blocsImgs[i].getNext('.handler')){
									handlerToMove = blocsImgs[i].getNext('.handler');
								}else if(deplacement < 0 && blocsImgs[i].getPrevious('.handler')){
									handlerToMove = blocsImgs[i].getPrevious('.handler');
								}
								if(handlerToMove)
									handlerToMove.setStyle('top', handlerToMove.getStyle('top').toInt()+deplacement);
								continue;
							}
						}
						
					}
					//DIRTY FIX FOR DIRTY BUG
					handler.setStyle('top',  blocsImgsDown[0].getPosition(contener).y-3);
					
				}
	 
	 });
	 return dragHandler;
	 
 }
 


		
	

