/**
 * Romain Huet <http://romainhuet.com/>
 */

var romainhuet = {};

/**
 * Display a slide
 * @param {String} The slide ID to display
 */
romainhuet.showSlide = function(slide) {
    // Animate the slide to display it
    jQuery('.slide:not("#' + slide + '")').addClass('hidden');
    jQuery('#' + slide).removeClass('hidden');
    // Update the navigation
    jQuery('nav li a').removeClass('selected');
    jQuery('nav li a[href="#' + slide + '"]').addClass('selected');
};

/**
 * Display a relative time from a date time string
 * @param time
 * @return {String} The relative time
 */
romainhuet.relativeTime = function(time) {
    var parsedDate = Date.parse(time);
    var relativeTo = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relativeTo.getTime() - parsedDate) / 1000, 10);

    if (delta < 60) {
        return 'less than a minute ago';
    } else if (delta < 120) {
        return 'about a minute ago';
    } else if (delta < (45*60)) {
        return (parseInt(delta / 60, 10)).toString() + ' minutes ago';
    } else if (delta < (90*60)) {
        return 'about an hour ago';
    } else if (delta < (24*60*60)) {
       return 'about ' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
    } else if (delta < (48*60*60)) {
       return '1 day ago';
    } else {
       return (parseInt(delta / 86400, 10)).toString() + ' days ago';
    }
};

/**
 * Callback for the Twitter API
 * @param tweet The latest tweets in JSON
 */
romainhuet.twitter = function(tweets) {
    for (var i = 0; i < Math.min(tweets.length, 5); i++) {
        var words = tweets[i].text.split(' ');
        for (var j = 0; j < words.length; j++) {
            if (words[j].indexOf('@') == 0) {
                // Parsing of @username links
                words[j] = '@<a href="http://twitter.com/' + words[j].substr(1, words[j].length) + '">' + words[j].substr(1, words[j].length) + '</a>';
            } else if (words[j].indexOf('www.') != -1 && words[j].indexOf('http://') == -1 && words[j].indexOf('https://') == -1) {
                // Parsing of www.example.com links
                words[j] = '<a href="http://' + words[j] + '">' + words[j] + '</a>';
            } else if (words[j].indexOf("http://") != -1 || words[j].indexOf("https://") != -1) {
                // Parsing of http://www.example.com/ links
                words[j] = '<a href="' + words[j] + '">' + words[j] + '</a>';
            }
            words[j] = words[j].replace(/<a href="(.*?)[\.\!\?\:]">(.*?)([\.\!\?\:])<\/a>/g, '<a href="$1">$2</a>$3');
        }
        var tweet = words.join(' ');
        var date = tweets[i].created_at;
        jQuery('#tweets ul').append(jQuery('<li>' + tweet + ' <a href="http://twitter.com/romainhuet/status/' + tweets[i].id_str + '" class="date">' + romainhuet.relativeTime(date) + '</a></li>'));
    }
};

jQuery(function() {
    // Bind navigation clicks
    jQuery('nav li a').click(function(event) {
        romainhuet.showSlide(event.target.href.split('#')[1]);
        return false;
    });
    // Bind the header logo
    jQuery('header a').click(function(event) {
        romainhuet.showSlide('about');
        return false;
    });
});

