jQuery.fn.extend( {
	findPos : function() {
		obj = $(this).get(0);
		var curleft = obj.offsetLeft || 0;
		var curtop = obj.offsetTop || 0;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
		return {
			x :curleft,
			y :curtop
		};
	}
});

var album;

function MyAlbum(galleryFile) {
	this.galleryFileXml = galleryFile;
	this.images = new Array();
	this.captions = new Array();
	this.firstImage = 0;
	this.repImages = "";
	this.repThumb = "";

	this.thumbnailColumns = 3;
	this.thumbnailRows = 4;
	this.thumbnailWidth = 65;
	this.imageWidth = 640;
	this.nbMiniaturesAffichees = this.thumbnailColumns * this.thumbnailRows;

	this.largeurBordureBig = 10; // frameWidth dans le XML
	this.largeurBordureThumb = 3;
	this.thumbMargin = 10;
	this.gap = 30; // stagePadding dans le XML
	this.imageEnCours;
	this.usedBuffer = 1;
	this.unusedBuffer = 2;
	this.fadeDuration = 500;
	
	this.origAction = null;

	this.clicked = null;

	this.loadGallery = loadGallery;
	this.onLoadXml = onLoadXml;
	this.afficherImage = afficherImage;
	this.afficherMiniatures = afficherMiniatures;
	this.previousPage = previousPage;
	this.nextPage = nextPage;
	this.previousImage = previousImage;
	this.nextImage = nextImage;
	this.hideArrows = hideArrows;
	this.hideArrowsIfNeeded = hideArrowsIfNeeded;
	this.showArrows = showArrows;
	this.positionneFleches = positionneFleches;

	$(document).ready(this.loadGallery);

};

function loadGallery() {
	album.hideArrows(1);
	album.hideArrows(2);

	$("#big1").css("border-width", album.largeurBordureBig);
	$("#big2").css("border-width", album.largeurBordureBig);
	$(".imgthumb").css("border-width", album.largeurBordureThumb);
	$(".imgthumb").css("margin", album.thumbMargin);

	// largeur de la partie miniatures
	largThumb = (album.thumbnailColumns * (album.thumbnailWidth + (2 * album.largeurBordureThumb)))
			+ (album.thumbnailColumns * 2 * album.thumbMargin);
	// calcul de l'espace restant
	restant = $("body").width() - largThumb
			- ((album.largeurBordureBig * 2) + album.imageWidth);

	$("#thumb").width(largThumb);
	$("#thumb").css("left", restant / 2);
	$("#bigDiv1").css("left", (restant / 2) + largThumb + album.gap);
	$("#bigDiv1").css("right", 10);
	$("#bigDiv2").css("left", (restant / 2) + largThumb + album.gap);
	$("#bigDiv2").css("right", 10);

	$("#big1").mouseover( function() {
		album.showArrows(1, true);
	});

	$("#big1").mouseout( function() {
		album.hideArrows(1);
	});

	$("#big2").mouseover( function() {
		album.showArrows(2, true);
	});

	$("#big2").mouseout( function() {
		album.hideArrows(2);
	});

	// ce qui se passe une fois l'image centrale définie
	$("#big1").load( function() {

		$("#bigDiv2").fadeOut(album.fadeDuration);
		$("#bigDiv1").fadeIn(album.fadeDuration);
		
		// load caption
		$("#legende").text(album.captions[album.imageEnCours]); 
		$("#form_legende").attr("value", album.captions[album.imageEnCours]);
		$("#form_filename").attr("value", album.images[album.imageEnCours]);
		
		album.positionneFleches();
	});

	// ce qui se passe une fois l'image centrale définie
	$("#big2").load( function() {
		$("#bigDiv1").fadeOut(album.fadeDuration);
		$("#bigDiv2").fadeIn(album.fadeDuration);
		
		// load caption
		$("#legende").text(album.captions[album.imageEnCours]); 
		$("#form_legende").attr("value", album.captions[album.imageEnCours]);
		$("#form_filename").attr("value", album.images[album.imageEnCours]);
		
		album.positionneFleches();
	});

	// on affiche nbMiniaturesAffichees images à la fois
	// preparer les emplacement d'affichage des vignettes
	for (i = 0; i != album.nbMiniaturesAffichees; i++) {
		var aImg = document.createElement("a");
		// creation d'une image de vignette
		var imgThumb = new Image();

		$(imgThumb).css( {
			opacity :0
		});
		$(imgThumb).attr("id", "img" + i);
		$(imgThumb).addClass("imgthumb");
		$(aImg).attr("id", "aImg" + i);
		$(aImg).attr("href", "#aImg" + i);
		$(aImg).click( function() {
			album.clicked = this;
			album.imageEnCours = parseInt($(album.clicked).attr("index"));
			album.afficherImage(album.imageEnCours);
		});
		$(imgThumb).load( function() {
			$(this).animate( {
				opacity :1
			}, 1000);
		});
		$(aImg).append(imgThumb);
		$("#thumbImages").append(aImg);
		$("#thumbImages").css("height",
				(album.thumbnailRows * (album.thumbnailWidth + album.gap)));
	}

	// charger le fichier gallery.xml
	$.get(album.galleryFileXml, null, album.onLoadXml, "xml");

	$("#bigDiv1").click( function(event) {
		clicX = event.clientX - $(this).findPos().x;
		if (clicX > ($(this).width() / 2)) {
			// click sur la droite de la zone
			album.nextImage();
		} else {
			// click sur la gauche de la zone
			album.previousImage();
		}
	});
	$("#bigDiv2").click( function(event) {
		clicX = event.clientX - $(this).findPos().x;
		if (clicX > ($(this).width() / 2)) {
			// click sur la droite de la zone
			album.nextImage();
		} else {
			// click sur la gauche de la zone
			album.previousImage();
		}
	});
	
	album.origAction = $("#form_rename").attr("action");
};

