// JavaScript Document
firstStart = true;
catIndex = 0;
aktItemIndex = -1;
var data_arr;
var thumb_arr;	
function initNews(arr,arr2,arr3) {
	data_arr = arr;
	thumb_arr = arr2;
	urls_arr = arr3;
	initList(catIndex);
	initCatNav();
}
function initList(catIndex) {
	resetList();
	numEntry = data_arr.length;
	numIndex =  catIndex*5+5;
	if(numIndex > numEntry) {
	 numIndex = numEntry;	
	}
	counter = 0;
	for(var i=0;i<numEntry;i++) {
		myDate = data_arr[i].post_date;
		
		if(myDate.length == 7) {
			myDate = "0"+myDate;	
		}
		
		
		
		if(data_arr[i].post_title) {
			myTitle = data_arr[i].post_title;
		} else {
			myTitle = "";	
		}
		if(data_arr[i].post_content) {
			myTeaser = data_arr[i].post_content;
		} else {
			myTeaser = "";
		}
		
		// Strip Teasertext
		var indexPos = myTeaser.indexOf('<!--more-->');
		myTeaser = myTeaser.substr(0,indexPos-1);
		
		if(thumb_arr[i]) {
			elementContent = "<div class='newsThumb'>";
			elementContent += "<img src='"+thumb_arr[i]+"' alt='"+myTeaser+"' />";	
			elementContent += "</div>";
		} else {
			elementContent = "";	
		}
				
		
		elementContent += "<span class='newslistTitle'>"+myDate+' '+myTitle+"</span></br>";
		elementContent += "<span class='newslistContent'>"+myTeaser+"</span>"; 	
		
		
		
		element = $('#newslist #news'+counter).html(elementContent);
		
		counter++;
		

	}
	showEntry(0);	
	
}

function resetList() {
	for(var i=0;i<5;i++) {
		$('#newslist #news'+i).empty();
	}
}



function showEntry(index) {
	if(index != aktItemIndex) {
		aktItemIndex = index;
		myDate = data_arr[index].post_date;
		
		
		
		myTitle = data_arr[index].post_title;
		if(data_arr[index].post_content) {
			myPost = data_arr[index].post_content;
		} else {
			myPost = "";
		}
		
		if(firstStart != true) {
				$('#newsview_content').animate( {opacity:0}, {duration: 500,easing:"easeOutExpo", complete: showElement});
			} else {
				elementContent = "<span class='newslistTitle'>"+myDate+' '+myTitle+"</span></br>";
				elementContent += "<span class='newslistContent'>"+myPost+"</span>"; 
				$('#newsview_content').html(elementContent);
				firstStart = false;
			}
		
		}
	}
function showElement() {
		elementContent = "<span class='newslistTitle'>"+myDate+' '+myTitle+"</span></br>";
		elementContent += "<span class='newslistContent'>"+myPost+"</span>"; 		 
		// Show Entry
		$('#newsview_content').html(elementContent);
	
	
	$('#newsview_content').animate( {opacity:1}, {duration: 500,easing:"easeOutExpo"});
}

function initCatNav() {
	var catNum = Math.round((data_arr.length-1) / 5);
	
	/* Disabled 
	if((data_arr.length) % 5 > 0) {
		catNum++;	
	}

	*/

	var resultContent;
	for (i=0;i<catNum;i++) {
		elementContent = "<li id=cat"+i+">"+Number(i+1)+"</li>";
		
		
		
		$('#news_cat_nav #newsnavlist').append(elementContent);
		$("#cat"+i).click(function(e) {
			
			$('#newsnavlist li').removeClass("active");
			
			$(this).addClass("active");
			
			showCat($(this).html());	
		});
		
		if(i == 0) {
			$("#cat"+i).addClass("active");	
		}
		
		
		
	}
}

function showCat(index) {
	catIndex = index-1;
	initList(catIndex);	
}


