
$(document).ready(function(){
	// executes when HTML-Document is loaded and DOM is ready.
});

$(window).load(function(){
	// executes when complete page is fully loaded, inlcuding all frames, objects and images.
	if ($(".accordion").length > 0) {
		loadAccordion();
	}
	newBooksInitialize();
});

function loadAccordion() {
	var accordion;
	var openfirst = 0;
	
	if ($(".openfirst").length > 0) {
		openfirst = $(".openfirst").index() + 1;
	}
		
	accordion =   $('.accordion').classicAccordion({width:1200, 
													height:343, 
													initialPanel:openfirst, 
													slideshow:false, 
													shadow:true, 
													alignType:'centerCenter', 
													closedPanelSize:128, 
													panelProperties:{} });
}

function truncate(string, threshold) {
	var titleString = string;
	if (string.length > threshold) {
		titleString = titleString.trim()    // remove leading and trailing spaces
		    .substring(0, threshold)    // get first 600 characters
		    .split(" ") // separate characters into an array of words
		    .slice(0, -1)    // remove the last full or partial word
		    .join(" ") + "...";
	}
	return titleString;
}

function newBooksInitialize() {
	$("#bookcovers #bookfeed a").click(function(){
		if ($(this).parent().hasClass("info")) {return true;}
		$("#bookcovers .active").removeClass("active");
		$(this).parent().addClass("active");
		
		if ($(this).parent().find(".info a").height() > 30) {
			$(this).parent().find(".info").addClass("smaller");
		}
		
		$(this).parent().find(".info a").text(truncate($(this).parent().find(".info a").text(),38));
		$(this).parent().find(".info p.subtitle").text(truncate($(this).parent().find(".info p.subtitle").text(),45));
		$(this).parent().find(".info p.author").text(truncate($(this).parent().find(".info p.author").text(),50));
		
		return false;
	});
	
	jQuery.fn.exists = function(){return jQuery(this).length>0;}

	var currentX = 0;
	var currentBook = 1;
	
	$("#bookcovers a img").each(function() {
		if ($(this).height() < 135) {
			
			$(this).height(110);
			$(this).width("auto");
			
			var img = $(this);
			
			$("#bookcovers .info").each(function() {
				var thisInfo = $(this);
				var difference = img.width() - 90;
				var newWidth = thisInfo.width() - difference;
				$(this).width(newWidth);
			});
		}
	});
	

	
	$("#bookcovers a.nextbook").click(function() {
		if ($("#bookwrapper .bookcover").length <= 4) {
			return false;
		}
		currentBook += 4;
		if (currentBook > 100) {
			currentBook = 09;
			$("#bookwrapper").animate({
			    marginLeft: currentX
			  }, 500);
		} else {
			// console.log("current book: "+currentBook);
			
			$("#bookcovers .active").removeClass("active");
			$(".bookcover:nth-child("+currentBook+")").addClass("active");
			currentX = currentX - booksWidth(currentBook-4, currentBook-1);
			$("#bookwrapper").animate({
			    marginLeft: currentX
			  }, 500);
		}
		return false;
	});
	
	$("#bookcovers a.prevbook").click(function() {
			if ($("#bookwrapper .bookcover").length <= 4) {
				return false;
			}
		currentBook -= 4;
		if (currentBook < 1) {
			currentBook = 1;
			currentX = 0;
		} else {
			// console.log("current book: "+currentBook);
				$("#bookcovers .active").removeClass("active");
				$(".bookcover:nth-child("+currentBook+")").addClass("active");
			
			currentX = currentX + booksWidth(currentBook, currentBook+3);
			
			$("#bookwrapper").animate({
			    marginLeft: currentX
			  }, 500);				
			
		
		}
		return false;
	});
	
	
}

function booksWidth(startBook, endBook) {
	var totalWidth = 0;
	for (var i=startBook; i<=endBook; i++) {
		totalWidth += $(".bookcover:nth-child("+i+") img").width() + 10;
		// console.log(i+ " " +$(".bookcover:nth-child("+i+") img").width());
	}
	// console.log("total: "+totalWidth);
	return totalWidth;
}

function filter() {
	$("#bookwrapper").find("div").removeClass();
	$("#bookwrapper").find("h2").removeClass();
	$("#bookwrapper").find("h3").removeClass();
	$("#bookwrapper").find("img").removeClass();
	$("#bookwrapper div:empty").remove();
	$("#bookwrapper").find("img").parent().parent().addClass("bookcover");
	
	$("#bookwrapper div div").not($('div.bookcover')).addClass("remove");
	//$('#bookwrapper .remove').remove();
	$("#bookwrapper .bookcover .remove").removeClass("remove");
	$('#bookwrapper .remove').remove();
	
	
	$("#bookwrapper .bookcover div:first-child").addClass("info");
	
	$("#bookwrapper .bookcover img").each(function(){
		$(this).css("width", "90px")
	    $(this).prependTo($(this).parent().parent());
	});
	
	$("#bookwrapper .bookcover img").wrap('<a href="#" />');
	$("#bookwrapper .bookcover:first").addClass("active");
	$("#bookwrapper .bookcover .info div").remove();
}

