var hNews="";
var hNewsDesc=new Array();
var ua = navigator.userAgent.toLowerCase();
var isFirefox=ua.indexOf('firefox')>0;
var dbg=true;
var newsDivHeight=-1;
//var newsLeft=xWidth("logoframe")+xLeft("logoframe");
var newsLeft=175;
var newsLeftContent=83;
function debug(msg)
{
	if(dbg)
	{
		//if(isFirefox) console.debug(msg);
	}
}
function newRss()
{
	if(newsDivHeight<=0)newsDivHeight=xHeight("newsDiv");
	jQuery("#newsFirst").html('newRss');
	 jQuery.ajax({
	   type: "POST",
	   url: pardailhan_url+"/rss_req.php",
	   data: "",
	   success: function(r,status){get_new_rss(r,status);}
	   });
}
function get_new_rss(rep,status)
{
	if (status != 'success')
	{
		alert("st:"+status);return;
	}
	hNews="";
	hNewsDesc=new Array();
	hNewsDesc=rep.split("|%|");
	
	jQuery("#newsDiv").show();
	jQuery("#newsContent").show();
	debug("get_new_rss");
	debug("get_new_rss "+xHeight("bando")+" "+xHeight("newsDiv"));
	//jQuery("#newsDiv").css("top",xHeight("bando")-xHeight("newsDiv")+6).css("left",newsLeft+"px");
	jQuery("#newsDiv").css("top",(200-newsDivHeight+12)+"px").css("left",newsLeft+"px");
	fireNews2();
}
var currentNews=-1;
var hNews="";
function fireNews2()
{
	currentNews++;
	debug("fireNews2 "+currentNews+"/"+hNewsDesc.length);
	if(currentNews>=hNewsDesc.length)
	{
			currentNews=-1;
			newRss();
			return;
	}
	rr=hNewsDesc[currentNews].split('|||');
	rImg=rr[5];
	hNews='<span class="newsDesc">'+rr[2]+'</span>.....';
	jQuery("#newsFirst").html(currentNews+'/'+hNewsDesc.length+'<span class="newsDate">'+rr[0]+' - </span>'+'<span class="newsTitle">'+rr[1]+'</span>');
	jQuery("#newsContent").html(hNews);
	jQuery("#newsSource").html('<a href="'+rr[3]+'" target="_blank"><img src="'+rImg+'"><a>');
	
	xMoveTo("newsContent",newsLeftContent);
	
	
	debug("top1 "+xHeight("newsDiv"));
	jQuery("#newsDiv").css("top",(200-newsDivHeight)+"px").css("left",newsLeft+"px");
	jQuery("#newsDiv").fadeIn(2000);
	if(xWidth("newsContent")>xWidth("newsDiv"))
	{
		waitReading(((xWidth("newsContent")-xWidth("newsDiv"))/100)*1000+2000,true);
	}
	else
	{
		waitReading((hNews.length/20)*1000+2000,false);
		
	}
}
function waitReading(waitsecs,callScroll)
{
	
	debug("waitReading:"+'waitsecs'+waitsecs+" "+callScroll);
	if(callScroll)
	{
		debug("wait before scroll:");
		setTimeout(function(){doScroll(waitsecs); parameter = null},((xWidth("newsDiv")-newsLeftContent)/200)*1000);
		//setTimeout("doScroll("+waitsecs+")",4000);
	}
	else
		setTimeout("newNews()",waitsecs);
}

function doScroll(waitSecs)
{
	jQuery("#newsContent").animate({
			left: -(xWidth("newsContent")-(xWidth("newsDiv")+newsLeftContent)+newsLeft)
		  }, (hNews.length/30)*1000,"linear", callNewNews);
}
function callNewNews()
{
	
	debug("top2 "+xHeight("newsDiv"));
	debug("wait newNews ");
	setTimeout(newNews,5000);
}
function newNews()
{
	debug("top3"+xHeight("newsDiv"));
	jQuery("#newsDiv").fadeOut("2000",fireNews2);
}
var hImagesId=new Array();
var hImagesParent=new Array();
var hImagesUrl=new Array();
var hImagesWidth=new Array();
var hImagesHeight=new Array();
var hImagesTitle=new Array();
var hArticleUrl=new Array();
var countImg=0;
var updtattachs= new xHttpRequest();


