
//--------------- AJAX -------------------------
var cache_news = new Array();
var cache_synopsis = new Array();
var offset_news = 0;

function expand_all_news(nbNews){
	// TODO modifier pour quand moins de 10 news
	document.getElementById('expand_all_news_img').style.display = 'none';
	document.getElementById('collapse_all_news_img').style.display = '';
	for(i=0; i<nbNews; i++){
		document.getElementById('expand_img_'+i).onclick();
	}
}

function collapse_all_news(nbNews){
	document.getElementById('collapse_all_news_img').style.display = 'none';
	document.getElementById('expand_all_news_img').style.display = '';
	for(i=0; i<nbNews; i++){
		document.getElementById('collapse_img_'+i).onclick();
	}

}

function fetch_next_news(nb_news){
	var obj = new NewsFetcher(nb_news);
	obj.next_news();
}

function fetch_previous_news(nb_news){
	obj = new NewsFetcher(nb_news);
	obj.previous_news();
}

function NewsFetcher(nb_news)
{
	this.new_offset = 0;
	//=== success =====//
	this.success = function(o)
	{
		if(o.responseText !== undefined){
			//alert('['+o.responseText+']');
			if(o.responseText.replace(/(^\s*)|(\s*$)/g,'') != ''){
				document.getElementById('news').innerHTML = o.responseText;
				offset_news = this.new_offset;
				cache_news = new Array();
				cache_synopsis = new Array();
				document.getElementById('collapse_all_news_img').style.display = 'none';
				document.getElementById('expand_all_news_img').style.display = '';
			}
		}
		else{
			this.failure(o);
		}
	}
	
	//=== failure =====//
	this.failure = function(o){
		document.getElementById('news').innerHTML = 'ERREUR';
	}
	
	//=== next_news =====//
	this.next_news = function()
	{
		this.new_offset = offset_news+nb_news;
		var sUrl = "pap.php?type=fn&o="+(this.new_offset); 
		var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, {success:this.success,  failure:this.failure,scope: this}); 		
	}

	//=== prec_news =====//
	this.previous_news = function()
	{
		if(offset_news >= nb_news){
			this.new_offset = offset_news-nb_news;
			var sUrl = "pap.php?type=fn&o="+(this.new_offset); 
			var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, {success:this.success,  failure:this.failure,scope: this}); 
		}
	}
	
	
}



function expand_news(id,threadid){
	obj = new NewsExpander(id,threadid);
	obj.do_expand();
}

function collapse_news(id,threadid){
	obj = new NewsExpander(id,threadid);
	obj.do_collapse();
}

function NewsExpander(id,threadid)
{
	this.id = id;
	this.threadid = threadid;
	this.text = '';

	//=== do_collapse =====//
	this.do_collapse = function (){
		this.text = cache_synopsis[this.id];
		this.collapse_synopsis();
	}
	
	//=== success =====//
	this.success = function(o){
		if(o.responseText !== undefined){
			this.text = o.responseText;
			cache_news[this.id] = this.text;
			this.collapse_news();
		}
		else{
			this.failure(o);
			this.collapse_news();
		}
	}
	
	//=== failure =====//
	this.failure = function(o){
		this.text = 'ERREUR';
	}
	
	//=== do_expand =====//
	this.do_expand = function (){
		// sauvegarde du synopsis
		if(cache_synopsis[this.id] == undefined || cache_synopsis[this.id] == null){
			cache_synopsis[this.id] = document.getElementById('news_content_text_'+this.id).innerHTML;
		}
		// si la news n'est pas en cache on récupère celle-ci
		if(cache_news[this.id] == undefined || cache_news[this.id] == null){
			var sUrl = "pap.php?type=nt&ti="+this.threadid; 
			var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, {success:this.success,  failure:this.failure,scope: this}); 
		}
		else{
			this.text = cache_news[this.id];
			this.collapse_news();
		}
	}
	
	//=== collapse_news =====//
	this.collapse_news = function(){
		//document.getElementById('news_content_'+this.id).style.overflow = 'hidden';		// IE 
		var anim = new YAHOO.util.Anim('news_content_'+this.id, { height: { to: 0}},0.5, YAHOO.util.Easing.easeOut);
		anim.onComplete.subscribe(this.expand_news,this,true); 
		anim.animate();
	}
	
	//=== expand_news =====//
	this.expand_news = function(){
		//document.getElementById('news_content_'+this.id).style.overflow = 'visible';		// IE 
		document.getElementById('news_content_text_'+this.id).innerHTML = this.text;
		document.getElementById('expand_img_'+this.id).style.display = 'none';
		document.getElementById('collapse_img_'+this.id).style.display = '';
		var anim = new YAHOO.util.Anim('news_content_'+this.id, {height: {to: 100, unit: '%'}},2, YAHOO.util.Easing.easeNone);
		anim.animate();
		//document.getElementById('news_content_'+this.id).style.overflow = 'visible';		
		//document.getElementById('news_content_'+this.id).style.height = '100%';		
		
	}

	//=== collapse_synopsis =====//
	this.collapse_synopsis = function(){
		//document.getElementById('news_content_'+this.id).style.overflow = 'hidden';		// IE 
		var anim = new YAHOO.util.Anim('news_content_'+this.id, { height: { to: 0}},1, YAHOO.util.Easing.easeOutStrong);
		anim.onComplete.subscribe(this.expand_synopsis,this,true); 
		anim.animate();
	}
	
	//=== expand_synopsis =====//
	this.expand_synopsis = function(){
		//document.getElementById('news_content_'+this.id).style.overflow = '';		// IE 
		document.getElementById('news_content_text_'+this.id).innerHTML = this.text;
		document.getElementById('collapse_img_'+this.id).style.display = 'none';
		document.getElementById('expand_img_'+this.id).style.display = '';
		var anim = new YAHOO.util.Anim('news_content_'+this.id, {height: {to: 54}},0.5, YAHOO.util.Easing.easeOut);
		anim.animate();
		
	}
	
}