jQuery.noConflict();
(function($){
  $(function(){
    var location;
    var $tweets = $('.twitter');
    if ($tweets.size() > 0)
    {
      var utils = { 
        getTimestamp : function(time)
        {
          //--------------------------------------------------------------
          // http://af-design.com/blog/2009/02/10/twitter-like-timestamps/
          //--------------------------------------------------------------
          var system_date = new Date(time.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/, "$1 $2 $4 $3 UTC"));
          var user_date = new Date();
          var diff = Math.floor((user_date - system_date) / 1000);
          if (diff <= 1) return "just now";
          if (diff < 20) return diff + " seconds ago";
          if (diff < 40) return "half a minute ago";
          if (diff < 60) return "less than a minute ago";
          if (diff <= 90) return "one minute ago";
          if (diff <= 3540) return Math.round(diff / 60) + " minutes ago";
          if (diff <= 5400) return "1 hour ago";
          if (diff <= 86400) return Math.round(diff / 3600) + " hours ago";
          if (diff <= 129600) return "1 day ago";
          if (diff < 604800) return Math.round(diff / 86400) + " days ago";
          if (diff <= 777600) return "1 week ago";
          return "on " + time;
        },
        linkify: function(text) {
          text = text.replace(/(https?:\/\/\S+)/gi, function (s) {
              return '<a href="' + s + '">' + s + '</a>';
          });
  
          text = text.replace(/(^|)@(\w+)/gi, function (s) {
              return '<a href="http://twitter.com/' + s + '">' + s + '</a>';
          });
  
          text = text.replace(/(^|)#(\w+)/gi, function (s) {
              return '<a href="http://search.twitter.com/search?q=' + s.replace(/#/,'%23') + '">' + s + '</a>';
           });
          return text;
        }
      };
      
      $.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=nutritionistPEI&callback=?&include_rts=true&count=5&callback=?', function(result) { 
        var html = [], tweet;
        for(var i=0;i<result.length;i++){
          tweet = result[i];
          html.push('<div class="tweet">');
          html.push('<p>'+utils.linkify(tweet.text)+'</p>');
          html.push(' <div class="links">');
          html.push('   <ul>');
          html.push('     <li><a href="http://twitter.com/nutritionistPEI/status/',tweet.id,'">',utils.getTimestamp(tweet.created_at),'</a></li>');
          html.push('     <li><a href="http://twitter.com/intent/reply?tweet_id=',tweet.id,'">reply</a></li>');
          html.push('     <li><a href="http://twitter.com/intent/retweet?tweet_id=',tweet.id,'">retweet</a></li>');
          html.push('     <li><a href="http://twitter.com/intent/favorite?tweet_id=',tweet.id,'">favorite</a></li>');
          html.push('   </ul>');
          html.push(' </div>');
          html.push('</div>');
        }
  
        $tweets.append(html.join(''));
        $tweets.append('<div class="banner clearfix"><div class="left"><a href="http://twitter.com"><img src="/themes/positivechange/images/twitter-logo.jpg" alt="Twitter" /></a></div><div class="right"><a href="http://twitter.com">Join the conversation</a></div></div>');
      });
    }
	
    function extractPageName(hrefString)
    {
      var arr = hrefString.split('/');
      return  (arr.length < 2) ? hrefString : arr[arr.length-2].toLowerCase() + arr[arr.length-1].toLowerCase();
    }
     
    function setActiveMenu(arr, crtPage)
    {
      for (var i=0; i < arr.length; i++)
      {
        if(extractPageName(arr[i].href) == crtPage)
        {          
          if (arr[i].parentNode.tagName != "DIV")
          {
            arr[i].className = "selected";
            arr[i].parentNode.className = "selected";
          }
        }
      }
    }
     
    function setPage()
    {
      hrefString = document.location.href ? document.location.href : document.location;
     
      if (document.getElementById("nav") !=null )
        setActiveMenu(document.getElementById("nav").getElementsByTagName("a"), extractPageName(hrefString));
    }
    setPage();
    
    $('.tabs').tabs();
  });
})(jQuery);
