/**
 * albumPhoto plugin
 * version 1.0
 * date 04/2009
 */

/* définition des constants du plugin */
CAL_TYPE_CAROUSSEL = 0;

(function($){

	$.fn.albumPhoto = function(param) {
		return this.each(function() {
			new $.albumPhoto( $(this), param);	
		});
	};
	
	$.albumPhoto = function(jEl, param){
	
		param = $.extend({
			type: CAL_TYPE_CAROUSSEL,
			nbImgVisible: 4,
			largeurImg: 122
		}, param);
		
		if (param.type == CAL_TYPE_CAROUSSEL) 
			genererCaroussel();
		
		/*
		 * @name genererCaroussel
		 * @desc place pour les photos dans un caroussel
		 * return void
		 */
		function genererCaroussel(){
			// préparation  du html
			jEl.removeAttr('id').addClass('monCarousel');
			jEl.append('<div id="btG"></div><div id="btD"></div><div class="contenu"><ul> </ul><p class="supprFloat"></p></div>');
			
			$("a", jEl).each(function(){
				var d = $("<li></li>");
				d.append($(this)).appendTo($("ul", jEl));
			});
			
			$("> *", jEl).each(function(){
				if ($(this).attr('id') != 'btG' && $(this).attr('id') != 'btD' && !$(this).hasClass('contenu')) 
					$(this).remove();
			});
			
			// mise en place des actions
			pos = 0;
			posMax = $("li", jEl).length - 1;
			$("ul", jEl).css('width', ($("li", jEl).length * (param.largeurImg) ) + 'px');
			if (posMax >= param.nbImgVisible) {
				$("#btD", jEl).css('background-position', '-64px').css('cursor', 'pointer');
				$("#btD", jEl).click(function(){
					pos = pos + param.nbImgVisible <= posMax ? pos + param.nbImgVisible : 0;
					actionBtCaroussel();
				});
				$("#btG", jEl).click(function(){
					pos = pos - param.nbImgVisible <= 0 ? 0 : pos - param.nbImgVisible;
					actionBtCaroussel();
				});
			}
		}
		/*
		 * @name actionBtCaroussel
		 * @desc définition des action sur les boutons droites et gauches
		 * return void
		 */
		function actionBtCaroussel(){
			$("ul", jEl).animate({
				left: -(pos * param.largeurImg) + 'px'
			}, "speed");
			if (pos != 0) 
				$("#btG", jEl).css('background-position', '-64px').css('cursor', 'pointer');
			else 
				$("#btG", jEl).css('background-position', '-96px').css('cursor', 'default');
			$("#btD", jEl).css('background-position', pos + param.nbImgVisible <= posMax ? '-64px' : '-32px');
		}
	};
})(jQuery)
