// jQuery onLoad
$(function () {
    var loadTwitterWidget = function (twtrwidget) {
        if ("undefined" == typeof TWTR) {
            return;
        }

        new TWTR.Widget({
            id: twtrwidget,
            version: 2,
            type: "profile",
            rpp: 6,
            interval: 6000,
            width: 180,
            height: 310,
            theme: {
                shell: { background: "#fff", color: "#ffffff" },
                tweets: { background: "#eeeeee", color: "#333333", links: "#579579" }
            },
            features: { scrollbar: true,
                loop: true,
                live: true,
                hashtags: true,
                timestamp: true,
                avatars: false,
                behavior: "default"
            }
        }).render().setUser("tbshouston").start();
    };

    // Turn on caching for AJAX requests, needing for getScript
    $.ajaxSetup({
        cache: true
    });

    var $twtrWidget = $("#twtr-widget");
    if (0 < $twtrWidget.length) {
        $.getScript("http://widgets.twimg.com/j/2/widget.js", function () {
            $twtrWidget.each(function () {
                if (this.id)
                    loadTwitterWidget(this.id);
            });
        });
    }

    $(".hover-fade").hover(function (event) { $(this).stop(true, true).fadeTo("slow", 0.6); }, function (event) { $(this).stop(true, true).fadeTo("slow", 1.0); });

    $(".email-me").each(function () {
        var $this = $(this);
        $this.attr("href", "mailto:" + $this.attr("rel") + "@" + "texasbusinesssystems.com");
    });

    (function () {
        if (null != document.referrer && 0 == document.referrer.indexOf(document.location.protocol + "//" + document.location.host))
            return;

        var $slogan = $("q#slogan");
        var left = $slogan.position().left;
        $slogan.addClass("nofx").animate({ left: left + "px" }, 1500);
    })();

    if (0 < $(".validate").length) {
        $("label.required").append(" <em>*</em>");

        var validate = function () {
            if ($.fn.validate) {
                $("form").validate();
            }
        };

        var getLocal = function () {
            $.getScript("/s/js/jquery.validate.min.js", validate);
        };

        try {
            var url = "http://ajax.microsoft.com/ajax/jquery.validate/1.6/jquery.validate.min.js";
            $.ajax({ type: "GET", contentType: "text/javascript, application/javascript; charset=utf-8", url: url, success: function () { $.getScript(url, validate); }, error: getLocal, dataType: "text", cache: true });
        }
        catch (ex) {
            getLocal();
        }
    }

    function DelayLoadImage() {
    }

    // Delay Load Large Image...
    setTimeout(function () { DelayLoadImage(); }, 500);

    var $scroll = $("#scroll");
    if (0 < $scroll.length) {
        // initialize scrollable and return the programming API
        var api = $scroll.scrollable({
            items: '#scrollables',
            size: 1,
            clickable: false,
            loop: true
            // use the navigator plugin
        }).navigator({ api: true });

        api.onStart(function (e, i) {
            // when on the first item: hide the intro
            if (i) {
                $("#intro").fadeOut("slow");
            } else {
                $("#intro").fadeIn(1000);
            }

            // toggle activity for the intro thumbnail
            $("#t0").toggleClass("active", 0 == i);
        });

        // a dedicated click event for the intro thumbnail
        $("#t0").click(function () {
            // seek to the beginning (the hidden first item)
            $("#scroll").scrollable().begin();
        });

        $(".t").click(function () {
            api.stop();
        });

        setTimeout(function () { $("#scroll").autoscroll(9000); }, 18000);
    }
});
