/*

	----------------------------jQuery BasicSlider-----------------------------

	Version : 2.6
	Date : 20/04/2010
	Requires jQuery 1.4.x
	Homepage : http://jonathan-thuau.fr/basicslider/
	Project : http://code.google.com/p/basicslider/
	Copyright (c) 2010 Jonathan Thuau (jonathan.thuau@laposte.net)
	License MIT - http://www.opensource.org/licenses/mit-license.php

	---------------------------------------------------------------------------

*/

(function($){
	$.fn.BasicSlider = function(settings){
		var defaults ={
			speed : 'normal',
			nbslide : 1,
			auto : false,
			autodelay : 1000,
			mode : 'horizontal',
			navigation : true,
			showtitle : false,
			fixtitle : false,
			pagination : false,
			limit : false,
			limitdisplay : false,
			fadereturn : true
		};
		var settings = $.extend(defaults, settings);
		var $$ = $(this);
		return this.each(function(){
			$.fn.BasicSlider.run($$, settings);
		});
	};

	$.fn.BasicSlider.run = function($$, settings){
		$('.javascript_css', $$).css('display', 'none');
		$('.pagination-no').css('display', 'none');

		var ul = $('ul', $$);
		ul.css('display', 'block');
		var li = ul.children();
		var pos = 0;
		var active = ul.parent('.slider');
		var navigation = $('.navigation > a', $$);
		var prev = $('.prev > a', $$);
		var next = $('.next > a', $$);
		var displayed = ul.width()/li.width();
		var liHeight = $(li[0], $$).height();
		var liWidth = $(li[0], $$).width();
		var total = li.size();
		var timer = Array();

		next.css('display', 'block');
		if (settings.limit == false){
			prev.css('display', 'block');
		}else if (settings.limitdisplay == true){
			prev.find('img').unwrap().css('opacity', '0.5');
		}

		function AutoNext(){
			next.click();
		}

		function FixTitle(){
			if ($(li[pos], active).find('img').attr('title') != ''){
				titlebar = $('<div class="titlebar">'+$(li[pos], $$).find('img').attr('title')+'</div>');
				titlebar.css('display', 'none');
				if ($('.titlebar', $$)[0]){
					$('.titlebar', $$).css('opacity', '0.6').fadeIn();
				}else{
					titlebar.prependTo(active).css('opacity', '0.6').fadeIn();
				}
			}
		}

		if (settings.navigation == false || (settings.navigation == false && total <= displayed)){
			prev.css('display', 'none');
			next.css('display', 'none');
		}

		if (settings.auto == true){
			idTimer = timer.length;
			lastTimer = idTimer+1;
			timer[lastTimer] = setTimeout(AutoNext, settings.autodelay);
			$$.live('mouseover',function(){
				clearTimeout(timer[lastTimer]);
			});
			$$.live('mouseleave',function(){
				timer[lastTimer] = setTimeout(AutoNext, settings.autodelay);
			});
		}

		if (settings.mode == 'horizontal' && settings.pagination == true && settings.showtitle == false){
			$('.pagination > a', $$).live('click',function(){
				new_pos = $(this).attr('rel');
				$(this).addClass('on');
				if (settings.auto == true){
					clearTimeout(timer[lastTimer]);
					timer[lastTimer] = setTimeout(AutoNext, settings.autodelay);
				}
				offsetLeft = -(liWidth * new_pos);
				if (!ul.is(':animated')){
					if (offsetLeft + ul.width() > 0){
						ul.fadeOut().animate({left: offsetLeft}, settings.speed).fadeIn();
						$('.pagination > a#slide-'+pos, $$).removeAttr('class').addClass('off');
						pos = new_pos;
						$('.pagination > a#slide-'+pos, $$).removeAttr('class').addClass('on');
					}
				}
				return false;
			});
			pages = '';
			for (p=0;p<total;p++){
				if (pos != p){
					pclass = 'off';
				}else{
					pclass = 'on';
				}
					pages += '<a href="#" class="'+pclass+'" id="slide-'+p+'" rel="'+p+'" onclick="return false;">'+(p+1)+'</a>';
			}
			pagination = $('<div class="pagination">'+pages+'</div>');
			pagination.prependTo(active);
		}

		if (settings.fixtitle == true){
			FixTitle();
		}

		if (settings.showtitle == true){
			$$.live('mouseover',function(){
				if ($(li[pos], active).find('img').attr('title') != ''){
					titlebar = $('<div class="titlebar">'+$(li[pos], $$).find('img').attr('title')+'</div>');
					titlebar.css('display', 'none');
					if ($('.titlebar', $$)[0]){
						$('.titlebar', $$).css('opacity', '0.6').fadeIn();
					}else{
						titlebar.prependTo(active).css('opacity', '0.6').fadeIn();
					}
				}
			});

			$$.live('mouseleave',function(){
				if ($(li[pos], $$).find('img').attr('title') != ''){
					$('.titlebar', $$).fadeOut();
				}
			});
		}

		if (total > displayed){
			if (settings.mode == 'vertical'){
				ul.css('height', (li.length*liHeight));
				next.live('click', function(){
					if (settings.showtitle == true || settings.fixtitle == true){
						$('.titlebar', $$).remove();
					}
					if (settings.auto == true){
						clearTimeout(timer[lastTimer]);
						timer[lastTimer] = setTimeout(AutoNext, settings.autodelay);
					}
					if (parseInt(ul.css('top'))){
						ultop = parseInt(ul.css('top'));
					}else{
						ultop = 0;
					}
					$('.pagination > a#slide-'+pos, $$).removeAttr('class').addClass('off');
					offsetTop = ultop - (liHeight * settings.nbslide);
					if (!ul.is(':animated')){
						if (offsetTop + ul.height() > 0){
							ul.animate({top: offsetTop}, settings.speed);
							pos++;
						}else{
							if (settings.fadereturn == true){
								ul.fadeOut().animate({top: 0}, 100).fadeIn();
							}else{
								ul.animate({top: 0}, 300);
							}
							pos = 0;
						}
						if (settings.fixtitle == true){
							FixTitle();
						}
					}
					if (settings.auto == true){
						clearTimeout(timer[lastTimer]);
						timer[lastTimer] = setTimeout(AutoNext, settings.autodelay);
					}
					$('.pagination > a#slide-'+pos, $$).removeAttr('class').addClass('on');
					/*if (settings.limit == true){
						if (pos == (total-1)){
							next.hide();
						}else{
							next.show();
						}
						if (pos == 0){
							prev.hide();
						}else{
							prev.show();
						}
					}*/
					if (settings.limit == true){
						if (pos >= (total/(displayed-1))){
							if (settings.limitdisplay == true){
								$('.next > a', $$).find('img').unwrap().css('opacity', '0.5');
								$('.prev > img', $$).wrap('<a href="#" title="Précédent"></a>').css('opacity', '1');
								$('.prev > a', $$).css('display','inline');
							}else{
								next.hide();
							}
						}else{
							next.show();
						}
						if (pos == 0){
							if (settings.limitdisplay == true){
								$('.prev > img', $$).unwrap().css('opacity', '0.5');
							}else{
								prev.hide();
							}
						}else{
							if (settings.limitdisplay == true){
								$('.prev > img', $$).wrap('<a href="#" title="Précédent"></a>').css('opacity', '1');
								$('.prev > a', $$).css('display','inline');
							}else{
								prev.show();
							}
						}
					}
					return false;
				});
				prev.live('click', function(){
					if (settings.showtitle == true || settings.fixtitle == true){
						$('.titlebar', $$).remove();
					}
					$('.pagination > a#slide-'+pos, $$).removeAttr('class').addClass('off');
					offsetBottom = parseInt(ul.css('top')) + (liHeight * settings.nbslide);
					if (!ul.is(':animated')){
						if (offsetBottom + ul.height() <= ul.height()){
							ul.animate({top: offsetBottom}, settings.speed);
							pos--;
						}else if((ul.height()/liHeight)%2 != 0){
							if (settings.fadereturn == true){
								ul.fadeOut().animate({left: -(li.length * liHeight - liHeight)}, 100).fadeIn();
							}else{
								ul.animate({left: -(li.length * liHeight - liHeight)}, 300);
							}
							pos = ul.width()/liWidth-1;
						}else{
							if (settings.fadereturn == true){
								ul.fadeOut().animate({top: -(li.length * liHeight - liHeight*settings.nbslide)}, 100).fadeIn();
							}else{
								ul.animate({top: -(li.length * liHeight - liHeight*settings.nbslide)}, 300);
							}
							pos = 0;
						}
						if (settings.fixtitle == true){
							FixTitle();
						}
					}
					$('.pagination > a#slide-'+pos, $$).removeAttr('class').addClass('on');
					/*if (settings.limit == true){
						if (pos == 0){
							prev.hide();
						}else{
							prev.show();
							next.show();
						}
					}*/
					if (settings.limit == true){
						if (pos == 0){
							if (settings.limitdisplay == true){
								$('.prev > a', $$).find('img').unwrap().css('opacity', '0.5');
							}else{
								prev.hide();
							}
							next.show();
						}else{
							prev.show();
							if (settings.limitdisplay == true){
								$('.next > img', $$).wrap('<a href="#" title="Suivant"></a>').css('opacity', '1');
								$('.next > a', $$).css('display','inline');
							}else{
								next.show();
							}
						}
					}
					return false;
				});
			}else{
				ul.css('width', (li.length*liWidth));
				next.live('click', function(){
					if (settings.showtitle == true || settings.fixtitle == true){
						$('.titlebar', $$).remove();
					}
					if (settings.auto == true){
						clearTimeout(timer[lastTimer]);
						timer[lastTimer] = setTimeout(AutoNext, settings.autodelay);
					}
					if (parseInt(ul.css('left'))){
						ulleft = parseInt(ul.css('left'));
					}else{
						ulleft = 0;
					}
					$('.pagination > a#slide-'+pos, $$).removeAttr('class').addClass('off');
					offsetLeft = ulleft - (liWidth * settings.nbslide);
					if (!ul.is(':animated')){
						if (offsetLeft + ul.width() > 0){
							ul.animate({left: offsetLeft}, settings.speed);
							pos = -(offsetLeft/(liWidth * settings.nbslide));
						}else{
							if (settings.fadereturn == true){
								ul.fadeOut().animate({left: 0}, 100).fadeIn();
							}else{
								ul.animate({left: 0}, 300);
							}
							pos = 0;
						}
						if (settings.fixtitle == true){
							FixTitle();
						}
					}
					if (settings.auto == true){
						clearTimeout(timer[lastTimer]);
						timer[lastTimer] = setTimeout(AutoNext, settings.autodelay);
					}
					$('.pagination > a#slide-'+pos, $$).removeAttr('class').addClass('on');
					if (settings.limit == true){
						if (pos >= (total/displayed)){
							if (settings.limitdisplay == true){
								$('.next > a', $$).find('img').unwrap().css('opacity', '0.5');
								$('.prev > img', $$).wrap('<a href="#" title="Précédent"></a>').css('opacity', '1');
								$('.prev > a', $$).css('display','inline');
							}else{
								next.hide();
							}
						}else{
							next.show();
						}
						if (pos == 0){
							if (settings.limitdisplay == true){
								$('.prev > img', $$).unwrap().css('opacity', '0.5');
							}else{
								prev.hide();
							}
						}else{
							if (settings.limitdisplay == true){
								$('.prev > img', $$).wrap('<a href="#" title="Précédent"></a>').css('opacity', '1');
								$('.prev > a', $$).css('display','inline');
							}else{
								prev.show();
							}
						}
					}
					return false;
				});
				prev.live('click', function(){
					if (settings.showtitle == true || settings.fixtitle == true){
						$('.titlebar', $$).remove();
					}
					$('.pagination > a#slide-'+pos, $$).removeAttr('class').addClass('off');
					offsetRight = parseInt(ul.css('left')) + (liWidth * settings.nbslide);
					if (!ul.is(':animated')){
						if (offsetRight + ul.width() <= ul.width()){
							ul.animate({left: offsetRight}, settings.speed);
							pos = -(offsetRight/(liWidth * settings.nbslide));
						}else if((ul.width()/liWidth)%2 != 0){
							if (settings.fadereturn == true){
								ul.fadeOut().animate({left: -(li.length * liWidth - liWidth)}, 100).fadeIn();
							}else{
								ul.animate({left: -(li.length * liWidth - liWidth)}, 300);
							}
							pos = ul.width()/liWidth-1;
						}else{
							if (settings.fadereturn == true){
								ul.fadeOut().animate({left: -(li.length * liWidth - liWidth*settings.nbslide)}, 100).fadeIn();
							}else{
								ul.animate({left: -(li.length * liWidth - liWidth*settings.nbslide)}, 300);
							}
							pos = 0;
						}
						if (settings.fixtitle == true){
							FixTitle();
						}
					}
					$('.pagination > a#slide-'+pos, $$).removeAttr('class').addClass('on');
					if (settings.limit == true){
						if (pos == 0){
							if (settings.limitdisplay == true){
								$('.prev > a', $$).find('img').unwrap().css('opacity', '0.5');
							}else{
								prev.hide();
							}
							next.show();
						}else{
							prev.show();
							if (settings.limitdisplay == true){
								$('.next > img', $$).wrap('<a href="#" title="Suivant"></a>').css('opacity', '1');
								$('.next > a', $$).css('display','inline');
							}else{
								next.show();
							}
						}
					}
					return false;
				});
			}
		}else{
			if (settings.navigation == true){
				prev.find('img').unwrap().css('opacity', '0.5');
				next.find('img').unwrap().css('opacity', '0.5');
			}
		}
	};
})(jQuery);
