function submitForm() {
	$('cF').submit();
}

function init(subpage) {
	$('sage').observe('mouseover', sageOver);
	//$('sage').observe('mouseout', sageOut);
	
	
	$('doContact').observe('mouseover', contactPulse);
	
	Event.observe(document, 'mousemove', getMouseXY)

	if (subpage != undefined) {
		scroll(subpage);
	}
	
	try {
		restarte3();
		restarte2();
		restarte4();
	} catch (e) {
	}
	resize();
}

function getPageSize() {

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) { 
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	} 

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){ 
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	//arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return {width:pageWidth, height:pageHeight, windowWidth:windowWidth,windowHeight:windowHeight}; //arrayPageSize;
}

function resize() {
	var size = getPageSize();
		
	if(size.windowWidth < 1350) {
		$('site').style.marginLeft = "0px";
		$('site').style.marginRight = "0px";
		$('site').style.position = "absolute";
		$('site').style.left = "294px";
	} else {
		$('site').style.position = "";
		$('site').style.left = "";
		$('site').style.marginLeft = "auto";
		$('site').style.marginRight = "auto";	
	}
}

function showSearch(page, subpage, search) {
	window.location = "/"+page+"/?sub="+subpage+"&search="+search;
}

function search() {
	var word = $('searchBox').value;
	
	var opt  = {
	    method: 'post',
	    postBody: 'search='+word,
	    onSuccess: function(t) {
			var data = t.responseText.split("####");
			
			var final = '';
			for (var i = 0; i < data.length-1; i++) {
				var d = data[i].split("###");
				final += "<div class='sr'><a href='javascript:showSearch(\""+d[2]+"\","+d[3]+",\""+word+"\");'>"+d[0]+"</a><span class='sRes'>"+d[1]+"</span></div>"
			}
			$('searchResult').update(final);
	    }
	}
	
	if (word.length > 2) {
		new Ajax.Request('/search.php', opt);
	} else {
		$('searchResult').update('');	
	}
}

var curPage = '';
function scroll(text) {	
	scrollFn('text_'+text, 500);
}

function backToTop() {
	scrollToPos(0, 0, 500);
}

var easeIn = function(i, steps) {
	return Math.pow(2, steps - i);
};
var easeNone = function(i, steps) {
	return i;
};

var scrollInterval;
var stpI;
var xdiff;
var ydiff;
var offset;
var xPos;
var yPos;
var steps;

function scrollFn(elm, ms) {
	
	var position 	= $(elm).cumulativeOffset();
	var x = 0; //position.left;
	var y = position.top;
	
	scrollToPos(x,y,ms);
}

function scrollToPos(x,y,ms,ease) {	
	n = 12;
	var offset = document.viewport.getScrollOffsets(); //Core.Client.getScrollOffset(),
	steps = n || 5;
	stpI = steps;
	time = ms || 250;
	xPos = x;
	yPos = y;
	xdiff = x - offset.left;
	ydiff = y - offset.top;
	fx = ease ? ease : function(i, steps) {
		return Math.pow(2, i);
	};

	clearInterval(scrollInterval);
	scrollInterval = setInterval(function() {
		stpI -= 1;
		var divisor = fx(stpI, steps);

		//alert( offset.left);
		window.scrollTo(xdiff / divisor + offset.left, ydiff / divisor + offset.top);

		
		// last step
		if (0 === stpI) {
			//alert(yPos);
			clearInterval(scrollInterval);
			window.scrollTo(xPos, yPos);
		}
	}, time / steps);
};

//Gestion du bloc contact
function contactPulse() {
	Effect.Pulsate('doContact', { pulses: 1, from:0.5, duration: 2.0 });
}

//Gestion du bloc sage
var sageVisible		= false;
var sageAnimRunning = false;
var sageHidder;

function sageOver(event) {
	try {
		sageHidder.stop();
	} catch (e) {
	}
	if (!sageVisible && !sageAnimRunning) {
		var element = event.element();
	
		sageAnimRunning = true;
		
		$('sageLinks').setOpacity(0);
		$('sageLinks').show();
		
		new Effect.Parallel([
				new Effect.Morph('sage', { sync: true, style: 'width:172px;'}),
	 			new Effect.Opacity('sageLinks', { sync: true, from: 0, to: 1 }) 
			], { 
			duration: 0.3,
			afterFinish: function() {sageVisible = true; sageAnimRunning = false;}
		});
	}
}

function sageOut() {
	//sageHidder = new PeriodicalExecuter(function(pe) {
		if (sageVisible && !sageAnimRunning) {
			//var element = event.element();
	
			//var el = Event.findElement(event, 'div');

			//alert(Event.pointerX(event));
			//if(el.id != 'sageImage' && el.id != 'sageLinks') {
				
				sageAnimRunning = true;
					
				new Effect.Parallel([
						new Effect.Morph('sage', { sync: true, style: 'width:143px;'}),
		 				new Effect.Opacity('sageLinks', { sync: true, from: 1, to: 0 }) 
					], { 
					duration: 0.3,
					afterFinish: function() {$('sageLinks').hide(); sageVisible = false; sageAnimRunning = false;}
				});
			//}
		}
	//}, 0.1);
}

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(event) {
	var x = Event.pointerX(event);
	var y = Event.pointerY(event);

	var pos	= $('sage').cumulativeOffset();
	var dim	= $('sage').getDimensions();
	if (x < pos.left || x > pos.left+dim.width || y < pos.top || y > pos.top+dim.height) {
		sageOut(); //alert('a');
	}
}


//animation du logo
var e2 = true;			
function restarte2() {
    if (e2) {
    	new Effect.Move('2', {x:-400,y:0,mode:'absolute', duration:10+(Math.random()*5), afterFinish:restarte2});
    	e2 = false;
    } else {
    	new Effect.Move('2', {x:400,y:0,mode:'absolute', duration:10+(Math.random()*5), afterFinish:restarte2});
    	e2 = true;
    }
}

var e3 = true;			
function restarte3() {
    if (e3) {
    	new Effect.Move('3', {x:0,y:200,mode:'relative', duration:3+(Math.random()*3), afterFinish:restarte3});
    	e3 = false;
    } else {
    	new Effect.Move('3', {x:0,y:-200,mode:'relative', duration:3+(Math.random()*3), afterFinish:restarte3});
    	e3 = true;
    }
}

var e4 = true;			
function restarte4() {
    if (e4) {
    	new Effect.Move('4', {x:200,y:74,mode:'relative', duration:3+(Math.random()*3), afterFinish:restarte4});
    	e4 = false;
    } else {
    	new Effect.Move('4', {x:-200,y:-74,mode:'relative', duration:3+(Math.random()*3), afterFinish:restarte4});
    	e4 = true;
    }
}

