// Init courousel
$(function() {
					   
		// set carousel prefs
		$("#portfolioItems").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			visible: 4,
			scroll: 4,
			speed: 600,
			easing: 'easeOutCirc',
			circular: true
			
		});
		
		
	});
	

// Init on load
$(document).ready(function() {
	$('#li_5').addClass('current');
	// Replace portfolio spider friendly links with AJAX friendly ones
	$("#portfolioItems ul li a").each(function() {
		this.hrefOld = this.href;
		this.href = this.hash;
		
	});

	// content state array
	$contentState = new Array();
	
	// Slow connection?
	$slowConnection = false;
	
	// show first portfolio item
	$currentItem = $("#portfolioItemFirst")
	$currentItem.css({'display' : 'block'});
	$('#portfolio').slideDown({ duration: 1200, easing: 'easeOutBounce'} );

	// Fix
	$("#footerWrapper").css({'z-index' : '2', 'position' : 'relative'});
	
	// open portfolio item on click on thumbnail
	$("#portfolioItems ul li a").click(function () { 
		
			$newItem = this.hash;

			$id = this.id.replace('pflink','');
			
			// Switch portfolio in timeout, to prevent hickups
			setTimeout( function() { portfolioSwitch($newItem); },1);
		
			// Set pagetitle
			var sTitle = oPortfolioTitles[$id];
			$(document).attr({title: sTitle});
			
			// Set content in DOM using AJAX?
			if(!$contentState[$newItem]) {
			
				
				$url = LINKROOT+'/view/portfolio/ajax/'+$id;
				
			
				// Build AJAX request					
				$($newItem).load($url, function() {
					// Set state on finish
					$contentState[$newItem] = 1;
				});
									
			} 

			// I like stats
			portfolioAnalytics(this.hrefOld);
			
	});
});
	
	

// Portfolio switch function
function portfolioSwitch($newItem) {

	// Define speed for fold out
	if($slowConnection) {
		$speedOut = 1600;
		$speedIn = 1200;
	} else {
		$speedOut = 1200;
		$speedIn = 800;
	}

	// close portfolio block
	
	$('#portfolio').slideUp({ duration: $speedIn, easing: 'easeOutCubic', complete: function() { 
		
		
		// hide current portfolio item	
		$currentItem.css({'display' : 'none'});
		// show new portfolio item
		$($newItem).css({'display' : 'block'});
		// open portfolio block
		$currentItem = $($newItem);

		
		
		if(!$contentState[$newItem]) {
			// Ajax is behind on animation, slow speed connection detected
			$slowConnection = true; 
			$speedOut = 2000;
		} 
		setTimeout( function () {
			
			$('#portfolio').slideDown({ duration: $speedOut, easing: 'easeOutBounce'});
		},1);
		
		
		
		
	}});
	
	
}


function portfolioAnalytics(that) {
	// todo: Use this to log portfolio views to analytics API
}

