// uses jquery
$(function() {
	var calendar = $('#eventcalendar');
	var links = calendar.find('a.link, a.morelink');
	var display = $('#eventdisplay');
	var timeout = 1;
	display.hide();	
	calendar.before($.create('div', {'id': 'loading'}, ['div', {}, ['img', {'src': 'images/working.gif', 'alt': 'Loading'}, [], ['  Loading External Data...']]]));		
	links.click(
	  function() {
		var event_id = searchURL(this.href, 'event_id');	
		_working(true);		
		$.getJSON(
		  'extensions/com/events/EventAjaxGateway.cfc?method=init&methodname=get&event_id=' + event_id.value + '&timestamp=' + new Date().getSeconds(),
		  function(json) {setTimeout(function() {
			var d = json[0];
			display.html('');
			display.append(
			  $.create(
			    'div', {}, [
				  'p', {}, [
					'a', {'href': 'javascript:;', 'className': 'right', 'title': 'Close'}, ['X'], 
					'strong', {}, [d['event_from_date'].getDateString('%DD %D%OR %M %YYYY') + ' - ' + d['event_to_date'].getDateString('%DD %D%OR %M %YYYY')]
				  ],
				  'p', {}, [],
				  'p', {}, []
				]
			  )
			);
			$('#eventdisplay div a.right').click(function() { display.hide('slow'); });
			$('#eventdisplay p:eq(1)').html(_cleanIE(d['event_title']));
			$('#eventdisplay p:eq(2)').html(_cleanIE(d['event_text']));
			$('#eventdisplay div').css('height', 'auto');		
			display.css('height', 'auto');		
			display.hide(); 
			display.show('slow'); 
			window.scrollTo(0, _coord(display[0], 'offsetTop'));
			_working(false);
		  }, timeout * 1000)}
		);		
		return false;
	  }
	);
  },
  _coord = function (el,prop) {
	var c = el[prop], b = document.body;	
	while ((el = el.offsetParent) && (el != b)) {
	  if (!$.browser.msie || (el.currentStyle.position != 'relative')) {
	    c += el[prop];
	  }
	}
	return c;
  },
  _working = function(t) {
	var status = $('div#loading');
	if (t) {		
	  status.show();
	} else {
	  status.hide();
	}
  },
  _cleanIE = function(s) {
	return s.replace(/<\/?(div|p|ul|ol|li|hr|h[1-6]|pre|code|blockquote).*?>/gim, '');
  }
); 
