/********** CONFIGURATION DU PHX RULES ***************/
var PHX_rules_tpl = "front/";
var PHX_rules_task = "front/";
var PHX_rules_mode = "front";
var adm_duration_effect=0.5;

/****************CALLBACK********************/
function record_inscription_CallStart(param)
{
	$('record_inscription').href = "javascript:;";
	$('record_inscription').target = "";
	STD_show_wait();
	return param;
}

function record_inscription_CallBack(oRes,param)
{
	//RAZ error
	FORM_razerrormsg();
	FORM_clearcolormsg();
	if($('pdf_url') && $('record_inscription'))
	{
		var url = $('pdf_url').value;
		$('record_inscription').href = url + oRes.pdf_clepasse;
		$('record_inscription').target = "_blank";
	}

	if(oRes.msg && oRes.msg!="")
	{
		Modmsg.msg_duree=14000;
		STD_UserMsg(oRes.msg,class_msg_ok,param);
		Modmsg.msg_duree=2000;
	}

	STD_CallBack(oRes,param);
}

function record_contacttel_CallBack(oRes,param)
{
	//RAZ error
	FORM_razerrormsg();
	FORM_clearcolormsg();
	if(oRes.msg && oRes.msg!="")
	{
		Modmsg.msg_duree=6000;
		STD_UserMsg(oRes.msg,class_msg_ok,param);
		Modmsg.msg_duree=2000;
	}

	STD_CallBack(oRes,param);
}

function record_contactform_CallBack(oRes,param)
{
	//RAZ error
	FORM_razerrormsg();
	FORM_clearcolormsg();
	if(oRes.msg && oRes.msg!="")
	{
		Modmsg.msg_duree=6000;
		STD_UserMsg(oRes.msg,class_msg_ok,param);
		Modmsg.msg_duree=2000;
	}

	STD_CallBack(oRes,param);
}

function record_plaquette_CallStart(param)
{
	$('record_plaquette').href = "javascript:;";
	$('record_plaquette').target = "";
	STD_show_wait();
	return param;
}

function record_plaquette_CallBack(oRes,param)
{
	//RAZ error
	FORM_razerrormsg();
	FORM_clearcolormsg();
	if($('pdf_url') && $('record_plaquette'))
	{
		var url = $('pdf_url').value;
		$('record_plaquette').href = url + oRes.formation_id;
		$('record_plaquette').target = "_blank";
	}

	if(oRes.msg && oRes.msg!="")
	{
		Modmsg.msg_duree=14000;
		STD_UserMsg(oRes.msg,class_msg_ok,param);
		Modmsg.msg_duree=2000;
	}

	STD_CallBack(oRes,param);
}
/* Resize du site */
function resize_site(ctn)
{
	if(ctn)
	{
		var ovp = document.viewport.getDimensions();
		var oDim = ctn.getDimensions();
		if(oDim.height<(ovp.height+5))
		{
			ctn.style.height = ovp.height + "px";
			$('toTop').style.display="none";
			$('bloc_g_ctn').style.height = ovp.height + "px";
			$('bloc_d_ctn').style.height = ovp.height + "px";
		}
		else if(oDim.height>(ovp.height+5))
		{
			$('toTop').style.display="";
			$('bloc_g_ctn').style.height = oDim.height + "px";
			$('bloc_d_ctn').style.height = oDim.height + "px";
		}
	}
}

function resize_bg(ctn)
{
	if(ctn)
	{
		var pos_h = $('rep_h').cumulativeOffset();
		var pos_b = $('rep_b').cumulativeOffset();
		var h = pos_b[1] - pos_h[1] + 2;
		ctn.style.height = h + "px";		
	}
}

var info_vitesse = 0.015; //(en heure/km ^^)
//défilement de la barre d'info
var info_elems = new Array();
var info_indice = -1;
var info_step = 1;
var info_pause = 1;
var info_effect;
var info_dest=0;

