$(document).ready(function() {
						   
	var mf = {};
	
// ******************************************************* SNIFFING  FOR PROBLEM BROWSERS *******************************************************

		 if( navigator.userAgent.indexOf("Opera") != -1 ){ mf.browser = "opera"; mf.browserVersion = 0; }
	else if( navigator.userAgent.indexOf("MSIE 8") != -1 ){ mf.browser = "IE"; mf.browserVersion = 8; }
	else if( navigator.userAgent.indexOf("MSIE 7") != -1 ){ mf.browser = "IE"; mf.browserVersion = 7; }
	else if( navigator.userAgent.indexOf("MSIE 6") != -1 ){ mf.browser = "IE"; mf.browserVersion = 6; } 
	else if( navigator.userAgent.indexOf("iPad") != -1 ){ mf.browser = "ipad"; mf.browserVersion = 0; }	
	else if( navigator.userAgent.indexOf("Android") != -1 || navigator.userAgent.indexOf("webOS") != -1 || navigator.userAgent.indexOf("Palm") != -1 || navigator.userAgent.indexOf("Nokia") != -1 || navigator.userAgent.indexOf("Mini") != -1 || navigator.userAgent.indexOf("IEMobile") != -1 || navigator.userAgent.indexOf("iPhone") != -1 || navigator.userAgent.indexOf("iPod") != -1 || navigator.userAgent.indexOf("BlackBerry") != -1)
	{
		mf.browser = "smartphone"; mf.browserVersion = 0;
	}
	
	else{ mf.browser = 'decent browser'; mf.browserVersion = 0; }
	
	// attach a class of "mobile" to the page if we're on smartphone or ipad
	if( mf.browser == "smartphone" || mf.browser == "ipad" )
	{
		$('body').addClass('mobile');
	}
	
// ********************************************* SET MOBILE VIEWPORT *********************************************

	if( mf.browser == "smartphone" )
	{
		$('meta[name=viewport]').prop('content', 'initial-scale = 1.0, minimum-scale = 1.0, maximum-scale = 1.0');
	}
	else
	{
		$('meta[name=viewport]').prop('content', 'width = 1024');
	}
	
// **************************************************************************************************************
	
	// remove and replace input field default values
	// first lets' find all the input fields on the page that have a default value and remove the placeholder values into a separate array
	mf.defaultInputValues = new Array();
	
	$('input[type=text], input[type=email]').each( function(i)
	{		
		if( $(this).attr('placeholder') )
		{
			mf.defaultInputValues[$(this).attr('id')] = $(this).attr('placeholder');
			
			$(this).prop('placeholder', ''); // clear the placeholder attribute - we're using the array to store the values
			
			// now assign the functions that will cause the default text to be hidden and shown
			$(this).focus( function()
			{		
				if( $(this).prop('value') == mf.defaultInputValues[$(this).attr('id')] )
				{
					$(this).prop('value', '')
				}
			});
			
			$(this).focusout( function()
			{
				if( $(this).prop('value') == '' )
				{
					$(this).prop('value', mf.defaultInputValues[$(this).attr('id')])
				}
			});
		}
	});

// ******************************************* HOME PAGE CAROUSEL *******************************************
	
	// carousel namespace
	mf.homecarousel = {};
	
	// ******** carousel properties ******** 
	mf.homecarousel.slideWidth = $('#carousel_container').width() + 100; // puts a gutter of 50px between each slide
	mf.homecarousel.slideHeight = 0;
	
	mf.homecarousel.currentSlide = 0;
	mf.homecarousel.totalSlides = $('#carousel_container .carousel_slide').length;
	
	// touch swipe-related properties	
	mf.homecarousel.touchStartX = 0;
	mf.homecarousel.touchPreviousX = 0;
	mf.homecarousel.touchCurrentX = 0;
	mf.homecarousel.touchXMovement = 0;	
	
	mf.homecarousel.touchStartY = 0;
	mf.homecarousel.touchCurrentY = 0;
	mf.homecarousel.touchYMovement = 0;
	
	mf.homecarousel.swipeSpeedThreshold = 3;
	mf.homecarousel.touchInProgress = false;
	mf.homecarousel.touchScrollPage = false;
	mf.homecarousel.firstTouchMove = true;
	mf.homecarousel.usingTouchEvents = false;
	
	
	mf.homecarousel.slideContainerPosition = 0;

	
	// ******** carousel methods ******** 
	
	mf.homecarousel.repositionSlides = function()
	{
		// resposition each slide
		$('#carousel_container .carousel_slide').each( function(i)
		{
			$(this).css('position', 'absolute');
			$(this).css('left', (i * mf.homecarousel.slideWidth) + 'px');
			$(this).css('top', 0 + 'px');
		});
	}
	
	/*
	mf.homecarousel.repositionSlideContainer = function()
	{
		// cancel any CSS transitions applied to the slideshow, then effect the position change
		$('#main_carousel_slides').css( '-webkit-transition' , 'none');			
		$('#main_carousel_slides').css('-webkit-transform', 'translate3D(' + (0 - (mf.homecarousel.slideWidth * mf.homecarousel.currentSlide)) + 'px,0,0)');;
	}
	*/
	
	mf.homecarousel.resizeSlideContainer = function()
	{
		$('#carousel_container #slides_container').css('width', (mf.homecarousel.slideWidth * mf.homecarousel.totalSlides) + 'px');
	}
	
	
	/*
	mf.homecarousel.resizeSlides = function()
	{
		// resizes the slides and carousel for smartphones
		
		// set the new width;
		mf.homecarousel.slideWidth = $('#main_carousel').width();
		$('#main_carousel article').css('width', mf.homecarousel.slideWidth + 'px');
		
		// use the first image in the slideshow as the basis for determining the height / width ratio
		var imageWidth = $('#main_carousel article img:first').attr('width');
		var imageHeight = $('#main_carousel article img:first').attr('height');
		var imageRatio = imageWidth / imageHeight;		
		var newImageHeight = mf.homecarousel.slideWidth / imageRatio;
		
		// determine the height of the tallest text panel
		var maxTextHeight = 0;
		
		$('#main_carousel article p').each( function(i)
		{
			if( $(this).outerHeight(true) > maxTextHeight )
			{
				maxTextHeight = $(this).outerHeight(true);	
			}
		});
		
		// set the new carousel height
		mf.homecarousel.slideHeight = newImageHeight + maxTextHeight;
		$('#main_carousel').css('height', mf.homecarousel.slideHeight + 'px');
		
		// set the height of each slide
		$('#main_carousel article').each( function()
		{
			$(this).css('height', mf.homecarousel.slideHeight + 'px');
		});
		
		// reposition pagination
		$('#main_carousel .carousel_pagination').css('top', (newImageHeight + 16) + 'px');
		
		// reposition <p> and <h1> for each slide
		$('#main_carousel .carousel_slides article h1').each( function()
		{
			$(this).css('bottom', maxTextHeight + 'px');
		});
		
	}
	*/
	
	/*
	mf.homecarousel.updatePagination = function()
	{
		$('#carousel .carousel_pagination .current_slide').html( mf.homecarousel.currentSlide + 1 );
	}
	*/
	
	mf.homecarousel.advanceCounter = function( desiredSlide )
	{		
		newSlide = desiredSlide;
		
		if( newSlide > mf.homecarousel.totalSlides - 1 )
		{
			newSlide = mf.homecarousel.totalSlides - 1;
		}
		else if( newSlide < 0 )
		{
			newSlide = 0;
		}
		
		if( newSlide == mf.homecarousel.currentSlide )
		{
			return;
		}
		
		// sort out the classes on the nav buttons
		$( '#carousel nav a' ).eq( newSlide ).closest('li').addClass('current');
		$( '#carousel nav a' ).eq( mf.homecarousel.currentSlide ).closest('li').removeClass( 'current' );
		
		mf.homecarousel.currentSlide = newSlide;
	}
	
	mf.homecarousel.clickSlideNav = function(e)
	{
		e.preventDefault();		
		if( ! touchEventsInUse() )
		{
			mf.homecarousel.advanceCounter( $(e.target).attr('data-goto-slide') )
			mf.homecarousel.clickTransition();
		}
	}
	
	mf.homecarousel.touchSlideNav = function(e)
	{
		mf.homecarousel.usingTouchEvents = true;
		
		e.preventDefault();
		e.stopPropagation();
		mf.homecarousel.advanceCounter( $(e.currentTarget).attr('data-goto-slide') );
		
		$('#slides_container').css( '-webkit-transition' , '-webkit-transform 0.4s');		
		$('#slides_container').css( '-webkit-transform','translate3D(' + (0 - (mf.homecarousel.currentSlide * mf.homecarousel.slideWidth)) + 'px,0,0)' );
	}
	
	mf.homecarousel.clickTransition = function()
	{
		$('#carousel_container #slides_container').animate( { left: (0 - (mf.homecarousel.currentSlide * mf.homecarousel.slideWidth)) }, 400 );
	}
	
	mf.homecarousel.clickNext = function(e)
	{
		e.preventDefault();
		if( ! touchEventsInUse() )
		{
			mf.homecarousel.advanceCounter( mf.homecarousel.currentSlide + 1 );
			mf.homecarousel.clickTransition();
		}
	}
	
	mf.homecarousel.clickPrev = function(e)
	{
		e.preventDefault();
		if( ! touchEventsInUse() )
		{
			mf.homecarousel.advanceCounter( mf.homecarousel.currentSlide - 1 );	
			mf.homecarousel.clickTransition();
		}
	}
	
	mf.homecarousel.touchNext = function(e)
	{
		mf.homecarousel.usingTouchEvents = true;
		
		e.preventDefault();
		e.stopPropagation();
		
		mf.homecarousel.advanceCounter( mf.homecarousel.currentSlide + 1 );		
		//mf.homecarousel.updatePagination();
		//mf.homecarousel.updatePopupPlayer( mf.homecarousel.currentSlide );
		
		// effect the transition
		$('#slides_container').css( '-webkit-transition' , '-webkit-transform 0.4s');		
		$('#slides_container').css( '-webkit-transform','translate3D(' + (0 - (mf.homecarousel.currentSlide * mf.homecarousel.slideWidth)) + 'px,0,0)' );
	}
	
	mf.homecarousel.touchPrev = function(e)
	{
		mf.homecarousel.usingTouchEvents = true;
		
		e.preventDefault();
		e.stopPropagation();
		
		mf.homecarousel.advanceCounter( mf.homecarousel.currentSlide - 1 );		
		//mf.homecarousel.updatePagination();
		//mf.homecarousel.updatePopupPlayer( mf.homecarousel.currentSlide );
		
		// effect the transition
		$('#slides_container').css( '-webkit-transition' , '-webkit-transform 0.4s');		
		$('#slides_container').css( '-webkit-transform','translate3D(' + (0 - (mf.homecarousel.currentSlide * mf.homecarousel.slideWidth)) + 'px,0,0)' );
	}
	
	mf.homecarousel.touchStart = function(e)
	{
		mf.homecarousel.usingTouchEvents = true;
		
		// cancel any CSS transitions applied to the slideshow + cancel slideshow hover effects
		$('#slides_container').css( '-webkit-transition' , 'none');	
		//$('#carousel #slides_container article a').addClass('no_hover');
		
		mf.homecarousel.touchInProgress = true;
		
		mf.homecarousel.touchStartX = e.touches[0].pageX;
		mf.homecarousel.touchStartY = e.touches[0].pageY;
		mf.homecarousel.touchPreviousX = 0;
		mf.homecarousel.touchCurrentX = e.touches[0].pageX;
		mf.homecarousel.touchCurrentY = e.touches[0].pageY;
		mf.homecarousel.touchXMovement = 0;
		mf.homecarousel.touchYMovement = 0;
		mf.homecarousel.touchScrollPage = false;
		mf.homecarousel.firstTouchMove = true;
		
		mf.homecarousel.nextSlidePosition = mf.homecarousel.slideWidth * (mf.homecarousel.currentSlide + 1);
		mf.homecarousel.previousSlidePosition = mf.homecarousel.slideWidth * (mf.homecarousel.currentSlide - 1);
		
		// get the initial position of the user's finger
		mf.homecarousel.touchCurrentX = mf.homecarousel.touchPreviousX = mf.homecarousel.touchStartX;
	}
	
	mf.homecarousel.touchMove = function(e)
	{		
		mf.homecarousel.touchXMovement = e.touches[0].pageX - mf.homecarousel.touchStartX;
		mf.homecarousel.touchYMovement = e.touches[0].pageY - mf.homecarousel.touchStartY;
		
		// determine how much horizontal & vertical finger movement there has been since the touch event started
		mf.homecarousel.touchPreviousX = mf.homecarousel.touchCurrentX;
		mf.homecarousel.touchCurrentX = e.touches[0].pageX;
		mf.homecarousel.touchCurrentY = e.touches[0].pageY;
		
		// if it's the first time touchMove has been called during the current touch event, determine whether this will be a vertical page scroll or a carousel swipe
		if( mf.homecarousel.firstTouchMove && Math.abs(mf.homecarousel.touchYMovement) > Math.abs(mf.homecarousel.touchXMovement) )
		{
			mf.homecarousel.touchScrollPage = true;
		}
		
		mf.homecarousel.firstTouchMove = false;
		
		// if the current touch movement is not a page scroll movement, move the carousel accordingly
		if( !mf.homecarousel.touchScrollPage )
		{
			e.stopPropagation();
			e.preventDefault();
	
			// get the current horizontal offset of the corrent slide
			mf.homecarousel.getSlideContainerPosition( mf.homecarousel.currentSlide );
			
			// move the slides according to how far the user's finger has moved
			$('#slides_container').css('-webkit-transform', 'translate3D(' + (mf.homecarousel.slideContainerPosition + mf.homecarousel.touchXMovement) + 'px,0,0)');
		}
	}
	
	mf.homecarousel.touchEnd = function(e)
	{
		e.stopPropagation();
		e.preventDefault();
		
		mf.homecarousel.touchInProgress = false;
		
		var touchXSpeed = mf.homecarousel.touchCurrentX - mf.homecarousel.touchPreviousX;
		var xTravelSinceStart = Math.abs( mf.homecarousel.touchCurrentX - mf.homecarousel.touchStartX );
		var yTravelSinceStart = Math.abs( mf.homecarousel.touchCurrentY - mf.homecarousel.touchStartY );
		var nextSlide = mf.homecarousel.currentSlide;
		
		// if we've had virtually no movement since the use pressed his finger, it's a press / tap rather than a swipe, so let's load the link
		/*
		if( xTravelSinceStart == 0 && yTravelSinceStart == 0 )
		{
			window.location.href = $('#main_carousel .carousel_slides article:eq(' + mf.homecarousel.currentSlide + ')').find('a:first').attr('href');
		}
		*/
		
		// if the finger has moved more than half a slide width in either direction, or was moving fast enough, trigger a slide change
		if( (mf.homecarousel.touchXMovement > mf.homecarousel.slideWidth / 2 ||  touchXSpeed > mf.homecarousel.swipeSpeedThreshold) && mf.homecarousel.currentSlide > 0 )
		{
			nextSlide = parseInt(mf.homecarousel.currentSlide) - 1;
		}
		else if( (mf.homecarousel.touchXMovement < 0 - (mf.homecarousel.slideWidth / 2) || touchXSpeed < 0 - mf.homecarousel.swipeSpeedThreshold) && mf.homecarousel.currentSlide < mf.homecarousel.totalSlides - 1 )
		{
			nextSlide = parseInt(mf.homecarousel.currentSlide) + 1;
		}
		
		// initiate the transition
		mf.homecarousel.getSlideContainerPosition( nextSlide );
		$('#slides_container').css( '-webkit-transition' , '-webkit-transform 0.4s');		
		$('#slides_container').css( '-webkit-transform','translate3D(' + mf.homecarousel.slideContainerPosition + 'px,0,0)' );
		
		//mf.homecarousel.currentSlide = nextSlide;
		mf.homecarousel.advanceCounter( nextSlide );
		
	}
	
	
	// checks to see if the user has previously interacted using touch. if so, returns true and cancels all click event handlers
	function touchEventsInUse()
	{
		if( mf.homecarousel.usingTouchEvents == true )
		{
			$('#carousel #next_slide').unbind( 'click');			
			$('#carousel #prev_slide').unbind( 'click');
			$('#carousel nav a').unbind( 'click' );
			
			return true;
		}
		else
		{
			return false	
		}
	}
	/*
	mf.homecarousel.updatePopupPlayer = function( slideNumber )
	{				
		// if the popup player is currently open and the current slide is a podcast type slide, cause the popup player to change to match the current slide
		if( mf.popupPlayer.showingPlayer )
		{						
			if( $('#slide_' + slideNumber).hasClass('podcast') )
			{
				mf.popupPlayer.changePopupPlayer( slideNumber );
			}
		}
	}
	*/
	
	mf.homecarousel.getSlideContainerPosition = function( slideNumber )
	{
		mf.homecarousel.slideContainerPosition = 0 - (slideNumber * mf.homecarousel.slideWidth);
	}
	
// ******************************************* INITIALISE HOME PAGE CAROUSEL *******************************************
	
	// show all slides
	// $('#main_carousel .carousel_slides article').css('display', 'block');
	
	// loop through all slides and position one after the other horizontally
	mf.homecarousel.repositionSlides();
	
	// set the width of the slide container element to be large enough to contain all slides
	mf.homecarousel.resizeSlideContainer();
	
	// set the carousel pagination to display correct number ot total slides
	//$('#carousel .carousel_pagination .total_slides').html( mf.homecarousel.totalSlides );
	
	// event handlers for clicking the slide advance button
	$('#carousel #next_slide').bind( 'click', function(e){
		mf.homecarousel.clickNext(e);
	});
	
	$('#carousel #prev_slide').bind( 'click', function(e){
		mf.homecarousel.clickPrev(e);
	});
	
	//event handlers for the navigation buttons
	$('#carousel nav a').bind( 'click', function(e){
		mf.homecarousel.clickSlideNav(e);
	});
	
	// event handler for touchstart event
	if ( 'ontouchstart' in document && $('#carousel_container').length > 0 )
	{		
		// slideshow swiping
		document.getElementById('slides_container').addEventListener("touchstart", mf.homecarousel.touchStart, false);
		document.getElementById('slides_container').addEventListener("touchmove", mf.homecarousel.touchMove, false);
		document.getElementById('slides_container').addEventListener("touchend", mf.homecarousel.touchEnd, false);
		
		// touch advance button
		document.getElementById('next_slide').addEventListener("touchstart", mf.homecarousel.touchNext, false);
		document.getElementById('prev_slide').addEventListener("touchstart", mf.homecarousel.touchPrev, false);
		
		document.getElementById('slide_nav_0').addEventListener("touchstart", mf.homecarousel.touchSlideNav, false);
		document.getElementById('slide_nav_1').addEventListener("touchstart", mf.homecarousel.touchSlideNav, false);
		document.getElementById('slide_nav_2').addEventListener("touchstart", mf.homecarousel.touchSlideNav, false);
	}
	
	// if it's a smartphone, resize the slides & container elements accordingly
	/*
	if( mf.browser == "smartphone" )
	{
		mf.homecarousel.resizeSlides();
		
		window.onorientationchange = function(){
			mf.homecarousel.resizeSlides();
			mf.homecarousel.repositionSlides();
			mf.homecarousel.resizeSlideContainer();
			mf.homecarousel.repositionSlideContainer();
		}
		
		$(window).resize( function(){
			mf.homecarousel.resizeSlides();
			mf.homecarousel.repositionSlides();
			mf.homecarousel.resizeSlideContainer();
			mf.homecarousel.repositionSlideContainer();
		});
	}
	*/
	
});
