// Explorateur de photos
function photoExplorer(p_big_el, p_thumbs_elname, p_comment_el, p_source_el, p_params) {
	
	// mettre à jour les champs
	this.div_big 			= p_big_el;
	this.div_comment 		= p_comment_el;
	this.div_source 		= p_source_el;
	this.emptyMessage 		= 'Aucune photo';
	this.loadedThumbs 		= 0;
	this.errorThumbs		= 0;
	this.wndo 				= null;
	this.currScrollImg		= 0;
	this.currScrollDir 		= 1;
	this.div_scroll			= $get(p_thumbs_elname + "_main");
	this.div_scroll_hold 	= $get(p_thumbs_elname + "_hold");
	this.div_scroll_wn 		= $get(p_thumbs_elname + "_wn");
	this.div_scroll_lyr1	= $get(p_thumbs_elname + "_lyr1");
	this.bt_scroll_left 	= $get(p_thumbs_elname + "_scroll_left");
	this.bt_scroll_right 	= $get(p_thumbs_elname + "_scroll_right");
	
	// objets de présentation de l'image
	var me = this;
	this.img_div = $create("div", { "id" : "photoExplorer_img_div" }, null, this.div_big);
		
	// appliquer les styles necessaires
	this.img_div.style.height 	= "100%";
	this.img_div.style.width 	= "100%";
	this.img_div.style.overflow	= "hidden";
	
	// calculer les tailles par defaut
	this.thumb_height = this.div_scroll_lyr1.offsetHeight - 6;
	this.photo_max_height = this.img_div.offsetHeight;
	this.photo_max_width = this.img_div.offsetWidth;
	
	// parametres spécifiés 
	if (p_params.thumb_height != null)
		this.thumb_height = p_params.thumb_height;
	if (p_params.photo_max_height != null)
		this.photo_max_height = p_params.photo_max_height;
	if (p_params.photo_max_width != null)
		this.photo_max_width = p_params.photo_max_width;
		
}

photoExplorer.prototype.load = function(p_photos) {
	this.photos = p_photos;
	this.div_comment.innerHTML 	= "";
	this.div_source.innerHTML 	= "";
	$display(this.div_scroll);
	
	// si aucune photo
	if (this.photos.length == 0) {
		this.div_comment.innerHTML = this.emptyMessage;
		$undisplay(this.div_scroll);
		if (this.defaultPhoto != null)
			this._setPhoto(this.defaultPhoto);
		return;
	}
	
	this._loadThumbs();
}

photoExplorer.prototype._loadThumbs = function() {
	
	// vider les miniatures
	this.div_scroll_lyr1.innerHTML = "";
	this.loadedThumbs = 0;
	
	// créer le tableau
	this.div_scroll_table = $create("table", { "id" : "photoExplorer_t1" }, null, this.div_scroll_lyr1);
	var tbody = $create("tbody", null, null, this.div_scroll_table);
	var tr = $create("tr", null, null, tbody);
	
	// ajouter chaque miniature
	var me = this;
	for (i=0; i<this.photos.length; i++) {
		var td = $create("td", null, null, tr);
		var thumb = $create("img", 
							{ "className" : "thumb", "id" : "photoExplorer_thumb_" + i, "alt" : "photo " + (i + 1) }, 
							{ "click" : function(e) { me.showPhoto(e); }, "load" : function(e) { me.adapteThumb(e, this); }, "error" : function(e) { me.errorThumbs++; me.checkIfAllThumbsLoaded(); } }, 
							td
							);
		thumb.style.position = "absolute";
		$hide(thumb);
		thumb.src = this.photos[i].mini_url;
	}
	
	// charger la première photo
	if (this.photos.length > 0)
		this.showPhoto();
}
photoExplorer.prototype.adapteThumb = function(p_event, p_obj) {
	var thumb = (p_event.srcElement || p_obj);
	var new_height = this.thumb_height;
	var new_width  = thumb.width * ( new_height / thumb.height);
	thumb.height = new_height;
	thumb.width	= new_width;
	thumb.style.position = "static";
	$show(thumb);
	this.loadedThumbs++;
	this.checkIfAllThumbsLoaded();
}
photoExplorer.prototype.checkIfAllThumbsLoaded = function() {
	if (this.loadedThumbs + this.errorThumbs == this.photos.length)
		this.AllThumbsLoaded();
}
		
