
/**
 * Intended to work with A elements...
 * It goes through and determines whether a REL attribute
 * exists for this particular A tag, and if so, and it equals 'external',
 * add the non-xHTML standard target parameter so that the link
 * opens in a new window
 */
$.fn.initExternalLinks = function() {
	$(this).each(function() {
		obj = jQuery(this);
		if(obj.attr('rel') == 'external') obj.attr('target', "_blank");
	});
}

/**
 * Execute on DOM ready 
 */
$(document).ready(function () {

	// see function comment above
	$('a').initExternalLinks();
	
	// set up the tooltips that appear when a user hovers their mouse
	// cursor over a glossary-ised A tag
	$('a.glossary_tooltip').tooltip({
		tip: '#tooltip', 
		effect: 'fade', 
		fadeOutSpeed: 100, 
		predelay: 200, 
		position: "bottom right",         
		offset: [-50, -80] 
	});
	
	// set up any lightboxes.
	if ($.colorbox) $("a.lightbox").colorbox();
	
	// initialise the main blade kitten menu... basically what happens here is we
	// parse the UL menu list and dynamically add certain elements to style the menu
	$('#bk_menu ul li a').hover(
		function() {
			if (!($(this).parent().hasClass('active'))) {
				var children = $(this).children();
				var child_height = 0;
				children.each(function(i) {
				
					if (child_height == 0) child_height = $(this).height();
					$(this).css('background-position', '0px -'+child_height+'px').hasClass('bk_menu_item_left');
					$(this).css('background-position', '0px -'+child_height+'px').hasClass('bk_menu_item');
					$(this).css('background-position', 'right -'+child_height+'px').hasClass('bk_menu_item_right');
				});
			}				
		},
		function() {
			if (!($(this).parent().hasClass('active'))) {
				var children = $(this).children();
				children.each(function(i) {
					$(this).css('background-position', '0px 0px').hasClass('bk_menu_item_left');
					$(this).css('background-position', '0px 0px').hasClass('bk_menu_item');
					$(this).css('background-position', 'right 0px').hasClass('bk_menu_item_right');
				});
			}
		}
	);	
 
 	// similiar to the main BK menu, this goes through each h3 block-header tag (the
 	// rounded header title boxes) and dynamically adds elements such as to allow
 	// fancy styling. We don't do this via HTML as it's a lot of code and I want to
 	// apply the DRY concept (don't repeat yourself).
	$('h3.block-header').each(function(i) {
		$(this).replaceWith(
			'<div class="'+$(this).attr('class')+'">'+
				'<div class="block-header-overlay"></div>'+
				'<table cellpadding="0" cellspacing="0">'+
					'<colgroup><col width="22"/><col/><col width="22"/></colgroup>'+
					'<tbody><tr>'+
						'<td class="block-l"></td>'+
						'<td class="block-m"><span>'+$(this).text()+'</span></td>'+
						'<td class="block-r"></td>'+
					'</tr></tbody>'+
				'</table>'+
			'</div>');
	});
	

});


