
if (typeof vividvisions == "undefined") 
   var vividvisions = {};

vividvisions.addresses = {
   "theOffice": "-6F-66-66-69-63-65-40-76-69-76-69-64-76-69-73-69-6F-6E-73-2E-63-6F-6D"
}

vividvisions.twitterCache = window.name;

vividvisions.relativeTime = function (date) {
   var now = new Date();
   var diff = (now.getTime() - date.getTime()) / 1000;

   if (isNaN(diff)) return "Unknown";   
	else if (diff < 60) return "Just now";
	else if (diff < 120) return "One minute ago";
	else if (diff < 3600) return Math.floor(diff / 60) + " minutes ago";
	else if (diff < 7200) return "One hour ago";
	else if (diff < 86400) return Math.floor(diff / 3600) + " hours ago";
	else if (diff < 172800) return "One day ago";
	else return Math.floor(diff / 86400) + " days ago"; 
}

$(function() {
   
/*
   if (/Safari/.test(navigator.userAgent) == true) {
      var search = document.getElementById('searchstring');
      search.setAttribute("type", "search");
      search.setAttribute("results", "5");
      search.setAttribute("autosave", "com.vividvisions.search");
   }
*/
   $('#menu').css('-moz-border-radius-topleft', '10px').css('-moz-border-radius-topright', '10px').css('-webkit-border-top-left-radius', '10px').css('-webkit-border-top-right-radius', '10px');
   
   
   $('a[href*="#mailto"]').each(function() {
      $this = $(this);
      var to = $this.attr('href').split(":")[1];
      $this.attr('href', 'mailto:' + unescape(vividvisions.addresses[to].replace(/\-/g,"%")));
      $this.empty();
      $this.append(unescape(vividvisions.addresses[to].replace(/\-/g,"%")));
   });
   
   $('#projects ul').css('width', ($('#projects ul li').length * 230)  + 'px');
   
   $('.comment-author span').each(function() {
      if ($(this).children('a').length > 0) {
         if ($(this).children('a').text().length > 23) $(this).children('a').text($(this).children('a').text().substr(0,23) + '...');
      } else {
         if ($(this).text().length > 23) $(this).text($(this).text().substr(0,23) + '...');
      }
   });

   $("#commentform").submit( function () { 
   	if (fb_connect_user) {
   		var profile = $('#fb-user .FB_ElementReady .FB_Link')[1]['href'];
   		var userId = profile.substring(profile.indexOf('?id=')+4);
   		var name = $('#fb-user #fb-title a').text();

         $('#author').val(name);
   		$('#email').val(userId + '@facebook.com'); 
   		$('#url').val(profile);
   	}
   });   
   
   var fetchTwitterStatus = true;
   window.name = vividvisions.twitterCache;
	
   if (window.name && window.name.indexOf('ts,') == 0) {
   	var lastUpdate, cachedContent, tweetDate;
   	
   	window.name.replace(/^ts,(\d{1,}),(\d{1,}),(.*)$/, function (match, time1, time2, content, offset, fullStr) {
   	   lastUpdate = Number(time1);
   	   tweetDate = Number(time2);
   	   cachedContent = content;
   	});
   	
   	if (((new Date()).getTime() - lastUpdate) < 15 * 60 * 1000) {
   		$('#twitter-status').html(cachedContent).find('.twitter-timestamp').html(vividvisions.relativeTime(new Date(tweetDate)));
   		fetchTwitterStatus = false;
   	}
   }
   
   if (fetchTwitterStatus) {
      $.getJSON(
         'http://twitter.com/statuses/user_timeline/vividvisions.json?count=1&callback=?', 
   	   function (data, textStatus) {
   	      var content = data[0].text.replace(/(https?:\/\/[^\s]*)/g, '<a href="$1">$1</a>').
                                       replace(/@([^\s]*)/g,'@<a href="http://twitter.com/$1">$1</a>');

            var tweetDate;
            data[0].created_at.replace(/^\w{3} (\w{3}) (\d{2}) (\d{2}):(\d{2}):(\d{2}) \+0000 (\d{4}).*$/, function(match, month, day, hours, minutes, seconds, year, offset, fullStr) {
               tweetDate = new Date(month + " " + day + ", " + year + " " + hours + ":" + minutes + ":" + seconds);
            });

            var now = new Date();
            var timezone = now.getTimezoneOffset() / 60 * -1;
   	      tweetDate = new Date(tweetDate.getTime() + 3600000 * timezone);
   			var relativeTime = vividvisions.relativeTime(tweetDate);

   	      var html = '<blockquote cite="http://twitter.com/vividvisions/statuses/' + data[0].id + '"><p>' + content + '</p></blockquote><p><cite><a href="http://twitter.com/vividvisions/statuses/' + data[0].id + '" class="twitter-link" title="Link to this Twitter status">This status</a></cite> <span class="twitter-timestamp">' + relativeTime + '</span></p>';
            $('#twitter-status').html(html);	

            window.name = 'ts,' + (new Date()).getTime() + ',' + tweetDate.getTime() + "," + html;
   	   }
      );
   }
   

});

jQuery.fn.vv_fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback); 
};

var fb_connect_user = false;

function updateFacebookComment() {
   if (!$('#fb-user').length) {
		$('#comment-user-details').hide().after("<div id='fb-user'>" +
		"<fb:profile-pic uid='loggedinuser' facebook-logo='true' height='50'></fb:profile-pic>" +
		"<span id='fb-msg'><span id='fb-title'>Hello <fb:name uid='loggedinuser' useyou='false'></fb:name>!</span><br />" +
		"You are logged in with your Facebook account. " +
		"<a href='#' onclick='FB.Connect.logoutAndRedirect(\"" + location.href + "\"); return false;'>Logout</a>." +
		"</span></div>");
	}

   FB.XFBML.Host.parseDomTree();
   fb_connect_user = true;
}




