(function($) {

    $.fn.vehicleSlider = function(options) {

        // default configuration properties
        var defaults = {
            prevId: 'prevBtn',
            prevText: 'Previous',
            nextId: 'nextBtn',
            nextText: 'Next',
            controlsShow: true,
            controlsBefore: '',
            controlsAfter: '',
            controlsFade: true,
            firstId: 'firstBtn',
            firstText: 'First',
            firstShow: false,
            lastId: 'lastBtn',
            lastText: 'Last',
            lastShow: false,
            vertical: false,
            speed: 800,
            auto: false,
            pause: 2000,
            continuous: false,
            numeric: false,
            numericId: 'controls',
            nbitems: 0
        };

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

        this.each(function() {
            var position = 0;
            var obj = $(this);
            var s = $("li", obj).length;
            var w = $("li", obj).width();
            var h = $("li", obj).height();
            var clickable = true;
            obj.width(w);
            obj.height(h);
            obj.css("overflow", "hidden");
            var ts = s - 1;
            var t = 0;
            $("ul", obj).css('width', s * w);

            if (opt.continuous) {
                $("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left", "-" + w + "px"));
                $("ul", obj).append($("ul li:nth-child(2)", obj).clone());
                $("ul", obj).css('width', (s + 1) * w);
            };

            if (!opt.vertical) $("li", obj).css('float', 'left');

            //Render previous/next controls and numeric list
            if (opt.controlsShow) {
                var html = opt.controlsBefore;
                if (opt.numeric) {
                    html += '<ol id="' + opt.numericId + '"></ol>';
                }
                if (opt.firstShow) html += '<span id="' + opt.firstId + '"><a href=\"javascript:void(0);\">' + opt.firstText + '</a></span>';
                html += ' <span id="' + opt.prevId + '"><a href=\"javascript:void(0);\">' + opt.prevText + '</a></span>';
                html += ' <span id="' + opt.nextId + '"><a href=\"javascript:void(0);\">' + opt.nextText + '</a></span>';
                if (opt.lastShow) html += ' <span id="' + opt.lastId + '"><a href=\"javascript:void(0);\">' + opt.lastText + '</a></span>';

                html += opt.controlsAfter;
                $(obj).after(html);
            };

            if (opt.numeric) {
                for (var i = 0; i < s; i++) {
                    $(document.createElement("li"))
                    .attr('id', opt.numericId + (i + 1))
                    .html('<a rel=' + i + ' href=\"javascript:void(0);\">' + (i + 1) + '</a>')
                    .appendTo($("#" + opt.numericId))
                    .click(function() {
                        var pos = parseInt($("a", $(this)).attr('rel'));
                        position = pos;
                        animate(pos, true);
                    });
                };
            }

            //Add Events
            $("a", "#" + opt.nextId).click(function() {
                //animate($("#" + opt.numericId + next).children('a').attr('rel'), true);

                if (clickable) 
                {
                    position = position == parseInt(opt.nbitems) ? 0 : (position + 1);
                    animate("next", true);
                }

                //alert(opt.nbitems+" - "+position);
                //alert(position);
                Search("", 0, position);
                setURLAnchor("Search");
                SetBreadCrumb(position);
                RefreshHighlights(false);

                AllBodySelected = false;
            });

            $("a", "#" + opt.prevId).click(function()
            {
                //animate($("#" + opt.numericId + prev).children('a').attr('rel'), true);
                if (clickable)
                {
                    animate("prev", true);
                    position = position == 0 ? parseInt(opt.nbitems) : position - 1; 
                }
                //alert(position);
                Search("", 0, position);
                setURLAnchor("Search");
                SetBreadCrumb(position);
                RefreshHighlights(false);

                AllBodySelected = false;
            });

            //============== CREATION DOUBLE CLIC FAKE =============//
            $("a", "#" + opt.nextId).dblclick(function() {
                if (clickable)
                {
                    position = position == parseInt(opt.nbitems) ? 0 : (position + 1);
                    animate("next", true);
                }
            });

            $("a", "#" + opt.prevId).dblclick(function() {
                if (clickable)
                {
                    animate("prev", true);
                    position = position == 0 ? parseInt(opt.nbitems) : position - 1;
                }
            });

            //======================================================//

            $("a", "#" + opt.firstId).click(function()
            {
                animate("first", true);
            });

            $("a", "#" + opt.lastId).click(function()
            {
                animate("last", true);
            });           

            function setCurrent(i)
            {
                i = parseInt(i) + 1;
                $("li", "#" + opt.numericId).removeClass("current");
                $("li#" + opt.numericId + i).addClass("current");
            };

            function adjust()
            {
                if (t > ts) t = 0;
                if (t < 0) t = ts;
                if (!opt.vertical)
                {
                    $("ul", obj).css("margin-left", (t * w * -1));
                }
                else
                {
                    $("ul", obj).css("margin-left", (t * h * -1));
                }
                clickable = true;
                if (opt.numeric) setCurrent(t);
                updateStatus("#icon-body-item" + (position));
            };

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

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

                    if (!opt.continuous && opt.controlsFade) {
                        if (t == ts) {
                            $("a", "#" + opt.nextId).hide();
                            $("a", "#" + opt.lastId).hide();
                        } else {
                            $("a", "#" + opt.nextId).show();
                            $("a", "#" + opt.lastId).show();
                        };
                        if (t == 0) {
                            $("a", "#" + opt.prevId).hide();
                            $("a", "#" + opt.firstId).hide();
                        } else {
                            $("a", "#" + opt.prevId).show();
                            $("a", "#" + opt.firstId).show();
                        };
                    };

                    if (clicked) clearTimeout(timeout);

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

                };


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

            if (opt.numeric) setCurrent(0);

            if (!opt.continuous && opt.controlsFade) {
                $("a", "#" + opt.prevId).hide();
                $("a", "#" + opt.firstId).hide();
            };

        });
        return this;
    };

})(jQuery);

