/*
 * Original: http://www.robpoyntz.com/blog/?p=223
 * Modified to pause slider when mouse is over the slide
 */

(function ($) {
    $.fn.fadeTransition = function (options) {
        var options = $.extend({
            pauseTime: 5000,
            transitionTime: 2000,
            ignore: null,
            delayStart: 0,
            pauseNavigation: false
        }, options);
        var transitionObject;

        Trans = function (obj) {
            var timer = null;
            var current = 0;
            var els = (options.ignore) ? $("> *:not(" + options.ignore + ")", obj) : $("> *", obj);
            $(obj).css("position", "relative");
            els.css({"display": "none", "left": "0", "top": "0", "position": "absolute"});

            $(".slider1Expand").click(function() {
                if (document.location.pathname != "/")  // any page but the homepage
                {
                    $(".slider1Expand").blur().hide();
                    $("#slider1").animate({height: "220px"}, 500, function() {
                        $("#slider1 div span").fadeIn(200);
                    });
                }
            });

            els.each(function(i) {
                $(els[i]).mouseenter(function(e) {
                    options.pauseNavigation = true;
                    if (timer) clearTimeout(timer);
                    /*if (document.location.pathname != "/")  // any page but the homepage
                    {
                        $(".slider1Expand").hide();
                        $("#slider1").animate({height: "220px"}, 500, function() {
                            $("#slider1 div span").fadeIn(200);
                        });
                    }*/
                });
                $(els[i]).mouseleave(function(e) {
                    options.pauseNavigation = false;
                    cue();
                    if (document.location.pathname != "/")  // any page but the homepage
                    {
                        // This should be fine, but for some reason it won't slide
                        // in anymore the second time on mouse enter, if we use this
                        /*
                        $("#slider1 div span").fadeOut(200, function() {
                            $("#slider1").animate({height: "110px"}, 500);
                        });
                        */
                        $("#slider1 div span").hide();
                        $("#slider1").animate({height: "110px"}, 500, function() {
                            $(".slider1Expand").show();
                        });
                    }
                });
            });

            if (options.delayStart > 0) {
                setTimeout(showFirst, options.delayStart);
            }
            else showFirst();

            function showFirst()
            {
                if (options.ignore)
                {
                    $(options.ignore, obj).fadeOut(options.transitionTime);
                    $(els[current]).fadeIn(options.transitionTime);
                }
                else
                {
                    $(els[current]).css("display", "block");
                }
            }

            function transition(next)
            {
                if (!options.pauseNavigation)
                {
                    $(els[current]).fadeOut(options.transitionTime);
                    $(els[next]).fadeIn(options.transitionTime);
                    current = next;
                    cue();
                }
            };

            function cue()
            {
                if ($("> *", obj).length < 2) return false;
                if (timer) clearTimeout(timer);
                if (!options.pauseNavigation)
                {
                    timer = setTimeout(function() {
                        transition((current + 1) % els.length | 0)
                    }, options.pauseTime);
                }
            };

            this.showItem = function(item)
            {
                if (timer) clearTimeout(timer);
                transition(item);
            };

            cue();
        }

        this.showItem = function(item)
        {
            transitionObject.showItem(item);
        };

        return this.each(function()
        {
            transitionObject = new Trans(this);
        });
    }

})(jQuery);

