/********************************
 * 
 *  	CODED BY:
 *  	Sebastien Tremblay <seb.creative@gmail.com>
 *  	FOR:
 * 		Atelier 288
 * 
 ********************************/

function fcm(c){if(console)console.debug("---------------------------------------------------------- \n>> output test: \n"+">>> "+c+"\n---------------------------------------------------------- \n");}



/******************************
 * 
 * EXECUTIONS
 * 
 ******************************/


var slides;
window.addEvent('domready', function() {
		
	$$(".wr").each(function(v,i){
		var s = 0;
		v.getChildren().each(function(t){s += t.getDimensions().x;});
		v.childrenLength = Math.ceil(s/720)-1;
		v.slideCount = 0;
	});
	
	// set the width to have no scroll to images echo
	$$(".wr.wz").each(function(t,i){
		var posY = t.getCoordinates().left;
		var width = (window.innerWidth==undefined?document.documentElement.clientWidth:window.innerWidth)-posY-15;
		t.setStyle("width",width+"px")
	});
	
	var colRightHeight = window.getScrollSize().y-$("colRight").getCoordinates().top;
	$("colRight").setStyle("height",colRightHeight+"px");
	
	$$(".next").addEvent("mouseup", function(e){
		var obj = new Object();
		obj.operation = "-1";
		obj.targetContainer = e.target.getParent().getParent().getChildren(".container")[0].getChildren(".wr")[0];
		slide(obj);
		
	});
	$$(".prev").addEvent("mouseup", function(e){
		var obj = new Object();
		obj.operation = "1";
		obj.targetContainer = e.target.getParent().getParent().getChildren(".container")[0].getChildren(".wr")[0];
		slide(obj);
	});
	
	$$("ul a").addEvent("mouseup", function(e){
		var t = e.target;
		var id = e.target.id;
		var element = id.toLowerCase().substr(1,id.length);
		var y = $(element).getPosition().y;
		var scrollY = (y<=290?0:y-290);
		new Fx.Scroll(window).start(0, scrollY);
		return false;
	});

});


/******************************
 * 
 * FUNCTIONS
 * 
 ******************************/


function slide(obj) {
	var t = obj.targetContainer;
	// estimated value of slide count with asked operation
	var ev = t.slideCount+obj.operation.toInt(); 
	if(ev >= -t.childrenLength && ev <= 0) {
		t.slideCount = ev;
		var sibling = t.getSiblings()[0].getChildren()[0];
		var fx =  new Fx.Tween(t).start("margin-left",ev*720+"px");
		fx =  new Fx.Tween(sibling).start("margin-left",ev*720+"px");
	}
}
