$(document).ready(function(){

    var spd = 500;

    // Scroll to top
    (function scrollToTop() {
        $.localScroll({
            duration: spd,
            hash: true
        });
    })();

    // Homepage Slideshow Functionality
    (function heroSlideshow() {

        $.getJSON("http://pipes.yahoo.com/pipes/pipe.run?_id=oKDO0So83hGW1NSL_w6H4A&_render=json&_callback=?", function(data) {

            var text = "", numDone = 0, maxItems = 3, items = [],
                rTwitter = /^http:\/\/twitter\.com/,
                rDelicious = /^http:\/\/delicious\.com/,
                rJournal = /^http:\/\/iamdanielmarino\.com/,
                bs = / (\S+)$/;

            try { items = data.value.items; } catch (e) {}

            $.each(items, function (index, i) {

                var regTest = i["y:id"].value;

                if(regTest.match(rTwitter)) {
                  var d = / [a-zA-Z]* [0-9]* /;
                  text += '<div class="slide tweet"><div><blockquote>' + i.description.replace(bs, '&nbsp;$1') + '</blockquote><p>via <a href="' + i.link + '">@starzonmyarmz</a></p></div></div>';
                };

                if(regTest.match(rDelicious)) {
                  text += '<div class="slide bookmark"><div><h2>' + i.title.replace(bs, '&nbsp;$1') + '</h2><p>' + i.description.replace(bs, '&nbsp;$1') + '</p><p><a href="' + i.link + '">Read more&hellip;</a></p></div></div>';
                };

// Hide for now...
//                if(regTest.match(rJournal)) {
//                  text += '<div class="slide journal"><h2>' + i.title.replace(bs, '&nbsp;$1') + '</h2></div>';
//                };

            });

            $('#hero')
                .html(text)
                .children()
	          .hide()
                  .fadeIn(spd)
                  .each(function (el) {
                    var e = $(this).children(),
                        h = e.height(),
                        n = (400 - h) / 2;
                    e.css({ top: n + "px" });                		
                })
                .parent()
                .after('<div id="pager"></div>')
                .cycle({
                    fx:     'scrollLeft',
                    speed:  spd,
                    timeout: 5000,
                    pager:  '#pager'
            });

        });

    })();

    // Portfolio Slideshow functionality
    (function slideshow() {

        var arr = $.makeArray(document.getElementsByTagName("article"));

        $.each(arr, function() {

            var slideshowImages       = "#" + this.id + " .slideshow-images",
                slideshowControls     = "#" + this.id + " .slideshow-controls",
                slideshowControlsHTML = '<div class="slideshow-controls"><a href="#" class="prev">Previous</a><a href="#" class="next">Next</a></div>';

            if ($(slideshowImages).children().length > 1) {

                $(slideshowImages).after(slideshowControlsHTML);
                $(slideshowControls).css({ opacity: 0 });

                $(slideshowControls).hover(
                    function() {
                        $(this).fadeTo(spd, 1.0);
                    },
                    function() {
                        $(this).fadeTo(spd, 0.0);
                    }
                );

                $(slideshowImages).cycle({
                    fx:     'fade',
                    speed:   spd,
                    timeout: 0,
                    next:   '#' + $(this).attr("id") + ' .next',
                    prev:   '#' + $(this).attr("id") + ' .prev'
                });

            }

        });

    })();

});