function get_vitesse(size)
{
	return size*info_vitesse;
}

function info_init(ctn)
{
	var i = 0;
	var cumul_size = 0;
	var max_size = 0;
	var info_zone_move = $('info_zone_move');
	//Init des tailles
	var item = $("info_item_" + i);
	while(item)
	{
		var oD = item.getDimensions();
		item = Element.remove(item);
		item.style.display="";
		info_elems[i]={width:oD.width,elem:item,duration:get_vitesse(oD.width)};
		cumul_size+=oD.width;
		if(max_size<oD.width)
			max_size = oD.width;
		Element.insert(info_zone_move, {bottom:item});
		i++;
		var item = $("info_item_" + i);
	}
	
	//Init le tableau html
	info_zone_move.style.width = cumul_size + "px";
}

function info_stop(event)
{
	info_effect.cancel();
	//Init des underline;
	$$('.info_item').each(function(item){
		var opos = item.cumulativeOffset();
		var odim = item.getDimensions();
		var tabMouse = TOOL_getMousePosition(event);
		if((tabMouse[0]>=opos[0] && tabMouse[0]<(opos[0]+odim.width) && tabMouse[1]>=opos[1] && tabMouse[1]<(opos[1]+odim.height)))
			item.className = item.className + " info_item_hover"
		});
}

function info_restart()
{
	$$('.info_item').each(function(item){item.className = item.className.replace(" info_item_hover","")});
	var indice_next = info_indice%info_elems.length;
	var info_zone_move = $('info_zone_move');
	var start_x = parseInt(info_zone_move.style.left.replace("px",""));
	if(isNaN(start_x))
		start_x=0;
	var size = start_x - info_dest;
	var info_duration = get_vitesse(size);
	info_effect = new Effect.Move(info_zone_move,{x: info_dest, mode: 'absolute', duration:info_duration, transition:Effect.Transitions.linear, afterFinish:function(){
		//Switch first element
		var ind_to_switch = (info_elems.length + indice_next - info_step) % info_elems.length;
		var item = $("info_item_" + ind_to_switch);
		if(info_indice>=info_step && item)
		{
			var item = info_zone_move.removeChild(item);
			info_zone_move.style.left = (info_dest + info_elems[ind_to_switch].width) + "px";
			Element.insert(info_zone_move, {bottom:item});
		}
		info_defil();
	}});
}

function info_defil()
{
	info_indice ++;
	var indice_next = info_indice%info_elems.length;
	var info_zone_move = $('info_zone_move');
	var start_x = parseInt(info_zone_move.style.left.replace("px",""));
	if(isNaN(start_x))
		start_x=0;
	info_dest = start_x - info_elems[indice_next].width;
	var info_duration = info_elems[indice_next].duration;
	info_effect = new Effect.Move(info_zone_move,{x: info_dest, mode: 'absolute', duration:info_duration, transition:Effect.Transitions.linear, afterFinish:function(){
		//Switch first element
		var ind_to_switch = (info_elems.length + indice_next - info_step) % info_elems.length;
		var item = $("info_item_" + ind_to_switch);
		if(info_indice>=info_step && item)
		{
			var item = info_zone_move.removeChild(item);
			info_zone_move.style.left = (info_dest + info_elems[ind_to_switch].width) + "px";
			Element.insert(info_zone_move, {bottom:item});
		}
		info_defil();
	}});
}

//switch des bloc de resultat et image
var objSwitch = new Array();
function getType(ctnid)
{
	return ctnid.replace("animsmooth_","");
}

function getOption(ctnid,opt)
{
	if($(ctnid + '_' + opt))
		return $(ctnid + '_' + opt).value;
	return false;
}

function getNextIndice(type)
{
	var next = (objSwitch[type].current+1);
	if(next<=objSwitch[type].length)
		return next;
	else
		return 1;
}

