/* Author: 

*/

function level(objects)
{
	var maxBottom = false;
	
	$(objects).each(function()
	{
		var offset = $(this).offset();
		var bottom = offset.top + $(this).outerHeight();
		
		if (bottom > maxBottom) { maxBottom = bottom; }
	});
	
	if (maxBottom > 0)
	{
		$(objects).each(function()
		{
			var offset = $(this).offset();
			var diff = maxBottom - (offset.top + $(this).outerHeight());
			$(this).height($(this).height() + diff);
		});
	}
}

$(document).ready(function()
{
	$('.row').not('.nolevel').each(function()
	{
		level($(this).find('.side, .column'));
	});
	
	
	// activate sector scroller when present
	if ($('#scroller').length)
	{
		var scrollerHeight = false;
		$('#scroller li').each(function()
		{
			if ($(this).height() > scrollerHeight) { scrollerHeight = $(this).height(); }
		});
		$('#scroller li').height(scrollerHeight);
		$('#scroller, #scrollerPrev, #scrollerNext').height($('#scroller li').outerHeight());
		var heading = $('#scroller').parent().prev('.side');
		heading.height($('#scroller li').outerHeight() - (heading.innerHeight() - heading.height())); // account for padding on heading
		/*
		
		$('#scroller').cycle({ 
		    fx:     'scrollHorz', 
		    prev:   '#scrollerPrev', 
		    next:   '#scrollerNext', 
		    timeout: 0 
		});
		*/
	}
	

	function scroller_initCallback(carousel) 
	{
	    jQuery('.jcarousel-control a').bind('click', function() 
	    {
	        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
	        return false;
	    });
	
	    jQuery('#scrollerNext').bind('click', function() 
	    {
	        carousel.next();
	        return false;
	    });
	
	    jQuery('#scrollerPrev').bind('click', function() 
	    {
	        carousel.prev();
	        return false;
	    });
	};
	
	$("#scroller").jcarousel(
	{
        scroll: 1,
        wrap: 'circular',
        initCallback: scroller_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	// make entire item clickable
	$('#scroller li, #sectorList li').addClass('clickable').click(function()
	{
		window.location = $(this).children('a').attr('href');		
	});
	
	// activate scroller banners on sector detail pages
	if ($('#sectorBanner').length)
	{
		$('#sectorBanner').cycle(
		{ 
		    fx:     'scrollHorz', 
		    prev:   '#sectorBannerPrev', 
		    next:   '#sectorBannerNext', 
		    timeout: 0 
		});
	}
	
	if ($('#banner div').length)
	{
		$('#banner div').cycle(
		{ 
		    fx:     'scrollHorz', 
		    prev:   '#bannerPrev', 
		    next:   '#bannerNext', 
		    timeout: 0 
		});
	}
	
	// set sector list item heights to same
	if ($('#sectorList').length)
	{
		var maxHeight = 0;
		$('#sectorList section').each(function()
		{
			if ($(this).height() > maxHeight)
			{
				maxHeight = $(this).height();
			}
		});
		
		$('#sectorList section').height(maxHeight);
	}
});


















