
function cbanners(path_imgs,temps_entre_banners,temps_transicio,despl_transicio,mida,nom,img_control,elms) {

	this.banners = new Array();
	this.index_actual = 0;
	this.banner_actual = null;
	this.temps_entre_banners = temps_entre_banners;
	var self = this; 
	for (i in elms) {
		var b = new banner(i,temps_transicio,despl_transicio,mida,self,elms[i].titol,elms[i].link,elms[i].imgs,path_imgs,(i==0),nom,img_control);
		this.banners.push(b);
	}
	if (this.banners.length<=1) {
		for (i in this.banners) this.banners[i].amaga_control();
		return;
	}
	
	this.banner_actual = this.banners[0];
	this.canvi = function() {
		var anterior=this.index_actual;
		var seguent=this.index_actual+1;
		if (seguent>=this.banners.length) seguent=0;
		if (!this.banners[seguent].comprova_carregat()) this.timer = setTimeout( function() { self.canvi(); }, 1000); 
		else {
			this.desactiva_control();
			this.index_actual=seguent;
			if (anterior>=0) this.banners[anterior].amaga();
			else this.canvi2();
		}
	}
	this.canvi2 = function() {
		this.banners[this.index_actual].ensenya();
	}
	this.canvi3 = function() {
		this.activa_control();
		this.timer = setTimeout( function() { self.canvi(); }, this.temps_entre_banners); 
	}
	
	this.desactiva_control = function() {
		this.banners[this.index_actual].control_off();
		for (i in this.banners) this.banners[i].control_desactivat();
	}
	this.activa_control = function() {
		this.banners[this.index_actual].control_on();
		for (i in this.banners) 
			if (i!=this.index_actual) this.banners[i].control_activat();
	}

	this.canvi_manual = function(i) {
		clearTimeout(this.timer);
		this.desactiva_control();
		this.banners[this.index_actual].amaga_manual();
		this.index_actual = i;
		this.activa_control();
		this.banners[this.index_actual].ensenya_manual();
		this.timer = setTimeout( function() { self.canvi(); }, this.temps_entre_banners); 
	}
	this.timer = setTimeout( function() { self.canvi(); }, this.temps_entre_banners); 
}