function updateAttachments()
{
	updtattachs.send("POST",pardailhan_url+"/attach_req.php","cmd=imgstoattach",5000,true,false,'',get_updtattachs);
}
function get_updtattachs(r,status)
{
	if (status != updtattachs.OK)
	{
		alert("st:"+status);return;
	}
	else
		rep=r.responseText;
}
var attachs= new xHttpRequest();
var articles=Array;
var articlesIndx=0;


function req_nb_attachments(nb)
{
	 jQuery.ajax({
	   type: "POST",
	   url: pardailhan_url+"/attach_req.php",
	   data: "cmd=getrand&nb="+nb,
	   success: function(r,status){get_nb_attachments(r,status);}
	   });
	//attachs.send("POST",pardailhan_url+"/attach_req.php","cmd=getrand&nb="+nb,5000,true,false,'',get_nb_attachments);
}
function get_nb_attachments(rep,status)
{
	if (status != 'success')
	{
		alert("st:"+status);return;
	}
	else
	repArray=rep.split("|%|");
	for(x=0;x<repArray.length;x++)
	{
		rr=repArray[x].split('|||');
		hImagesId[x]=rr[0];
		hImagesParent[x]=rr[1];
		hImagesUrl[x]=rr[3];
		hImagesWidth[x]=rr[4];
		hImagesHeight[x]=rr[5];
		hImagesTitle[x]=rr[6];
		hArticleUrl[x]=rr[7];
	}
	//st="";
	//for(x=0;x<hImagesUrl.length;x++)st+=hImagesUrl[x]+x+"  ";
	//xInnerHtml("insmsg",rep);
	animStart();
}
function animStart()
{
	var bando=xGetElementById("bando");
//alert(bando);
	for(x=0;x<hImagesUrl.length;x++)
	{
		//posh=Math.round(Math.random()*xHeight("bando"))-100;
		posh=3;
		//posw=Math.random()*(xWidth("bando")-hImagesWidth[x]-newsLeft)+newsLeft;
		posw=Math.random()*(xWidth("bando")-newsLeft)+newsLeft;
		posh=posh<0?0:posh;
		var b=new bloc(bando,hImagesWidth[x],hImagesHeight[x],posw,posh,"img_"+x);
		xOpacity(b.blocFrame,.7);
		b.setContent('<a href="'+hArticleUrl[x]+'" title="'+hImagesTitle[x]+'"><img src="'+hImagesUrl[x]+'">');
		b.setTitle(hImagesTitle[x]+" - ");
		b.resizeBloc(hImagesWidth[x]+10,hImagesHeight[x]+10);
		xMoveTo(b,posw,posh);
		b.show();
	}
	rndMove();
}
var oldMove=null;
function rndMove()
{
	if(oldMove!=null)xOpacity(oldMove,.5+Math.floor(Math.random()/2));
	x=Math.round(Math.random()*(hImagesUrl.length-1));
	el=xGetElementById("blocimg_"+x);
	if (el==null) alert("el null: image "+x+" sur "+hImagesUrl.length);
	el.obj.foreground();
	posw=Math.random()*(xWidth("bando")-hImagesWidth[x]-newsLeft)+newsLeft;
	oldMove=el;
	xOpacity(oldMove,1);
	jQuery("#blocimg_"+x).animate({ 
        left: posw,
		top : 3
      }, 6000,"linear", callbackMove );
	  
}
function callbackMove()
{
	setTimeout("rndMove()",Math.round(Math.random()*10000));
}
function winOnResize()
{
	
	xw=xClientWidth();xh=xClientHeight();
	for(x=0;x<hImagesUrl.length;x++)
	{
		//posh=Math.round(Math.random()*xHeight("bando"))-100;
		posh=3;
		posw=xLeft("blocimg_"+x)+xWidth("blocimg_"+x);
		if(posw>xw)
		{
			posw=Math.random()*xWidth("bando")-newsLeft+3;
			posh=posh<0?0:posh;
			xMoveTo("blocimg_"+x,posw,posh);
		}
	}
	xWidth("newsDiv",xw-(xWidth("logoframe")+10));
	debug(xWidth("logoframe"));
	xWidth("page",xw-10);
}
function bandoHeader()
{
	debug("bandoheader");
	jQuery(document).ready(function(){
	xAddEventListener(window, 'resize', winOnResize, false);
	xAddEventListener(window, 'scroll', winOnResize, false);
	req_nb_attachments(20);
	winOnResize();
	newRss();
	
 }); 
	
}