function switch_init(ctn)
{
	var type = getType(ctn.id);
	objSwitch[type] = {};
	objSwitch[type].interval=null;
	objSwitch[type].duration=parseInt(getOption(ctn.id,'duration'));
	objSwitch[type].time=parseInt(getOption(ctn.id,'time'));
	objSwitch[type].delay=parseInt(getOption(ctn.id,'delay'));
	objSwitch[type].length = $$("." + ctn.id).length;
	if(objSwitch[type].length<=1)
		objSwitch[type].disabled=true;
	else
		objSwitch[type].disabled=false;
	objSwitch[type].current = 1;
}

function switch_defil(ctn)
{
	var type = getType(ctn.id);
	var prev = $(ctn.id + '_' + objSwitch[type].current);
	if(prev)
	{
		var next_indice = getNextIndice(type);
		var next = $(ctn.id + '_' + next_indice);
		//Switch zone
		new Effect.Appear(next, {duration:objSwitch[type].duration});
		new Effect.Fade(prev, {duration:objSwitch[type].duration});
		objSwitch[type].current = next_indice;
	}
}

var addLetters_time = 20;
// Ajout de lettre 
function addLetters(input,indice)
{
	setTimeout(function(){input.value += input.startValue[indice];if(input.startValue[indice+1])addLetters(input,indice+1)},addLetters_time);
}

var myrules = {
	'#full_content': function(ctn)
	{
		resize_site(ctn);
		setInterval(function(){resize_site(ctn)},500);
	},
	'#bloc_bg': function(ctn)
	{
		resize_bg(ctn);
	},
	'#bloc_info': function(ctn)
	{
		if(ctn.initialized!==true)
		{
			ctn.initialized = true;
			info_init(ctn);
			info_defil();
		}
	},
	'.info_zone_fixe_over': function(zone)
	{
		zone.onmouseover = function(event)
		{
			var e = event || window.event;
			info_stop(event);
		},
		zone.onmouseout = function()
		{
			info_restart();
		}
	},
	'#toTop': function(btn)
	{
		btn.onclick = function()
		{
			window.scrollTo(0,0);
		}
	},
	'.animsmooth':function(ctn)
	{
	 	if(ctn.initialized!==true)
	 	{
			ctn.initialized=true;
			var type = getType(ctn.id);
			switch_init(ctn);
			if(!objSwitch[type].disabled)
				setInterval(function(){switch_defil(ctn)},objSwitch[type].time);
	 	}
	},
	'.resize_src':function(ctn)
	{
	 	if(ctn.initialized!==true)
	 	{
			ctn.initialized=true;
			var oDim = ctn.getDimensions();
			$$('.resize_dest').each(function(item){item.style.width=oDim.width + "px"});
	 	}
	},
	'select.ultralight':function(select)
	{
		if(select.selectedIndex > 0)
			select.className = select.className.replace(" mode_lib","");
		select.onchange = function()
		{
			TOOL_switch(select.id,select.value);
			if(select.value==0)
				select.className = select.className + " mode_lib";
			else
				select.className = select.className.replace(" mode_lib","");
		}
		
	},
	'input.ultralight':function(input)
	{
		input.startValue = input.value;
		input.onfocus = function()
		{
			if(input.value==input.startValue)
			{
				input.value="";
				input.className = input.className.replace(" mode_lib","");
			}
		},
		input.onblur = function()
		{
			if(input.value=="")
			{
				input.className = input.className + " mode_lib";
				addLetters(input,0);
			}
		}
		
	},
	'textarea.ultralight':function(input)
	{
		input.startValue = input.value;

		input.onfocus = function()
		{
			if(input.value==input.startValue)
			{
				input.value="";
				input.className = input.className.replace(" mode_lib","");
			}
		},
		input.onblur = function()
		{
			if(input.value=="")
			{
				input.className = input.className + " mode_lib";
				addLetters(input,0);
			}
		}
		
	}
};


// *******************************************
//
// REGLES COMPORTEMENT ENREGISTREMENT
//
// *******************************************

Behaviour.register(myrules);
