itemClickHandler = function(event){
	var target = $(event.target);
	if (!target.hasClass('.client_item')){
		target = target.closest('.client_item');
	}

	if (target.index() == $('#client_frame').ysp('state','currentItem')){
		var url = target.children('.client-link').attr('href');
		window.location = url;
	}else{
		if (target.index() > $('#client_frame').ysp('state','currentItem')){
			$('#client_frame').ysp('next');
		}else{
			$('#client_frame').ysp('previous');
		}

	}
}

var slideHandler = function(data,fromClick){
	$this = data.items.eq(data.currentItem);
	$(data.pane).children('.current').removeClass('current').children('.client-hover').not($this.children()).stop(true,true).fadeIn('fast');

	$this.addClass('current');//.children('.client-hover').stop(true,true).fadeOut('fast');

	if ( firstLoad !== true ){
		//track navigation event
		var label = fromClick ? 'home-click-slider' : 'home-drag-slider'
		_gaq.push(['_trackEvent', $this.attr('id'), 'Navigate', label]);
	}else{
		firstLoad = false;
	}

	if ( data.currentItem == 0 ){
		$('#left_arrow').css('visibility','hidden');
	}else{
		$('#left_arrow').css('visibility','visible');
	}

	if ( (data.currentItem + 1) == data.itemCount ){
		$('#right_arrow').css('visibility','hidden');
	}else{
		$('#right_arrow').css('visibility','visible');
	}

}

var firstLoad = true; //set to prevent event tracking on the initial slide

$(document).ready(function(){

	var startPosition = 1;

	$('.client_item').eq(startPosition).children('.client-hover').fadeOut(5);

	$('#client_frame').ysp({
		paneSelector : '#client_pane',
		itemSelector : '.client_item',
		controlsSelector : false,
		showItemCount : false,
		clickFunction : itemClickHandler,
		positionOffset : '30px',
		intentRatio : .2,
		initialItem : startPosition,
		slideCallback : slideHandler,
		easing : 'easeOutSine'
	});


	$('#left_arrow').click(function(){
		$('#client_frame').ysp('previous');
	});

	$('#right_arrow').click(function(){
		$('#client_frame').ysp('next');
	});

	$('.nav_arrow').fadeTo(1,0).hover(
		function(){
			$('.nav_arrow').stop(true,false).fadeTo(150,.75);
		},
		function(){
			$('.nav_arrow').stop(true,false).fadeTo(150,.5);
		}
	);
	$("#client_pane").mouseenter(
		function(){
			$('.nav_arrow').stop(true,false).fadeTo(150,.5);
		});
	$("#client_pane").mouseleave(function(){
			$('.nav_arrow').stop(true,false).fadeTo(150,0);
		}
	);

	$('.client-meta').css('visibility','hidden');
	//$('#client_pane').animate({'left':'-=180px'},1)

	$('.client_item').mouseenter(function(event){
		$this = $(this);
		if ($this.index() == $('#client_frame').ysp('state','currentItem')){
			$this.children('.client-hover').stop(true,true).fadeIn('fast');
		}else{
			$this.children('.client-hover').stop(true,true).fadeOut('fast');
		}

	});
	$('.client_item').mouseleave(function(event){
		$this = $(this);
		if ($this.index() == $('#client_frame').ysp('state','currentItem')){
			$this.children('.client-hover').stop(true,true).fadeOut('fast');
		}else{
			$this.children('.client-hover').stop(true,true).fadeIn('fast');
		}
	});

	$('.press-item a','#press-list').click(function(){
		trackVirtualPageview($(this).attr('href'));
	});

	$('#client_bar a').click(function(event){
		var pathParts = $(event.currentTarget).attr('href').split("/"),
			client = pathParts[pathParts.length - 1];

		_gaq.push(['_trackEvent', client, 'Navigate', "home-click-bar"]);

	});
});

var pressInterval = false;
var mouseIsOverPress = false;
var pressScrolled = false;

//scrolling press

function scrollPress(){
	if ( !mouseIsOverPress && !pressScrolled){
		var	pressList = $("#press-list"),
			pressListContainer = $('#press-list-container'),
			lastPos = pressList.children().last().offset();
			maxScroll = lastPos.top + pressList.children().last().height() - pressListContainer.height();
		if ( pressList.scrollTop() < maxScroll ){
			//bottom not reached
			pressList.animate({scrollTop:pressList.scrollTop() + 97},1000);
		}else{
			//at bottom
			pressList.animate({scrollTop:0},1000);
		}

	}
}

function pressMouseWheel(event){
	clearInterval(pressInterval);
	pressScrolled = true;
}

$(window).load(function(){


	//$('#client_pane').delay(500).animate({'left':'+=180px'},250,function(){$('.client-meta').css('visibility','visible');});

	$('.client-meta').css('visibility','visible');

	/*
	$('.client_item').eq($('#client_frame').ysp('state','currentItem') - 1).children('.client-hover').delay(250).fadeOut('fast').delay(250).fadeIn('fast').delay(250).fadeOut('fast').delay(250).fadeIn('fast');
	$('.client_item').eq($('#client_frame').ysp('state','currentItem') + 1).children('.client-hover').delay(250).fadeOut('fast').delay(250).fadeIn('fast').delay(250).fadeOut('fast').delay(250).fadeIn('fast');
	*/

	//start scrolling
	pressInterval = setInterval(scrollPress, 6000);

	//mouse interaction for scrolling pres
	$("#press-list").hover(
		function(){
			mouseIsOverPress = true;
			clearInterval(pressInterval);
		},
		function(){
			mouseIsOverPress = false;
			pressInterval = setInterval(scrollPress, 6000);
		}
	);
	$("#press-list").bind('mousewheel',pressMouseWheel);
	$("#press-list").bind('onmousewheel',pressMouseWheel);
	$("#press-list").bind('DOMMouseScroll',pressMouseWheel);


});

