// JavaScript Document
window.addEvent('domready', function() {
	var actIndex = 0;
	var nextIndex = 0;
	var timer;
	var speed = 5000
	var start = false;
	function unactiveLink(){
		for(d=0;d<linkIndex-2;d++){
			var thisLink = document.getElementById('link_'+d);
			thisLink.className = '';
		}
	}
	function activeLink(){
		var isSet = false;
		for(d=0;d<linkIndex-2;d++){
			var thisLink = document.getElementById('link_'+d);
			var nextIndex2 = nextIndex;
			if(nextIndex2 > txtIndex-1){
				nextIndex2 = 0;
			}
			if(isSet === false){
				//alert(nextIndex+'>='+thisLink.rel.toFloat()+' et '+nextIndex+'<'+thisLink.lang.toFloat());
				//console.log(nextIndex2+' >= '+thisLink.rel.toFloat()+' && '+nextIndex2+' < '+thisLink.lang.toFloat());
				if(nextIndex2 >= thisLink.rel.toFloat() && nextIndex2 < thisLink.lang.toFloat()){
					thisLink.className = 'active';
					//console.log('ACTIVE:'+d);
					isSet = true;
				}
			}
		}
	}
	function showItem(){
		if(start === true){
			hideItem();
		}
		
		unactiveLink();
		activeLink();
		actIndex = nextIndex;
		//console.log('TXTINDEX:'+txtIndex);
		if(actIndex > txtIndex-1){
			actIndex = 0;
		}
		if(start === true){
			var txtEle = $('txt_'+actIndex);
			txtEle.setStyle('display', 'block');
			
			var morphObject = new Fx.Morph(txtEle);
			morphObject.start({
				'opacity': 1
			});
			
			speed = txtEle.get('rel').toFloat()*1000;
			//console.log(speed);
		
			$clear(timer);
			timer = (function(){ showItem(); }).periodical(speed);
		}
		
		
		
		nextIndex = actIndex+1;
		start = true;
	};
	function hideItem(){
		//console.log('HIDE:'+actIndex);
		var txtEle = $('txt_'+actIndex);
		var morphObject = new Fx.Morph(txtEle);
		morphObject.start({
			'opacity': 0
		});
		
		txtEle.setStyle('display', 'none');
		
		if(txtEle.get('flash') == 'true'){
			txtEle.set('html','<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="667" height="237"><param name="movie" value="anim.swf" /><param name="quality" value="high" /><embed src="anim.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="667" height="237"></embed></object>');
		}
		
	};
	// GET DATA
	var headerData = Array();
	var txtIndex = 0;
	var linkIndex = 0;
	$$('#mainPub div.boxNews').each(function(item){
		
		item.id = 'txt_'+txtIndex;
		if(txtIndex != 0){
			item.setStyle('opacity', 0);
			item.setStyle('display', 'none');
		}
		txtIndex++;
		//console.log('DATA TXT:'+item.id);
	});
	$$('#headerMenu a').each(function(item){
		item.addEvent('click', function(){
			nextIndex = this.rel.toFloat();
			//console.log('INDEX:'+nextIndex);
			$clear(timer);
			showItem();
			//timer = (function(){ showItem(); }).periodical(speed);
		});
		item.id = 'link_'+linkIndex;
		linkIndex++;
	});
	$$('#leftBtnPub').each(function(item){
		item.addEvent('click', function(){
			nextIndex = actIndex-1;
			if(nextIndex < 0){ nextIndex = txtIndex-1 }
			//console.log('INDEX:'+nextIndex);
			$clear(timer);
			showItem();
			//timer = (function(){ showItem(); }).periodical(speed);
		});
		linkIndex++;
	});
	$$('#rightBtnPub').each(function(item){
		item.addEvent('click', function(){
			nextIndex = actIndex+1;
			if(nextIndex > txtIndex-1){ nextIndex = 0 }
			//console.log('INDEX:'+nextIndex);
			$clear(timer);
			showItem();
			//timer = (function(){ showItem(); }).periodical(speed);
		});
		linkIndex++;
	});
	/*$$('#mainPub div.boxNews').each(function(item){
		item.addEvent('mouseover', function(){
			$clear(timer);
		});
		item.addEvent('mouseout', function(){
			timer = (function(){ showItem(); }).periodical(speed);
		});
	});*/
	showItem();
	timer = (function(){ showItem(); }).periodical(speed);
	
	
});