/* ------
This script initializes the work teaser carousels on work.html and index.html, both full-width
and half-width, and implements the hovering functionality on the info modules contained therein.
------ */

function advanceCounter(state, carouselDiv) {
	var prevEnabled = false;
	switch(carouselDiv.attr("id")) {
		case "fullWidthCarousel":
			prevEnabled = fullEnabled;
		break;
		case "firstHalfCarousel":
			prevEnabled = firstEnabled;
		break;
		case "secondHalfCarousel":
			prevEnabled = secondEnabled;
		break;
	}
	if(state === "next") {
		carouselDiv.siblings(".dotCounter").children("img#current").attr("src","/img/lightdot.png");
		if(carouselDiv.siblings(".dotCounter").children("img#current").is(":last-child")) {
			carouselDiv.siblings(".dotCounter").children("img:first").attr("src","/img/darkdot.png");
			carouselDiv.siblings(".dotCounter").children("img:first").attr("id","next");
		} else {
			carouselDiv.siblings(".dotCounter").children("img#current").next().attr("src","/img/darkdot.png");
			carouselDiv.siblings(".dotCounter").children("img#current").next().attr("id","next");
		}
		carouselDiv.siblings(".dotCounter").children("img#current").removeAttr("id");
		carouselDiv.siblings(".dotCounter").children("img#next").attr("id", "current");
	} else if(state === "prev" && prevEnabled) {
		carouselDiv.siblings(".dotCounter").children("img#current").attr("src","/img/lightdot.png");
		carouselDiv.siblings(".dotCounter").children("img#current").prev().attr("src","/img/darkdot.png");
		carouselDiv.siblings(".dotCounter").children("img#current").prev().attr("id","next");
		carouselDiv.siblings(".dotCounter").children("img#current").removeAttr("id");
		carouselDiv.siblings(".dotCounter").children("img#next").attr("id", "current");
	}
};

function tinyInit(carousel, carouselDiv) {
	carouselDiv.siblings(".dotCounter").children("img:first").attr("src","/img/darkdot.png");
	carouselDiv.siblings(".dotCounter").children("img:first").attr("id","current");
	carouselDiv.siblings(".dotCounter").children("img").bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval($(this).attr("class")));
		$(this).siblings().attr("src","/img/lightdot.png");
		$(this).siblings().removeAttr("id");
		$(this).attr("id","current");
		$(this).attr("src","/img/darkdot.png");
	return false;
	});
	carouselDiv.parent().siblings(".jcarousel-next").bind('click', function() {
		carousel.next();
		advanceCounter("next",carouselDiv);
		return false;
	});
	carouselDiv.parent().siblings(".jcarousel-prev").bind('click', function() {
		carousel.prev();
		advanceCounter("prev",carouselDiv);
		return false;
	});
};

function prevCallback(carousel, button, enabled, carouselDiv) {
		switch(carouselDiv) {
			case "fullWidthCarousel":
				fullEnabled = enabled;
			break;
			case "firstHalfCarousel":
				firstEnabled = enabled;
			break;
			case "secondHalfCarousel":
				secondEnabled = enabled;
			break;
		}
};

$(document).ready(function() {
	var fullEnabled = false;
	var firstEnabled = false;
	var secondEnabled = false;

	// Preload the infomodule shadow image.
	if (document.images) {
		preloadoverbox = new Image();
		preloadoverbox.src = "/img/over-box.png";
	}

	// Initialize all work carousels.
	$("#fullWidthCarousel").jcarousel({
		scroll:4,
		animation:600,
		wrap:'last',
		initCallback: function (carousel) {tinyInit(carousel, $("#fullWidthCarousel"))},
		buttonPrevCallback: function (carousel, button, enabled) {prevCallback(carousel, button, enabled, "fullWidthCarousel")}
	});

	$("#firstHalfCarousel").jcarousel({
		scroll:2,
		animation:400,
		wrap:'last',
		initCallback: function (carousel) {tinyInit(carousel, $("#firstHalfCarousel"))},
		buttonPrevCallback: function (carousel, button, enabled) {prevCallback(carousel, button, enabled, "firstHalfCarousel")}
	});

	$("#secondHalfCarousel").jcarousel({
		scroll:2,
		animation:400,
		wrap:'last',
		initCallback: function (carousel) {tinyInit(carousel, $("#secondHalfCarousel"))},
		buttonPrevCallback: function (carousel, button, enabled) {prevCallback(carousel, button, enabled, "secondHalfCarousel")}
	});

	// Implement info module onHover functionality on all carousels with work content.
	$(".contentCarousel li").hover(
		function() {
			$(this).css("background-image","url(/img/over-box.png)");
			$(this).children(".smallTeaserWrapper").children(".projectDesc").css("display","block");
		},
		function() {
			$(this).css("background-image","none");
			$(this).children(".smallTeaserWrapper").children(".projectDesc").css("display","none");
		}
	);
	
	// Implement info module onHover functionality on all carousels with work content.
	$(".contentCarousel2 li").hover(
		function() {
			$(this).css("background-image","url(/img/over-box.png)");
			$(this).children(".smallTeaserWrapper").children(".projectDesc").css("display","block");
		},
		function() {
			$(this).css("background-image","none");
			$(this).children(".smallTeaserWrapper").children(".projectDesc").css("display","none");
		}
	);
});
