var loadHorSlide = function (objectName,speed,delay,viewPage,nbFiche,urlCatalogue,idCatalogue){

		if (urlCatalogue!=''){
			
			var myAjax = new Ajax.Request(urlCatalogue,{
				method: 'get',
				parameters: {},
				onSuccess: function (xhr){
				
					$('id_'+objectName).innerHTML='';
					var countNbFiche=0
					var nodeExport = xhr.responseXML;
					var nodesFiche = nodeExport.getElementsByTagName('fiche');
					var ul=document.createElement('ul');
					
					for (i=0 ; i<nodesFiche.length ; i++){
						countNbFiche++
						if (countNbFiche==1&&countNbFiche<=nbFiche){
							var li=document.createElement('LI');
							var table=document.createElement('TABLE');
							var tbody = document.createElement("TBODY");
							table.width='100%';
							table.border='0';
							table.margin='0';
							table.padding='0';
							table.style.textAlign='center';
							var tr=document.createElement('TR');
							table.appendChild(tbody);
							tbody.appendChild(tr);
							li.appendChild(table);
						}
						
						var td=document.createElement('TD');
						var img=document.createElement('IMG');
						
						var nodeFiche = nodesFiche[i];

						var attributes = nodeFiche.attributes;
						var id = attributes.getNamedItem('id').nodeValue;

						var nodeTitle = nodeFiche.getElementsByTagName('title')[0];
						var title = nodeTitle.firstChild.nodeValue;

						var nodeUrl = nodeFiche.getElementsByTagName('url')[0];
						var url = nodeUrl.firstChild.nodeValue;

						var nodePicture = nodeFiche.getElementsByTagName('picture')[0];
						var picture = nodePicture.firstChild.nodeValue;			

						img.src='/images/catalogue/id_'+idCatalogue+'/images/'+picture;
						img.title=title;
						img.border='0';
						img.style.cursor='pointer';
						eval('img.onclick=function(){document.location.href="'+url+'"}');
						
						td.appendChild(img);
						tr.appendChild(td);
						
						if (countNbFiche==nbFiche||i==nodesFiche.length-1){
							ul.appendChild(li);
							countNbFiche=0
						}
					}

					$('id_'+objectName).appendChild(ul);
					eval(objectName+'=new horslider("id_'+objectName+'",'+speed+','+delay+','+viewPage+');');
				} 
			});
	
		}else{
			eval(objectName+'=new horslider("id_'+objectName+'",'+speed+','+delay+','+viewPage+');');
		}
};

Object.extend(Event, {
        wheel:function (event){
        var delta = 0;
        if (event.wheelDelta) {
			delta = event.wheelDelta/120;
			if (window.opera) delta = -delta;
        } else if (event.detail) {
            delta = -event.detail/3;
        }
		if(navigator.userAgent.indexOf('Mac') != -1) delta = -delta; 	 
		return Math.round(delta);
	}
});

