// var length = $(".text-with-image-project").length;
var clientWidth = null;

var content = null;

var contentInner= null;

var leftArrow = null;

var rightArrow = null;

function News(el) {
	this.htmlEl = $(el);
	
	this.has2colText = false;
	this.leftBoxWidth = 315 + 35;
	
	// falls zweispaltiger Projektartikel aendern sich die Werte
	if(this.htmlEl.hasClass('text-with-image-2col')) {
		this.has2colText = true;
		this.leftBoxWidth = 630 + 35;
	}
	// this.leftBoxWidth = deletePX(this.htmlEl.find('.text-with-image-leftbox').css('width')) + 35;
	
	this.id = $(el).attr('id');
	
	this.isActive = false;
	
	this.titleText = this.htmlEl.find('.text-with-image-title').text();
	
	this.images = this.htmlEl.find('.images');
	
	var imagesWidth = 0;
	this.images.children().each(function(index){
		imagesWidth += this.width + 15;
	});
	this.rightBoxWidth = imagesWidth;
	
	// this.rightBoxWidth = deletePX(this.htmlEl.find('.text-with-image-rightbox').css('width'));
	
	this.totalWidth = this.leftBoxWidth + this.rightBoxWidth;
	
	this.distanceFromLeft = this.leftBoxWidth;
	
	this.positionOfLeftArrow = this.leftBoxWidth;
	
	this.scrolledPixels = 0;
	
	this.number = 0;
	
	this.scrollCounter = 0;
	
	News.totalWidthOfAllNewss += this.totalWidth;
	
	News.totalNumberOfNewss += 1;
	
	this.prevNews = null;
	
	this.nextNews = null;
}

News.newss = new Array();

News.totalWidthOfAllNewss = 0;

News.totalNumberOfNewss = 0;

News.articleIdArray = new Array();

News.indexArray = new Array();

// Object functions

News.prototype.setActive = function() {
	this.isActive = true;
	this.htmlEl.css('display','block');
	// this.images.addClass('activeImages');
	
	leftArrow.css('display','block');
	rightArrow.css('display','block');
}

News.prototype.setInactive = function() {
	this.isActive = false;
	this.htmlEl.css('display','none');
	// this.images.removeClass('activeImages');
}

News.hideAllNewss = function() {
	for(i = 0; i < News.newss.length ; i++) {
		News.newss[i].setInactive();
	}
}

News.showAllNewss = function() {
	for(i = 0; i < News.newss.length ; i++) {
		News.newss[i].setActive();
	}
}

News.getNewsById = function(checkId) {
	for(i = 0; i < News.newss.length ; i++) {
		if(checkId == News.newss[i].id) {
			return News.newss[i];
		}
	}
	
	return false;
}


var counter = 0;

$(document).ready(function(){
	if ( $("div.text-with-image-project").length <= 0 ) {
		content = $('#content');
		contentInner= $("#content-inner");
		leftArrow = $('#leftarrow');
		rightArrow = $('#rightarrow');
		
		// if ($(".text-with-image-project").length <= 0) {
			contentInner.css('width','10000px');
			
			var previous = null;
			$('.text-with-image').each(function(index, el){
				// Mehrfach verkettete Listen
				// damit man feststellen kann, was das Folge- und Vorgängerprojekt zu einem Projekt ist
				var news = new News(el);
				news.prevNews = previous;
				if(news.prevNews != null) {	
					news.prevNews.nextNews = news;
				}
				previous = news;
				
				news.number += index;
				news.htmlEl.css('display','block');
				
				if(news.prevNews != null) {	
					news.scrolledPixels = news.prevNews.totalWidth;
				}
				
				news.images.children().each(function(img){
					this.width;
				});
	
				News.newss[news.number] = news;
				
				var color = $('.field-box .inner-trans-inner').attr('style');
				
				var li = $('<li><a onclick="showNews(\'' + news.id + '\');" style="' + color + ';">' + news.titleText + '</a></li>');
				li.appendTo('#linklist-ul');
				
				li.find('a').hover(function(){
					var tags = news.htmlEl.find(':first').attr('class');
					if(tags != 'text-with-image-leftbox') {
						var taggedItemsCount = 0;
						$('#menu-innerbox a').each(function(index2, menuEl){
							var alias = $(menuEl).attr('class');
							if(alias && (tags.indexOf(alias) != -1)) {
								$(menuEl).addClass('highlight');
								taggedItemsCount += 1;
							}
						});
					}
				}, function(){
					var tags = news.htmlEl.find(':first').attr('class');
					if(tags != 'text-with-image-leftbox') {
						$('#menu-innerbox a').each(function(index2, menuEl){
							var alias = $(menuEl).attr('class');
							$(menuEl).removeClass('highlight');
						});
					}
				});
				
			}); // end of foreach
	
			contentInner.css('width','100%');
			
			News.hideAllNewss();
			
			if(News.totalNumberOfNewss > 0) {
				$('#linklist').css('display','block');
			}
			
			var korrekteHoehe = $('.field-box').height()-20;
			$('.field-box .inner-trans-inner').css('height', korrekteHoehe + 'px');
			
			
			
			$('#closebutton').click(function(){
				content.removeClass('whitebg');
				for(i = 0; i < News.newss.length ; i++) {
					$('#' + News.newss[i].id).css('display','none');
					contentInner.css('margin-left','0');
				}
				$('.images').css('margin-left','0');
				
				return false;
			});
			
			leftArrow.hover(function(){
				leftArrow.everyTime(25,'myTimer',function() {
					// IE7/8 Bugfix - from ist am Anfang NaN, nicht 0!
					// if (isNaN(from)) from = 0;
					// (-to + subtraction) = anzahl pixel, die der uberlappende Bereich einnimmt als NEGATIVER wert
					
					if(true) {
						contentInner.animate({
							marginLeft: "-=25px"
						}, 25 );
					}

				});
			},function(){
				leftArrow.stopTime('myTimer');
			});

			rightArrow.hover(function(){
				rightArrow.everyTime(25,'myTimer',function() {
					if(true) {
						contentInner.animate({
					        marginLeft: "+=25px"
					    }, 25 );
					}
				});
			},function(){
				rightArrow.stopTime('myTimer');
			});
		
	//		if ($('.text-with-image-2col').length > 0) {
	//			$('#leftarrow').css('left','661px');
	//		}
		//}
	}
});

function showNews(idString) {
	News.showAllNewss();
	clientWidth = document.body.clientWidth;
	
	var news = News.getNewsById(idString);
	
	content.addClass('whitebg');
	
	content.css('width',(clientWidth+20) + 'px');
	contentInner.css('width', News.totalWidthOfAllNewss + 'px');

	var jumpTo = -1 * news.scrolledPixels;
	$("#content-inner").css('margin-left',jumpTo);
	
	news.setActive();
}
