var DEBUG = false;
// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time) {	
	var date = new Date((time || "").replace(/-/g,"/")),
	diff = (((new Date()).getTime() - date.getTime()) / 1000),
	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 < 14 && "1 week ago" || 
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
	jQuery.fn.prettyDate = function(){
		return this.each(function(){
			var date = prettyDate(this.title);
			if ( date )
				jQuery(this).text( date );
		});
	};


function processTwitterMessageLink(message) {
	// replace http://
	if (message != null) {
		var start = message.indexOf("http://");
		if (start != -1) {
			var end = message.indexOf(" ", start);
			if (end == -1) end = message.length;
			var link = message.substring(start,end);
			message = message.replace(link, "<a href=\"" + link + "\" target=\"_blank\">" + link + "</a>");
		}
								
		// replace #
		start = message.indexOf("#");
		if (start != -1) {
			var end = message.indexOf(" ", start);
			if (end == -1) end = message.length;
			var tag = message.substring(start,end);
			message = message.replace(tag, "<a href=\"http://twitter.com/#!/search?q=%23" + tag.substring(1) + "\" target=\"_blank\">" + tag + "</a>");
		}

		// replace @
		start = message.indexOf("@");
		if (start != -1) {
			var end = message.indexOf(" ", start);
			if (end == -1) end = message.length;
			var mention = message.substring(start,end);
			message = message.replace(mention, "<a href=\"http://twitter.com/#!/" + mention.substring(1) + "\" target=\"_blank\">" + mention + "</a>");
		}
	}
	return message;
}

						
/*function callJSTwitterUser() {
	var b = document.createElement('script'); 
	b.language = "javascript";
	b.type ='text/javascript';
	b.async = !0;
	b.src = "http://api.twitter.com/1/users/show.json?callback=writeUserData&screen_name=orangehillsgmbh&include_entities=true";
	document.getElementsByTagName("head")[0].appendChild(b);
}*/




function updateClock() {
  var currentTime = new Date ( );
  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );

  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  //currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
  var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
  currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
  currentHours = ( currentHours == 0 ) ? 12 : currentHours;
  var currentTimeString = currentHours + ":" + currentMinutes + " " + timeOfDay;

  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString; 
}

function popupCenter(pageURL, title,w,h) {
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}

function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

function inspect(obj, maxLevels, level)
{
  var str = '', type, msg;

    // Start Input Validations
    // Don't touch, we start iterating at level zero
    if(level == null)  level = 0;

    // At least you want to show the first level
    if(maxLevels == null) maxLevels = 1;
    if(maxLevels < 1)     
        return '<font color="red">Error: Levels number must be > 0</font>';

    // We start with a non null object
    if(obj == null)
    return '<font color="red">Error: Object <b>NULL</b></font>';
    // End Input Validations

    // Each Iteration must be indented
    str += '<ul>';

    // Start iterations for all objects in obj
    for(property in obj)
    {
      try
      {
          // Show "property" and "type property"
          type =  typeof(obj[property]);
          str += '<li>(' + type + ') ' + property + 
                 ( (obj[property]==null)?(': <b>null</b>'):('')) + '</li>';

          // We keep iterating if this property is an Object, non null
          // and we are inside the required number of levels
          if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))
          str += inspect(obj[property], maxLevels, level+1);
      }
      catch(err)
      {
        // Is there some properties in obj we can't access? Print it red.
        if(typeof(err) == 'string') msg = err;
        else if(err.message)        msg = err.message;
        else if(err.description)    msg = err.description;
        else                        msg = 'Unknown';

        str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';
      }
    }

      // Close indent
      str += '</ul>';

    return str;
}

function printSelection(node) {
  var content=node.innerHTML
  var pwin=window.open('','print_content','width=100,height=100');
  pwin.document.open();
  pwin.document.write('<html><body onload="window.print()">'+content+'</body></html>');
  pwin.document.close();
 
  setTimeout(function(){pwin.close();},100);
}



