var slideTimer;
var slideInterval = 5000;

var slideNews = function(dir){
	
	items = ($('newsItems').getStyle('width').toInt()) / 586; 		
	nowDis = (-1*($('newsItems').getStyle('left').toInt()) / 586) + 1;
	
	switch(dir){
		case "next":
			nextDis = (nowDis < items) ? nowDis + 1 : 1 ;
		break;
		case "prev":
			nextDis = (nowDis > 1) ? nowDis -1 : items ;
		break;
	}
	
	nowPos = $('newsItems').getStyle('left').toInt();
	nextPos = (nextDis -1) * -586;
	
	$('newsItems').set('morph',{duration:500});
	$('newsItems').morph({'left':[nowPos,nextPos]});
	
	//remove the IndexItem now style
	$$('.now').removeClass('now');
	
	//set the new IndexItem now
	$('iI-'+nextDis).addClass('now');
};

var pauseAutoSlide = function(){
 	if($('newscarroussel')){
		$('newscarroussel').addEvent('mouseenter',function(){
			clearTimeout(slideTimer);
		}).addEvent('mouseleave',function(){
			slideTimer = autoSlideNews.delay(slideInterval); 
		});
	}
};

var autoSlideNews = function(){

	if($('newsItems') && $('newsItems').getStyle('width').toInt() > 586){
		
		slideNews('next');
		slideTimer = autoSlideNews.delay(slideInterval);
	}
	
};

var is_Int = function(){
	totest = (-1*($('newsItems').getStyle('left').toInt()) / 586) + 1;
	return /^ *[0-9]+ *$/.test(totest);
};

var slideByIndex = function(){
	$$('.indexItem').each(function(iIt){
		iIt.addEvent('click',function(){
			item = iIt.get('id').replace('iI-','').toInt();
			
			nowPos = $('newsItems').getStyle('left').toInt();
			nextPos = (item -1) * -586;

			$('newsItems').set('morph',{duration:500});
			$('newsItems').morph({'left':[nowPos,nextPos]});

			//remove the IndexItem now style
			$$('.now').removeClass('now');

			//set the new IndexItem now
			$(iIt).addClass('now');
		});
	});

};

var initNews = function(){
	$('iI-1').addClass('now');
	slideTimer = autoSlideNews.delay(slideInterval);
};

var prev_nextNews = function(){
	
	if($('prevNews')){
		$('prevNews').setStyle('opacity',.25);
		$('prevNews').addEvent('mouseenter',function(){
			fadeInOut('prevNews',300,1);
		}).addEvent('mouseleave',function(){
			fadeInOut('prevNews',300,.25);
		}).addEvent('click',function(){
			
			if(is_Int() == true){
				slideNews('prev');
			}
		});
	}
	
	if($('nextNews')){
		$('nextNews').setStyle('opacity',.25);
		$('nextNews').addEvent('mouseenter',function(){
			fadeInOut('nextNews',300,1);
		}).addEvent('mouseleave',function(){
			fadeInOut('nextNews',300,.25);
		}).addEvent('click',function(){
			
			if(is_Int() == true){
				slideNews('next');
			}
		});	
	}
};


var readMore = function(){
	$$('.newsVisit').each(function(rmObj){
		rmObj.setStyle('opacity',.3);
		var rmId = rmObj.get('id');
		rmObj.addEvent('mouseenter',function(){
			fadeInOut(rmId,300,1);
		}).addEvent('mouseleave',function(){
			fadeInOut(rmId,300,.3);
		}).addEvent('click',function(){
			window.parent.location.href = "?p=nieuws&article="+rmId.replace('rm-','');
		});
	});
	$$('.newsTeaser').each(function(nwObj){
		nwObj.addEvent('click',function(){
			window.parent.location.href = "?p=nieuws&article="+nwObj.get('id').replace('nw-','');
		});
	});
};

window.addEvent('domready', function() {
	initNews();
	prev_nextNews();
	readMore();
	pauseAutoSlide();
	slideByIndex();
});
