﻿/// <reference path="http://ajax.microsoft.com/ajax/$/$-1.4.1-vsdoc.js" />

var CMQ0001CarouselHome = (function ($) {
	var current = 1;
	var myInterval, Distance, AnimationTime, AnimationInterval, ContainerId;
	var LockUserActions = false;
	/*var Distance = 700;
	var AnimationTime = 1500;
	var AnimationInterval = 7000;*/

	var MoveBack = function (itemNumber) {
		LockUserActions = true;
		$('#' + ContainerId + ' ._Item[data-pos="' + itemNumber + '"]').animate({
			left: "-" + Distance + "px"
		}, AnimationTime, null, function () {
			//after animation is complete.
			$(this).css({ 'left': Distance + 'px' }); //move it back to the starting posistion
			LockUserActions = false; 			//Unlock it.
		});
	};

	var Rotate = function (itemNumber) {

		var count = $('#' + ContainerId + ' ._Item').length;
		var previous = itemNumber - 1;
		if (itemNumber > count) {
			current = 1;
			itemNumber = current;
		}
		var newItem = $('#' + ContainerId + ' ._Item[data-pos="' + itemNumber + '"]');
		UpdateLink(newItem);
		newItem.animate({
			left: "0px"
		}, AnimationTime, null, MoveBack(previous));
	};

	var GoBackOne = function (itemNumber) {
		LockUserActions = true;
		var currentItem = $('#' + ContainerId + ' ._Item[data-pos="' + itemNumber + '"]');
		var newItemId = itemNumber - 1;
		if (newItemId < 1) {//if the id for the data-sortOrder is less then 0 set it to the last item.
			newItemId = $('#' + ContainerId + ' ._Item').length;
		}
		var newItem = $('#' + ContainerId + ' ._Item[data-pos="' + newItemId + '"]');
		current = newItemId;
		newItem.css({ 'left': '-' + Distance + 'px' }); //push the new item to stage right ready to enter.
		currentItem.animate({				//move current off to stage right
			left: Distance + "px"
		}, AnimationTime, null, null);
		newItem.animate({					//move the new item to be on the stage
			left: "0px"
		}, AnimationTime, null, function () { LockUserActions = false; UpdateLink(newItem); });
	};

	var RotateTo = function (itemNumber) {
		if (current == itemNumber)
			return;
		//console.log(current);
		var previous = current;
		current = itemNumber;
		var newItem = $('#' + ContainerId + ' ._Item[data-pos="' + current + '"]');
		UpdateLink(newItem);
		newItem.animate({
			left: "0px"
		}, AnimationTime, null, MoveBack(previous));
	};

	var UpdateLink = function (obj) {
		var Link = $('#' + ContainerId + ' ._overlay');
		var LinkTo = obj.attr('href');
		if (LinkTo.indexOf('#fBoxWrapper') != -1) {
			Link.addClass('fbOpen');
		} else {
			Link.removeClass('fbOpen');
		}
		Link.attr('href', LinkTo);
		$('#' + ContainerId + ' ._goToButton').removeClass('on');
		$('#' + ContainerId + ' ._goToButton[data-pos=' + current + ']').addClass('on');
		$('#player').attr('href', obj.attr('data-video'));
	};

	return {
		Init: function (containerId, distance, animationTime, animationInterval) {
			if (containerId == null || containerId == "") {
				alert('no container supplied');
				return false;
			}
			ContainerId = containerId;
			Distance = distance;
			AnimationTime = animationTime;
			AnimationInterval = animationInterval;

			//move all items accross off the stage.
			$('#' + ContainerId + ' ._Item').css({ 'left': Distance + 'px' });

			//set the first item to the stage.
			var firstItem = $('#' + ContainerId + ' ._Item:first');
			firstItem.css({ 'left': '0px' });
			UpdateLink(firstItem);

			//start the rotation if we have more then one element
			if ($('#' + ContainerId + ' a._Item').length > 1) {
				CMQ0001CarouselHome.StartRotation();
				$('#' + ContainerId + ' ._moveLeft').click(function () {
					CMQ0001CarouselHome.MovePrevious();
				});
				$('#' + ContainerId + ' ._moveRight').click(function () {
					CMQ0001CarouselHome.MoveNext();
				});
				$('#' + ContainerId + ' ._goToButton').click(function () {
					if (!LockUserActions) {
						CMQ0001CarouselHome.StopRotation();
						RotateTo($(this).attr('data-pos'));
						CMQ0001CarouselHome.StartRotation();
					}
				});
			} else {

				$('#' + ContainerId + ' ._goToButton').hide();
			}
			$('#' + ContainerId + ' ._moveLeft').hide();
			$('#' + ContainerId + ' ._moveRight').hide();
		},

		StopRotation: function () {
			clearInterval(myInterval);
		},

		StartRotation: function () {
			myInterval = setInterval(function () { Rotate(++current); }, AnimationInterval);
		},

		MoveNext: function () {
			if (!LockUserActions) {
				CMQ0001CarouselHome.StopRotation();
				Rotate(++current);
				CMQ0001CarouselHome.StartRotation();
			}
		},

		MovePrevious: function () {
			if (!LockUserActions) {
				CMQ0001CarouselHome.StopRotation();
				GoBackOne(current);
				CMQ0001CarouselHome.StartRotation();
			}
		}
	}
})(jQuery);
