
/**
 * Fading eye candy for the images
 */
function setupSectorImageFader(sectorBlock){
	linkImages = sectorBlock.find('img').not('.arrow');
	
	linkImages.each(function(){
	
		$(this).hover(function(){
			linkImages.not(this).stop().animate({
				opacity: 0.4
			}, 200);
			$(this).stop().animate({
				opacity: 1.0
			}, 200);
			
		}, function(){
			linkImages.stop().animate({
				opacity: 1.0
			}, 200);
		});
		
	});
	
}
/**
 * Function getFeed()
 * creates the #news section
 */
function getFeed(sender, uri){

        jQuery.getFeed({
            url: '' + uri,
            success: function(feed){
				var html = '';
                
                for (var i = 0; i < feed.items.length && i < 3; i++) {
                
                    var item = feed.items[i];
                   
                    html += '<h3>' +
                    '<a href="' +
                    item.link +
                    '">' +
                 	item.title +
                    '</a>' +
                    '</h3>';
                                                            
                    html += '<div class="newsDescription">' +
                    truncate(item.description, 70) +
                    '<a href="' +
					item.link +
                    '">more</a>' +
					
					'</div>';
                }
              
                jQuery(sender).append(html);
            }
        });
    }
 

	
	


/**
 * Truncate: 
 * Author: Andrew Hedges, andrew@hedges.name
 * License: free to use, alter, and redistribute without attribution
 */

/**
 * Truncate a string to the given length, breaking at word boundaries and adding an elipsis
 * @param string str String to be truncated
 * @param integer limit Max length of the string
 * @return string
 */
var truncate = function (str, limit) {
	
	var bits, i;
	bits = str.split('');
	if (bits.length > limit) {
		for (i = bits.length - 1; i > -1; --i) {
			if (i > limit) {
				bits.length = i;
			}
			else if (' ' === bits[i]) {
				bits.length = i;
				break;
			}
		}
		bits.push('...');
	}
	return bits.join('');
};
// END: truncate



/**
 * We use jFeed to grab the blog rss feed in XML format via ajax
 * We then are returned an array object feed which contains the items in date order
 * we select the top 3
 */
$(document).ready(function(){
	/* We use a variable to store the jquery search to save repeating it to perform an
	 * operation on it.
	 * Then we determine if the selector exists by using selectorVar.get(0)
	 * which returns false if the selector has no first value
	 * 
	 * If it does have a value we do the setup for that particular block on the page.
	 * That way we can mix and match if a function is needed or not. 
	 * 
	 * Blocks:
	 * 
	 * Contact Form
	 * 	Set up validation plugin for the contact form. 
	 * News
	 *  use jfeed to get the blog feed and display the last 3 posts in the #news block
	 *  on the page
	 *  
	 * Sector Block
	 *  adds the opacity fade effect to the #sectorBlock images on the sector_expertise page
	 */
	
	var theform = $('#contactform');
	
	if (theform.get(0)) {	
		theform.validate();
	}
	
	var news = $('#news');
    if (news.get(0)) {
		/* INFO: -- modify this to the correct absolute path on the server -- */
	
		getFeed(news, '/blog/feed/');

	};
// disabled until sector sections need expanding beyond a page.
// This gives a fade over the sector images	
//	var sectorBlock = $('#sectorBlock'); 
//    if	(sectorBlock.get(0)) {
// 	setupSectorImageFader(sectorBlock);
//	 };
	 
	 // Menu is on every(nearly) page so add this enhanced suckerfish module
	var ulMenu = $('ul#menu');
	if (ulMenu.get(0)) {
		ulMenu.superfish({
			autoArrows: false,
			dropShadows: true,
			speed: 'normal'
		});
	}
	
	 var maxHeight  = 0;
$('#colspace2, #mainBody, #news, #gallery').each( function(column) {
height = $(this).offset().top;
if (height > maxHeight) maxHeight = height
  }).css('height', $('#footer').offset().top - maxHeight + 88  + "px");
 /*
 * Setup lightbox for images with suitable classes
 * If a link has class lightbox the click is hijacked to the lightbox instead of being followed.
 * The resultant image src is then displayed in the lb along with title. Multiple images on a page will form a single gallery
 */
	try {
		$('a[rel*=lightbox]').lightBox();
	} catch(err) {};
});


$(window).load(function(){
  //  Fix the column heights against the mainBody which pushes the footer down


var maxHeight  = 0;
$('#colspace2, #mainBody, #news, #gallery').each( function(column) {
height = $(this).offset().top;
if (height > maxHeight) maxHeight = height
  }).css('height', $('#footer').offset().top - maxHeight + 88  + "px");
});


