﻿var currentIndex = 0;
var maxFeaturedClients = 4;
var animationDelay = 125;
var clientAnchors;

var moveRight = false;

var leftArrow;
var rightArrow;

var lastMouseXPosition = -1;
var hiddenOpacity = 0.0;
var showOpacity = 0.7;
var fadeSpeed = "fast";

$(document).ready(initFeaturedClients);

function initFeaturedClients()
{
	clientAnchors = $(".featuredClientsHolder .clientsLogo");
	leftArrow = $("#homepageClientSelector_Left");
	rightArrow = $("#homepageClientSelector_Right");

	$(".featuredClientsHolder").mousemove(featuredClientMouseMove);
	$(".featuredClientsHolder").mouseover(featuredClientMouseOver);
	$(".featuredClientsHolder").mouseleave(featuredClientMouseOut);

	leftArrow.click(handle_moveLeftClick);
	rightArrow.click(handle_moveRightClick);
	hideRightArrow();
	hideLeftArrow();	
}

function featuredClientMouseOut()
{
	lastMouseXPosition = -1;
	
	hideRightArrow();
	hideLeftArrow();
}

function featuredClientMouseOver(e)
{
	featuredClientMouseMove(e);
}

function featuredClientMouseMove(e)
{
	var mouseXPos = e.pageX ;
    var containerXPos = $(".wrapperWithDivider").position().left;
	var containerHalfWidth =  $(".wrapperWithDivider").width() / 2;
	
	var onLeft = false;
	if(lastMouseXPosition < containerHalfWidth)
	{
		onLeft = true;
		showLeftArrow();
	}

	lastMouseXPosition = mouseXPos - containerXPos;
	if(lastMouseXPosition < containerHalfWidth)
	{
		if(onLeft)
		{
			showLeftArrow();
		}
	}
	else
	{
		if(!onLeft)
		{
			showRightArrow();
		}
	}
}

function hideLeftArrow()
{
	leftArrow.stop();
	if(currentIndex > 0)
	{
		leftArrow.fadeTo(fadeSpeed, hiddenOpacity);
	}
	else
	{	
		leftArrow.fadeTo(fadeSpeed, 0.0);
	}
}

function hideRightArrow()
{
	rightArrow.stop();
	if((currentIndex+maxFeaturedClients) < (clientAnchors.size() - 4))
	{
		rightArrow.fadeTo(fadeSpeed, hiddenOpacity);
	}
	else
	{
		rightArrow.fadeTo(fadeSpeed, 0.0);
	}	
}

function showLeftArrow()
{
	hideRightArrow();
	
	leftArrow.stop();
	
	if(currentIndex > 0)
	{
		leftArrow.fadeTo(fadeSpeed, showOpacity);
	}
}

function showRightArrow()
{

	hideLeftArrow();
	
	rightArrow.stop();
	var adjustedIndex = currentIndex+maxFeaturedClients;
	var topSize = (clientAnchors.size() - 0);
	
	if(adjustedIndex < topSize)
	{
		rightArrow.fadeTo(fadeSpeed, showOpacity);
	}
}

function handle_moveLeftClick()
{
	if(currentIndex > 0 && ($(".featuredClientsHolder :animated.clientsLogo").length == 0))
	{
		moveRight = false;
		currentIndex--;
		clientAnchors.each(slideClient);
	}
}

function handle_moveRightClick()
{
	if((currentIndex+maxFeaturedClients) < clientAnchors.length && ($(".featuredClientsHolder :animated.clientsLogo").length == 0))
	{
		moveRight = true;
		currentIndex++;
		clientAnchors.each(slideClient);
	}
}

function slideClient()
{
	var positionIndex = $(this).attr("ClientIndex") - currentIndex;
	var duration = 0;
	if(positionIndex >= -1 && positionIndex <= maxFeaturedClients)
	{
		if(!moveRight)
		{
			positionIndex = maxFeaturedClients - positionIndex;
		}
		duration = (positionIndex + 1) * animationDelay;
	}
	$(this).animate({opacity:1.0}, duration, "linear", delayComplete);
}

function delayComplete()
{ 
	var animationValues = new Object();
	var positionIndex = $(this).attr("ClientIndex") - currentIndex;
	animationValues.left = positionIndex * 252;
		
	var animationParams = new Object();
	animationParams.duration = "normal";
	
	$(this).animate(animationValues, animationParams);	
}