/**
* Standardlife.com 2010
* Tab container (with autoplay functionality)
*
* @author Brian Coit <brian@line.uk.com>
*/
if (typeof jQuery == 'undefined')
{
	var _jQerr = 'Tab container requires the jQuery library to be loaded first.';

    window.console
	? console.log(_jQerr)
	: alert(_jQerr);
}

(function($)
{	
	$(document).ready(function()
	{
		var container = $('.tab-container');
		var delay = 4; /* 4 sec */

		container.addClass('js');

		container.each(function()
		{
			var _container = $(this);
			var tabs = $('.tab', _container);
			/* create tab controls container */
			var tc = $('<div />')
				.addClass('ctrl')
				.appendTo(_container);

			/* add <ul> to tab controls */
			var tcl = $('<ul />').appendTo(tc);

			/**
			 * Loop through each '.tab' element, creating controls
			 * and binding click events
			 */
			var j = 0;
			var tw = 0;
			var mh = 0;
			tabs.each(function()
			{
				var tab = $(this);
				var title = $('.puff-content', this).find('> *').get(0);
				var li = $('<li />');

				var anchor = $('<a />')
					.click(function(e)
					{
						e.preventDefault();
						showTab(tab, li);
					})
					.appendTo(li)
					.attr('href', '#');

				var span = $('<span />')
					.appendTo(anchor);

				var text;
					
				if(!_container.hasClass('carousel'))
				{
					text = $(title).text().replace(/ /g, '&nbsp;');
				}
				else
				{
					text = ++j;
				}
					
				/* if tab element contains title attribute, use this text instead of first element */
				if(tab.attr('title'))
				{
					text = tab.attr('title').replace(/ /g, '&nbsp;');;
				}

				var em = $('<em />')
					.appendTo(span)
					.html(text);

				/** ie6 width fix */
				if($.browser.msie && parseFloat($.browser.version) < 7)
				{
					li.css({width: '1%'});
				}

				tcl.append(li);

				$(_container)
					.find('.puff-content')
					.each(function()
					{
						if($(this).height() > mh)
						{
							mh = $(this).height();
						}
					})
					.height(mh);

				$(this).hide();
			});
			

			$(tabs.get(0)).show();

			$(_container.find('.ctrl a').get(0)).click();

			/* carousel autoplay */
			if(_container.hasClass('carousel'))
			{
				autoplay();
			}


			function showTab(el, li)
			{
				_container.find('.ctrl li').removeClass('active');
				li.addClass('active');

				tabs.hide();
				el.show();
			}

			function autoplay()
			{
				/* convert ms to s */
				var _d = delay * 1000;
				var interval;

				tick();
				
				_container.hover(function()
				{
					clearInterval(interval);
				},
				function()
				{
					tick();
				});

				function tick()
				{
					interval = setInterval(function()
					{
						if($(_container).find('.ctrl li.active').next().length)
						{
							$(_container).find('.ctrl li.active').next().find('a').click();
						}
						else
						{
							$(_container.find('.ctrl a').get(0)).click();
						}
					}, _d);
				}
			}

			if(typeof navigator != 'undefined')
			{
				try
				{
					var platform = navigator.platform.toLowerCase();

					if(platform.match(/mac/))
					{
						_container.find('li em').css({
							fontSize: '14px'
						});
					}
				}
				catch(err) {}
			}

		});
	});
})
(jQuery);

/**
 * Corporate Responsibility tabs
 */
(function($)
{
	$(document).ready(function()
	{
		$('.regular-tab-container').each(function()
		{
			var container = $(this);
			var controls = $(this).find('> .ctrl');
			var tabs = $(this).find('> .tabs > .tab');
			
			tabs.hide();
			
			controls.find('li').each(function()
			{
				var idx = $(this).index();
				
				$(this).click(function(e)
				{
					e.preventDefault();
					
					$(this).addClass('active')
						.siblings()
						.removeClass('active');
					
					tabs.removeClass('active').hide();
					$(tabs.get(idx)).addClass('active').show();
				});
			});
			
			$(tabs.get(0)).addClass('active').show();
		});
	});
})
(jQuery);
