/*
 * 	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 jQuery("#cc-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(jQuery) {

    jQuery.fn.easySlider = function(options) {

        // default configuration properties
        var defaults = {
            prevId: 'prevBtn',
            prevText: 'Previous',
            nextId: 'nextBtn',
            nextText: 'Next',
            controlsShow: true,
            controlsBefore: '',
            controlsAfter: '',
            controlsFade: true,
            hoverFade: true,   // KB: Added
            speed: 800,
            auto: false,
            pause: 2000
        };

        var options = jQuery.extend(defaults, options);

        this.each(function() {
            var obj = jQuery(this);
            var s = jQuery("li", obj).length;
            var w = jQuery("li", obj).width();
            var h = jQuery("li", obj).height();
            obj.width(w);
            obj.height(h);
            obj.css("overflow", "hidden");
            var ts = s - 1;
            var t = 0;
            jQuery("ul", obj).css('width', (s + 2) * w);
            jQuery("li", obj).css('float', 'left');
            jQuery("ul", obj).append(jQuery("ul li:first", obj).clone());

            if (options.controlsShow) {
                var html = options.controlsBefore;
                html += ' <span id="' + options.prevId + '"><a href=\"javascript:void(0);\">' + options.prevText + '</a></span>';
                html += ' <span id="' + options.nextId + '"><a href=\"javascript:void(0);\">' + options.nextText + '</a></span>';
                html += options.controlsAfter;
                jQuery(obj).append(html);
            };

            jQuery("a", "#" + options.nextId).click(function() {
                animate("next", true);
            });
            jQuery("a", "#" + options.prevId).click(function() {
                animate("prev", true);
            });

            jQuery("a", "#" + options.nextId).fadeOut(1);
            jQuery("a", "#" + options.prevId).fadeOut(1);

            if (options.hoverFade) {
                jQuery(obj).hoverIntent({
                    sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
                    interval: 10, // number = milliseconds for onMouseOver polling interval    
                    timeout: 100, // number = milliseconds delay before onMouseOut
                    over: function() {
                        if (jQuery.browser.msie) {
                            jQuery("a", "#" + options.nextId).css('display', 'block');
                            jQuery("a", "#" + options.prevId).css('display', 'block');
                        } else {
                            jQuery("a", "#" + options.nextId).fadeIn(300);
                            jQuery("a", "#" + options.prevId).fadeIn(300);
                        }
                    },
                    out: function() {
                        if (jQuery.browser.msie) {
                            jQuery("a", "#" + options.nextId).css('display', 'none');
                            jQuery("a", "#" + options.prevId).css('display', 'none');
                        } else {
                            jQuery("a", "#" + options.nextId).fadeOut(300);
                            jQuery("a", "#" + options.prevId).fadeOut(300);
                        } 
                    }
                });
            }

            function animate(dir, clicked) {
                if (animateInProgress) return;
                animateInProgress = true;

                var ot = t;
                switch (dir) {
                    case "next":
                        t = (ot >= ts) ? 0 : t + 1;
                        var diff = Math.abs(ot - t);
                        var speed = diff * options.speed;
                        p = (t * w * -1);
                        if (p == 0) {
                            jQuery("ul", obj).animate({ marginLeft: (ot + 1) * -1 * w }, options.speed, function() {
                                jQuery("ul", obj).css({ marginLeft: p });
                                animateInProgress = false;
                            });
                        } else {
                            jQuery("ul", obj).animate({ marginLeft: p }, speed, function() { animateInProgress = false; });
                        }
                        break;
                    case "prev":
                        t = (t <= 0) ? ts : t - 1;

                        var diff = Math.abs(ot - t);
                        var speed = diff * options.speed;
                        p = (t * w * -1);
                        if (t == ts) {
                            jQuery("ul", obj).css({ marginLeft: (t + 1) * -1 * w }).animate({ marginLeft: (t) * -1 * w }, options.speed,
	        				    function() { animateInProgress = false; }
	    	        	    );
                        } else {
                            jQuery("ul", obj).animate({ marginLeft: p }, speed, function() {
                                animateInProgress = false;
                            });

                        }
                        break;

                    default:
                        break;
                };


                if (clicked) clearTimeout(timeout);

                if (options.auto && dir == "next" && !clicked) {
                    timeout = setTimeout(function() {
                        animate("next", false);
                    }, diff * options.speed + options.pause);
                };

            };


            // init
            var animateInProgress = false;
            var timeout;
            if (options.auto) {
                timeout = setTimeout(function() {
                    animate("next", false);
                }, options.pause);
            };

        });

    };

})(jQuery);



