function setupCarousel(wrapper) {
    
    var slides = wrapper.children();
    slides.css("position", "absolute");
    
    return slides.each(function(i){
        var slide = $(this);
        slide.css("z-index", i);
        if (i != 0) slide.hide();
    });
    
}

function startCarousel(slides) {
    return setInterval(function(){
        advanceCarousel(slides);
    }, 11000);
}

function advanceCarousel(slides, direction, callback) {
	//console.log(slides);
	currentSlide = 0;
	$(slides).each(function(index){
		if ($(this).css("display") != "none")
			currentSlide = index;
	});
    
    var nextSlide = 0;
    if (direction == "back") {
        if (currentSlide == 0) nextSlide = slides.length - 1;
        else nextSlide = currentSlide - 1;
    }
    else {
        if (currentSlide + 1 == slides.length) nextSlide = 0;
        else nextSlide = currentSlide + 1;
    }
    
    var duration = 1000;
    $(slides[currentSlide]).fadeOut(duration);
    $(slides[nextSlide]).fadeIn(duration, callback);
	
}

function changeSlides(event) {
    
    var direction = "forward";
    if (event && $(event.srcElement).hasClass("prev"))
        direction = "back";
    
    $("#slideshow .slides").each(function() {
        advanceCarousel($(this).children(), direction, changeCaption);
    });

}

function changeCaption() {
	if (! $(this).parent().hasClass("center")) return;
	$("#slideshow .current-slide").text($(this).add($(this).siblings()).index(this) + 1);
	$("#slideshow .caption-text").text($(this).find("img").attr("title"));
}


function displayAllReviews() {
    var praise = $("ul.praise");
    clearInterval(praise.data("praiseInterval"));
    praise.children().css("position", "static").show()
    .each(function(){
        $(this).children().last().remove();
    });
}

$(document).ready(function(){
    
    // reviews
    var praise = $("ul.praise");
	setupCarousel(praise);
    praise.data("praiseInterval", startCarousel(praise.children()));
    praise.children().append('<a href="javascript:void(0);"><em>Read all reviews</em></a>')
    .each(function(){
        $(this).children().last().click(displayAllReviews);
    });
    
    // excerpts & contents
    var excerpts = $("div#excerpts dl").add($("div#contents dl"));
    var truncatedHeight = 0;
    var fullHeight = 0;
    excerpts.css("overflow", "hidden");
	excerpts.nextAll().hide();
    excerpts.children().each(function(i){;
        fullHeight += $(this).outerHeight(true);
        if (i < 6) truncatedHeight = fullHeight;
    });
    excerpts.height(truncatedHeight);
    
    excerpts.after('<a href="javascript:void(0);">Read more</a>').next()
    .click(function(){
		excerpts.nextAll().show();
        $(this).hide();
        excerpts.css("height", fullHeight);
    });
    
    // links
    var also = $("div#also ul");
    also.prepend('<li><a href="javascript:void(0);">Read the reviews</a></li>')
    .children().first().click(displayAllReviews);
    
    $("dl.tag-list dt").click(function(){$(this).next().fadeToggle("fast"); });
    
    // modal
    var overlay;
    $(".modal-toggle, a.order-the-book, .order-the-book a").live("click", function(e){
        e.preventDefault();
        if ($("#overlay").length == 0) {
            $("body").append("<div id=\"overlay\"></div>");
            overlay = $("#overlay");
            overlay.load($(this).attr("href") + " #modal", function(response, status, xhr) {
            if (status == "success")
                $("#modal").append("<a class=\"close modal-toggle\" href=\"javascript:void(0);\">Close</a>");
            else
                window.location.href = $(this).attr("href");
            });
            overlay.click(function(e){if (e.target == this) overlay.fadeOut();});
        }
        overlay.fadeToggle();
    });
    
	/* var slides = new Array();
    slides[0] = setupCarousel($(".slides.left"));
    slides[1] = setupCarousel($(".slides.right"));
    slides[2] = setupCarousel($(".slides.center"));
    setInterval(function(){ changeSlides() }, 11000);
    
    $("#slideshow .total-slides").text($("#slideshow .slides.center").children().length);
    $("#slideshow .caption-text").text($(".slides.center").find("img").first().attr("title"));
    $("#slideshow .prev").add($("#slideshow .next")).click(changeSlides); */
    
});
