var ListeFiche;
var BaseImg="/images/catalogue/id_2/images/";
var TimeEnd;
var ladate = new Date();
function ds_getleft(el) {
	var tmp = el.offsetLeft;
	el = el.offsetParent
	while(el) {
		tmp += el.offsetLeft;
		el = el.offsetParent;
	}
	return tmp;
}

function Init(){
	var Noeuds = new Array('date','titre','description','lien','photo');
	var time = ladate.getMilliseconds();
	var myAjax = "";

	myAjax = new Ajax.Request(
      'exports/xml_catalogue.asp?start=0&limit=4&sid=5f96934d373f978f94453a81762d8847&date='+time,
      {
        method: 'get',
        parameters: {},
        onSuccess: function (xhr)
          {
			ListeFiche = new Array();
			var xml = xhr.responseXML;
			var nodeExports=xml.getElementsByTagName('export')[0];
			var Fiches = nodeExports.getElementsByTagName('fiche');
			for(var i=0;i<Fiches.length;i++)
			{

				var Indice = ListeFiche.length;
				var Fiche = Fiches[i];
				var attributes = Fiche.attributes;
				ListeFiche[i] = new Array();
				for(var j=0;j<attributes.length;j++)
				{
					ListeFiche[Indice][attributes[j].name]= attributes[j].nodeValue;
				}
				for(var j=0;j<Noeuds.length;j++)
				{
					
					ListeFiche[Indice][Noeuds[j]] = typeof(Fiche.getElementsByTagName(Noeuds[j])[0].text)=="undefined"?Fiche.getElementsByTagName(Noeuds[j])[0].textContent:Fiche.getElementsByTagName(Noeuds[j])[0].text;
				}
			}

			buildIt();

		 },
		onFailure: function(xhr)
		{

			buildIt();
		}
      }
    );
};

function buildIt()
{
	var container = $('hp_news_box');
	
	container.innerHTML = "";
	
	for(var i=0;i<ListeFiche.length;i++)
	{
		var div = el("div",container);
		div.addClassName("news_box");
		
		var divtitle = el("div",div);
		divtitle.addClassName("news_title");
		divtitle.innerHTML = ListeFiche[i]['titre'];		
		
		var divtxt = el("div",div);
		divtxt.addClassName("news_txt");
				
		// Teraz pod tym DIVem wstawiłem obrazek z stylem
		var imgtitle = el("img",divtxt);
		imgtitle.style.marginBottom = "10px"; 
		imgtitle.style.marginLeft = "10px";
		imgtitle.style.border=0;
		imgtitle.setAttribute("align","right");
		imgtitle.src = "/images/catalogue/id_2/images/"+ListeFiche[i]['photo'];
		
		// obok jest dany opis (tekst) newsa 
		var description = document.createTextNode(ListeFiche[i]['description']);
		divtxt.appendChild(description);
		divtxt.innerHTML+="<br />";
		
		var a =el("a",divtxt);
		a.href = ListeFiche[i]['lien'];		
		img = el("img",a);
		img.src = "/images/sites/site/hp_plus_info.png";
		img.addClassName("hp_img");			
		
	}
}


function getTime()
{
	return ladate.getTime();
}

function in_array(val,array)
{
	for(var i=0;i<array.length;i++)
		if(val==array[i])
			return true;
	return false;
}

function el(type,attach)
{
	var el = document.createElement(type);
	Element.extend(el);
	if(typeof(attach)!="undefined")
		attach.appendChild(el);
	return el;
}
setTimeout("Init()",200);
