//var url = 'http://feeds.feedburner.com/jQueryHowto';
//var url = 'http://preproduction1.profils.net/SophiaConseil.ProfilsNET-Cand/offre-de-emploi/flux-rss.ashx';
var url = 'http://sophiaconseil.profils.org/offre-de-emploi/flux-rss.ashx';
//	var url = 'http://preproduction1.profils.net/SophiaConseil.ProfilsNet-Cand/offre-de-emploi/tous-les-flux-rss.aspx';
var max = 5;		// Nombre d'items à charger 
$.jGFeed(
	url,
	function(feeds){
		if(!feeds) return false;									// On n'affiche rien en cas d'erreur
		
		var annonces = $('#ads');
		var liste = $('#liste-rss');
		
		for(var i=0; i<feeds.entries.length; i++){					// Génération des items
			var entry = feeds.entries[i];
			var ctn = txtMax( entry.content, 255 );
			var item = $(
				'<li '+((i==0) ? 'class="plane"' : '' )+' style="background-color: #fff">'+		// debeug anticrénelage sur ie
					'<a href="'+entry.link+'" traget="_blank">'+	// urlEmploi dans view/home
//					'<a href="'+entry.link+'" traget="_blank">'+
						'<h4>'+entry.title+'</h4>'+
						'<p style="background-color: #fff">'+ctn+'</p>'+
					'</a>'+
				'</li>'
			);
			liste.append(item);
		}

		var courant = $('#ads .plane');								// Mise en place de l'animation
		var suivant = null;
		
		$('#ads .next').live( 'click', function(e){
			e.preventDefault();
			if ( courant.next('li').length == 0 ) {
				suivant = liste.children('li:first');
				liste.stop(true, true).animate ({ 'margin-left': 0 }, 500 );
			}
			else {
				suivant = courant.next('li');
				liste.stop(true, true).animate ({ 'margin-left': '-=290px' }, 500 );
			}
			courant = suivant;
		});
		
		$('#ads .prev').live( 'click', function(e){
			e.preventDefault();
			if ( courant.prev('li').length == 0 ) {
				suivant = liste.children('li:last');
				liste.animate ({ 'margin-left': '-'+((max-1)*290)+'px' }, 500 );
			}
			else {
				suivant = courant.prev('li');
				liste.animate ({ 'margin-left': '+=290px' }, 500 );
			}
			courant = suivant;
		});
		
		annonces.css({opacity:0});
		annonces.removeClass('hidden');
		annonces.animate( {opacity:1}, 500 );
		
		// Défilement automatique
		setInterval(function(){
			$('#ads .next').trigger('click');
		}, 9000);
		
	},
	max
);



function txtMax( chaine, max, prefixe ) {
	if( chaine.length <= max )
		return;
	if( !prefixe )
		prefixe = '...';
	var tmp = chaine.substr( 0, max );
	var dernier_espace = tmp.lastIndexOf( ' ' );
	tmp = tmp.substr( 0, dernier_espace );
	return tmp+prefixe;
}


(function() {
	var panneau = "#news";
	var liste = $('#news ul');
	var max = liste.children('li').length;
	var courant = $(panneau+' .plane');								// Mise en place de l'animation
	var suivant = null;
	
	$(panneau+' .next').live( 'click', function(e){
		e.preventDefault();
		if ( courant.next('li').length == 0 ) {
			suivant = liste.children('li:first');
			liste.stop(true, true).animate ({ 'margin-left': 0 }, 500 );
		}
		else {
			suivant = courant.next('li');
			liste.stop(true, true).animate ({ 'margin-left': '-=290px' }, 500 );
		}
		courant = suivant;
	});
	
	$(panneau+' .prev').live( 'click', function(e){
		e.preventDefault();
		if ( courant.prev('li').length == 0 ) {
			suivant = liste.children('li:last');
			liste.animate ({ 'margin-left': '-'+((max-1)*290)+'px' }, 500 );
		}
		else {
			suivant = courant.prev('li');
			liste.animate ({ 'margin-left': '+=290px' }, 500 );
		}
		courant = suivant;
	});
	
	// Défilement automatique
	setInterval(function(){
		$(panneau+' .next').trigger('click');
	}, 5000);
})();

