//--- PLACE

(function($) { $.fn.place = function () {
	var elt = $(this);
	// marginRight explicit case of Webkit CSS rendering
	var eltMarginRight = 0;
	if ($.browser.webkit && elt.css('display') == 'block' && elt.css('float') == 'none' && elt.css('position') == 'static'){
		elt.css('display', 'inline-block');
		eltMarginRight = getNumber(elt.css('marginRight'));
	// note : if was FF < 3 then
	//	elt.css('display', 'block');
	}
	// marginRight in all cases (and browsers)
	else { eltMarginRight = getNumber(elt.css('marginRight')); }
	// width + paddings + margins
	var eltWidth = getNumber(elt.outerWidth()) + getNumber(elt.css('marginLeft')) + eltMarginRight;
	// height + paddings + margins
	var eltHeight = getNumber(elt.outerHeight()) + getNumber(elt.css('marginTop')) + getNumber(elt.css('marginBottom'));
	// return both
	return { width: eltWidth, height: eltHeight };
}; })(jQuery);

function getNumber(nb) {
	nb = parseInt(nb);
	if (!isNaN(parseFloat(nb)) && isFinite(nb)){ return nb; }
	else { return 0; }
}

//--- SLIDER

(function($) { 
	$.fn.jslider = function(o)
	{
		o = $.extend({
			speed: 1500,
			continuous: false,
			auto: 0,
			btns: true,
			prevTitle: 'Précédent',
			nextTitle: 'Suivant'
		}, o);

		$(this).each(function(){
			var way = -1;
			var slider = $(this);
			var maskW = $('.mask', slider).css('overflow','hidden').width();
			var list = $('.list', slider);
			var items = $('.case', list);

			// fonction de calcul de taille reelle d'un elt
			var eltSizeReturn = function(elt){
				var eltSize = elt.place().width;
				return eltSize;
			};
			var itemW = eltSizeReturn(items.eq(0));

			var listW = 0;
			items.css('float', 'left').each(function(){ listW += eltSizeReturn($(this)); });
			list.width(listW);

			if (listW > maskW) {

				var slideFunc = function(){
					if (!slider.hasClass('slidering')){
						slider.addClass('slidering');

						if (((o.auto > 0) || (o.continuous == true)) && (way > 0)){
							items = $('.case', list);
							list.prepend(items.last());							
							list.css('marginLeft', parseInt(list.css('marginLeft'))- itemW);
						}

						var listL = parseInt(list.css('marginLeft')) + itemW*way;

						if (o.btns && (o.auto <= 0) && (o.continuous == false)){
							var btnHide = function(btn){ btn.animate({'opacity':0}, 250, function(){ btn.css('visibility','hidden'); }); };
							var btnShow = function(btn){ btn.css('visibility','visible'); btn.animate({'opacity':1}, 250); };
							listL < 0 ? btnShow(prev) : btnHide(prev);
							listL > (-listW + maskW) ? btnShow(next): btnHide(next);
						}

						list.stop().animate({'marginLeft':listL}, o.speed, function(){

							if (((o.auto > 0) || (o.continuous == true)) && (way < 0)){
								list.append(items.eq(0));
								list.css('marginLeft', 0);
								items = $('.case', list);
							}

							slider.removeClass('slidering');
						});
						way = -1;
					}
					return false;
				};

				if (o.btns){
					var prev = $('.prev', slider);
					if (prev.length < 1){
						prev = $(document.createElement('a')).addClass('btn prev').attr('title', o.prevTitle);
						var prevIcon = prev.append($(document.createElement('span')).addClass('icon'));
						slider.append(prev);
					}
					if ((o.auto <= 0) && (o.continuous == false)){ prev.css('visibility','hidden'); }
					prev.click(function(){ if (!slider.hasClass('slidering')){ way = 1; slideFunc();} });

					var next = $('.next', slider);
					if (next.length < 1){
						next = $(document.createElement('a')).addClass('btn next').attr('title', o.nextTitle);
						var nextIcon = next.append($(document.createElement('span')).addClass('icon'));
						slider.append(next);
					}
					next.css('visibility','visible').click(function(){ if (!slider.hasClass('slidering')){ way = -1; slideFunc();} });
				}
				else { $('.btn', slider).hide(); }

				if (o.auto > 0){
					var auto = false;
					var autoFunc = function(){
							clearInterval(auto);
							auto = setInterval(slideFunc, o.auto *1000);
					};
					items.hover(
						function(){ clearInterval(auto); },
						function(){ autoFunc(); }
					);
					autoFunc();
				}
			}
		});
	};
})(jQuery);
