// Edit these variables to change the speed of animations in the carousel
// All Values are in milliseconds - so 1000 == 1 second
var fadeDelay = 400; // fade animation time when showing/hiding thumbnails/slideshow
var slideSpeed = 1200; // Speed of sliding animation used in carousel slideshow
var slideDelay = 0; // Optional delay time between each slide in the slideshow (increases spacing between items)

// Do not edit below here
var carouselWrap;
var carouselSlideshow;
var carouselThumbs;
var currentView;
var currentCatLink;

function initCarousel(start){
	start = start || 0;
	$('#carouselwrap').CFSCarousel({slides:'.slide',start:start,speed:slideSpeed,delay:slideDelay});
}

function loadCarousel(){
	carouselWrap = $('#carouselwrap').get(0);
	if(!carouselWrap) return; // were not on portfolio page
	carouselSlideshow = $('#carouselwrap div.slideshow').get(0);
	carouselThumbs = $('#carouselwrap div.carouselthumbs').get(0);	
	currentCatLink = $('#portfolio_nav li.active a').get(0);
	if(carouselSlideshow)	currentView = carouselSlideshow;
	// add thumbnails or slideshow divs depending on ititial view
	addCarouselElements();
	if(!currentView) currentView = carouselThumbs;
	// preload slides for current category
	loadCarouselSlides(true);
	// bind click events
	bindPortfolioEvents();
}

function addCarouselElements(){
	if(!carouselSlideshow){
		carouselSlideshow = $('<div class="slideshow"/>').appendTo(carouselWrap).get(0);
		$('<div class="carousel"/>').appendTo(carouselSlideshow);
		$('<a class="prev" title="previous" />').appendTo(carouselSlideshow);
		$('<a class="next" title="next" />').appendTo(carouselSlideshow);
		$(carouselSlideshow).hide();
	}
	if(!carouselThumbs){
		loadContent($('#portfolio_nav li.active a').attr('href'), function(data){
			$(data).appendTo(carouselWrap);
			carouselThumbs = $('#carouselwrap div.carouselthumbs').get(0);
			$(carouselThumbs).hide();
		});
	}
}

function bindPortfolioEvents(){
	// Bind thumbnail link events
	var links = $('#carouselwrap div.thumbnail a');
	$('#carouselwrap div.thumbnail a').live('click', function(){
		var link = this;
		if(window.pageTracker) window.pageTracker._trackPageview(this.href);
		$('#carouselwrap div.thumbnail a').each(function(i,obj){
			if(obj == link){ showProjectSlideshow(i); return false; }
		});
		return false;
	});
	// Bind PortFolio Category link events
	$('#portfolio_nav a').live('click', function(){
		if(currentCatLink == this){
			if(currentView == carouselSlideshow){
				$(currentView).fadeOut(fadeDelay, function(){ $(carouselThumbs).fadeIn(fadeDelay) } );
			}
			return false;
		}else{
			currentCatLink = this;
			$('#portfolio_nav li').removeClass('active');
			$(this).parent('li').addClass('active');
		}
		var url = this.href;
		if(window.pageTracker) window.pageTracker._trackPageview(this.href);
		$(currentView).fadeOut(fadeDelay,function(){
			loadContent(url, updateThumbnails);
			loadCarouselSlides();
		});
		return false;
	});
}

function loadContent(url, callback, extravars){
	extravars = extravars? '&'+extravars : '';
	url = url.match(/\/?/)? url+'&nocache'+extravars : '?nocache'+extravars;
	$.get(url, callback);
}

function loadCarouselSlides(firstload){
	if(firstload && currentView == carouselSlideshow){
		loadContent( window.location.toString(), updateSlideshow);
	}else{
		loadContent( currentCatLink.href.toString(), refreshSlideshow, 'all=true');
	}
}

function updateThumbnails(data){
	$('#carouselwrap div.carouselthumbs').replaceWith($(data));
	carouselThumbs = $('#carouselwrap div.carouselthumbs').hide().fadeIn(fadeDelay).get(0);
	currentView = carouselThumbs;
}

function updateSlideshow(data){
	$('div.carousel', carouselSlideshow).append(data);
	if(currentView == carouselSlideshow) initCarousel();
}

function refreshSlideshow(data){
	$('div.carousel', carouselSlideshow).html(data);
	if(currentView == carouselSlideshow) initCarousel();
}

function showProjectSlideshow(i){
	$(carouselThumbs).fadeOut(fadeDelay, function(){
		$('div.carousel div.slide').css('left', '100%');
		$(carouselSlideshow).fadeIn(fadeDelay, function(){ initCarousel(i); });
		currentView = carouselSlideshow;
	});
}
// on dom ready load carousel
$(function(){
	loadCarousel();
	// unbind all events
	$(window).unload(function(){ $('a').unbind();	});
});