$('document').ready(function(){
	
	$('li.hoverable').each(function(){
		
		$(this).bind('click',function(){
			
			//On supprime la news si elle aparait
			if( $('#news-flash').length )
			{
				$('#news-flash').remove();			
			}
			
			open_project(this);
			
		})
	
	});
	
	$('.deployed h2.loaded_title ').live('click',function(){
		
		//console.log($(this).parent().parent().parent().find('.loaded_title'));
				
		$(this).parent().parent().parent().find('.loaded_title').removeClass('loaded_title');
				
		$(this).parent().parent().find('.post-content').remove();
		
		var is_img = $(this).parent().parent().find('#img-ctn').length;
	    if( is_img )
	    {
	        $('#img-ctn').remove();
	    }	
		
		$(this).parent().parent().removeClass('deployed').addClass('hoverable').bind('click',function(){
	    	
	    	open_project(this);
	    	
	    });
	
	});
	
	var is_news = $('#news,#actualites').length;
	
	if( is_news )
	{
		var _hash = window.location.hash;
		
		if( _hash == '#last-news' )
		{
			$('#news,#actualites').click();	
		}
	}
	
	

		
	    $('#menu-cat > a').click(function(){
	    
	    	//On vérifie si on est sur la bonne page
	    	var _path = document.location.pathname;
	    		is_info = /informations/.test(_path);
	    	
	    	if( !is_info )
	    	{
	    	
	    		//On vérifie si un article est déjà ouvert
				var deployed = $('.deployed');
				var is_one_deployed = deployed.length;
				
				
				//Si oui, on supprime son contenu et on lui change son style
				if( is_one_deployed )
				{
				 	$('.post-content').remove();
				 	
				 	//Si il y avait un diaporama autrefois on le supprime
				    var is_img = $('#img-ctn').length;
				    if( is_img )
				    {
				        $('#img-ctn').remove();
				    }
				    
				    deployed.find('.loaded_title').removeClass('loaded_title');	 
				 	   
				    deployed.removeClass('deployed').addClass('hoverable').bind('click',function(){
				    	
				    	open_project(this);
				    	
				    });
				}
			
			}
	    
	    });
	    
	    

});

function open_project(obj)
{

	//On vérifie si un article est déjà ouvert
	var deployed = $('.deployed');
	var is_one_deployed = deployed.length;
	
	//Si oui, on supprime son contenu et on lui change son style
	if( is_one_deployed )
	{
	 	$('.post-content').remove();
	 	
	 	//Si il y avait un diaporama autrefois on le supprime
	    var is_img = $('#img-ctn').length;
	    if( is_img )
	    {
	        $('#img-ctn').remove();
	    }
	    
	    $(obj).parent().parent().parent().find('.loaded_title').removeClass('loaded_title');	 
	 	   
	    deployed.removeClass('deployed').addClass('hoverable').bind('click',function(){
	    	
	    	open_project(this);
	    	
	    });
	}

	
	//On change le style de la boite pour accueillir le contenu
	$(obj).removeClass('hoverable').addClass('deployed');
	
	load_content(obj);
}

function load_content(obj)
{
	var content_url = $(obj).attr('rel');
	var content_url_hash = content_url + ' .content-out';
	
	//On insère le contenu
	$('.deployed').load(content_url_hash,function(){
		
		//On récupère les images et on crée un diaporama
		w.init();
		
		//On applique la scrollbar au contenu
		_mypane = $('.post-content').jScrollPane({verticalDragMinHeight:60,verticalDragMaxHeight:60});
		_elem.api = _mypane.data('jsp');
		
	});
	
	
	
	//On supprime les actions associées
	$('.deployed').unbind('click');
	
		
}
var _elem = {
	
	api : undefined

}

//Objet qui créer le diaporama
var w = {
    compteur: 0,
    obj : undefined,
    t_img	: new Array(),
    t_news	: new Array(),
    nb_img	: $('.post-content img').length,
    is_news : undefined,
    init : function()
    {
    	
    	var i = 0;
    	
    	//Si nous ne sommes pas sur la page #contact alors lançons l’indexation des images pour la galerie
    	if( $('.post-content img').eq(0).attr('class') != 'ajax-loader' )
    	{
    	
    		//console.log($('#news,#actualites').length);
    	
    		//On vérifie si on est dans une page news
    		w.is_news = $('.post-news-content').length;     		
    	
    	
    		$('.post-content img').each(function(index){
    		
    					w.t_img[index] = new Array('src');
    						    				
    					w.t_img[index]['src'] = $(this).attr('src');
    					
    					if( w.is_news )
    					{
    						w.t_news[index] = new Array('src');
    						w.t_news[index] = new Array('p');
    						w.t_news[index] = new Array('title');
    						w.t_news[index]['src'] =  $(this).attr('src');
    						w.t_news[index]['p'] =  $(this).parent().parent().parent();
    						w.t_news[index]['title'] =  $(this).parent().parent().parent().parent().find('h3').html();
    						
    						
    					}
    				
    					
    					
    		});
    		
    		w.nb_img = $('.post-content img').length;
    	}
    	
    	
    	//On prépare la séparation texte/image
    	$('.post-content img').parent('a').parent('p').remove();
    	$('.post-content img').remove();
    	   	
    	if( w.nb_img > 0 )
    	{
    		//Création du template de départ
    		var output  = ''
    			output += '<div id="img-ctn">';
    			output +=  '<img id="the_img" alt="" src="' + w.t_img[0]['src'] + '" />';
    			
    			if(  w.is_news  )
    			{
    				output += '<div id="title-img">' + w.t_news[0]['title'] + '</div>';
    			}
    			
    			
    			output += '</div>';//img-ctn
    								    		
    		//On ajoute le contenu
    		$('.deployed .content-out').append(output);
    		
    		//On style le comportement face à l'image
    		if( w.nb_img > 1 )
    		{
    			$('#the_img').css('cursor','pointer');
    		}
    		else
    		{
    			$('#the_img').css('cursor','default');
    		}
    		
    		//Gestionnaire d'évenements
    		$('#the_img').click(function(){
    			w.next_img();
    		});
    	
    	}
    	
  	    },
  
    next_img : function(){
    			
    			
    			w.compteur++;
    			
    			
    			if( w.compteur > w.nb_img-1 )
    			{
    				w.compteur = 0;
    				
    			}
    								
    			$('#the_img').attr('src',w.t_img[w.compteur]['src']);
    			
    			
    			if( w.is_news  )
    			{
    			    				
    				var _pos = $(w.t_news[w.compteur]['p']).position(); 
    				_elem.api.scrollToY(_pos.top-30);
    				
    				$('#title-img').html(w.t_news[w.compteur]['title']);
    				
    				
    			}
    			
    
    		}
}