function positionneFleches() {
	// positionne les fleches au milieu de l'image
	// taille image fleche
	tailleImageFleche = 50;

	leftFlechePrecedent = $("#big" + album.usedBuffer).findPos().x
			- $("#bigDiv" + album.usedBuffer).findPos().x
			+ album.largeurBordureBig - 1;
	topFleche = $("#big" + album.usedBuffer).findPos().y
			- $("#bigDiv" + album.usedBuffer).findPos().y
			+ $("#big" + album.usedBuffer).height() / 2
			+ album.largeurBordureBig - (tailleImageFleche / 2);
	leftFlecheSuivant = $("#big" + album.usedBuffer).findPos().x
			- $("#bigDiv" + album.usedBuffer).findPos().x
			+ $("#big" + album.usedBuffer).width() + album.largeurBordureBig
			- tailleImageFleche - 1;

	$("#imgPrecedent" + album.usedBuffer).css("left", leftFlechePrecedent);
	$("#imgPrecedent" + album.usedBuffer).css("top", topFleche);

	$("#imgSuivant" + album.usedBuffer).css("left", leftFlecheSuivant);
	$("#imgSuivant" + album.usedBuffer).css("top", topFleche);

}
function onLoadXml(xml, textStatus) {
	simpleViewer = xml.getElementsByTagName("simpleviewerGallery")[0];
	album.repImages = simpleViewer.getAttribute("imagePath");
	album.repThumb = simpleViewer.getAttribute("thumbPath");
	$(xml).find("filename").each( function() {
		album.images.push($(this).text());
	});
	$(xml).find("caption").each( function() {
		album.captions.push($(this).text());
	});
	// tout est chargé, on regarde le hash (#xxx)
	if (location.hash.length > 1) {
		album.imageEnCours = parseInt(location.hash.substring(1,
				location.hash.length));
	} else {
		album.imageEnCours = 0;
	}
	album.afficherImage(album.imageEnCours);
	album.afficherMiniatures();
};

