if(isiPhone) {
	myScroll = new iScroll('wrapper');
	myScroll.refresh();
}

if( $('html[lang="fr"]').size() ) {
	var addthis_config = { ui_language: "fr" }
}

$('.secondary li, .secondary td').click(function() {
	window.location = $('a', this).attr('href');
});

// Ref: http://girliemac.com/blog/2008/07/19/webkit-css-animation-examples/
function doScroller() {

	var frame = $( '.scroller .success-stories' );

	/* The number of items in the carousel - not including the clones that
	are created below. */
	var numItems = $( '.article', frame ).size();

	/* Initialize the circular scrolling effect by cloning the first and
	last items in the carousel. */
	if(!isiPhone) {
		var cloneFirst = $('.article', frame).first().clone(true).addClass('cloned'),
			cloneLast = $('.article', frame).last().clone(true).addClass('cloned');
		$(frame).append(cloneFirst).prepend(cloneLast);
	}

	// The width of each item in the carousel.
	var itemWidth = $('.article', frame).outerWidth(true);

	// Total width of the carousel, based on item width defined above.
	var frameWidth = itemWidth * $('.article', frame).size();

	// Total width of the scroller container/wrapper.
	var scrollerWidth = $( '.scroller' ).outerWidth();

	// Amount of gutter space (left/right) left over after ensuring two items
	// are displayed onscreen.
	var gutterWidth = parseInt( ( scrollerWidth - ( itemWidth * 2 ) ) / 2 );


	/* The current index (item) that the carousel is positioned at. This should
	always be set to 1. */
	var currIdx = 1;

	/* Number of visible items on the screen. */
	var numVisibleItems = (isiPhone) ? 1 : 2;

	var translate3dOffset = 0;



	$(frame).css({
		left: (isiPhone) ? 0 : -(itemWidth - gutterWidth),
		width: frameWidth
	});


	function goLeft() {

		if( currIdx <= 1 )  return;

		// Check for translate3d support.
		if( Modernizr.csstransforms3d ) {

			translate3dOffset += itemWidth;

			$(frame).css({
				'-moz-transform-style': 'preserve-3d',
				'-moz-transform': 'translate3d(' + translate3dOffset + 'px, 0px, 0px)',
				'-webkit-transform': 'translate3d(' + translate3dOffset + 'px, 0px, 0px)'
			});

		}

		// Otherwise use jQuery-based solution.
		else {

			$(frame).stop().animate({
				left: '+=' + itemWidth
			}, 250, function() {

			});

		}

		// Update the current item index.
		currIdx--;

		// Update the status of the navigation arrows.
		updateNavStatus();

	}



	function goRight() {

		if( currIdx + numVisibleItems > numItems ) return;

		// Check for translate3d support.
		if( Modernizr.csstransforms3d ) {

			translate3dOffset -= itemWidth

			$(frame).css({
				'-moz-transform-style': 'preserve-3d',
				'-moz-transform': 'translate3d(' + translate3dOffset + 'px, 0px, 0px)',
				'-webkit-transform': 'translate3d(' + translate3dOffset + 'px, 0px, 0px)'
			});

		}

		// Otherwise use jQuery-based solution.
		else {

			$(frame).stop().animate({
				left: '-=' + itemWidth
			}, 250);

		}

		// Update the current item index.
		currIdx++;

		// Update the status of the navigation arrows.
		updateNavStatus();

	}



	function updateNavStatus() {

		( currIdx <= 1 ) ? $('.scroller a.left').fadeOut(250) : $('.scroller a.left').fadeIn(250);
		( currIdx + numVisibleItems > numItems ) ? $('.scroller a.right').fadeOut(250) : $('.scroller a.right').fadeIn(250);

	}

	updateNavStatus();

	$('.scroller a.left').click( goLeft );
	$('.scroller a.right').click( goRight );

	$(frame).bind( 'swipeLeft', goRight );
	$(frame).bind( 'swipeRight', goLeft );

	if(isiPad) {
		$('a.left, a.right', '.scroller').remove();

		$('.scroller .instructions').fadeIn('fast').one( 'swipeLeft swipeRight', function(e) {
			( e.type === 'swipeLeft' ) ? goRight() : goLeft();
			$(this).fadeOut('fast');
		});
	}

	if(!isiPhone) {
		$('.overlay .close, .mask').click(function() {
			$('.overlay .layout').empty();
			$('.overlay').hide();
			$('.mask').fadeOut();
		});

		// Video overlay
		$('.scroller .article').not('.textonly, .cloned').click(function(e) {
			var overlay = $('.overlay'),
				mask = $('.mask'),
				video = $('footer a', this).data('videoid');

			$(mask).fadeIn();
			$('.layout', overlay).append('<iframe width="1280" height="750" src="http://www.youtube.com/embed/' + video + '?rel=0&amp;hd=1" frameborder="0" allowfullscreen></iframe>');
			$(overlay).show();

			e.preventDefault();
		});

		if(!$('html.ie6, html.ie7, html.ie8').size()) {
			$('.scroller .article.textonly:not(.external) a').click(function(e) {
				var overlay = $('.overlay'),
					mask = $('.mask');

				$(mask).fadeIn();
				$('.layout', overlay).append('<div class="generic" />').load($(this).attr('href') + ' .generic', function() {
					$(overlay).show();
				});

				e.preventDefault();
			});
		}
	} else {
		$('.scroller .article').click(function(e) {
			document.location = $('footer a', this).attr('href');
			e.preventDefault();
		});
	}

}
