var automatic = true,
pics_o  = $(".slider").children("div.imgContainer").children(".image").find("img").size(),
index_o = $(".slider").children("div.imgContainer").children(".active").prevAll().length+1,
next_o  = $(".slider").children("div.imgContainer").children(".active").prevAll().length+2,
prev_o  = pics_o;

function lazySwap(path) {
	$(path).attr("src", $(path).attr("original"));
}

function nextSlide(){
	$(".slider").children("div.imgContainer").children(".active").removeClass("active").fadeOut(function() {
		lazySwap(".imgContainer .image:nth-child("+next_o+") img");
		lazySwap(".imgContainer .image:nth-child("+(next_o+1)+") img");
		$(".slider").children("div.imgContainer").children(".image:nth-child("+next_o+")").addClass("active").fadeIn(function() {
			index_o ++;
			next_o ++;
			prev_o ++;
			if (index_o == pics_o) {
				next_o = 1;
			}
			if (index_o > pics_o) {
				index_o = 1;
			}
			if (prev_o > pics_o) {
				prev_o = 1;
			}
			scrollToActive();
		});
	});
	$("#picNav img").css("opacity", "1").removeClass("active");
	$("#picNav img:nth-child("+next_o+")").css("opacity", ".3").addClass("active");
};

function prevSlide(){
	$(".slider").children("div.imgContainer").children(".active").removeClass("active").fadeOut(function() {
		lazySwap(".imgContainer .image:nth-child("+prev_o+") img");
		lazySwap(".imgContainer .image:nth-child("+(prev_o-1)+") img");
		$(".slider").children("div.imgContainer").children(".image:nth-child("+prev_o+")").addClass("active").fadeIn(function() {
			index_o --;
			prev_o --;
			next_o --;
			if (prev_o == 0) {
				prev_o = pics_o;
			};
			if (index_o == 0) {
				index_o = pics_o;
			};
			if (next_o == 0) {
				next_o = pics_o;
			};
			scrollToActive();
		});
	});
	$("#picNav img").css("opacity", "1").removeClass("active");
	$("#picNav img:nth-child("+prev_o+")").css("opacity", ".3").addClass("active");
};

function directSlide(path) {
	index_o = $(path).prevAll().length+1;
	if (index_o == pics_o) {
		next_o  = 1;
	} else {
		next_o  = index_o+1;
	}
	if(index_o == 1) {
		prev_o  = pics_o;
	} else {
		prev_o = index_o-1;
	}
	$(".slider").children("div.imgContainer").children(".image:not(:nth-child("+index_o+"))").removeClass("active").fadeOut(function() {
		$(".slider").children("div.imgContainer").children(".image:nth-child("+index_o+")").addClass("active").fadeIn();
		lazySwap(".imgContainer .image:nth-child("+index_o+") img");
		lazySwap(".imgContainer .image:nth-child("+(index_o+1)+") img");
	});
}

function autoAdvance(){
	if(automatic) {
		nextSlide();
		window.setTimeout('autoAdvance()', 2000);
	}
};

function scrollToActive() {
	$("#picNav").scrollTo($("#picNav div").children(".active"), 500);
}

$(document).ready(function(){

/*INITIALS*/

	var totalPic = $("div.imgContainer").children(".image").find("img").size();
	var picWidth = 750;
	var totalPicWidth = totalPic * picWidth;
	if (totalPic === 1) {
		$(".slideNav a.rightSlideNav, .slideNav a.leftSlideNav").hide();
	}
	$("#picNav img:first").css("opacity", ".3").addClass("active");
	$(".imgContainer div:first").addClass("active");
	$("div.imgContainer .image:not(:first)").hide();

/*AUTO SLIDER*/

	var curl = document.URL;
	if(automatic) {
		if (curl === "http://www.raphaeljust.com/" || curl === "http://raphaeljust.com/") {
			window.setTimeout('autoAdvance()', 2000);
		} else {
			automatic = false;
		}
	};

/*SLIDE VIA BUTTON*/

	$(".slideNav a.rightSlideNav").click(function() {
		nextSlide();
	});
	$(".slideNav a.leftSlideNav").click(function() {
		prevSlide();
	});

/*SLIDE VIA MINI PIC*/

	$("#picNav img").click(function() {
		directSlide($(this));
		$("#picNav img").css("opacity", "1").removeClass("active");
		$(this).css("opacity", ".3").addClass("active");
		scrollToActive();
	});

});
