function getMyWidth() 
{
	var myWidth = 0;
 
	if ( typeof( window.innerWidth ) == 'number' ) 
	{ 
		// Non-IE
		myWidth = window.innerWidth;
	} 
	else if ( document.documentElement && document.documentElement.clientWidth ) 
	{
		// IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} 
	else if ( document.body && document.body.clientWidth ) 
	{
		// IE 4 compatible
		myWidth = document.body.clientWidth;
	}
 
	return myWidth; 
}

function ExternalWrite(s)
{
	if ( s )
	{
		document.writeln( s );
	}
}

	
function popup_window( url, id, width, height )
{
	var popup = window.open(url, id, 'toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=no,width=' + width + ',height=' + height + ',left=100,top=100' );
	popup.focus();
}

function isAppleMobile()
{
	return navigator.userAgent.toLowerCase().indexOf('iphone') != -1 || navigator.userAgent.toLowerCase().indexOf('ipod') != -1;
}


Date.prototype.setISO8601 = function(dString)
{
   var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;  

   if (dString.toString().match(new RegExp(regexp)))
   {
      var d = dString.match(new RegExp(regexp));  
      var offset = 0;  

      this.setUTCDate(1);  
      this.setUTCFullYear(parseInt(d[1],10));  
      this.setUTCMonth(parseInt(d[3],10) - 1);  
      this.setUTCDate(parseInt(d[5],10));  
      this.setUTCHours(parseInt(d[7],10));  
      this.setUTCMinutes(parseInt(d[9],10));  
      this.setUTCSeconds(parseInt(d[11],10));  
      if (d[12])  
         this.setUTCMilliseconds(parseFloat(d[12]) * 1000);  
      else  
         this.setUTCMilliseconds(0);  
      if (d[13] != 'Z')
      {
         offset = (d[15] * 60) + parseInt(d[17],10);  
         offset *= ((d[14] == '-') ? -1 : 1);  
         this.setTime(this.getTime() - offset * 60 * 1000);  
      }
   }  
   else
   {
      this.setTime(Date.parse(dString));  
   }  

   return this;  
};

/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time)
{
   var date = new Date();  
   date.setISO8601(time);

   var diff = (new Date().getTime() - date.getTime()) / 1000;
   var day_diff = Math.floor(diff / 86400);		
	
   if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
   return;
			
   return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

function makePrettyDates()
{
   var flag = false;

   var links = document.getElementsByTagName("abbr");	
   for (var i = 0; i < links.length; i++)
   {
      if (links[i].title)
      {
         var date = prettyDate(links[i].title);
         if (date)
         {
            links[i].innerHTML = date;
            flag = true;
         }
      }
   }

   return flag;
}

function isIE6()
{
   if (Prototype.Browser.IE)
   {
      var ua = navigator.userAgent;
      var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null)
      {
         return 6.0 == parseFloat( RegExp.$1 );
      }
   }

   return false;
}

function doDisqusComments()
{
   var links = document.getElementsByTagName('a');
   var query = '?';
   for (var i = 0; i < links.length; i++)
   {
      if (links[i].href.indexOf('#disqus_thread') >= 0)
      {
         query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
      }
   }

   if (query != '?')
   {
      var headID = document.getElementsByTagName("head")[0];         
      var newScript = document.createElement('script');
      newScript.type = 'text/javascript';
      newScript.src = 'http://disqus.com/forums/sydneyfuller/get_num_replies.js' + query;
      headID.appendChild(newScript);
   }
}

document.observe('dom:loaded', function()
{
   if (!isIE6())
   {
      if (makePrettyDates())
      {
         setInterval("makePrettyDates()", 60000);
      }
   }

   doDisqusComments();
});
