$(document).ready(function() {
	/*$('.MainMenuWrap ul ul').not('.MainMenuWrap ul ul ul').makeacolumnlists({cols:2,colWidth:300,equalHeight:false,startN:1});

	$('.MainMenuWrap .li_cont2').after('<div class="MenuPicture"></div>');*/
	
	$('.Home .LeftInformationPanel h3').siblings().wrapAll('<div class="MoreInfoContent"></div>');
	$('.Home .LeftInformationPanel h3 a').click(function() {	
		$('.MoreInfoContent').slideToggle(200);
		return false;
	});
	
	$('.Photos .RightContentWrap a').lightBox({
		fixedNavigation:true,
		imageLoading: '/skin/images/lightbox-btn-loading.gif',
		imageBtnClose: '/skin/images/lightbox-btn-close.gif',
		imageBtnPrev: '/skin/images/lightbox-btn-prev.gif',
		imageBtnNext: '/skin/images/lightbox-btn-next.gif'
	});
	
	$('.TitleImage').append('<div class="SlideNav"><div class="Prev"></div><div class="Next"></div></div>');
	$('.TitleImage p img').parent('p').addClass('SlideImage');
	$('.TitleImage .SlideImage').each(function(){
		var text = $(this).nextUntil('.SlideImage');
		$(this).add(text).wrapAll('<div class="Slide"></div>');
		text.wrapAll('<div class="HeaderText"></div>');
	});
	/*$('.FeaturePanel h2').each(function(){
		$(this).add($(this).next('p')).wrapAll('<div class="Slide"></div>');
	});*/
	$('.TitleImage').cycle({ 
		fx: 'fade', 
		speed: 1500,
		timeout: 8000,
		pause: true,
		slideExpr: '.Slide',
		fit: 1,
		next: '.Next',
		prev: '.Prev'
	});
	

	/***
	**** Meet the team page
	***/
	/* Encapsulate team members */
	$('.Team .MiddleContentWrap .Enhance2ContentElement3 h2, .Team .MiddleContentWrap .Enhance2ContentElement4 h2').each(function(){
		$(this).add($(this).nextUntil('h2')).wrapAll('<div class="TeamMember"></div>');
	});
	/* Seperate dialog content */
	$('.TeamMember h3').each(function(i){
		$(this).add($(this).nextAll()).wrapAll('<div class="DialogContent DialogContent' + i + '"></div>');
	});
	/* Team member layout */
	$('.TeamMember').each(function(i){
		// Copy image into dialog
		var newimg = $(this).find('img').clone();
		$(this).find('.DialogContent').prepend(newimg);
		newimg.attr('src', newimg.attr('src').replace('Thumbnail-','Person picture-'));
		// Swap image and main heading
		swap($(this).find('img').parent('p'), $(this).find('h2'));
		// Add more link
		$(this).find('h2').after('<p class="More"><a href="#">&gt; click here</a></p>');
		// Copy heading into dialog
		var newh2 = $(this).find('h2').clone();
		$(this).find('.DialogContent').prepend(newh2);
		// Dialog opener
		var dialog = $(this).find('.DialogContent');
		$(this).find('.More a').colorbox({width:'50%', href:'.DialogContent'+i, inline: true, rel:'team'});
		dialog.wrap('<div style="display:none"></div>');
	});
	
	
	/**
	*** Reception
	**/
	if($('.Reception').length > 0){
		/* Cycle between slides */
		$('.Reception .ContentWrap > div').wrap('<div class="Slide"></div>');
		$('.Reception .ContentWrap').cycle({
			fx: 'fade', 
			speed: 1500,
			timeout: 8000,
			before: changeTheme
		});
	}
	
});

var changeTheme = function(currSlideElement, nextSlideElement, options, forwardFlag){
	var flag = 0;
	$(nextSlideElement).find('img').each(function(){
		if($(this).attr('src').indexOf('RBG icon') != -1 || $(this).attr('src').indexOf('RBG-icon') != -1){ flag = 0; $(this).hide(); }
		if($(this).attr('src').indexOf('RBMR icon') != -1 || $(this).attr('src').indexOf('RBMR-icon') != -1){ flag = 1; $(this).hide(); }
		if($(this).attr('src').indexOf('WBL icon') != -1 || $(this).attr('src').indexOf('WBL-icon') != -1){ flag = 2; $(this).hide(); }
		if($(this).attr('src').indexOf('RBB icon') != -1 || $(this).attr('src').indexOf('RBB-icon') != -1){ flag = 3; $(this).hide(); }
		if($(this).attr('src').indexOf('RBC icon') != -1 || $(this).attr('src').indexOf('RBC-icon') != -1){ flag = 4; $(this).hide(); }
	});
	switch(flag){
		case 1:	/* Minerals */
			changeSkin(1, '/skin/images/Header-graphic-reception-recycling.png', '#0B9444');
			break;
		case 2:	/* Wilkins */
			changeSkin(2, '/skin/images/Header-graphic-reception-wilkins.png', '#3D3E9A');
			break;
		case 3:	/* Building */
			changeSkin(3, '/skin/images/Header-graphic-reception-building.png', '#0B9444');
			break;
		case 4:	/* Construction */
			changeSkin(4, '/skin/images/Header-graphic-reception-construction.png', '#ED1B24');
			break;
		default:	/* Group */
			changeSkin(0, '/skin/images/Header-graphic-reception-group.png', '#B06110');
			break;
	}
}

var changeSkin = function(logo, headerimage, footercolour){
	$('.Reception .SubHeader p:nth-child(' + (logo + 1) + ') img').fadeIn(1500);
	$('.Reception .SubHeader p:not(:nth-child(' + (logo + 1) + ')) img').fadeOut(1500);

	$('.Reception .Header').css({'background-image': $('.Reception .Header2').css('background-image')});
	$('.Reception .Header2').css({'display': 'none', 'background-image': 'url("' + headerimage + '")'});
	$('.Reception .Header2').fadeIn(1500, function(){
		$('.Reception .Header').css({'background-image': 'none'});
	});
	
	$('.Reception .SiteFooter').animate({'backgroundColor': footercolour}, 1500);
}


/*
**** Swap 2 elements in the markup
***/
function swap(swap1, swap2){
	var swap1clone = swap1.clone(true);
	var swap2clone = swap2.clone(true);
	swap1.before(swap2clone);
	swap2.before(swap1clone);
	swap1.remove();
	swap2.remove();
}

