﻿/* ellipsis plugin: http://devongovett.wordpress.com/2009/04/06/text-overflow-ellipsis-for-firefox-via-jquery/ */
(function($) {
    $.fn.ellipsis = function(enableUpdating) {
        var s = document.documentElement.style;
        if (!('textOverflow' in s || 'OTextOverflow' in s)) {
            return this.each(function() {
                var el = $(this);
                if (el.css("overflow") == "hidden") {
                    var originalText = el.html();
                    var w = el.width();

                    var t = $(this.cloneNode(true)).hide().css({
                        'position': 'absolute',
                        'width': 'auto',
                        'overflow': 'visible',
                        'max-width': 'inherit'
                    });
                    el.after(t);

                    var text = originalText;
                    while (text.length > 0 && t.width() > el.width()) {
                        text = text.substr(0, text.length - 1);
                        t.html(text + "...");
                    }
                    el.html(t.html());

                    t.remove();

                    if (enableUpdating == true) {
                        var oldW = el.width();
                        setInterval(function() {
                            if (el.width() != oldW) {
                                oldW = el.width();
                                el.html(originalText);
                                el.ellipsis();
                            }
                        }, 200);
                    }
                }
            });
        } else return this;
    };
})(jQuery);

$(function() {
    // scrollify, returning a ref to its api
    var scrollableApi = $("div.scrollable")
            .scrollable({
                size: 1
                , speed: 700
                , clickable: false
            })
            .circular()
            .autoscroll({
                autoplay: false
                , interval: 4000
                , api: true
            });
    // bind prev/next manually to match auto scroll speed
    $("div.scrollable a.leftarrow").hide().click(function(e) { scrollableApi.prev(); e.preventDefault(); });
    $("div.scrollable a.rightarrow").hide().click(function(e) { scrollableApi.next(); e.preventDefault(); })
    $(window).load(function() {
        if ($("div.scrollable ul.slider li").length > 1) {
            // fadeIn buttons then play
            $("div.scrollable a.leftarrow, div.scrollable a.rightarrow").fadeIn("slow", function() { scrollableApi.play(); });
        } else {
            // show buttons and play
            $("div.scrollable a.leftarrow, div.scrollable a.rightarrow").show();
            scrollableApi.play();
        }
    });
    // ellipsify:
    $("div.name").ellipsis();
});