/*
max by ProgramMax is licensed under a Creative Commons Attribution 3.0 Unported License:
http://creativecommons.org/licenses/by/3.0/

To learn more visit http://www.programmax.net/projects/max
*/

if( typeof max  == "undefined" ) {
	var max = { };
}

if( typeof max.ui == "undefined" ) {
	max.ui = { };
}

max.ui.VerticalPagingCarousel = function( parameters ){
	max.common.checkParameters({
		"parameters.Viewport": {
					value: parameters.Viewport,
					type: Object
		}
	});

	var CurrentPage = 1;
	var CurrentlyAnimating = false;

	var ViewportHeight = parameters.Viewport.offsetHeight;

	var Pages = max.nonobtrusive.getElementsByClassName({ parent: parameters.Viewport, className: "Page" });
	var PagesLength = Pages.length;

	function RenderPosition( Position ) {
		for( var i = 0; i < PagesLength; ++i ) {
			Pages[ i ].style.top = -((CurrentPage - 1) * ViewportHeight) + Position + 'px';
		}
	}

	this.ScrollPrevious = function( event ) {
		if( CurrentlyAnimating === false ) {
			CurrentlyAnimating = true;
			--CurrentPage;

			max.animation.Duration({ durationInMilliseconds: 1000, callback: function( PercentComplete ) {
				var PixelsToMove = max.animation.SineInterpolation({ percentComplete: PercentComplete }) * ViewportHeight;

				RenderPosition( PixelsToMove - ViewportHeight );

				// temporarily change the last page's position to be before 0
				if( CurrentPage === 0 ) {
					Pages[ PagesLength - 1 ].style.top = -(PagesLength * ViewportHeight) + PixelsToMove + 'px';
				}

				if( PercentComplete === 100 ) {
					CurrentlyAnimating = false;
					if( CurrentPage === 0 ) {
						// We temporarily moved the last page. Now lets put it back.
						Pages[ PagesLength - 1 ].style.top = ((PagesLength - 1) * ViewportHeight);
			

						// then put our current page back in range
						CurrentPage = PagesLength;

						// and finally update page tops to reset our view
						RenderPosition( 0 );
					}
				}
			} });
		}
	};

	this.ScrollNext = function( event ) {
		if( CurrentlyAnimating === false ) {
			// first setup the pages

			CurrentlyAnimating = true;
			++CurrentPage;

			max.animation.Duration({ durationInMilliseconds: 1000, callback: function( PercentComplete ) {
				var PixelsToMove = max.animation.SineInterpolation({ percentComplete: PercentComplete }) * ViewportHeight;

				RenderPosition( -PixelsToMove + ViewportHeight );

				// temporarily change the first page's position to be after the last
				if( CurrentPage === PagesLength + 1 ) {
					Pages[ 0 ].style.top = ViewportHeight - PixelsToMove + 'px';
				}

				if( PercentComplete === 100 ) {
					CurrentlyAnimating = false;

					if( CurrentPage === PagesLength + 1 ) {
						// We temporarily moved the last page. Now lets put it back.
						Pages[ 0 ].style.top = '0';

						// then put our current page back in range
						CurrentPage = 1;

						// and finally update page tops to reset our view
						RenderPosition( 0 );
					}
				}
			} });
		}
	};
};

max.ui.HorizontalPagingCarousel = function( parameters ){
	max.common.checkParameters({
		"parameters.Viewport": {
					value: parameters.Viewport,
					type: Object
		}
	});

	var CurrentPage = 1;
	var CurrentlyAnimating = false;

	var Pages = max.nonobtrusive.getElementsByClassName({ parent: parameters.Viewport, className: "Page" });
	var PagesLength = Pages.length;

	var ViewportWidth = parameters.Viewport.offsetWidth / PagesLength;

	function RenderPosition( Position ) {
		for( var i = 0; i < PagesLength; ++i ) {
			Pages[ i ].style.left = -((CurrentPage - 1) * ViewportWidth) + Position + 'px';
		}
	}

	this.ScrollPrevious = function( event ) {
		if( CurrentlyAnimating === false ) {
			CurrentlyAnimating = true;
			--CurrentPage;

			max.animation.Duration({ durationInMilliseconds: 1000, callback: function( PercentComplete ) {
				var PixelsToMove = max.animation.SineInterpolation({ percentComplete: PercentComplete }) * ViewportWidth;

				RenderPosition( PixelsToMove - ViewportWidth );

				// temporarily change the last page's position to be before 0
				if( CurrentPage === 0 ) {
					Pages[ PagesLength - 1 ].style.left = -(PagesLength * ViewportWidth) + PixelsToMove + 'px';
				}

				if( PercentComplete === 100 ) {
					CurrentlyAnimating = false;
					if( CurrentPage === 0 ) {
						// We temporarily moved the last page. Now lets put it back.
						Pages[ PagesLength - 1 ].style.left = ((PagesLength - 1) * ViewportWidth);
			

						// then put our current page back in range
						CurrentPage = PagesLength;

						// and finally update page tops to reset our view
						RenderPosition( 0 );
					}
				}
			} });
		}
	};

	this.ScrollNext = function( event ) {
		if( CurrentlyAnimating === false ) {
			// first setup the pages

			CurrentlyAnimating = true;
			++CurrentPage;

			max.animation.Duration({ durationInMilliseconds: 1000, callback: function( PercentComplete ) {
				var PixelsToMove = max.animation.SineInterpolation({ percentComplete: PercentComplete }) * ViewportWidth;

				RenderPosition( -PixelsToMove + ViewportWidth );

				// temporarily change the first page's position to be after the last
				if( CurrentPage === PagesLength + 1 ) {
					Pages[ 0 ].style.left = ViewportWidth - PixelsToMove + 'px';
				}

				if( PercentComplete === 100 ) {
					CurrentlyAnimating = false;

					if( CurrentPage === PagesLength + 1 ) {
						// We temporarily moved the last page. Now lets put it back.
						Pages[ 0 ].style.left = '0';

						// then put our current page back in range
						CurrentPage = 1;

						// and finally update page tops to reset our view
						RenderPosition( 0 );
					}
				}
			} });
		}
	};
};
