Element.implement({
	/*
	Event delegation - instead of attaching events to new elements, attach to parent and use bubbling to fire children events

	Useage:

		Foo
		Bar
		Bar

	$("list").delegateEvent("click","li",function(e) { this==li },false,true);
	// or
	$("list").delegateEvent("click",function (el) { return Element.get(el,'tag')=="li"; },function(e) { this==li },false,true);
	*/
	delegateEvent:function (type,selector,fn,preventDefault,stopPropagation) {
		var check=$type(selector)=="function" ? selector : function (target) { return Element.match(target,selector) };
		return this.addEvent(type,function (e) {
			var target=e.target;
			while (target!=document.body) {
				if (check(target)) {
					if (preventDefault) e.preventDefault();
					if (stopPropagation) e.stopPropagation();
					return fn.apply($(target),[e]);
				}
				target=target.parentNode;
			}
		}.bind(this));
	}
});


// extend prototype Event with a method to determine of activation occurred - ie mouse click or enter
// :NOTE: problems with isLeftClick in prototype library
Object.extend(Event, {
  isActivationKey: function(event) {
    if (event.type != "undefined" && event.type == "click") { return true; }
    var charCode = (event.charCode) ? event.charCode : ((event.which) ? event.which : event.keyCode);
    return (charCode == Event.KEY_RETURN) ? true:false;
  }
});

var __initNewsHeadlines = function() {
  var headlines = $$('.headline');
  if (headlines.length > 0) {
    headlines.each(function(item) {
	  var anchor = item.getElement('a');
	  if (anchor && anchor.href) {
        item.href = anchor.href;
		item.delegateEvent('click', '*', function() {window.location = item.href;});		
	  }	 
    });
  }
}

var __initNewWindowAnchors = function() {
  var anchors = $$('a');
  anchors.each(function(elem) {
    if (/(new|thumb)/.test(elem.className)) {
	  elem.target = "blank";
	  if (elem.title.length == 0) {
		elem.title = "Open page in a new window";
	  }
	  else {
		elem.title += " - New Window";
	  }
	}		
  });
}

var __initLogoSize = function() {
  if ($('container').offsetWidth < 940) {
    document.getElement('h1').className = 'small';
  }
}

var __initToolTips = function() {
	//store titles and text
	$$('img.tipz').each(function(element,index) {
		var content = element.get('title').split('::');
		element.store('tip:title', content[0]);
		element.store('tip:text', content[1]);
	});
	
	//create the tooltips
	var tipz = new Tips('.tipz',{
		className: 'tipz',
		fixed: true,
		hideDelay: 50,
		showDelay: 50
	});
	/*
	tipz.addEvents({  
	    'show': function(tip) {  
	        tip.fade('in');  
	    },  
	    'hide': function(tip) {  
	        tip.fade('out');  
	    }  
	});
	*/	
}

window.addEvent('domready', function() {
	__initNewsHeadlines();
	__initNewWindowAnchors();
	__initLogoSize();
	__initToolTips();
});