function afficherImage(index) {
	this.imageEnCours = index;
	// switch buffers
	tmp = album.usedBuffer;
	album.usedBuffer = album.unusedBuffer;
	album.unusedBuffer = tmp;
	
	// load next buffer
	$("#big" + album.usedBuffer).attr("src",
			album.repImages + album.images[index]);
	$("#big" + album.usedBuffer).attr("index", index);
	location.hash = index;
	$("#form_rename").attr("action", album.origAction + "#" + index);
	pageEnCours = index / album.nbMiniaturesAffichees;
	album.firstImage = Math.floor(pageEnCours) * album.nbMiniaturesAffichees;
	album.afficherMiniatures();
	album.hideArrowsIfNeeded();

};

function showArrows(idBuffer, fade) {
	if (this.imageEnCours >= album.images.length - 1) {
		$("#imgSuivant" + idBuffer).hide();
	} else {
		if (fade = true) {
			$("#imgSuivant" + idBuffer).fadeIn(album.fadeDuration);
		} else {
			$("#imgSuivant" + idBuffer).show();
		}
	}
	if (this.imageEnCours == 0) {
		$("#imgPrecedent" + idBuffer).hide();
	} else {
		if (fade = true) {
			$("#imgPrecedent" + idBuffer).fadeIn(album.fadeDuration);
		} else {
			$("#imgPrecedent" + idBuffer).show();
		}
	}
}

function hideArrows(idBuffer) {
	$("#imgSuivant" + idBuffer).hide();
	$("#imgPrecedent" + idBuffer).hide();
}

function hideArrowsIfNeeded() {
	if (this.imageEnCours >= album.images.length - 1) {
		$("#imgSuivant" + album.usedBuffer).hide();
	}
	if (this.imageEnCours == 0) {
		$("#imgPrecedent" + album.usedBuffer).hide();
	}
}

function afficherMiniatures() {
	// afficher les images

	for (i = 0; i != album.nbMiniaturesAffichees; i++) {
		if (album.firstImage + i < album.images.length) {
			$("#img" + i).attr("src",
					album.repThumb + album.images[album.firstImage + i]);
			$("#aImg" + i).attr("index", album.firstImage + i);
			$("#aImg" + i).attr("href", "#" + (album.firstImage + i));
			// $("#aImg" + i).css("opacity", "1");
			$("#aImg" + i).show();

		} else {
			$("#img" + i).attr("src", "");
			$("#aImg" + i).attr("index", "-1");
			// $("#aImg" + i).css("opacity", "0");
			$("#aImg" + i).hide();

		}
		if (album.imageEnCours == (album.firstImage + i)) {
			$("#img" + i).css("border-color", "red");
		} else {
			$("#img" + i).css("border-color", "white");
		}
	}

	if ((album.firstImage + album.nbMiniaturesAffichees) > album.images.length) {
		$("#suivant").hide();
	} else {
		$("#suivant").show();
	}
	if (album.firstImage == 0) {
		$("#precedent").hide();
	} else {
		$("#precedent").show();
	}
};

function previousPage() {
	album.firstImage = album.firstImage - album.nbMiniaturesAffichees;
	album.afficherMiniatures();
};

function nextPage() {
	album.firstImage = album.firstImage + album.nbMiniaturesAffichees;
	album.afficherMiniatures();
};

function previousImage() {
	album.imageEnCours = parseInt($("#big" + album.usedBuffer).attr("index"));
	if (album.imageEnCours > 0) {
		album.imageEnCours = album.imageEnCours - 1;
		album.afficherImage(album.imageEnCours);
	}
};

function nextImage() {
	album.imageEnCours = parseInt($("#big" + album.usedBuffer).attr("index"));
	if (album.imageEnCours + 1 < this.images.length) {
		album.imageEnCours = album.imageEnCours + 1;
		album.afficherImage(album.imageEnCours);
	}
};


