/*
 * 	Easy Slider 1.5 - jQuery plugin
 *	written by Alen Grakalic
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

/*
 *	markup example for $("#slider").easySlider();
 *
 * 	<div id="slider">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

    var isBadIE = parseFloat(navigator.appVersion.split("MSIE")[1]) < 8;

	$.fn.easySlider = function(options){

		// default configuration properties
		var defaults = {
            controls: {
                show:   true,
                before: '',
                after:  '',
                fade:   '',
                prev: {
                    className: 'prev',
                    text:      'Précédent',
                    title:     'Précédent'
                },
                next: {
                    className: 'next',
                    text:      'Suivant',
                    title:     'Suivant'
                },
                first: false,
                last:  false
            },
			vertical:   false,
			speed:      800,
			auto:       false,
			pause:      2000,
			continuous: true
		};

		var options = $.extend(true, defaults, options);

		this.each(function() {
			var obj = $(this);
			var items = $("li", obj);

			var w = 0;
			var h = 0;
			var s = items.length;

			items.each(function () {
                var $this = $(this);
                var width = $this.width();
                var height = $this.height();
                if (width > w) {
                    w = width;
                }
                if (height > h) {
                    h = height;
                }
			});
			items.each(function () {
                var $this = $(this);
                var height = $this.height();
                if (height < h) {
                    $this.css({
                        marginTop:
                            parseInt($this.css("marginTop"))
                            + parseInt((h - height)/2)
                            + 'px'
                    });
                }
			});

			obj.width(w);
			obj.height(h);
			obj.css("overflow", "hidden");
			var ts = s-1;
			var t = 0;
			items.parent().css('width', s*w);
			if (! options.vertical) {
                items.css({"float" : "left"})
            };

			if (options.controls && options.controls.show) {
                function createControl(type, options) {
                    if (!options || (typeof options.show != "undefined" && !options.show)) {
                        return false;
                    }
                    var control = document.createElement("span");
                    control.innerHTML = options.text || '';
                    if (typeof options.id != "undefined" && options.id) {
                        control.id = options.id;
                    }
                    if (typeof options.className != "undefined" && options.className) {
                        control.className = options.className;
                    }
                    control = $(control);
                    control.click(function () {animate(type, true)});
                    return control;
                }

                if (options.controls.before) {
                    obj.append(options.controls.before);
                }
                if (options.controls.first && options.controls.first.show) {
                    var firstControl = createControl("first", options.controls.first);
                    obj.append(firstControl);
                }
                var prevControl = createControl("prev", options.controls.prev);
                obj.append(prevControl);
                var nextControl = createControl("next", options.controls.next);
                obj.append(nextControl);
                if (options.controls.last && options.controls.last.show) {
                    var lastControl = createControl("last", options.controls.last);
                    obj.append(lastControl);
                }
                if (options.controls.after) {
                    obj.append(options.controls.after);
                }
			};

			function animate(dir,clicked){
				var ot = t;
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;
						break;
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						break;
					case "first":
						t = 0;
						break;
					case "last":
						t = ts;
						break;
					default:
						break;
				};

				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;
				if(!options.vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p },
						speed
					);
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p },
						speed
					);
				};

				if(!options.continuous && options.controls.fade){
					if(t==ts){
                        nextControl.hide();
                        if (typeof lastControl != "undefined") {
                            lastControl.hide();
                        }
					} else {
                        nextControl.show();
                        if (typeof lastControl != "undefined") {
                            lastControl.show();
                        }
					};
					if(t==0){
                        prevControl.hide();
                        if (typeof firstControl != "undefined") {
                            firstControl.hide();
                        }
					} else {
                        prevControl.show();
                        if (typeof firstControl != "undefined") {
                            firstControl.show();
                        }
					};
				};

				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};

			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};

			if(!options.continuous && options.controls.fade){
                prevControl.hide();
                if (typeof firstControl != "undefined") {
                    firstControl.hide();
                }
			};

			if (isBadIE) {
                setTimeout(function () {obj.css({zoom: 1})}, 1);
			}
		});

	};

})(jQuery);

