/********** 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;

/********** Animation de la galerie d'image ************/
var imggal = Array();
var imggal_indice = 0;
var imggal_initialized = false;
var imggal_enable = true;
var imggal_interval = 0;
var imggal_width = 0;
var imggal_time = 0.8;
var imggal_trans = Effect.Transitions.sinoidal;

var idtimerbg = 0;

/********** Rotation des post its ************/
var idtimerpostit = null;
var clicked = false;

function imggal_init()
{
	//Construction du tableau d'image
	var imgs = $$('.imggal');
	imgs.each(function(val){
		var tab = val.id.split("_"); 
		var id = tab[1];
		imggal[id] = val.src;
	});

	//Recuperation de la taille des images
	var img1 = $('imggalaff_0');
	var dim = img1.getDimensions();
	imggal_width = dim.width;

	//Activatiopn de l'animation
	if(imggal.length>=2) 
		imggal_enable = true;
	else
	{
		//Si il faut faire disparaitre les boutons quand il y a moins de deux image il fauit le faire ici
	}
}

function imggal_maj(indice)
{
	//Mise à jour des textes sur l'image
	$('cpt_img').update((indice+1) + "/" + imggal.length);
	var texte = $('textgal_defaut').value;
	if($('textgal_'+indice))
		texte = $('textgal_'+indice).value;
	$('text_portfolio').update(texte);
}

function imggal_move(indice,sens)
{
	var ctn = $('imggal_ctn');
	var img1 = $('imggalaff_0');
	var img2 = $('imggalaff_1');
	
	//Selection de l'image à inserer
	if(img1.style.display=="none")
	{
		var n = img1;
		var p = img2;
	}
	else
	{
		var n = img2;
		var p = img1;
	}
	n.src = imggal[indice];
	

	//Insertion dans le dom
	ctn.removeChild(n);
	if(sens==1)
	{
		ctn.className = ctn.className.replace("element_right","element_left");
		Element.insert(ctn, {bottom:n});
		var pos = "0px";
	}
	else
	{
		ctn.className = ctn.className.replace("element_left","element_right");
		Element.insert(ctn, {bottom:n});
		var pos = imggal_width * -1 +"px";
	}
	
	//Deplacement
	ctn.style.left = pos;
	n.style.display="";
	var dest = imggal_width*sens*-1;
	//On deplace les images
	new Effect.Move(ctn, { x: dest, mode: 'relative',duration:imggal_time,transition:imggal_trans ,afterFinish:function(){
		imggal_indice = indice;
		p.style.display="none";
		ctn.style.left = pos;
		imggal_maj(indice);
		imggal_enable = true;
	}});
}

// Ajout d'une fonction 'shuffle' aux tableaux (Array)
Array.prototype.shuffle = function() {
  this.sort(function() { return 0.5 - Math.random() })
};


function record_abonnement_CallBack(response,param)
{
	phxAjaxCall(true,urlaff,'tpl=front/form/abonnement_callback',abonnement_callback,STD_error_handler);
}

function abonnement_callback(res,param)
{
	if($('abonnement_callback'))
	{
		$('abonnement_callback').update(res);
	}
}

function record_newsletter_CallBack(response,param)
{
	phxAjaxCall(true,urlaff,'tpl=front/form/newsletter_callback',newsletter_callback,STD_error_handler);
}

function newsletter_callback(res,param)
{
	if($('newsletter_callback'))
	{
		$('newsletter_callback').update(res);
	}
}

function record_commentaire_CallBack(response,param)
{
	phxAjaxCall(true,urlaff,'tpl=front/article/commentaire/callback',commentaire_callback,STD_error_handler);
}

function commentaire_callback(res,param)
{
	if($('commentaire_callback'))
	{
		$('commentaire_callback').update(res);
	}
}

function postitchange(idelem)
{
	phxAjaxCall(true,urlaff,'tpl=front/postit/postit&idelempostit='+idelem,edit_postit_CallBack,STD_error_handler,{type:'postit',btn_id:'',blocdestination:$('liste_postit'),opt:''});
}

function edit_postit_CallBack(res,param)
{
	if(param.opt.clic && param.opt.clic == 1)
	{
		if(idtimerpostit != null)
			clearTimeout(idtimerpostit);
		$('liste_postit').update(res);
		clicked = true;
		PHX_reloadRules('liste_postit');
	}
	else if(!clicked)
	{
		if($('liste_postit'))
		{
			new Effect.Fade($('liste_postit'), {
				afterFinish:function(){
					$('liste_postit').update(res);
					new Effect.Appear($('liste_postit'));
					PHX_reloadRules('liste_postit');
			}} );
		}
	}
}

function NextArticles_display_CallBack(response,param)
{
	var receivers = $$('.receiver');
	if(receivers.length > 0)
	{
		var lastreceiver = receivers[receivers.length-1];
		lastreceiver.update(response);
		PHX_reloadRules();
	}
}

function NextArticle_getLastParam(rule)
{
	var arr = $$(rule);
	var last = ""
	if(arr.length > 0)
	{
		last = arr[(arr.length-1)].value
	}
	return last;
}

function ListeDepot_display()
{
	var val_ville = $('liste_ville_depot').value;
	var val_cat = $('liste_cat_depot').value;
	phxAjaxCall(true,urlaff,'tpl=front/depot/depots&ville='+val_ville+"&cat="+val_cat,ListeDepot_CallBack,STD_error_handler,{type:'postit',btn_id:'',blocdestination:$('liste_postit'),opt:''});
}
function ListeDepot_CallBack(response,param)
{
	$('displayed_depot').update(response);
}

