/*
 * This method needs the file jquery-1.3.2-patched.js patched to fix animations in FireFox 3 and Opera, download from http://dev.jquery.com/ticket/4218
 */

var $animation=jQuery.noConflict();

var cardsGallery = 'div#cardGallery ul';
var cardGalleryArea = 'div#cardGallery';
var cardGalleryCloneAreaID = "cardGalleryClone";
var cardsGalleryClone = 'div#' + cardGalleryCloneAreaID + ' ul';
var cardGalleryCloneArea = 'div#' + cardGalleryCloneAreaID;
var navigationButtons = 'div.cardsHolder div.navigationButtons input';
var previousNavigationButton = 'div.cardsHolder div.navigationButtons input.previous';
var nextNavigationButton = 'div.cardsHolder div.navigationButtons input.next';
var moveTime = 1000;

/**
 * Clones Card's Gallery to get ready for card's animation. if forwardDirection = true, moves to the right, if forwardDirection = false moves to the left.
 * @param forwardDirection
 * @return
 */
function cardsAnimationCloning(forwardDirection) {
		
	// If animation is still running, returns
	if(checkAnimationRunning()) {
		return;
	}

	// Checks the animation direction
	if(forwardDirection) {
		shift = - $animation(cardGalleryArea).width();
	} else {
		shift = $animation(cardGalleryArea).width();
	}

	// Disable all navigation buttons
	disableNavigationButtons();

	// Clones the cardGalleryArea.
	var clone = $animation(cardGalleryArea).clone(true).appendTo($animation(cardGalleryArea).parent());
	clone.attr("id", cardGalleryCloneAreaID);

	// Changes clone position to the same than cardGalleryArea.
	$animation(cardGalleryCloneArea).css('position', 'absolute');
	$animation(cardGalleryCloneArea).css('top', '0');
	
	// Moves the cardsGallery to the start animation position, off view.
	$animation(cardsGallery).css('left', shift);
}

/**
 * Starts cards animation
 * @param forwardDirection
 * @return
 */
function startCardsAnimation(forwardDirection) {
	// If animation is still running, returns
	if(checkAnimationRunning()) {
		return;
	}
	 
	if(forwardDirection) {
		movePixels = '+='+$animation(cardGalleryArea).width() + 'px';
		shift = - $animation(cardGalleryArea).width();
	} else {
		movePixels = '-='+$animation(cardGalleryArea).width() + 'px';
		shift = $animation(cardGalleryArea).width();
	}
	
	// Disable all navigation buttons
	disableNavigationButtons();

	// Moves the cardsGallery to the start animation position, off view.
	$animation(cardsGallery).css('left', shift);
	
	// Starts the card's animation
	moveCards($animation(cardsGallery), movePixels, moveTime, null);
	moveCards($animation(cardsGalleryClone), movePixels, moveTime, function(){ enableNavigationButtons(); removesCardGalleryCloneArea(); });
}

/**
 * If cardGalleryCloneArea exists, removes it.
 * @return
 */
function removesCardGalleryCloneArea() {
	if(undefined != $animation('div#' + cardGalleryCloneAreaID)) {
		$animation('div#' + cardGalleryCloneAreaID).remove();
	}
}

// this method is user when the card's animation starts and, to disable the navigation buttons because of the 
// loading that is done to reload the cards when the user press the back button in browser.
function disableNavigationButtons() {
	$animation(navigationButtons).attr("disabled", true);
}

function enableNavigationButtons() {
	$animation(navigationButtons).removeAttr('disabled');
}
 
/**
 * Checks if the animation is already running
 * @return
 */
function checkAnimationRunning() {
	if($animation(cardsGalleryClone).is(':animated') || $animation(cardsGallery).is(':animated') ) {
		return true;
	} else
		return false;
}


/**
 * Starts the cards animation.
 * @param obj object to be animated
 * @param px number of pixeis to animate
 * @param time animation speed
 * @param nextAction action to be performed after this animation
 * @return
 */
function moveCards(obj, px, time, nextAction) {
	
	if(undefined != obj) {
		if(undefined != nextAction) {
			obj.animate({'left': px}, time, nextAction);
		} else {
			obj.animate({'left': px}, time);
		}
	}
}

function showOrHideNavigationButtons(showPreviousButton, showNextButton) {
	if(showPreviousButton) {
		$animation(previousNavigationButton).removeClass('hide');
	} else {
		$animation(previousNavigationButton).addClass('hide');
	}
	
	if(showNextButton) {
		$animation(nextNavigationButton).removeClass('hide');
	} else {
		$animation(nextNavigationButton).addClass('hide');
	}
}
