var carouselExecuter = false;

// if user changed position, stop animation!
var lastCarouselPosition = 0;

document.observe("dom:loaded", function(){
	// add click event to existing toolbox fold element
	$("main").select(".module_teaser3Column")[0].select(".pager").each(function(s){
		s.setStyle({
			display: "block",
			cursor: "pointer"
		});
	});

	// init carousel
	initTeaserCarousel(true, 3, 3, 10);
});


/*
* function: initTeaserCarousel
*
*  intializes and generates the dynamic teaser carousel	
*
*  parameters:
*
*  @autoScroll: 		set autoScroll true|false to enable / disable autoscrolling
*  @steps: 				num of elements to be scrolled per autoScroll step
*  @elementsPerGroup:	max visible elements per group
*  @duration:			the duration for automatic scrolling in seconds
*/
function initTeaserCarousel(autoScroll, steps, elementsPerGroup, duration){
	// get transofrmed Elements
	var collectedElements = transformElements(elementsPerGroup, 'teaserCarousel', 'teaserRow');

	// set container to highest element
	var maxHeight = findMaxHeight(collectedElements);

	// set container to maxHeight
	$('teaserRow').setStyle({'height':maxHeight+'px'});

	// init the carousel
//	var carousel = new UI.Carousel("teaserCarousel", {scrollInc: 3, nbVisible: elementsPerGroup});
	var carousel = new UI.ImmoKompassCarousel("teaserCarousel", {scrollInc: 3, nbVisible: elementsPerGroup});
	

	// start automatic scroll if autoScroll = true
	if (autoScroll) {
		startScroll(carousel, 'teaserCarousel', duration, steps);
	}

	if (Prototype.Browser.IE) {
		// teach IE the disabled status of prev button
		$("teaserCarousel").select(".previous_button")[0].writeAttribute("class", "previous_button previous_button_disabled");
	}
}


/*
* function: transformElements
*
*  transforms elements, so they can be used for the auto carousel
*
*  parameters:
*
*  @elementsPerGroup:	max visible elements per group
*  @carousel:			the id for the carousel container  
*  @listContainer: 		the id for the list elements container
*/
function transformElements(elementsPerGroup, carousel, listContainer){
    // get all teasers
	var teaserElements = $(carousel).select('.teaser');

	// remove clearfix to show elements in a single row 
	var removeClearfix = $(carousel).select('.clearRow');
	removeClearfix.each(function(element){
		element.removeClassName('clearRow');			
	});

	// fill elements so total is a multiple of elementsVisible
	var numOfElements 	= teaserElements.size();
	var elementsVisible = elementsPerGroup;
	var elementsToFill	= elementsVisible - (numOfElements % elementsVisible);
/*
	for(var i=0; i<elementsToFill; i++){
		$(listContainer).insert({
			bottom: '<li class="teaser" style="margin:0;"></li>'
		});
	}
*/
	// reselect elements after fillinig
	teaserElements = $(carousel).select('.teaser');

    var counter = 0;

	teaserElements.each(function(element){
		//set flow

		var leftPos = 267 * (counter);
		var offset  = 0;
		element.addClassName('positionAbsolute');

		//if not last element, add right margin +10
		element.setStyle({
			'width':       267 + 'px',
			'left':        leftPos + 'px',
			'marginRight': '0px'
		});

		counter = counter + 1;
	});

   return teaserElements;
}


/*
* function: startScroll
*
*  parameters:
*
*  @carousel:			the generated carousel variable
*  @carouselContainer:	the id for the carousel container  
*  @duration: 			the duration of the auto scrolling in seconds
*  @steps: 				num of elements to be scrolled per autoScroll step
*/
function startScroll(carousel, carouselContainer, duration, steps) {
	var scrollMax = $(carouselContainer).select('li').size();
	lastCarouselPosition = Math.floor(carousel.currentIndex());
	var i;
	carouselExecuter = new PeriodicalExecuter(function(pe) {
		i = Math.floor(carousel.currentIndex());
		
		// Stop Carousel if user changed position or end reached
		if ( lastCarouselPosition != i ) {
			carouselExecuter.stop();
			return;
		}
		
		//scroll right til last element is reached
		if(scrollMax-steps > i){
			direction = 1;
			carousel.scrollTo(lastCarouselPosition = Math.min(i+steps, scrollMax-steps) );			
		}else{
			carousel.scrollTo(0);
			direction = 10;
		}
	}, duration);
}

function findMaxHeight(elements) {
	var max = 0;
	elements.each(function(element){
		var height = element.getHeight();
		if (height > max) {
			max = height; 
		}
	});
	return max;
}
