var IE6 = false, IE7 = false,
FIREFOX2 = false, FIREFOX = false,
NETSCAPE7 = false, NETSCAPE = false,
OPERA9 = false, OPERA = false,
AUTRE = false;
var strChUserAgent = navigator.userAgent;
var intSplitStart = strChUserAgent.indexOf("(",0);
var intSplitEnd = strChUserAgent.indexOf(")",0);
var strChStart = strChUserAgent.substring(0,intSplitStart);
var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
var strChEnd = strChUserAgent.substring(strChEnd);

if(strChMid.indexOf("MSIE 7") != -1)
IE7 = true;
else if(strChMid.indexOf("MSIE 6") != -1)
IE6 = true;
else if(strChEnd.indexOf("Firefox/2") != -1)
FIREFOX2 = true;
else if(strChEnd.indexOf("Firefox") != -1)
FIREFOX = true;
else if(strChEnd.indexOf("Netscape/7") != -1)
NETSCAPE7 = true;
else if(strChEnd.indexOf("Netscape") != -1)
NETSCAPE = true;
else if(strChStart.indexOf("Opera/9") != -1)
OPERA9 = true;
else if(strChStart.indexOf("Opera") != -1)
OPERA = true;
else
AUTRE = true;

function clear_info_text(element, check_text)
{ 
	if (check_text == null || check_text == element.value)
	{ 
		element.value = ''; 
	} 
}

function set_info_text(element, info_text)
{
	if (element.value == '')
	{
		element.value = info_text;
	}
}


function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}


function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function plie_plan()
{
	transition_plan("panelplan", '1', 500, '500', function () { $("panelplan").setStyle('display', 'none'); if (IE6) { $$("select").setStyle('visibility', 'visible'); } })
}

function deplie_plan()
{
	if (IE6)
	{
		$$("select").setStyle('visibility', 'hidden'); 
	}
	$("panelplan").setStyle('display', 'block');
	transition_plan("panelplan", '500', 500, '500');
}

function resume_class_element_width(element_class_name, max_height, setCssHeight)
{ 
	var item_orig_text, item_text, chomped_height, compl, txt;
 		
	$$("." + element_class_name).each(function (item, i)
	{	
		item_orig_text = item.get('html');
		
		compl = '';
		chomped_height = false;
		
		control_item_name = item.getProperty('id') + '_ctrl';
		overflow_item_name = item.getProperty('id') + '_ovfl';
		
		if (max_height != null && setCssHeight != null) { compl = ' height: ' + max_height + 'px; ';  }
		
		item.set('html', '<div style="overflow: hidden; ' + compl + '" id="' + overflow_item_name + '"><div style="display: block;" id="' + control_item_name + '">' + 
			item_orig_text + '</div></div>'); 
		
		control_item = $(control_item_name);
		overflow_item = $(overflow_item_name);
		
		if (max_height != null)
		{
			while (control_item.offsetHeight >= max_height) 
			{
			   chomped_height = true;
			   item_text = String(control_item.get('text'));
			   txt=item_text.substring(0, (item_text.length - 1))
			   control_item.set('html', txt);
			   if ((item_text.length - 1) == 0) { break; }
			}
			if (chomped_height)
			{
			   item_text = control_item.get('text');
			   item_text = item_text.substring(0, item_text.length - 3);
			   item_text = item_text.replace(/\s+[^\s]+\s*$/, '...');
			   control_item.set('html', item_text);
			}
		}
   });
}

function go_url(url)
{
	document.location.href = url;	
}

/* Gestion du zoom */
var zoom_courant = 0;
var zoom_max = 2;
var zoom_min = 0;

window.addEvent('domready', function () {
	zoom_init();
});

function zoom_init(){
	var izCookie = parseInt(Cookie.read('zoom'));
	if (izCookie > zoom_min && izCookie <= zoom_max) {
		zoom_courant = izCookie;
		process_zoom($$('div'), null, izCookie);
		process_zoom($$('span'), null, izCookie);
		process_zoom($$('td'), null, izCookie);
		process_zoom($$('p'), null, izCookie);
	}
}

function zoom_page_plus()
{
	var res = null;
	var new_size = null;
	var unit = null;
	var child = null;
	
	if ((zoom_courant + 1) <= zoom_max) {
		zoom_courant++;
		process_zoom($$('div'));
		process_zoom($$('span'));
		process_zoom($$('td'));
		process_zoom($$('p'));
		var zCookie = Cookie.write('zoom', zoom_courant, {duration: 1, domain: window.location.hostname, path: '/'});
	}
}

function zoom_page_moins()
{
	var res = null;
	var new_size = null;
	var unit = null;
	var child = null;
	
	if ((zoom_courant - 1) >= zoom_min) {
		zoom_courant--;
		process_zoom($$('div'), 1);
		process_zoom($$('span'), 1);
		process_zoom($$('td'), 1);
		process_zoom($$('p'), 1);
		var zCookie = Cookie.write('zoom', zoom_courant, {duration: 1, domain: window.location.hostname, path: '/'});
	}
}

function process_zoom(elements, dezoom, init) {
	if (init == null) {
		init = 1;
	}
	elements.each(function (it) {
		child = it.getElement('div');
		if (child == null && !it.hasClass('noresize')) {
			var size = it.getStyle('font-size');
			if (res = size.match(/(\d+)(\w+)/)) {
				if (dezoom == null) {
					new_size = parseInt(res[1]) + init;
				}
				else {
					new_size = parseInt(res[1]) - init;
				}
				unit = res[2];
				if (unit == 'pt') {
					new_size = 11 + init;
					unit = 'px';
				} 
				it.setStyle('font-size', new_size + unit);
			}
		}
	});
}

function transition_plan(element, target_height, time, real_height, callback, interval)
{
	cur_height = $(element).getStyle('height');
	unit_pos = cur_height.indexOf('px');
	cur_height_val = parseInt(cur_height.substring(0, unit_pos));
	
	//nombre de pixels ajoutes ou soustraits a chaque etape
	stepPlan = 20;
	
	//calcul de l'interval
	if (interval == null) { interval = Math.floor(time / (Math.abs(cur_height_val - target_height) / stepPlan)); }
	
	new_height = null;
	
	//calcul de la nouvelle taille
	//plie
	if (cur_height_val > target_height)
	{
		if((cur_height_val - stepPlan) >= target_height)
		{
			new_height = (cur_height_val - stepPlan);
		}
		else if ((cur_height_val - stepPlan) < target_height)
		{
			new_height = target_height;
		}
	}
	//deplie
	else if (cur_height_val < target_height)
	{
		if((cur_height_val + stepPlan) <= target_height)
		{
			new_height = (cur_height_val + stepPlan);
		}
		else if ((cur_height_val + stepPlan) > target_height)
		{
			new_height = target_height;
		}
	}
	
	if(new_height != null)
	{
		 $(element).setStyle('height', new_height + 'px');
		 window.setTimeout(function () { transition_plan(element, target_height, time, real_height, callback, interval); }, interval);
	}
	else
	{
		if(callback != null) { callback.call(); }
	}
}
