// JavaScript Document
jQuery(document).ready(function($){
    
	//Tabbed box setup with equal heights functionality                          
    // Set to a different number to start on a different tab.
    var currentTab = 0; 
    // Set height of tabbed box to be the height of the longest tab box
    // then change tab content based on tab clicked
    jQuery(document).ready(function($) {
        $(".tabbed-content").equalHeights();
        $(".tabbed-box .tabs li a").click(function() { 
            openTab($(this)); return false; 
        });
        $(".tabbed-box .tabs li a:eq("+currentTab+")").click()
    });
    
// Animate Social Bookmarks
    $('#social_section a').animate({
        opacity:.5
    });
    $('#social_section a').hover(function(){
        $(this).stop().animate({ 
            opacity:1,
            top:"-10px"
        }, 250);
    }, function() {
        $(this).stop().animate({ 
            opacity:.5,
            top:'0px'
        });                
    });
    
    //Get last tweet from users
    $('#twitter div').each(function() {
        var handle = $(this).attr('id');
        tweet(handle);
    });
    $.getJSON('http://search.twitter.com/search.json?ref=travisnow&rpp=3&callback=?', function(data){
        $.each(data.results, function(index, item){
            $('#twitter .mentions').append('<li class="tweet"><img src="' + item.profile_image_url + '" alt="avatar" /><div class="tweet_data"><p>' + item.text + '</p><span class="twitter_meta">From <a href="http://twitter.com/'+item.from_user+'" target="_blank" title="reply to author">@'+item.from_user+'</a></span></div></li>');
            });
        });
    
});


// Function to convert plain text url's into hypertext use item.text.linkify()
/*String.prototype.linkify = function() {
    return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
        return m.link(m);
    });
};*/


// Function to pull in twitter feed
function tweet(username) {
    jQuery.getJSON('http://twitter.com/statuses/user_timeline/'+username+'.json?count=3&callback=?', function(data){
        jQuery.each(data, function(index, item){
            jQuery('#'+username+'').append('<li class="tweet"><img src="' + item.user.profile_image_url + '" alt="avatar" /><div class="tweet_data"><p>' + item.text + '</p><span class="twitter_meta">' + relative_time(item.created_at) + ' from  <a href="http://twitter.com/'+username+'" target="_blank" title="reply to author">@'+username+'</a></span></div></li>');
        });
    });
}

// Function to parse a twitter time string into more readable text
function relative_time(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);
    var r = '';
    if (delta < 60) {
        r = 'a minute ago';
    } else if(delta < 120) {
        r = 'couple of minutes ago';
    } else if(delta < (45*60)) {
            r = (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if(delta < (90*60)) {
        r = 'an hour ago';
    } else if(delta < (24*60*60)) {
        r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if(delta < (48*60*60)) {
        r = '1 day ago';
    } else {
        r = (parseInt(delta / 86400)).toString() + ' days ago';
    }
    return r;
};
/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 * 
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com) 
 *
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 * 
 * Example 1: $(".cols").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".cols").equalHeights(400); Sets all cols to at least 400px tall.
 * Example 3: $(".cols").equalHeights(100,300); Cols are at least 100 but no more
 * than 300 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 */
 
    // Fuction to switch tabs
function openTab(clickedTab) {
    var thisTab = jQuery(".tabbed-box .tabs a").index(clickedTab);
    jQuery(".tabbed-box .tabs li a").removeClass("active");
    jQuery(".tabbed-box .tabs li a:eq("+thisTab+")").addClass("active");
    jQuery(".tabbed-box .tabbed-content").hide();
    jQuery(".tabbed-box .tabbed-content:eq("+thisTab+")").show();
    currentTab = thisTab;
}
(function($) {
    $.fn.equalHeights = function(minHeight, maxHeight) {
        tallest = (minHeight) ? minHeight : 0;
        this.each(function() {
            if($(this).height() > tallest) {
                tallest = $(this).height();
            }
        });
        if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
        return this.each(function() {
            $(this).height(tallest).css("overflow","auto");
        });
    }
})(jQuery);