function banner(id,temps,despl,alt,obj_banners,titol,link,imgs,path_imgs,primer,nom,img_control) {
	var self = this;
	this.id = parseInt(id);
	this.top = 0;
	this.alt = alt;
	this.temps = temps;
	this.despl = despl;
	this.obj_banners = obj_banners;
	this.elms = new Array;
	this.elm = null;
	this.carregat = false;
	this.link=link;
	this.titol=titol;

	if (primer) {
		this.elm = document.getElementById(nom);
		this.carregat=true;
		for (i in imgs) { 
			this.elms[i] = document.getElementById(nom+"_"+i);
			this.elms[i].onload=function() { this.carregada=true; }
		}
	} else {
		this.elm = document.createElement("a");
		this.elm.setAttribute("href",link);
		if (link=="") this.elm.onclick = function() { return(false); }
		this.elm.setAttribute("title",titol);
		this.elm.className=nom;
		for (i in imgs) {
			this.elms[i] = document.createElement("img");
			this.elms[i].src=path_imgs+imgs[i];
			this.elms[i].className=nom+"_"+i+" png24";
			this.elms[i].carregada=false;
			this.elms[i].onload=function() { this.carregada=true; }
			this.elm.appendChild(this.elms[i]);
		}
		this.elm_tot = document.getElementById(nom+"s");
		this.elm_tot.appendChild(this.elm);
	}

	this.control_on = function() {
		this.elm_control_img.src="imatges/"+img_control+"_on.png";
		this.elm_control.onmouseover= function() { self.elm_titol.innerHTML = self.titol; };
		this.elm_control.onmouseout= function() { self.elm_titol.innerHTML = self.obj_banners.titol_tmp; };
	}
	this.control_off = function() {
		this.elm_control_img.src="imatges/"+img_control+"_off.png";
		this.elm_control.onmouseover=function() { self.elm_titol.innerHTML = self.titol; self.elm_control_img.src="imatges/"+img_control+"_on.png"; }
		this.elm_control.onmouseout=function() { self.elm_titol.innerHTML = self.obj_banners.titol_tmp; self.elm_control_img.src="imatges/"+img_control+"_off.png"; }
	}
	this.control_desactivat = function() {
		this.elm_control.onclick=function() { return(false); }
	}
	this.control_activat = function() {
		this.elm_control.onclick=function() { self.obj_banners.canvi_manual(self.id); return(false); }
	}
	this.amaga_control = function() {
		this.elm_control.style.display="none";
	}
	
	var div_controls=document.getElementById("control_"+nom);
	this.elm_titol = div_controls.getElementsByTagName("p")[0];
	this.elm_control=document.createElement("a");
	this.elm_control.setAttribute("title",titol);
	this.elm_control.setAttribute("href","#");
	this.elm_control_img=document.createElement("img");
	if (primer) {
		this.control_desactivat();
		this.control_on();
		self.obj_banners.titol_tmp = this.titol;
	} else {
		this.control_activat();
		this.control_off();
	}
	this.elm_control.appendChild(this.elm_control_img);
	div_controls.insertBefore(this.elm_control,this.elm_titol);
	
	this.amaga = function () {
		this.top = 0;
		this.index_img = this.elms.length-1; 
		this.img = this.elms[this.index_img];
		this.control_off();
		this.elm_titol.innerHTML = "";
		self.obj_banners.titol_tmp = "";
		setTimeout(function(){self.amaga2();}, this.temps);
	}
	this.amaga2 = function () {
		this.top = this.top + this.despl;
		if (this.top>this.alt) this.top = this.alt;
		this.img.style.top = this.top+"px";
		if (this.top<this.alt) setTimeout(function(){self.amaga2();}, this.temps);
		else { 
			this.top=0; 
			if (this.index_img>0) {
				this.index_img--;
				this.img=this.elms[this.index_img];
				setTimeout(function(){self.amaga2();}, this.temps);
			} else {
				this.elm.className="";
//				this.control_activat();
				this.obj_banners.canvi2();
			}
		}
	}
	this.amaga_manual = function() {
		for (i in this.elms) this.elms[i].style.top = this.alt + "px";
		this.elm.className="";
		this.elm_titol.innerHTML = "";
		self.obj_banners.titol_tmp = "";
	}
	this.ensenya_manual = function() {
		for (i in this.elms) this.elms[i].style.top = "0px";
		this.elm.className="actiu";
		this.elm_titol.innerHTML = this.titol;
		self.obj_banners.titol_tmp = this.titol;
	}
	this.comprova_carregat = function() {
		if (!this.carregat) { 
			var carregat=true;
			for (i in this.elms) 
				if (!this.elms[i].carregada) carregat=false;
			this.carregat=carregat;
		}
		return(this.carregat);
	}
	this.ensenya = function () {
		this.top = this.alt;
		this.index_img = 0;
		this.img = this.elms[this.index_img];
		for (i in this.elms) this.elms[i].style.top = this.alt+"px";
		this.elm.className="actiu";
//		this.control_desactivat();
		setTimeout(function(){self.ensenya2();}, this.temps);
	}
	this.ensenya2 = function () {
		this.top = this.top - this.despl;
		if (this.top<0) this.top = 0;
		this.img.style.top = this.top+"px";
		if (this.top>0) setTimeout(function(){self.ensenya2();}, this.temps);
		else { 
			this.top=this.alt; 
			if (this.index_img<this.elms.length-1) {
				this.index_img++;
				this.img=this.elms[this.index_img];
				setTimeout(function(){self.ensenya2();}, this.temps);
			} else {
				this.elm_titol.innerHTML = this.titol;
				self.obj_banners.titol_tmp = this.titol;
				this.obj_banners.canvi3();
				this.control_on();
			}
		}
	}
}