photoExplorer.prototype.AllThumbsLoaded = function() {
	// initialiser le scroll
		this.div_scroll_table.style.width = this.div_scroll_table.clientWidth;
		this.wndo = new dw_scrollObj('Photo_Selector_wn', 'Photo_Selector_lyr1', 'photoExplorer_t1');
		dw_scrollObj.slideDur = 400;
 		dw_scrollObj.GeckoTableBugFix('photoExplorer_wn');
		var me = this;
		this.wndo.on_slide_end = function() { me.currScrollImg += me.currScrollDir; me.setScrollButtonsState();};
		this.setScrollButtonsState();
}
photoExplorer.prototype.scrollLeft = function() {
	if (this.currScrollImg > 0) {
		this.currScrollDir 	= -1;
		var thumb = $get("photoExplorer_thumb_" + (this.currScrollImg - 1));
		// trouver le bord gauche de l'image
		var x = 0;
		for (var i=0; i<(this.currScrollImg - 1); i++)
			x += $get("photoExplorer_thumb_" + i).clientWidth + 10;
		dw_scrollObj.scrollTo('Photo_Selector_wn',x,0);
	}
}
photoExplorer.prototype.scrollRight = function() {
	if (this.currScrollImg < this.photos.length && Math.abs(this.wndo.x) < this.wndo.maxX) {
		this.currScrollDir 	= 1;
		var thumb = $get("photoExplorer_thumb_" + this.currScrollImg);
		var scrollby = 0 - (thumb.clientWidth + 10);
		dw_scrollObj.scrollBy('Photo_Selector_wn',scrollby,0);
	}
}
photoExplorer.prototype.showPhoto = function(p_event) {
	var photo_index = 0;
	if (p_event != null)
		photo_index = (p_event.srcElement || p_event.target).id.substr(20);
		
	// déselectionner toutes les miniatures
	for (var i=0; i<this.photos.length; i++)
		$removeCssClass($get("photoExplorer_thumb_" + i), "thumb_selected");
	
	// récupérer les infos et la vignette concernée
	var photo = this.photos[photo_index];
	var thumb = $get("photoExplorer_thumb_" + photo_index);
	
	// afficher la photo dans le div
	this._setPhoto(photo.url);
	
	// afficher les infos
	this.div_source.innerHTML = photo.source || "- source non spécifiée -";
	this.div_comment.innerHTML = photo.comment || "- sans titre -";
	
	$addCssClass(thumb, "thumb_selected");
}
photoExplorer.prototype._setPhoto = function(p_url) {
		
		// supprimer la photo si existante
		if (this.img_img != null)
			this.img_div.removeChild(this.img_a);
		// lien ou div
		var has_url = (at_url && at_url.trim() != '');
		this.img_a = $create(has_url ? 'a' : 'div', null, null, this.img_div);
		
		// créer l'objet photo
		this.img_img = $create("img", { "id" : "photoExplorer_img_img" }, null, this.img_a);
		
		// url et bordure
		if (has_url) {
			this.img_a.style.display = 'block';
			this.img_a.href = at_url;
			this.img_a.target = '_blank';
			this.img_img.style.border = 'none';
		}
		
		// évenement au chargement de la photo
		var me = this;
		this.img_load_handler = $addHandler(this.img_img, "load", function() { me.adaptePhoto(); });

		// masquer l'image
		this.img_img.style.position = "absolute";
		$hide(this.img_img);
		
		
		// réinitialiser la taille
		this.img_img.removeAttribute("height");
		this.img_img.removeAttribute("width");
		this.img_img.style.marginTop = "0px";
			
		// affecter la source
		this.img_img.src = p_url;
		
		

}
photoExplorer.prototype.adaptePhoto = function() {
	// calculer la nouvelle taille
	var img_height = this.img_img.height;
	var img_width  = this.img_img.width;
	var new_height = img_height;
	var new_width  = img_width;
	
	if (new_width > this.photo_max_width) {
		new_width = this.photo_max_width;
		new_height = img_height * ( new_width / img_width );
	}
	if (new_height > this.photo_max_height) {
		new_height = this.photo_max_height;
		new_width = img_width * ( new_height / img_height );
	}
		
	// affecter la taille
	this.img_img.height = Math.round(new_height);
	this.img_img.width	= Math.round(new_width);
	
	// afficher l'image
	$show(this.img_img);
	this.img_img.style.position = "static";
	
	// centrer l'image
	var m_x = Math.round((this.photo_max_width - new_width)  / 2);
	var m_y = Math.round((this.photo_max_height - new_height) / 2);
	this.img_a.style.marginTop  = m_y + "px";
	this.img_a.style.marginLeft = m_x + "px";
	// marge indésirable IE	
	if (navigator.isIE) {
		var version=parseFloat(navigator.appVersion.split('MSIE')[1]);
		if (version < 8 && (m_y != this.img_img.offsetTop)) 
			this.img_a.style.marginTop = (parseInt(m_y) - this.img_a.offsetTop + parseInt(m_y)) + "px";
	}

}
photoExplorer.prototype.setScrollButtonsState = function() {
	var left_enabled	= this.currScrollImg > 0;
	var right_enabled 	= this.currScrollImg < this.photos.length && Math.abs(this.wndo.x) < this.wndo.maxX;
	EdLib.setObjectOpacity(this.bt_scroll_left, left_enabled ? 1 : 0.3);
	EdLib.setObjectOpacity(this.bt_scroll_right, right_enabled ? 1 : 0.3);
	this.bt_scroll_left.style.cursor = left_enabled ? "pointer" : "default";
	this.bt_scroll_right.style.cursor = right_enabled ? "pointer" : "default";
}