var myrules = {	
	'#liste_ville_depot' : function(cv)
	{
		cv.onchange = function()
		{
			ListeDepot_display("ville",cv.value);			
		}
	},
	'#liste_cat_depot' : function(cc)
	{
		cc.onchange = function()
		{
			ListeDepot_display("cat",cc.value);			
		}
	},
	'.imggal_anim' : function(ctn)
	{
		if(!imggal_initialized)
		{
			imggal_initialized = true;
			imggal_init();
		}
	},

	'.imggalbtn' : function(btn)
	{
		btn.onclick = function()
		{
			if(imggal_enable)
			{
				imggal_enable = false;
				var tab = btn.id.split("_"); 
				var btnid = tab[1];
				var sens = parseInt(btnid);
				var indice = imggal_indice + sens;
				if(indice<0)
					indice = imggal.length-1;
	
				if(indice >= imggal.length)
					indice = 0;
				imggal_move(indice,sens);
			}
		}
	},
	
	'.switch_on' : function(sw)
	{
		sw.onclick = function()
		{
			var lst = $$('.explode');
			lst.shuffle();
			var lst_f = $$('.explode_f');
			for(var i = 0; i < lst_f.length; i++)
			{
				lst.unshift(lst_f[i]);
			}					
			for(var i = 0; i < lst.length; i++)
			{
				new Effect.Puff(lst[i], {duration:0.1, queue:'end'});
			}
			sw.className = sw.className.replace('_on', '_off');
			$('lib_switch').update('- switch off -');
			if($('fd_size_height') && $('body_kiblind'))
			{
				$('body_kiblind').setStyle('min-height:'+$('fd_size_height').value+'px');
			}
			Behaviour.apply();
		}
	},
	
	'.switch_off' : function(sw)
	{
		sw.onclick = function()
		{
			var lst = $$('.explode');
			lst.shuffle();
			var lst_f = $$('.explode_f');
			for(var i = 0; i < lst_f.length; i++)
			{
				lst.push(lst_f[i]);
			}
			for(var i = 0; i < lst.length; i++)
			{
				new Effect.Appear(lst[i], {duration:0.1, queue:'end'});
			}
			sw.className = sw.className.replace('_off', '_on');
			$('lib_switch').update('- switch on -');
			Behaviour.apply();
		}
	},
	
	'.deco_show' : function(b)
	{
		b.onmouseover = function()
		{
			if($('deco_'+b.id.replace('lml_','')))
				$('deco_'+b.id.replace('lml_','')).style.display = "block";
		}
		
		b.onmouseout = function()
		{
			var idtheme = b.id.replace('lml_','');
			if($('current_deco'))
			{
				if($('current_deco').value == idtheme)
					return;
			}
			if($('deco_'+idtheme))
			{
				$('deco_'+idtheme).style.display = "none";
			}
		}
	},
	
	'.show_theme' : function(s)
	{
		s.onmouseover = function()
		{
			var theme = s.id.replace('show_theme_','');
			var tab = theme.split('_');
			var idtheme = tab[0];
			if($('deco_'+idtheme))
				$('deco_'+idtheme).style.display = "block";
			if($('libtheme') && $('lib_theme_'+idtheme))
			{
				$('libtheme').update($('lib_theme_'+idtheme).value);
			}			
		}
		
		s.onmouseout = function()
		{
			var theme = s.id.replace('show_theme_','');
			var tab = theme.split('_');
			var idtheme = tab[0];
			if($('current_deco'))
			{
				if($('current_deco').value == idtheme)
					return;
			}
			if($('deco_'+idtheme))
			{
				$('deco_'+idtheme).style.display = "none";
				if($('libtheme') && $('current_libtheme'))
				{
					$('libtheme').update($('current_libtheme').value);
				}				
			}
		}
	},
	
	'#plusdarticle' : function(n)
	{
		n.onclick = function()
		{
			var tpl = 'front/article/liste_pleine_page';
			var post_supp = '&idspublication='+NextArticle_getLastParam('.ids');
			post_supp += "&page="+NextArticle_getLastParam('.params_page');
			post_supp += "&theme="+NextArticle_getLastParam('.params_theme');
			post_supp += "&idelemfiltre="+NextArticle_getLastParam('.params_idelemfiltre');
			post_supp += "&idelemordre="+NextArticle_getLastParam('.params_idelemordre');
			post_supp += "&typeordre="+NextArticle_getLastParam('.params_typeordre');
			
			phxAjaxCall(true,urlaff,'tpl='+tpl+post_supp,NextArticles_display_CallBack,STD_error_handler);
		}
	},
	
	'#currentpostit' : function(p)
	{
		var lst = $$('.lstpostit');
		var tabId = Array();
		var current = 0;
		var indice = 0;
		lst.each(function(val){
			var id = val.id.replace('lstpostit_','');
			tabId.push(id);
			if(p.value == id)
				current = indice;
			indice ++;
		});
		if(tabId.length > 1)
		{
			current += 1;
			if(current > tabId.length - 1)
				current = 0;

			if(idtimerpostit != null)
				clearTimeout(idtimerpostit);

			var wait_duration = 0;
			if(!clicked)
			{
				wait_duration = 5000;
			}
			else
			{
				wait_duration = 10000;
				clicked = false;
			}
			idtimerpostit = setTimeout("postitchange("+tabId[current]+")",wait_duration);
		}
	},
	
	'.detail_article' : function(d)
	{
		d.onclick = function()
		{
			if($('detail_article'))
			{
				if($('detail_article').style.display != "none")
				{
					new Effect.SlideUp($('detail_article'), {duration:0.5});
				}
				else
				{
					new Effect.SlideDown($('detail_article'), {duration:0.5});
				}
			}
		}
	}
};

// *******************************************
//
// REGLES COMPORTEMENT ENREGISTREMENT
//
// *******************************************

Behaviour.register(myrules);
