/*
 * 	Easy Slider 1.0 - jQuery plugin
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#Slider").slider();
 *	
 * 	<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 ($) {

    $.fn.slider = function (options) {

        // default configuration properties
        var defaults = {
            controlsShow: true,
            controlsBefore: '<span class="navigation">',
            controlsAfter: '</span>',
            controlsFade: true,
            vertical: false,
            speed: 1000,
            auto: false,
            pause: 5000,
            continuous: false
        };

        var options = $.extend(defaults, options);

        this.each(function () {
            var obj = $(this);
            var length = $("li", obj).length;
            var w = $("li", obj).width();
            var className = $("li", obj).attr("class");
            var h;
            if (className == 'globalBenefit') {
                h = $("li", obj).height() + 100;
            }
            else {
                h = $("li", obj).height();
            }
            var timeout;
            //Add random time to pause
            var pause = options.pause + (Math.random() * 1000);
            var speed = options.speed;
            obj.width(w);
            obj.height(h);
            obj.css("overflow", "hidden");
            var ts = length - 1;
            var currentIndex = 1;
            $("ul", obj).css('width', length * w);
            if (!options.vertical) $("li", obj).css('float', 'left');

            if (options.controlsShow) {
                var html = options.controlsBefore;

                for (i = 0; i < length; i++) {
                    html += ' <span class="' + i + '"><a href=\"javascript:void(0);\">&nbsp;</a></span>';
                }
                html += options.controlsAfter;
                $(obj).append(html);

                obj.find(".navigation span").first().addClass('selectedSlide');
            };
            obj.find('.navigation span').each(function (index) {
                $(this).click(function () {
                    var currentId = $(this).attr('class');
                    slide(currentId, true);
                });
            });

            obj.mouseenter(function () {
                clearTimeout(timeout);
            });
            obj.mouseleave(function () {
                init();
            });

            //Init the slides
            init();
            function slide(index, clicked) {
                if (!options.vertical) {
                    p = (index * w * -1);
                    $("ul", obj).animate(
						{ marginLeft: p },
						speed
					);
                } else {
                    p = (index * h * -1);
                    $("ul", obj).animate(
						{ marginTop: p },
						speed
					);
                };

                obj.find('.navigation span').removeClass('selectedSlide');
                obj.find('.' + index).addClass('selectedSlide');

                if (clicked) {
                    clearTimeout(timeout);
                }

                init();
            };

            function next() {
                if (currentIndex == length) {
                    currentIndex = 0;
                    return currentIndex++;
                }
                else if (currentIndex < length) {
                    return currentIndex++;
                }
                currentIndex = 0;
                return 0;
            };
            // init
            function init() {
                if (options.auto) {
                    ;
                    timeout = setTimeout(function () {
                        slide(next(), false);
                    }, pause);
                };
            };
        });
    };
})(jQuery);




