var CATEGORY_CLASS = "fmu_news_dates";
var CATEGORY_ITEMS_PER_PAGE = 4;
var POST_CLASS = "post";
var DETAIL_LINK_CLASS = "jquery";
var DETAIL_TEXT_CLASS = "text";
var DETAIL_IMAGES_CLASS = "images";

//doc ready
$(function(){
	$("."+CATEGORY_CLASS).each(function() {
		showCategoryPage($(this), 1);
	});
	bind();
});

function bind() {
	//sylbentrennung <div class="fmu_entry hyphenate text" lang="de">
	Hyphenator.config({
	   	displaytogglebox :false,
    	minwordlength : 2
    });
	Hyphenator.run();
	
	
	
	// autom anfügen der funkt beim erstellen von elementen
	$(".pageIndicator > .gotoPageButton, .pageIndicator > .nextPageButton").live("click", function(e) {
		var buttonDivE = $(this);
		var newPageIdx = parseInt(buttonDivE.attr("pageIdx"));

		if(buttonDivE.is(".selected")) {
			return;
		}
		var parentE = buttonDivE.parent().parent();
		if(parentE.is("."+CATEGORY_CLASS)) {
			showCategoryPage(parentE, newPageIdx);
		} else if(parentE.is("."+DETAIL_TEXT_CLASS)) {
			showDetailTextPage(parentE, newPageIdx, true);
		} else if(parentE.is("."+DETAIL_IMAGES_CLASS)) {
			showDetailImagePage(parentE, newPageIdx);
		}
	});

	$("a."+DETAIL_LINK_CLASS).click(function(e){
		var categoryDivE = $(this).parents("."+CATEGORY_CLASS);
		e.preventDefault();
		openDetail(categoryDivE, $(this));
	});
	
	$("#detail > .closeButton").live("click", function(e) {
		closeDetail();
	});
}

function openDetail(categoryDivE, postLinkE) {
	closeDetail();
	var postDivE = postLinkE.parent().parent();
	
	var url = postLinkE.attr("href")+'&ajax=1';
	var detailDivE = $("<div id='detail'>"+
	                     "<div class='container'></div>"+
	                   "</div>").insertAfter(categoryDivE);
	$("#detail").hide();
	var containerDivE = detailDivE.find(".container");
	
	containerDivE.load(url, function() {
		categoryDivE.hide();
		$("#detail").fadeIn();
		
		//detailDivE.prepend("<div class='closeButton'></div>");
		detailDivE.append("<div class='closeButton'></div>");
		showDetailTextPage(containerDivE.find("."+DETAIL_TEXT_CLASS), 1, false);
		showDetailImagePage(containerDivE.find("."+DETAIL_IMAGES_CLASS), 1);
		//sylbentrennung <div class="fmu_entry hyphenate text" lang="de">
		Hyphenator.config({
        	displaytogglebox : false,
        	minwordlength : 2
        });
        Hyphenator.run();
	});
}

function closeDetail() {
	$("#detail").remove();
	$("."+CATEGORY_CLASS).show();
}
// 1. function
function showCategoryPage(categoryDivE, pageIdx) {
	var i = 0;
	categoryDivE.find("."+POST_CLASS).each(function() {
		var previousPageIdx = pageIdx-1;
		
		if(i < CATEGORY_ITEMS_PER_PAGE * previousPageIdx || i >= CATEGORY_ITEMS_PER_PAGE * pageIdx) {
			$(this).hide();
		} else {
			$(this).show();
		}
		i++;
	});
	var nbrOfItems = i;

	drawPageIndicator(categoryDivE, pageIdx, CATEGORY_ITEMS_PER_PAGE, nbrOfItems);
}

function showDetailTextPage(textDivE, pageIdx, doSlide) {
	if(textDivE.attr("contentHeight") == null) {
		textDivE.find("p").wrapAll("<div class='scrollContainer'><div class='scrollContent'></div></div>");
		textDivE.attr("contentHeight", textDivE.find(".scrollContent").height());
	}
	var contentHeight = parseInt(textDivE.attr("contentHeight"));
	var scrollContainerDivE = textDivE.find(".scrollContainer");
	var scrollContentDivE = textDivE.find(".scrollContainer > .scrollContent");

	var pageHeight = scrollContainerDivE.height();
	if(doSlide) {
		scrollContentDivE.animate({height: (pageHeight * pageIdx)+"px"}, 500);
	} else {
		scrollContentDivE.height(pageHeight * pageIdx);
	}
	
	drawPageIndicator(textDivE, pageIdx, pageHeight, contentHeight);
	
}

function showDetailImagePage(imagesDivE, pageIdx) {
	var i = 0;
	imagesDivE.find("img").each(function() {
		var previousPageIdx = pageIdx-1;
		
		if(i < 1 * previousPageIdx || i >= 1 * pageIdx) {
			$(this).hide();
		} else {
			$(this).show();
			
		}
		i++;
	});
	var nbrOfItems = i;

	drawPageIndicator(imagesDivE, pageIdx, 1, nbrOfItems);
}
// 2. function
function drawPageIndicator(parentE, pageIdx, nbrOfItemsPerPage, nbrOfItems) {
	var pageIndicatorDivE = parentE.find(".pageIndicator");
	if(pageIndicatorDivE.length == 0) {
		parentE.append("<div class='pageIndicator'>"+
                         "<div class='nextPageButton'></div>"+
                       "</div>");
		pageIndicatorDivE = parentE.find(".pageIndicator");
	}
	var nextPageButtonDivE = pageIndicatorDivE.find(".nextPageButton");

	var nbrOfPages = Math.floor(nbrOfItems / nbrOfItemsPerPage);
	if(nbrOfItems / nbrOfItemsPerPage > nbrOfPages) {
		nbrOfPages += 1;
	}
	
	if(nbrOfPages < 2) {
		pageIndicatorDivE.hide();
	} else {
		pageIndicatorDivE.find(".gotoPageButton").remove();	
		for(var i=1; i <= nbrOfPages; i++) {
			if(i == pageIdx) {
				$("<div class='gotoPageButton selected' pageIdx='"+i+"'></div>").insertBefore(nextPageButtonDivE);
			//	$("<div class='gotoPageButton selected' pageIdx='"+i+"'></div>").insertBefore(nextPageButtonDivE);
			} else {
				$("<div class='gotoPageButton' pageIdx='"+i+"'></div>").insertBefore(nextPageButtonDivE);
			}
		}
		
		if(pageIdx < nbrOfPages) {
			nextPageButtonDivE.attr("pageIdx", pageIdx+1);
			nextPageButtonDivE.show();
		} else {
			nextPageButtonDivE.hide();
		}
		
		pageIndicatorDivE.show();
	}
}
