$(function() {

	$('.post-item').each(function() {
		
		if (typeof($(this).find('div.post-content').attr('id')) == 'undefined') return;
		
		var postId = $(this).find('div.post-content').attr('id').substr(1);
		$('div#p'+postId).css('display', 'none');	
		
		$(this).find('a.more-content').click(function(){

			var newText = '';
			var classParent = '';

			$('.post-item').each(function() {
				var id = $(this).find('div.post-content').attr('id').substr(1);
				$('div#p'+id).slideUp('fast');											 
				$(this).find('a.more-content').html('<span class="access">' + expand + '</span>');
				$(this).find('a.more-content').parent().removeClass().addClass('read-it close');
			});
			
			if ($('div#p'+postId+':visible').length == 0)
			{
				$('div#p'+postId).slideDown('fast');
				newText = collapse;
				classParent = 'read-it open';
			}
			else{
				$('div#p'+postId).slideUp('fast');
				newText = expand;
				classParent = 'read-it close';
			}

			$(this).html('<span class="access">' + newText + '</span>');
			$(this).parent().removeClass().addClass(classParent);

			return false;
		});

	});
	
	/**
	* HACK FOR LIJIT
	*
	$('input#lwp_2_searchbutton').attr('value','ok');
	expression = new RegExp('\\?en');
	$('div#lwp_ps div:first').addClass('lijitTitle');
	if (!expression.test(document.location))
	{
		$('div#lwp_cloud h3').text('Recherches populaires');
	}
	$('input#lwp_sfd').attr('size', '15');
	*/
	$('div#lwp_f').remove();
	
	
	$('div#gmap').html('<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.fr/maps?f=q&amp;source=s_q&amp;hl=fr&amp;geocode=&amp;q=NextStage%2B25%2Brue%2BMurillo%2B75008%2BParis&amp;sll=47.15984,2.988281&amp;sspn=26.646422,67.675781&amp;ie=UTF8&amp;filter=0&amp;cid=17006357813641342674&amp;s=AARTsJoyLYyGa_f4_6wk1pzrGI1B-cAOig&amp;ll=48.892712,2.310905&amp;spn=0.039502,0.072956&amp;z=13&amp;iwloc=A&amp;output=embed"></iframe><br /><small><a href="http://maps.google.fr/maps?f=q&amp;source=embed&amp;hl=fr&amp;geocode=&amp;q=NextStage%2B25%2Brue%2BMurillo%2B75008%2BParis&amp;sll=47.15984,2.988281&amp;sspn=26.646422,67.675781&amp;ie=UTF8&amp;filter=0&amp;cid=17006357813641342674&amp;ll=48.892712,2.310905&amp;spn=0.039502,0.072956&amp;z=13&amp;iwloc=A">Agrandir le plan</a></small>');
	
		
	
	/* *
	* LIMIT UL 
	*/
	function limitLi(This) {
		
		var i = 0;
		$(This).find('li').each(function (){
			
			if (i > nb_limit) $(this).toggle();
			i++;
		});
		
	}
	
	if (typeof(colLimitUl) == 'undefined')
	{
		//return;
	}
	else
	{ 
		//$(colLimitUl).find('ul.limit').each(function(){
		$(colLimitUl).find('ul').each(function(){
											 
			var i = 0 ;					 
			var more = 0 ;
			var h = $(this).prev().text();
			var ul = this;
			
			$(this).find('li').each(function (){
			
				if (i > nb_limit) $(this).toggle();
				if (more == 0 && i > nb_limit) 
				{
					var a = document.createElement('a');
					var p = document.createElement('p');
					$(a).attr('href','#');
					$(a).text(expand);
					$(p).addClass('moreUl close');
					$(p).append(a);
					$(this).parent().after(p);
					
					$(a).toggle(
						function(){
							limitLi(ul);
							$(this).text(collapse);
							$(this).parent().removeClass().addClass('moreUl open');
							return false;
						},
						function(){
							limitLi(ul);
							$(this).text(expand);
							$(this).parent().removeClass().addClass('moreUl close');
							return false;
						}
					);
					more = 1;
				}
				i++;
			
			});
		});
	}
	
	$('form#go-to-extranet').each(function(){
		var This = this;
		$(this).find('input[type=submit]').hide();
		$(this).find('a#extranet-cgp').click(function(){
			$(This).submit();
			return false;
		});
	});
	
	
	$('div#page').after('<div id="tipcontent"></div>');
	$('a.tooltip[title]').tooltip({ 
	    position: "top right", 
	    opacity: 0.95, 
	    tip: 'div#tipcontent',
	    offset: [0,-100],
	    effect: 'toggle'
	});
	
	
	/********************************************************************
	 * Post Filter Form
	 * 
	 */
	var limit = 4;
	
	var filterNav 	= $('<ul>').addClass('filter-nav');
	var filterList 	= $('<div>').addClass('filter-list');
	var i = 0;
	$('.post-filters').find('h3').each(function(){
		var cl = $(this).text();
		var div = $('<div>').attr('id',cl);
		if(i>0) $(div).hide();
		
		var paginate = $('<ul>').addClass('paginate')
		var ul = $(this).next();
		var j = 0;
		var z = 0;
		
		var ulTmp = $('<ul>').attr('id',z);
		$(ul).find('li').each(function(){
			$(ulTmp).append($(this));
			j++;
			if(j>=limit)
			{
				var cur = (ulTmp).clone();
				$(div).append(cur);
				j = 0;
				if(z > 1) $(cur).hide();
				if(z%2 == 0) 
				{
					var p = z/2;
					var pli = $('<li><a>' + (p+1) + '</a></li>')
						.css('cursor', 'pointer')
						.click(function(){
							$(div).find('ul.paginate li').removeClass();
							$(this).addClass('current');
							$(div).find('ul').not('.paginate').hide();
							$(div).find('ul#'+(p*2)).show();
							$(div).find('ul#'+((p*2)+1)).show();
						});
					$(paginate).append(pli);
				}
				z++;
				$(ulTmp).empty().attr('id',z);
			}
		});
		if(z > 3) $(ulTmp).hide();
		$(div).append(ulTmp);
		if(z>2) $(div).append(paginate);

		$(div).appendTo(filterList);
		
		var li = $('<li>').attr('id',cl).html('<a>' + $(this).text() + '</a>');
		$(filterNav).append(li);
		i++;
	});
	
	$('.post-filters').empty().append(filterNav).append(filterList);
	
	$(filterNav).find('li').each(function(){
		$(this).click(function(){
			$(filterNav).find('li.current').removeClass('current');
			$(this).addClass('current');
			$(filterList).find('div').hide();
			$(filterList).find('div#'+$(this).attr('id')).show();
		});
	});
	$(filterNav).find('li:first').addClass('current');

	
	var button = $('<h3 id="link-filter"><a>' + FilterTitle + '</a></h3>')
		/*.css('cursor','pointer')*/
		.toggle(
			function(){
				$('.post-filters').show();
				$(this).toggleClass('open');
			},
			function(){
				$('.post-filters').hide();
				$(this).toggleClass('open');
			}
		);
	
	
	$('.post-filters')
		.before(button)
		.hide();

	/**
	 * homepage
	 */
	$('div.click-h3').each(function(){
		var href = $(this).find('h3 a').attr('href');
		$(this).click(function(){
			window.location.href = href;
		});
		
	});
	
	/**
	* Lewis page
	*/
	jstree = $('div.lewis-tree').jstree({
		"themes" : {
			"theme" : "apple",
			"dots" : false,
			"icons" : false
		},
		"plugins" : [ "themes", "html_data", "ui" ]
		});
		
	
	$('div.lewis-tree li').live('click', function(e){

		$(jstree).jstree("toggle_node",this);
		var liTree = this;
		if($(this).hasClass('jstree-closed')) return false;
		var id = $(this).attr('id');
		
		// save hit
		var tab = id.split('-'); 
		var params = {
			f: 'setHit',
			lang: lang,
			user_id: tab[1],
			lewis_tree_id: tab[2]
		};
		$.get('services.php',params,function(data){
			
			var rsp = $(data).children('rsp')[0];
			if ($(data).find('rsp').attr('status') != 'ok') { return; }
			
			if($(liTree).hasClass('jstree-open')) return false;
			if($(liTree).hasClass('jstree-leaf'))
			{
				window.location.href = $(liTree).find('a:first').attr('href');
			}
						
		});
		e.preventDefault();
		return false;
	});
	
	
});

