// Misc code in the public domain



// Write out a debug string to the top of document
// can be styled with class .debug {}

// Either call as 
// debug_log(object)

// or with context
// debug_log('String interpolation : #{myobj}',{myobj:object})

function debug_log(in_item,context,inside_element) {
	container = inside_element || $(document.body).down('div')
	
	if (!container)
		return
	
	if (!context)
		context = {}
	
	container.insert({top:'<p class="debug warning"> #{message} </p>\n'.interpolate(
		{message : String(in_item).interpolate(context)})
	})
}



// write out the current year for copyright lines
function year()  { 
	document.write(new Date().getFullYear())
}

// write out the current year for copyright lines
function writeDaysUntil(date_string)  { 
	document.write(daysUntil(date_string))
}


// Return the number of days till a date ( int is minus if it has passed)
function daysUntil(date_string) {
	milliseconds = Date.parse(date_string) - new Date()

	days_till = Math.ceil(parseFloat(milliseconds / 1000 / 60 / 60 / 24))

	return days_till
}





// These two fragments copyright their original authors



// Get Inner Text (Firefox doesn't support innerText property)
// http://tobielangel.com/2006/10/18/extending-prototype-getinnertext
//
// Is there a better way with prototype?
Element.addMethods({  
  getInnerText: function(element) {
    element = $(element);
	  return (element.innerText && !window.opera) ? element.innerText
      : element.innerHTML.stripScripts().unescapeHTML().replace(/[\n\r\s]+/g, ' ');
  }
});




// Extension to RegExp to escape regexp characters - taken from 
// http://simonwillison.net/2006/Jan/20/escape/
//
// Slightly modified to allow | symbol in string (unlikely in normal text)
RegExp.escape = function(text) {
  if (!arguments.callee.sRE) {
    var specials = [
    //  '/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'
	// removed pipe to allow simple 'or' searches - not often used in captions
     '/', '.', '*', '+', '?', '(', ')', '[', ']', '{', '}', '\\'
	];
    arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
  }
  return text.replace(arguments.callee.sRE, '\\$1');
}


