// Globals
var rotatorOldImg = 0;
var rotatorFirstImg = 1;
var imgElements = 0;
var rotatorCount = 0
var rotatorCurrent = 1;
var periodTimer = 0;
var exampleFx = false;
var tempTextCount = 0; 

// Dom-Ready Events
window.addEvent('domready', function() {
	setHeightInfoBlocks();	
	loadFlashRotator();
	initAccordeon();
	initTabs();
	
	// Rotator les pagina
	imgElements = $$('div.rotator img');
	textElement = $$('ul.bulletsText li');
	rotatorCount = imgElements.length;
	if (rotatorCount > 1){
		periodTimer = null;
		startRotator(imgElements);
  }	
		setBullets();
});

// Sets the height of the infoblocks
function setHeightInfoBlocks() {
	var infoBlocks = $$('div.infoblock');
	if (infoBlocks.length > 1) {
		var previousElement = false;
		infoBlocks.each(function(value) {
			if (previousElement == false) {
				previousElement = value;
			}
			else if (value.hasClass('last')) {
				var previousDimensions = previousElement.getComputedSize();
				var previousHeight = previousDimensions.height;
				var thisDimensions = value.getComputedSize();
				var thisHeight = thisDimensions.height;
				var maxHeight = new Array(previousHeight, thisHeight).max();
				previousElement.setStyle('height', maxHeight);
				value.setStyle('height', maxHeight);
				previousHeight = false;
				previousElement = false;
			}
		});
	}
}

// Loads the home flash rotator
function loadFlashRotator() {
	if ($('flashrotator')) {
		return false;
	}
}

// Initialize Accordeon on Lesson-page
function initAccordeon() {
	if ($$('div.accordion').length>0) {
		$$('div.accordion a.header').addEvent('click', function(e) {
			e.preventDefault();
		});
		var show = 0;		
		if(Browser.Engine.trident5 == true || Browser.Engine.trident4 == true ){
			var show = -1;
		}										
		var accordion = new Fx.Accordion($$('.toggler'),$$('.element'), {
			opacity: (Browser.Engine.trident) ? 0 : 1,
			display: show,
			alwaysHide: false,
			height: true,
			width: false,
			onActive: function(toggler) { toggler.getElement('span.indicator').setStyle('background-position','0px -13px'); toggler.setStyle('font-size','15px'); toggler.setStyle('color','#000');},
			onBackground: function(toggler) { toggler.getElement('span.indicator').setStyle('background-position','0px 0px'); toggler.setStyle('font-size','12px'); toggler.setStyle('color','#f60');}
		});
	}
}

// Rotator les-pagina functions
startRotator = function(imgElements) {
	imgElements.each( function(img, key){
		var temp = key + 1;
		if (temp == rotatorCurrent){
			img.setStyle('z-index','50');
			img.setStyle('opacity','1');				
		}
		else {
			img.setStyle('z-index','49');
			img.setStyle('opacity','0');
		}
	});
	$clear(periodTimer);
	periodTimer = nextRotator.periodical(5000); 
}

nextRotator = function(value) {  
	//alert(value);
	if (value){
		var tempvalue= rotatorCurrent;
		rotatorCurrent = value ;
	}
	else{
		if (rotatorCurrent < rotatorCount){
			rotatorCurrent ++;
		}
		else {
			rotatorCurrent = 1;
		}
	}
	changeImage(tempvalue);
}

changeImage = function(value){
	$clear(periodTimer);
	periodTimer = nextRotator.periodical(5000);  
	if (rotatorCurrent != value){
		if (exampleFx){
			exampleFx.cancel();
		}
		activateBullet(rotatorCurrent);
		imgElements.each( function(img, key){
			var temp = key + 1;
				
			if (temp == rotatorCurrent){
				img.setStyle('z-index','50');
					
				exampleFx = new Fx.Tween(img, {
					property: 'opacity',
					duration: 1000, 
					transition: Fx.Transitions.Quart.easeInOut,
					onComplete: function() { 
						removeImage(); 
					}
				});
					exampleFx.start([0,1], exampleFx);		
			}
			else {
				img.setStyle('z-index','49');
			}
		});
	}
}
	
removeImage = function() {
	imgElements.each( function(img, key){
		var temp = key + 1;
		if (temp == rotatorCurrent){
		}
		else {
			img.setStyle('opacity', '0');
		}
	});
}
setBullets = function() {
  if (rotatorCount > 1){
	htmlBullets = '';
	$('bullets').empty();
	for(i = 1; i <= rotatorCount; i++) {
		htmlBullets += '<a href="#" onclick="return false;" class="bullet bullet'+i+'" rel="'+i+'"></a>';
	}
	$('bullets').set('html', htmlBullets);
	
	$$('a.bullet').addEvent('click', function() {
		picNr = this.get('rel');
		nextRotator(picNr);
	});
	  }
	 activateBullet(rotatorCurrent);

}

activateBullet = function(nr) {
	textElement.each( function(img, key){
		tempTextCount = key + 1;
		if (tempTextCount == nr){
		//	alert('bla');
			myText = img.get('html');
			$$('ul.rotatorText').set('html', myText);
		}
		//alert(tempTextCount);
	});
	$$('a.bullet').removeClass('bulletactive');
	$$('a.bullet'+nr).addClass('bulletactive');
}


function initTabs(){
	var li_elements = $$('ul.tab_block li');
		var div_content = $$('div.tab_content');							
		li_elements.each(function(value, key){
		
			value.addEvent('click', function(e) {
				e.preventDefault();
				li_elements.removeClass('active_tab');
				div_content.removeClass('active');
				this.addClass('active_tab');
				div_content[key].addClass('active');
		});								
	}); 	
}



