$(function(){
var M={};

//plugins
(function($){
    $.linkify = function(txt){
        var regex1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,//URLs starting with http://, https://, or ftp://
            regex2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;//URLs starting with www. (without // before it, or it'd re-link the ones done above)   
        txt = txt.replace(regex1, '<a href="$1" target="_blank">$1</a>');
        txt = txt.replace(regex2, '$1<a href="http://$2" target="_blank">$2</a>');
        return txt;
    }
    
    $.niceTime = function(timestamp){
        var msPerMinute = 60 * 1000,
            msPerHour   = msPerMinute * 60,
            msPerDay    = msPerHour * 24,
            msPerMonth  = msPerDay * 30,
            msPerYear   = msPerDay * 365,
            timestamp   = timestamp.replace('+0000',''),
            now         = new Date(),
            then        = new Date(timestamp),
            elapsed     = now - then;
        
        if(elapsed < msPerMinute){
             return Math.round(elapsed/1000) + ' seconds ago';   
        }else if(elapsed < msPerHour){
             return Math.round(elapsed/msPerMinute) + ' minutes ago';   
        }else if(elapsed < msPerDay){
             return Math.round(elapsed/msPerHour ) + ' hours ago';   
        }else if(elapsed < msPerMonth){
             return Math.round(elapsed/msPerDay ) + ' days ago';   
        }else if(elapsed < msPerYear){
             return Math.round(elapsed/msPerMonth ) + ' months ago';   
        }else{
             return Math.round(elapsed/msPerYear ) + ' years ago';   
        }
    }
})(jQuery);


//$('.servicesBtns a').click(function(){return false;})


M.tweetStream = (function(){
    var pub={};

    var buildTweetBox = function(){
        var twitterName = 'newdigitalbiz',
            numTweets   = '4',
            url         = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' + twitterName + '&count=' + numTweets + '&callback=?',
            container   = $('div.tweets ul');
            
        $.getJSON(url,function(r){
            var tweets=r;
            $.each(tweets, function(i,tweet){
                var txt     = tweet.text,
                    date    = $.niceTime(tweet.created_at),
                    tweetID = tweet.id_str,
                    perma   = "http://twitter.com/#!/" + twitterName + "/status/" + tweetID,
                    html    = "<li><a href='" + perma + "' target='_blank'>&ldquo;" + txt + "&rdquo; <span>- " + date + "</span></a></li>";
                $(html).appendTo(container);
            });
            var items = container.find('li'),i=0;
            setTimeout(function(){
                //container.slideDown();
                (function(){$(items[i++]).slideDown(260,arguments.callee)})();//iteration effect    
            },1200) 
        });
    }

pub.init = function(){
    buildTweetBox();
}
return pub; 
})();




M.ourWork = (function(){
    var pub             = {},
        timeout         = 3200,
        speed           = 800,
        quotesWrap      = $('div.quotes'),
        quotesWrap_W    = quotesWrap.width(),
        quotesSlider    = quotesWrap.find('ul'),
        quotes          = quotesSlider.find('li'),
        numQuotes       = quotes.length,
        i               = 1,
        quote_W;

    var setupQuotes = function(){
        quotes.eq(0).clone().appendTo(quotesSlider);//MAKE COPY OF FIRST SLIDE AND INSERT IN LAST POSITION
        quotes = quotesSlider.find('li').css({width:quotesWrap_W, visibility:'visible'});
        quote_W = quotesSlider.width()/(numQuotes+1);
        setTimeout(cycleQuotes,2000);
    }
    
    var cycleQuotes = function(){
        quotesSlider.animate({left:'-='+quote_W},speed,function(){
            if(i==numQuotes){
                quotesSlider.css({left:0});
                i=1;    
            }else{
                i++;    
            }
            setTimeout(cycleQuotes,timeout);
        }); 
    }
pub.init = function(){
    setupQuotes();
}
return pub;     
})();


M.ourPeople = (function(){
    var pub             = {},
        facepics        = $('div.facepics li'),
        bios            = $('ul.bios li'),
        fadeInSpeed     = 600,
        fadeOutSpeed    = 120,
        hoverTimout;
    /**********************/

    var hoverOver = function(){
        var facepic = $(this),
            index   = facepics.index(facepic),
            bio     = bios.eq(index);
                    
        clearTimeout(hoverTimout);
        hoverTimout = setTimeout(function(){
            if(bio.is(':hidden')){//if this bio is hidden
                if(bios.is(':visible')){//if there is a visible bio
                    bios.filter(':visible').fadeOut(fadeOutSpeed,function(){//fadeout visible bio
                        bio.fadeIn(fadeInSpeed);//fade in new bio
                    });
                }else{//if no bios are visible
                    bio.fadeIn(fadeInSpeed);//just fade in this bio straight away
                }
            }
        },200)
    }
    //===========================================//
    var hoverOut = function(){} 
    /**********************/
    pub.init = function(){
        facepics.hover(hoverOver, hoverOut);
    }
return pub;     
})();



M.entryTasks = (function(){
    var pub={};
    /************************/
    var showNews = function(){
        var setNum      = 4,
            newsWrap    = $('div.news'),
            items       = newsWrap.find('div.announcement-list').slice(0, setNum),
            i           = 0;
        //items.show();
        (function(){$(items[i++]).fadeIn(260,arguments.callee)})();//iteration effect
    }
    
    var hideBlogIntros = function(){
        var ourIndustry = $('div#ourIndustry'),
            blogIntros  = ourIndustry.find('div.blogIntro').slice(1);
        blogIntros.hide();
    }
/************************/
pub.init = function(){
    showNews();
    hideBlogIntros();
}
return pub; 
})();



var init = (function(){
    M.tweetStream.init();
    M.entryTasks.init();
    M.ourWork.init();
    M.ourPeople.init(); 
})();
})
