/**
 * @author aprian@gmail.com
 */
function doCarousel(){
	
	// activate thumb on start
	getActiveThumb();
	
	// next & prev on start
	var elActive = $("#thumb_nav").find("a.active").attr("href");
	doPrevNext($(elActive));
	
	// highlite current thumbnail on click
	$("#thumb_nav a").click( function(){
		$("#thumb_nav a.active").removeClass('active');
		$(this).toggleClass('active');	
	} );
	
	
	// scrolling by thumbnail
	$('#thumb_nav ul').localScroll({
		target: '.works_images',
		duration: 1000,
		hash: true,
		onAfter: function(e, anchor, $target){
			
			// next & prev after scrolling
			doPrevNext(e);
			
		}
	});
	
	// scrolling by prev next
	$('#thumb_nav p').localScroll({
		target: '.works_images',
		duration: 1000,
		hash: true,
		onBefore: function(e, anchor, $target){
			
			//highlite thumbnail
			var elActive = $(anchor);
			var idActive = elActive.attr('id');
			$("#thumb_nav a.active").removeClass('active');
			$("a#image_" + idActive).addClass('active');	
		},
		onAfter: function(e, anchor, $target){
			
			// next & prev after scrolling
			doPrevNext(e);
			
		}
	});
}

function doPrevNext(e) {
	
	// next & prev after scrolling
	var elActive = $(e);
	var elPrev = elActive.prev();
	var elNext = elActive.next();
	var idActive = elActive.attr('id');
	var idPrev = elPrev.attr('id') || idActive;
	var idNext = elNext.attr('id') || idActive;
	
	$("a.prev").attr("href", '#' + idPrev);
	$("a.next").attr("href", '#' + idNext);
}

function getActiveThumb(){
	
	var hash = window.location.hash;
	
	if (hash != '') {
		hash = hash.substring(1);
		$("#image_" + hash).addClass('active');
	} else {
		$("div#thumb_nav li:eq(0) a").addClass('active');
	}
	
}

$(document).ready( function(){
	
	// centered thumb nav
	var offset = 8;
	var wDiv = $("#thumb_nav").outerWidth();
	var wUl = $("#thumb_nav ul").outerWidth();
	m = ( (wDiv - wUl) / 2 ) + offset;
	$("#thumb_nav ul").css("margin-left",m);
	
	// carousel
	doCarousel();
	
} );