var horslider = Class.create();
horslider.prototype = {
	initialize: function(capture, duree, secExecution, pagination){
		this.capture = capture;
		this.duree = duree;
		this.secExecution = secExecution;
		this.pagination = pagination;
		this.nCell = 0;
		this.id = 1;
		this.po = 0;	
		this.f = $$('div#'+this.capture+' ul li');
		this.px = $(this.capture).getWidth();
		//this.pxH = $(this.capture).getHeight();
		this.start();
	},
	start: function(){		
				this.mover = $(this.capture).firstDescendant();	
				this.nCell = this.f.length;	
				for(i=0;i!=this.nCell;i++){
					this.f[i].setStyle({
					  width: this.px+'px',
					  //height: (this.pxH-40)+'px',
					  //height: (this.pxH)+'px',
					  float: 'left'
					  });	
				}
				$(this.capture).setStyle({
					overflow:'hidden',
					position: 'relative'
					});
				
				$(this.capture).firstDescendant().setStyle({
					width:(this.px*this.nCell)+'px'
					});	
				if(this.pagination){
						$(this.capture).insert({bottom:'<ol class="horslider_pagination"></ol>'});
						this.olPagination  = $(this.capture).firstDescendant().next();
						$(this.olPagination).setStyle({
							width: this.px+'px'
							});
						this.wb = Math.floor(this.px/this.f.length);
						for(i=1;i!=(this.f.length+1);i++){
							$(this.olPagination).insert({bottom:'<li><a style="width:'+this.wb+'px;">'+i+'</a></li>'});
							if(i!=this.id){
								$(this.olPagination).childElements()[i-1].setStyle({
									opacity:0.2
									});					
							}
						}
						$(this.olPagination).childElements()[0].setStyle({
							opacity:1
						});	
						this.startOC();
				}
				this.startPe();
				Event.observe($(this.capture),"mouseout", this.startPe.bind(this));
				Event.observe($(this.capture),"mouseover", this.stopPe.bind(this));								
				Event.observe($(this.capture), "mousewheel", this.wheelwheel.bind(this));		
				Event.observe($(this.capture), "DOMMouseScroll", this.wheelwheel.bind(this));				
	},
	startOC: function(){
	Event.observe($(this.olPagination),"click", this.moveP.bind(this));
	},
	startPe: function(){
	this.periodik = new PeriodicalExecuter(this.Pe.bind(this),this.secExecution);	
	},
	stopPe: function(){
	this.periodik.stop();
	},
	effaceP: function(mop){
		this.mop = mop;
		if(this.pagination)
		new Effect.Fade($(this.olPagination).childElements()[this.mop-1],{duration:0.3,to:0.2})
	},
	move: function(xp){
		this.xp = xp;
		new Effect.Move(this.mover, { 
					x: this.xp, 
					y: 0,
					mode:'absolute',
					duration: this.duree,
					transition: Effect.Transitions.sinoidal
				});
				if(this.pagination){
					new Effect.Appear($(this.olPagination).childElements()[this.id-1],{duration:0.3,to:1})
				}	
	},	
	Pe: function(){
					if(this.id<this.nCell){
							this.po=this.po-this.px;
							this.effaceP(this.id);
							this.id=this.id+1;
							this.move(this.po);
						}else{
							this.po=0;
							this.effaceP(this.id);
							this.id=1;
							this.move(this.po);
						}
	},
	moveP: function(evt){
		var child = Event.element(evt);
		this.occ = parseInt(child.innerHTML);
		if(this.id>this.occ){
			this.diff= this.id-this.occ;
			this.po=this.po+(this.px*this.diff);
			this.effaceP(this.id);
			this.id=this.occ;
			this.move(this.po);
		}else if(this.id<this.occ){
			this.diff= this.occ-this.id;
			this.po=this.po-(this.px*this.diff);
			this.effaceP(this.id);
			this.id=this.occ;
			this.move(this.po);
		}
	},
	stopEvent:function(pE)
	{
	   if (!pE)
		 if (window.event)
		   pE = window.event;
		 else
		   return;
	  if (pE.cancelBubble != null)
		 pE.cancelBubble = true;
	  if (pE.stopPropagation)
		 pE.stopPropagation();
	  if (pE.preventDefault)
		 pE.preventDefault();
	} ,
	wheelwheel: function(e){
						this.event = e;
						this.stopPe();
						this.stopEvent(e);
						
						if (Event.wheel(this.event) < 0){
							if(this.id<this.nCell){
									this.po=this.po-this.px;
									this.effaceP(this.id);
									this.id=this.id+1;
									this.move(this.po);
								}
						}else{
							if(this.id!=1){
									this.po=this.po+this.px;
									this.effaceP(this.id);
									this.id=this.id-1;
									this.move(this.po);
								}
						}
	},
	slideVertical: function(dir,obj){
						this.direction = dir;
						this.stopPe();
												
						if (this.direction < 0){
							if(this.id<this.nCell){
									this.po=this.po-this.px;
									this.effaceP(this.id);
									this.id=this.id+1;
									this.move(this.po);
								}
						}else{
							if(this.id!=1){
									this.po=this.po+this.px;
									this.effaceP(this.id);
									this.id=this.id-1;
									this.move(this.po);
								}
						}
	}	
};
