/*

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

	Version : 1.1
	homepage : http://code.google.com/p/basicslider/
	Copyright (c) 2010 Jonathan Thuau
	Licence MIT - http://www.opensource.org/licenses/mit-license.php

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

*/

(function($){
    $.fn.BasicSlider = function(options) {
        var defaults = {
			vitesse : 'normal',
			nbSlide : 1,
			auto : false,
			delai : 1000,
			mode : 'horizontal',
			navigation : true,
			diaporama : false,
			titre : false
        }
        var options = $.extend(defaults, options);
		var $$ = $(this);

		return $.fn.BasicSlider.run($$, options);
    };

	$.fn.BasicSlider.run = function($$, options) {
		$('.javascript_css', $$).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 $precedent = $('.precedent > a', $$);
		var $suivant = $('.suivant > a', $$);
		var $SlideAffich = ul.width()/li.width();
		var liHeight = $(li[0], $$).height();
		var liWidth = $(li[0], $$).width();
		var timer = Array();

		$suivant.css('display', 'block');
		$precedent.css('display', 'block');

		function AutoNext() {
			$suivant.click();
		}

		if (options.navigation == false || (options.navigation == false && li.size() <= $SlideAffich)){
			$precedent.css('display', 'none');
			$suivant.css('display', 'none');
		}

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

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

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

		if (li.size() > $SlideAffich) {
			if (options.mode == 'vertical'){
				ul.css('height', (li.length*liHeight));
				$suivant.live('click', function(){
					if (options.diaporama == true && options.titre == true){
						$('.titlebar', $$).remove();
					}
					if (options.auto == true){
						timer[lastTimer] = setTimeout(AutoNext, options.delai);
					}
					if (parseInt(ul.css('top'))){
						ultop = parseInt(ul.css('top'));
					}else{
						ultop = 0;
					}
					offsetTop = ultop - (liHeight * options.nbSlide);
					if (!ul.is(':animated')) {
						if (offsetTop + ul.height() > 0) {
							ul.animate({top: offsetTop}, options.vitesse);
							pos++;
						}else{
							ul.fadeOut().animate({top: 0}, 100).fadeIn();
							pos = 0;
						}
					}
					return false;
				});
				$precedent.live('click', function(){
					if (options.diaporama == true && options.titre == true){
						$('.titlebar', $$).remove();
					}
					offsetBottom = parseInt(ul.css('top')) + (liHeight * options.nbSlide);
					if (!ul.is(':animated')) {
						if (offsetBottom + ul.height() <= ul.height()) {
							ul.animate({top: offsetBottom}, options.vitesse);
							pos--;
						}else if((ul.height()/liHeight)%2 != 0){
							ul.fadeOut().animate({left: -(li.length * liHeight - liHeight)}, 100).fadeIn();
							pos = ul.width()/liWidth-1;
						}else{
							ul.fadeOut().animate({top: -(li.length * liHeight - liHeight*options.nbSlide)}, 100).fadeIn();
							pos = 0;
						}
					}
					return false;
				});
			}else{
				ul.css('width', (li.length*liWidth));
				$suivant.live('click', function(){
					if (options.diaporama == true && options.titre == true){
						$('.titlebar', $$).remove();
					}
					if (options.auto == true){
						timer[lastTimer] = setTimeout(AutoNext, options.delai);
					}
					if (parseInt(ul.css('left'))){
						ulleft = parseInt(ul.css('left'));
					}else{
						ulleft = 0;
					}
					offsetLeft = ulleft - (liWidth * options.nbSlide);
					if (!ul.is(':animated')) {
						if (offsetLeft + ul.width() > 0) {
							ul.animate({left: offsetLeft}, options.vitesse);
							pos++;
						}else{
							ul.fadeOut().animate({left: 0}, 100).fadeIn();
							pos = 0;
						}
					}
					return false;
				});
				$precedent.live('click', function(){
					if (options.diaporama == true && options.titre == true){
						$('.titlebar', $$).remove();
					}
					offsetRight = parseInt(ul.css('left')) + (liWidth * options.nbSlide);
					if (!ul.is(':animated')) {
						if (offsetRight + ul.width() <= ul.width()) {
							ul.animate({left: offsetRight}, options.vitesse);
							pos--;
						}else if((ul.width()/liWidth)%2 != 0){
							ul.fadeOut().animate({left: -(li.length * liWidth - liWidth)}, 100).fadeIn();
							pos = ul.width()/liWidth-1;
						}else{
							ul.fadeOut().animate({left: -(li.length * liWidth - liWidth*options.nbSlide)}, 100).fadeIn();
							pos = 0;
						}
					}
					return false;
				});
			}
		}else{
			if (options.navigation == true){
				$precedent.find('img').unwrap().css('opacity', '0.5');
				$suivant.find('img').unwrap().css('opacity', '0.5');
			}
		}
	};
})(jQuery);
