var deroule=true;

$(document).ready(function(){
		
		//hide what content is 'shrunk 
		$('.content_to_hide[tohide="true"]').each(function(){
			 $(this).toggle(false);
		});
		
		//same here with headers to change of background
		$(".header_for_click").each(	
			function(){		
					//get ready 
					var hidden = ($(this).next().css('display')=='none');
					$(this).attr('toggled', hidden? 0 : 1 );
					
					//check
					var i = $(this).attr('toggled');
					if(!(i%2))$(this).css('backgroundPosition','0 0');
					if(i%2) $(this).css('backgroundPosition','0 -27px');
			}
		)
		.css('cursor','pointer')
		.click(ferme_ou_ouvre);
		
	 	
		
		
	
	 
															 
										
		
  });

function ferme_ou_ouvre(e){
	if (!deroule) {deroule=true;return;}		 
	var i = $(this).attr('toggled');
	$(this).attr('toggled',++i);
	$(this).next().slideToggle("slow",function(){if(!(i%2))$(this).prev().css('backgroundPosition','0 0');saveOrderAndStateModules();})
	if(i%2) $(this).css('backgroundPosition','0 -27px');
}


function saveOrderAndStateModules(){
	var res='';
	$('#sortable>li>div').each(function(index,el){
								if(index!=0)  res+= ',';
								var reg = $(this).attr('region')!=null ?":"+(+$(this).attr('region')):"";
								 res+=	$(this).attr('rel')+':'+
								 		(($(this).css('display')=='none')?'1':'0')+":"+
										(($(this).children('#module_content').css('display')=='none')?'1':'0')+
										reg;
								 
								 }
								 )
	
	$.ajax({
		   processData: false,
		   datatype: 'json',
		   type : 'GET',
		   cache: false,
		   
		   url : "scripts/handler_requetes_AJAX.php",
		   data: "action=ordre_module&ordre="+res,
		   success: testeFinDeRequete
		   });

	
}

function testeFinDeRequete(data,string,xhr){
	if(data==true){
		//success
		return;
	}else{
		return;
	}
}
