var coupsCoeur = function(imgs){
	this.imgs = imgs;
	this.current = [0,1,2];
	this.memLoad = 0;
	this.timerChange;
	this.timerViewer;
	this.memOpacity = 1;
	this.init();
	this.blockTitre = false;
}

// initialisation
coupsCoeur.prototype.init = function(){
	var self = this;
	var zones = document.getElementById('coupsCoeur').getElementsByTagName('div');
	for(var i=0; i<zones.length; i++){
		if(zones[i].addEventListener){
			zones[i].addEventListener("mouseover",function(e){self.viewHideTitle(e)},false);
			zones[i].addEventListener("mouseout",function(e){self.viewHideTitle(e)},false);
		}else{
			zones[i].attachEvent("onmouseover",function(e){self.viewHideTitle(e)});
			zones[i].attachEvent("onmouseout",function(e){self.viewHideTitle(e)});
		}
	}
	if(this.imgs.length > 3){
		for(var i=0; i<3; i++){
			if(i == 0){
				if(this.current[2] + 1 == this.imgs.length) this.current[i] = 0;
				else this.current[i] = this.current[2] + 1;
			}else{
				if(this.current[i-1] + 1 == this.imgs.length) this.current[i] = 0;
				else this.current[i] = this.current[i-1] + 1;
			}
		}
		this.loadPhoto();
	}
}

// affichage / masquage du titre
coupsCoeur.prototype.viewHideTitle = function(evt){
	if(this.blockTitre == false){
		if(evt.srcElement) var z = evt.srcElement;
		else var z = evt.target;
		if(z.tagName != "DIV") z = z.parentNode;
		var t = z.getElementsByTagName('span')[0];
		if(evt.type == "mouseover"){
			t.style.top = (210 - t.offsetHeight) + "px";
		}else{
			t.style.top = "210px";
		}
	}
}

// preload des images
coupsCoeur.prototype.loadPhoto = function(){
	var self = this;
	if(this.memLoad < 3){
		this.memLoad = this.memLoad + 1;
		var im = new Image();
		im.onload = function(){self.loadPhoto()};
		im.src = this.imgs[this.current[this.memLoad-1]]['fichier'];
	}else{
		this.memLoad = 0;
		this.timing();	
	}
}

// lancement du timer pour le changement de photo
coupsCoeur.prototype.timing = function(){
	var self = this;
	this.timerViewer = setTimeout(function(){self.transitionLoad()},5000);
}

// masquage des titres suite à la transition
coupsCoeur.prototype.transitionLoad = function(){
	var zones = document.getElementById('coupsCoeur').getElementsByTagName('span');
	for(var i=0; i<zones.length; i++){
		zones[i].style.top = "210px";
	}
	this.blockTitre = true;
	this.hidePhotos();
}

// masquage progressif des photos
coupsCoeur.prototype.hidePhotos = function(){
	clearTimeout(this.timerView);
	var self = this;
	var zones = document.getElementById('coupsCoeur').getElementsByTagName('img');
	this.memOpacity = this.memOpacity - 0.1;
	for(var i=0; i<zones.length; i++){
		zones[i].style.opacity = this.memOpacity;
		zones[i].style.filter = "alpha(opacity=" + (this.memOpacity*100) + ")";
	}
	if(this.memOpacity > 0){
		this.timerChange = setTimeout(function(){self.hidePhotos()},50);
	}else{
		this.memOpacity = 0;
		this.changePhotos();
	}
}

// modification des photos
coupsCoeur.prototype.changePhotos = function(){
	var zones = document.getElementById('coupsCoeur').getElementsByTagName('div');
	for(var i=0; i<zones.length; i++){
		zones[i].getElementsByTagName('img')[0].src = this.imgs[this.current[i]]['fichier'];
		zones[i].getElementsByTagName('img')[0].alt = this.imgs[this.current[i]]['alt'];
		zones[i].getElementsByTagName('img')[0].title = this.imgs[this.current[i]]['alt'];
		zones[i].getElementsByTagName('span')[0].innerHTML = this.imgs[this.current[i]]['titre'];
		zones[i].getElementsByTagName('img')[0].style.marginTop = ((210-zones[i].getElementsByTagName('img')[0].height)/2) + "px";; 
	}
	this.showPhotos();
}

// affichage progressif des photos
// masquage progressif des photos
coupsCoeur.prototype.showPhotos = function(){
	var self = this;
	var zones = document.getElementById('coupsCoeur').getElementsByTagName('img');
	this.memOpacity = this.memOpacity + 0.1;
	for(var i=0; i<zones.length; i++){
		zones[i].style.opacity = this.memOpacity;
		zones[i].style.filter = "alpha(opacity=" + (this.memOpacity*100) + ")";
	}
	if(this.memOpacity < 1){
		this.timerChange = setTimeout(function(){self.showPhotos()},50);
	}else{
		this.memOpacity = 1;
		clearTimeout(this.timerChange);
		for(var i=0; i<3; i++){
			if(i == 0){
				if(this.current[2] + 1 == this.imgs.length) this.current[i] = 0;
				else this.current[i] = this.current[2] + 1;
			}else{
				if(this.current[i-1] + 1 == this.imgs.length) this.current[i] = 0;
				else this.current[i] = this.current[i-1] + 1;
			}
		}
		this.blockTitre = false;
		this.loadPhoto();
	}
}
