﻿var itemCount = 0;
var activeItem = 0;
	
$(document).ready(function(){
	
	$("#folioLinks a").each(
		
		function (index) {
			$(this).click(function(event){
				$("#folioLinks a").removeClass("on");
				$(this).addClass("on");
				$(".folioItems").each(
					function (index2) {
						if (index != index2) {
							$(".col123 h3:eq("+index2+")").fadeOut(800)
							$(this).fadeOut(800,function(){$(this).css({position:"absolute",top:"55px",left:"0"})});
						}
					}
				);
										
				$(".folioItems:eq("+index+")").fadeIn(800)
				$(".col123 h3:eq("+index+")").fadeIn(800)

				initActivePF(index);

				return false;
			});	
		}
	);
	
	$(".col123 h3:gt(0)").hide();
	$(".folioItems:gt(0)").hide();
	$("#folioNav div").css({backgroundPosition:'top left'});
	$(".folioPanel").css({height:'290px', overflow:'hidden'});
	$("#portfolio h3").css({position:'absolute', top:'0', left:'0'});
	$(".folioItems").css({position:"absolute",top:"55px",left:"0"});
		
});

function showItem(index) {
	var el = $('.folioItems:visible');
	var leftVal = -(index*900);
	var leftVal2 = (index*30)+"px";
	el.animate({left: leftVal},{ "easing": "easeInOutCirc"}, {"duration": 1500});
	$("#folioNav div").animate({backgroundPosition: "'(" + leftVal2 + " 0)'"},{ "easing": "easeInOutCirc"}, {"duration": 1500});
	activeItem = index;
}

function initActivePF(index) {
	var pf = $(".folioItems:eq("+index+")");
	itemCount = pf.find(".summary").length;
	buildNav();
	$(".folioItems").css({"width" : (itemCount * 900) + "px"});
	activeItem = 0;
}

function buildNav() {
	var div = $('#folioNav');
	$(div).children().remove();
	$(div).append("<a href='#prev' class='prev' onclick='movePrev()'>&nbsp;</a>");
	var pageLinks = document.createElement("div");
	for(var i=1; i<=itemCount; i++) {
		$(pageLinks).append("<a href='#item' class='page' onclick='showItem(" + (i-1) + ")'></a>");
	}
	$(div).append(pageLinks);
	$(div).append("<a href='#next' class='next' onclick='moveNext()'>&nbsp;</a>")
}

function movePrev() {
	activeItem --;
	if (activeItem >= 0) {
		showItem(activeItem);
	}
	else {
		activeItem = 0;
	}
	return false;		
}		
function moveNext() {
	activeItem ++;
	if (activeItem < itemCount) {
		showItem(activeItem);
	}
	else {
		activeItem = itemCount;
	}				
	return false;		
}